Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/runtime/plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ import type { ColorModeInstance } from './types'
import { defineNuxtPlugin, isVue2, isVue3, useRouter, useHead, useState } from '#imports'
import { globalName, storageKey, dataValue, disableTransition, storage } from '#color-mode-options'

// Initialise to empty object to avoid hard error when hydrating app in test mode
const helper = (window[globalName] || {}) as unknown as {
type Helper = {
preference: string
value: string
getColorScheme: () => string
addColorScheme: (className: string) => void
removeColorScheme: (className: string) => void
}

let helper = window[globalName] as unknown as Helper

// Initialise to object with defaults and no-ops to avoid hard error when hydrating app in test mode
if (import.meta.test && !helper) {
helper = {
preference: 'light',
value: 'light',
getColorScheme: () => 'light',
addColorScheme: () => {},
removeColorScheme: () => {},
}
}

export default defineNuxtPlugin((nuxtApp) => {
const colorMode = useState<ColorModeInstance>('color-mode', () => reactive({
// For SPA mode or fallback
Expand Down