Skip to content

Commit

Permalink
feat(nuxt): save theme in cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ksem committed May 1, 2023
1 parent 2ab22ec commit b896d2a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 37 deletions.
20 changes: 2 additions & 18 deletions packages/docs/composables/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
export const useTheme = () => {
const colorMode = useColorMode()
const cookie = useCookie('vuestic-theme')

const { applyPreset } = useColors()

const setTheme = (theme?: string) => {
if (theme) {
colorMode.preference = theme
applyPreset(theme)
cookie.value = theme
return
}

if (cookie.value) {
applyPreset(cookie.value)
return
}

if (!colorMode.unknown) {
applyPreset(colorMode.value)
}
if (theme === undefined) { return }
applyPreset(theme)
}

return {
Expand Down
5 changes: 0 additions & 5 deletions packages/docs/config/vuestic-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const VaButtonLandingHeader = {
'hover-opacity': '1',
}

// const cookie = useCookie('vuestic-theme')

const theme = 'light'

export const VuesticConfig = defineVuesticConfig({
icons,
components: {
Expand All @@ -39,7 +35,6 @@ export const VuesticConfig = defineVuesticConfig({
},
},
colors: {
currentPresetName: theme,
presets: {
light: {
secondary: '#666E75',
Expand Down
3 changes: 1 addition & 2 deletions packages/docs/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
import { useColors } from 'vuestic-ui'
import { useDocsScroll } from '../composables/useDocsScroll';
const colorMode = useColorMode()
const cookie = useCookie('vuestic-theme')
const { applyPreset } = useColors()
const breakpoints = useBreakpoint()
const isSidebarVisible = ref(!breakpoints.smDown)
applyPreset(cookie.value || colorMode.preference)
applyPreset(cookie.value || 'light')
watch(() => breakpoints.smDown, (newValue, oldValue) => {
if (newValue && !oldValue) {
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default defineNuxtModule<VuesticOptions>({
config: {},
css: ['smart-helpers', 'typography'],
fonts: true,
themeCookieKey: 'vuestic-theme',
},

setup (options) {
Expand Down
28 changes: 22 additions & 6 deletions packages/nuxt/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
VaModalPlugin,
ColorsClassesPlugin,
BreakpointConfigPlugin,
type GlobalConfig,
type PartialGlobalConfig
} from 'vuestic-ui'
import { markRaw, computed } from 'vue'
import { markRaw, computed, watch, type Ref } from 'vue'

import { defineNuxtPlugin } from '#app'
import { defineNuxtPlugin, useCookie } from '#app'
import { useHead } from '#imports'
import NuxtLink from '#app/components/nuxt-link'
import configFromFile from '#vuestic-config'
Expand All @@ -19,16 +21,24 @@ function getGlobalProperty (app, key) {
return app.config.globalProperties[key]
}

export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(async (nuxtApp) => {
const { vueApp: app } = nuxtApp

// It's important to use `, because TS will compile qoutes to " and JSON will not be parsed...
const { config }: VuesticOptions = JSON.parse(`<%= options.value %>`)
const userConfig = configFromFile || config
const moduleOptions: VuesticOptions = JSON.parse(`<%= options.value %>`)
const themeCookie = useCookie(moduleOptions.themeCookieKey)
const userConfig = configFromFile || moduleOptions.config || {}
const configWithColors: PartialGlobalConfig = {
...userConfig,
colors: {
currentPresetName: themeCookie.value || userConfig.colors?.currentPresetName,
...userConfig.colors,
}
}

/** Use tree-shaking by default and do not register any component. Components will be registered by nuxt in use-components. */
app.use(createVuesticEssential({
config: { ...userConfig, routerComponent: markRaw(NuxtLink) },
config: { ...configWithColors, routerComponent: markRaw(NuxtLink) },
// TODO: Would be nice to tree-shake plugins, but they're small so we can let it be for now.
// Should be synced with create-vuestic.ts
plugins: {
Expand Down Expand Up @@ -63,4 +73,10 @@ export default defineNuxtPlugin((nuxtApp) => {
}
}))
}

// Watch for preset name change and update cookie
const { globalConfig } = getGlobalProperty(app, '$vaConfig') as { globalConfig: Ref<GlobalConfig> }
watch(() => globalConfig.value.colors.currentPresetName, (newTheme) => {
themeCookie.value = newTheme
}, { immediate: true })
})
13 changes: 7 additions & 6 deletions packages/nuxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ export interface VuesticOptions {
* @see https://vuestic.dev/en/getting-started/installation#assets-installation
*/
fonts: boolean
}

/** Declare Vuestic module options in NuxtConfig */
declare module '@nuxt/schema' {
interface NuxtConfig {
vuestic?: Partial<VuesticOptions>
}

/**
* Vuestic will automatically store its theme in cookies. If you want to change the key, you can do it here.
*
* @default 'vuestic-theme'
*/
themeCookieKey: string
}

0 comments on commit b896d2a

Please sign in to comment.