ui icon indicating copy to clipboard operation
ui copied to clipboard

Issue with using {color} in app.config.ts variants

Open letoast opened this issue 1 year ago • 4 comments

Environment


  • Operating System: Linux
  • Node Version: v18.20.3
  • Nuxt Version: 3.12.4
  • CLI Version: 3.12.0
  • Nitro Version: 2.9.7
  • Package Manager: [email protected]
  • Builder: -
  • User Config: modules
  • Runtime Modules: @nuxt/[email protected]
  • Build Modules: -

Version

2.18.1

Reproduction

https://stackblitz.com/edit/nuxt-ui-snm3l2?file=app.config.ts

Description

If using {color} in app.config.ts variants (https://ui.nuxt.com/getting-started/theming#smart-safelisting), even with "primary", the generated classes are ignored by tailwind. See reproduction (hover classes)

Additional context

No response

Logs

No response

letoast avatar Aug 02 '24 09:08 letoast

Update 4. aug. 2024: Managed to reproduce the issue, updated the reproduction link. Please help.

Update 3. aug. 2024: the issue still appears. After clearing my .nuxt folder, it came back. It looks like tailwind is not safelisting those classes. The problem is that I'm not able to reproduce it easily in stackblitz, because our project is quite complex (lots of nuxt layers). I'll try to reproduce it and if I manage, I will reopen the issue.

Update: 2. aug. 2024: Found out that the issue in the reproduction was the preferred color mode: system. Also I removed @nuxtjs/tailwindcss from my project (because it's included with @nuxt/ui) and it seems to have fixed the problem.

letoast avatar Aug 02 '24 19:08 letoast

Since hover:text-{color}-100 class doesn't exist, you need to safelist this class like we do here for example: https://github.com/nuxt/ui/blob/8d9b89dec7a82fab1379af0d7e7f237735ef3454/src/runtime/utils/colors.ts#L63

You can read more about this here: https://tailwindcss.com/docs/content-configuration#safelisting-classes

You can achieve this in your tailwind.config.ts:

import type { Config } from 'tailwindcss'

export default <Partial<Config>>{
  safelist: [{
    pattern: RegExp(`^text-(red|blue)-100$`),
    variants: ['hover']
  }]
}

It will be much easier in v3 as we've completely drop the need for safelists.

benjamincanac avatar Aug 07 '24 14:08 benjamincanac

Since hover:text-{color}-100 class doesn't exist, you need to safelist this class like we do here for example: https://github.com/nuxt/ui/blob/8d9b89dec7a82fab1379af0d7e7f237735ef3454/src/runtime/utils/colors.ts#L63

You can read more about this here: https://tailwindcss.com/docs/content-configuration#safelisting-classes

You can achieve this in your tailwind.config.ts:


import type { Config } from 'tailwindcss'



export default <Partial<Config>>{

  safelist: [{

    pattern: RegExp(`^text-(red|blue)-100$`),

    variants: ['hover']

  }]

}

It will be much easier in v3 as we've completely drop the need for safelists.

Right, but the documentation explicitly says

The module uses the Tailwind CSS safelist feature to force the generation of all the classes for the primary color only as it is the default color for all the components.

And I've used primary there.

I've looked through your source code and if I remember correctly nuxt/ui safelists only a few classes (bg- and some others), which means I have to safelist a load of classes. It's not just text-{color}- that I've used and the repro is just an example.

EDIT: Just an edit to my comment. The above might have come out in a negative way, but it wasn't meant so. I should have added above that a caveat in the documentation (that not all primary classes are actually generated) might be the right thing to do as there might be others who try to do the same.

letoast avatar Aug 08 '24 04:08 letoast

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

github-actions[bot] avatar Sep 08 '24 01:09 github-actions[bot]

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

github-actions[bot] avatar Nov 28 '24 02:11 github-actions[bot]

It will be much easier in v3 as we've completely drop the need for safelists.

Hi! @benjamincanac I'm currently using version 3 of Nuxt UI, but when setting up the safelist in tailwind.config.ts, it doesn't seem to work. How can something similar be implemented?

Thanks in advance

config file

import type { Config } from 'tailwindcss'
export default <Partial<Config>>{
  safelist: [
    {
      pattern: /ring-(slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(50|100|200|300|400|500|600|700|800|900|950)/,
    },
    {
      pattern: /accent-(slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(50|100|200|300|400|500|600|700|800|900|950)/,
    },
    {
      pattern: /text-(slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(50|100|200|300|400|500|600|700|800|900|950)/,
    },
    {
      pattern: /bg-(slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(50|100|200|300|400|500|600|700|800|900|950)/,
    },
    {
      pattern: /from-(slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(50|100|200|300|400|500|600|700|800|900|950)/,
    },
    {
      pattern: /from-(0%|5%|10%|15%|20%|25%|30%|35%|40%|45%|50%|55%|60%|65%|70%|75%|80%|85%|90%|95%|100%)/,
    },
    {
      pattern: /to-(slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(50|100|200|300|400|500|600|700|800|900|950)/,
    },
    {
      pattern: /to-(0%|5%|10%|15%|20%|25%|30%|35%|40%|45%|50%|55%|60%|65%|70%|75%|80%|85%|90%|95%|100%)/,
    },
    {
      pattern: /via-(slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(50|100|200|300|400|500|600|700|800|900|950)/,
    },
    {
      pattern: /via-(0%|5%|10%|15%|20%|25%|30%|35%|40%|45%|50%|55%|60%|65%|70%|75%|80%|85%|90%|95%|100%)/,
    },
    {
      pattern: /bg-linear-to-(t|tr|r|br|b|bl|l|tl)/,
    },
  ],
}

cesswhite avatar Jan 10 '25 23:01 cesswhite

@cesswhite You don't need safelists with Nuxt UI v3, would you mind opening a new issue with a reproduction of what you're trying to achieve?

benjamincanac avatar Jan 14 '25 09:01 benjamincanac

@benjamincanac The goal is to rebuild this tool: https://www.onamu.dev/ using UI v3. It's a tool for dynamic gradients, and the main objective is to ensure that all gradients are available, as we previously achieved with the safelist. However, some colors are not displaying correctly

Before submitting an issue, I’d like to confirm whether it’s truly an issue or just a mistake on my part.

cesswhite avatar Jan 14 '25 23:01 cesswhite

@cesswhite The safelist feature doesn't exists in Tailwind CSS v4, not sure to understand why you need it 🤔

benjamincanac avatar Jan 25 '25 13:01 benjamincanac

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

github-actions[bot] avatar Feb 25 '25 02:02 github-actions[bot]

I can confirm that I also get this bug in Nuxt UI v2.19.2

@benjamincanac Is there a plan to fix this in v2?

albertpratomo avatar Mar 31 '25 15:03 albertpratomo

@albertpratomo What bug? The solution to safelist classes has already been given above.

benjamincanac avatar Mar 31 '25 19:03 benjamincanac

@benjamincanac So do you mean the "Smart safelisting" feature by Nuxt UI is not working?

Image

And that we have to manually safelist classes via Tailwind config regex?

albertpratomo avatar Apr 01 '25 08:04 albertpratomo

I didn't mean that no, the smart safelisting is working fine. The smart safelisting applies when using Vue components, it's unrelated to the classes you write in your app.config.ts. As mentioned, here is the answer above https://github.com/nuxt/ui/issues/2007#issuecomment-2273551754 when using -{color}-. If it doesn't solve your issue or is unrelated, please open a new one with a reproduction.

benjamincanac avatar Apr 01 '25 09:04 benjamincanac

Ah okay, from the docs I assumed that the smart safelisting will check the classes I wrote in app.config.ts too. So seems like this assumption is wrong.

// in app.config.ts
ui: {
    button: {
      variant: {
        solid: 'bg-{color}-700',
      },
    },
}

// in nuxt.config.ts
ui: {
    safelistColors: ['primary'],
},

// in App.vue
<template>
  <UButton />
</template>

to clarify: Nuxt UI won't automatically safelist bg-primary-700?

albertpratomo avatar Apr 01 '25 11:04 albertpratomo

@benjamincanac sorry to ping, could you confirm?

albertpratomo avatar Apr 02 '25 09:04 albertpratomo