windicss / vue-windicss-preprocess

A Vue Loader to compile tailwindcss at build time.
MIT License
45 stars 3 forks source link

Programmatic dynamic classes do not render #17

Open itpropro opened 3 years ago

itpropro commented 3 years ago

I started using windicss in a Nuxt project and I have a lot of code like this:

      <NuxtLink
        v-for="(entry, index) in entries"
        :key="index"
        :to="entry.url"
        class="group flex items-center px-2 py-2 text-sm leading-6 font-normal rounded-md"
        :class="selectedClass(entry.url)"
      >
...
    function selectedClass(url) {
      return route.value.path === url
        ? 'bg-cyan-800 text-white'
        : 'text-cyan-100 hover:text-white hover:bg-cyan-600'
    }

But the above classes do not get evaluated. What can I do to make this work? EDIT: I have the most current version of Nuxt with vue-windicss-preprocess added as stated in the documentation.

theo-stocchetti commented 3 years ago

You can add your properties on safe list :

    WindiCSS({
      safelist: ['prose', 'prose-sm', 'm-auto', 'bg-cyan-800']
    })

At the same subject, I would like to find an indication for using a regex pattern on the safe list : bg-cyan-xxx , bg-xxx-xxx , '/^bg-\w+-\d{2,3}/'

itpropro commented 3 years ago

Thanks for your reply! I hoped there would be another way, as in the current version of tailwindcss it was not necessary to add these to the safelist.

arpadgabor commented 3 years ago

I have also encountered this issue. Such a feature would be useful for defining default classes in props (or objects where you can style an element based on a key) for more advanced styling (see Vue-Tailwind framework as example).

One solution I see that could work is exposing a function like apply(class) that can be used to define classes programatically and inject into the stylesheet.

voorjaar commented 3 years ago

I have also encountered this issue. Such a feature would be useful for defining default classes in props (or objects where you can style an element based on a key) for more advanced styling (see Vue-Tailwind framework as example).

One solution I see that could work is exposing a function like apply(class) that can be used to define classes programatically and inject into the stylesheet.

Thank you. I agree with this. But I think we will add twbg-red-500, add this class in the preprocessing, and remove the decorator, so that it will not affect the runtime, but also can complete the work.

alexanderniebuhr commented 3 years ago

cc @voorjaar do not know if that is anything helpful. But I liked the inspiration here. Just take it as an additional thought :) https://github.com/windicss/svelte-windicss-preprocess/issues/35#issuecomment-786499168

voorjaar commented 3 years ago

cc @voorjaar do not know if that is anything helpful. But I liked the inspiration here. Just take it as an additional thought :) windicss/svelte-windicss-preprocess#35 (comment)

Yes. that was what I mean.

voorjaar commented 3 years ago

we have a template syntax to solve this.

// .vue
import { windi } from 'windicss/helpers';

direction = windi`flex-row ...`

then we need vue preprocessor to support this.

we should match windi wrapped classes with regex, then interpret them when building css. @hannoeru

hannoeru commented 3 years ago

I think use https://github.com/windicss/nuxt-windicss-module can solve this problem, it will scan entire file and find classes.

voorjaar commented 3 years ago

Then we should archive this repo. Is there still someone using webpack with vue?

itpropro commented 3 years ago

we have a template syntax to solve this.

// .vue
import { windi } from 'windicss/helpers';

direction = windi`flex-row ...`

then we need vue preprocessor to support this.

we should match windi wrapped classes with regex, then interpret them when building css. @hannoeru

This doesn't work for me with the following code (with both, windicss and/or vue-windicss-preprocess):

<script>
import { windi } from 'windicss/helpers'
export default {
  setup() {
    const menuDefault = windi`bg-white text-brand-black`
    return { menuDefault }
  },
}
</script>

image

For now, I try to work with @apply statements wherever possible, but I don't really like using separate JS for this. It takes away the flexibility to create SFCs without a script block.

Then we should archive this repo. Is there still someone using webpack with vue?

As long as Vite is still in alpha and most production vue deployments are still vue 2 with webpack of course, this is the most effective way of using windicss in vue :)

hannoeru commented 3 years ago

@itpropro We have a webpack plugin can work with Vue 2, can you try it?