0.1.3 • Published 11 months ago

unocss-preset-colors-rgb v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

unocss-preset-colors-rgb

This UnoCSS preset converts your theme colors to RGB CSS variables for increased flexibility and control over your styles.

Installation

npm i -D unocss-preset-colors-rgb
yarn i -D unocss-preset-colors-rgb
pnpm i -D unocss-preset-colors-rgb

Why

If you have multiple themes controlled by CSS variables and you want to use the default color preset of UnoCSS, you might encounter issues with opacity modifier syntax.

:root {
  --theme-content: theme('colors.black');
}
.dark {
  --theme-content: theme('colors.white');
}

You can use something like text-$theme-content successfully, but you cannot use an opacity modifier syntax like text-$theme-content/50. To resolve this issue, you should declare it inside theme.color first.

// Warning: this is incorrect code.

defineConfig({
  theme: {
    colors: {
      content: 'rgba(var(--theme-content), <alpha-value>)'
    }
  }
})

However, this won't work because you can only use RGB values (e.g., 255, 255, 255) for the --theme-content CSS variable.

Usage

This preset must work with theme() from the default UnoCSS transformerDirectives transformer.

import { defineConfig, transformerDirectives } from 'unocss'
import presetColorsRGB from 'unocss-preset-colors-rgb'

export default defineConfig({
  presets: [
    presetColorsRGB(),
  ],
  transformers: [
    transformerDirectives(),
  ]
})

This preset helps you convert all of the theme colors (including all default preset colors) into RGB values (theme.rgbs). So, you can declare the CSS variable as theme('rgbs.<color>') to make it work.

/* style.css */

:root {
  --theme-base: theme('rgbs.blue.100');
  --theme-content: theme('rgbs.black');
}

.dark {
  --theme-base: theme('rgbs.blue.900');
  --theme-content: theme('rgbs.white');
}
// uno.config.ts

defineConfig({
  theme: {
    colors: {
      base: 'rgba(var(--theme-base), <alpha-value>)',
      content: 'rgba(var(--theme-content), <alpha-value>)'
    }
  }
})
<div class="bg-base/75 text-content">
  Hello <span class="bg-base text-content/50">World</span>
</div>

License

MIT License

0.1.3-beta.1

11 months ago

0.1.2

11 months ago

0.1.3

11 months ago

0.1.1

1 year ago

0.1.0

1 year ago