Get generated class(es) without adding styles to sheet #80
-
Hey guys! I have a potentially odd question: Is it possible to retrieve the generated class(es) from I'm still working out how to use Twind with Nuxt (Universal + Static) and I've gotten to a point where I've got a server-side directive with my own What I'm trying to resolve is having the client-side directive check to see if Twind classes have already been added to the element as to avoid unnecessary client-side styling (and duplicate element classes). Currently, I'm simply invoking the If anyone has any ideas on how to improve my approach, I would love some feedback! Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Hey 👋 good question and yes it is possible! We do our tests like this.. you will need to use a custom import { create } from 'twind'
import { virtualSheet } from 'twind/sheets'
const sheet = virtualSheet()
const { tw } = create({
sheet: sheet,
// Fail tests on unknown rules or theme values
mode: 'strict',
// Prevent preflight rules to be added into sheet
preflight: false,
// Do not prefix properties and values
prefix: false,
});
tw`text(2xl black)`
console.log(sheet.target)
// => .text-2xl{font-size:2rem}.text-black{color:black} With regards not duplicating work that the server has already done.. that is quite tough. See this issue #78 (comment) |
Beta Was this translation helpful? Give feedback.
-
Thanks for getting back to me, Luke! So thinking about this more, what if I were to do something crazy like create two instances of import { create } from 'twind'
import { virtualSheet } from 'twind/sheets'
import { theme } from '../../theme'
const { tw } = create({
darkMode: 'class',
preflight: true,
prefix: true,
theme,
})
const sheet = virtualSheet()
const { tw: preGen } = create({ theme, sheet })
export const twDirective = {
inserted(el, binding) {
const className = preGen(binding.value)
if (el.className.length > 0) {
if (!el.className.includes(className)) {
el.className += ` ${tw(binding.value)}`
}
} else {
el.className = tw(binding.value)
}
},
} Is this a good idea? |
Beta Was this translation helpful? Give feedback.
-
So I just wanted to share that I've taken a different look at my initial approach and have come up with what is probably a much better solution. https://codesandbox.io/s/nuxtjs-twind-s90bw?file=/plugins/twind.js Ultimately I've just included Twind as a global instance and, by using a computed property, I can apply styles directly to elements in my template via a class binding. I'm still testing it out, but I think this is a much simpler, much less over-engineered approach. |
Beta Was this translation helpful? Give feedback.
So I just wanted to share that I've taken a different look at my initial approach and have come up with what is probably a much better solution.
https://codesandbox.io/s/nuxtjs-twind-s90bw?file=/plugins/twind.js
Ultimately I've just included Twind as a global instance and, by using a computed property, I can apply styles directly to elements in my template via a class binding. I'm still testing it out, but I think this is a much simpler, much less over-engineered approach.