How to dedupe tailwind classes in bodyAttrs? #431
Unanswered
chrisjansky
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hi, thanks for the question. The simplest way to solve this would be to conditionally add the class in your app.vue. const route = useRoute()
useHead({
bodyAttrs: {
class: {
"bg-red-500": () => route.path !== '/'
}
},
}) There is a more complicated way to solve this in where we can use the Unhead hooks to modify the resolved tags using tw-merge, this is a rough untested code sample of how you might do that const head = injectHead()
head.hooks.hook('tags:afterResolve', (ctx) => {
// collect all body classes
const bodyClasses = ctx.tags.filter((tag) => tag.tag === 'bodyAttrs').flatMap((tag) => tag.props.class)
// apply tw-merge
const twClasses = twMerge(bodyClasses)
// filter out non-merged classes
for (const tag of ctx.tags) {
if (tag.tag === 'bodyAttrs') {
tag.props.class = twClasses
}
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I am using
unhead
as part ofnuxt@3
. Is there any way to dedupe tailwind classes whenuseHead()
is used in bothapp.vue
and inpages/
?Example:
Ideally I would dedupe them with something like tw-merge to get
class="bg-green-500"
only in the final output.Basically any approach for specifying default
<body>
classes and overrides for pages would be great.Thanks 🙏
Beta Was this translation helpful? Give feedback.
All reactions