Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(theme): support vueuse/head v1.0 #16721

Merged
merged 4 commits into from
Feb 21, 2023
Merged

fix(theme): support vueuse/head v1.0 #16721

merged 4 commits into from
Feb 21, 2023

Conversation

KaelWD
Copy link
Member

@KaelWD KaelWD commented Feb 20, 2023

Description

Markup:

<template>
  <v-app>
    <v-container>
      <v-btn color="primary" @click="toggle">Toggle theme</v-btn>
    </v-container>
  </v-app>
</template>

<script setup>
  import { useTheme } from 'vuetify'

  const theme = useTheme()

  function toggle () {
    theme.themes.value.light.colors.primary = theme.themes.value.light.colors.primary === '#1976d2' ? '#ff0000' : '#1976d2'
  }
</script>

@harlan-zw
Copy link

harlan-zw commented Feb 21, 2023

Not sure why the computed isn't being watched with watchEffects but you can make use of the API returned by push.

const getHead = () => ({
  style: [
    {
      children: styles.value,
      id: 'vuetify-theme-stylesheet',
      nonce: parsedOptions.cspNonce || false as never,
    },
  ],
})
// vueuse/head >= v1
if (head.push) {
  const entry = head.push(getHead)
  watch(styles, () => { entry.patch(getHead) })
} else {
  // vueuse/head < v1
  if (IN_BROWSER) {
    head.addHeadObjs(computed(getHead))
    watchEffect(() => head.updateDOM())
  } else {
    head.addHeadObjs(getHead())
  }
}

That seems to work but I only tested it with the playground code you posted

@harlan-zw
Copy link

harlan-zw commented Feb 21, 2023

Oh wait I know, because all of the reactivity is handled by the useHead function itself, these lower-level APIs are language agnostic, so they don't handle any of the reactivity for you.

See https://github.com/unjs/unhead/blob/main/packages/vue/src/runtime/composables/useHead/clientUseHead.ts, basically does the same thing as the code posted above

@KaelWD
Copy link
Member Author

KaelWD commented Feb 21, 2023

Makes sense, thanks. The unhead docs are a bit light on internals.

@KaelWD KaelWD added T: bug Functionality that does not work as intended/expected E: theme Theme composable labels Feb 21, 2023
@KaelWD KaelWD added this to the v3.1.x milestone Feb 21, 2023
@KaelWD KaelWD self-assigned this Feb 21, 2023
@KaelWD
Copy link
Member Author

KaelWD commented Feb 21, 2023

This was a breaking change: harlan-zw/zhead@20884dc

@KaelWD KaelWD changed the title chore: update @vueuse/head to 1.0.26 fix(theme): support vueuse/head v1.0 Feb 21, 2023
@KaelWD KaelWD marked this pull request as ready for review February 21, 2023 11:09
@KaelWD KaelWD merged commit f07101c into master Feb 21, 2023
@KaelWD KaelWD deleted the 16156-update-vueuse-head branch February 21, 2023 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E: theme Theme composable T: bug Functionality that does not work as intended/expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants