nuxt / ui

A UI Library for Modern Web Apps, powered by Vue & Tailwind CSS.
https://ui.nuxt.com
MIT License
3.79k stars 462 forks source link

Tailwind colors with CSS variables results in broken primary/gray palette #1986

Open aautcq opened 1 month ago

aautcq commented 1 month ago

Environment

Version

v2.17.0

Reproduction

https://stackblitz.com/edit/nuxt-ui-keperh

Description

When using CSS variables with tailwind, NuxtUI fails to generate the corresponding primary or gray color palette.

For example (see reproduction for more details):

// tailwind.config.ts
import type { Config } from 'tailwindcss';

export default <Partial<Config>>{
  theme: {
    extend: {
      colors: {
        foo: {
          50: 'rgb(var(--color-foo-50) / <alpha-value>)',
          100: 'rgb(var(--color-foo-100) / <alpha-value>)',
          ...
        },
      },
    },
  },
};

In the main CSS file:

@layer base {
  :root {
    --color-foo-50: 240 253 253;
    --color-foo-100: 191 239 242;
    ...
  }
}

In app.config.ts:

export default defineAppConfig({
  ui: {
    primary: 'foo',
  },
});

And the resulting styles injected in the root is:

:root {
    --color-primary-50: null;
    --color-primary-100: null;
    ...
}

Additional context

This, I believe, is due to the colors plugin which expects the tailwind config to contain HEX codes only, and attempts to convert them to RGB data using the hexToRgb utility function. I'll submit a PR soon to attempt to fix this.

Logs

No response

benjamincanac commented 1 month ago

What is it you're trying to achieve? This is exactly what the primary color is for already:

aautcq commented 1 month ago

Sorry if I wasn't very clear :)

Let’s say I have a color palette that is dynamic and I need to fetch it from my backend. I want to use this palette as NuxtUI's primary color and also for other stylings in my app. I guess I could simply work with the tailwind primary color generated by NuxtUI, and override the values of the CSS variables in the root after having fetched the palette, like so:

const palette = await fetchPaletteFromBackend() // { 50: '240 253 253', 100: '191 239 242', ...}

// Some formatting
const rootVariables = Object.entries(palette).reduce((acc, [key, value]) => {
  return acc + `--color-primary-${key}: ${value};`
}, '')

// Override the root
useHead({ style: `:root {${rootVariables}}` })

This works just fine, but I see a few catches here:

image

For all these reasons, it would feel cleaner to me if I could use a tailwind color palette that is written using CSS variables as NuxtUI's primary color.

Please let me know if I'm missing some other obvious way to achieve this. I'm still new to Nuxt, I might just be embarrassing myself here 🌝

Also, the above is just an example, and there is already an open issue regarding this precise use case. But there might be some other cases where I would need to use CSS variables with tailwind, e.g., if I want to use something like Radix colors and need to change the value of a color depending if I'm in light or dark mode.

The real question is: how should NuxtUI handle the case where a palette generated using CSS variables is used as the primary or gray color?

If we the answer is "use HEX codes only", then it might be worth raising an error in the colors plugin when something other than a HEX code palette is provided, and also maybe mention it in the docs?

github-actions[bot] commented 1 week ago

This issue is stale because it has been open for 30 days with no activity.