How to dynamically set body class?

let isDrawerOpen = $ref(false)

useHead({
bodyAttrs: {
class: isDrawerOpen ? 'overflow-hidden' : 'none',
},
})
let isDrawerOpen = $ref(false)

useHead({
bodyAttrs: {
class: isDrawerOpen ? 'overflow-hidden' : 'none',
},
})
toggling isDrawerOpen has no effect, class "none" added all the time.
15 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alexander Kim
Alexander KimOP2y ago
nah, i'm using reactivity transform, it works this way. The problem is useHead called only once, so it doesn't change when my reactive ref variable changes.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alexander Kim
Alexander KimOP2y ago
any ideas?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alexander Kim
Alexander KimOP2y ago
thought about it, but isn't it ugly?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alexander Kim
Alexander KimOP2y ago
seems like i have no other way, thanks
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alexander Kim
Alexander KimOP2y ago
yeah, i kinda recall it worked in Nuxt2 i could just call this.head but it was a bit hacky seems like no one has idea about that lol
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
harlan
harlan2y ago
useHead is fully reactive
harlan
harlan2y ago
Class Attribute · Unhead
Learn how to use the class attribute with Unhead.
harlan
harlan2y ago
the issue is that you're resolving the ref value as part of the argument
let isDrawerOpen = ref(false)

useHead({
bodyAttrs: {
class: () => isDrawerOpen.value ? 'overflow-hidden' : 'none',
},
})
let isDrawerOpen = ref(false)

useHead({
bodyAttrs: {
class: () => isDrawerOpen.value ? 'overflow-hidden' : 'none',
},
})
that should work
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server