-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useTheme() and useThemeProvider()
- Loading branch information
1 parent
a12933a
commit 31b9613
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default defineAppConfig({ | ||
themes: ['light', 'dark'], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { provide } from 'vue' | ||
import type { ComputedRef, InjectionKey } from 'vue' | ||
import { type Theme, useTheme } from '~/composables/use-theme' | ||
import type { ThemeOptions } from '~/composables/use-theme' | ||
|
||
export const THEME_PROVIDER_KEY: InjectionKey<ComputedRef<Theme | 'light'>> = Symbol('themeProviderKey') | ||
|
||
export function useThemeProvider(options?: ThemeOptions) { | ||
const { activeTheme, themeClass } = useTheme(options) | ||
|
||
const providedTheme = computed(() => { | ||
return activeTheme.value | ||
}) | ||
|
||
provide(THEME_PROVIDER_KEY, providedTheme) | ||
|
||
return { providedTheme, activeTheme, themeClass } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { inject } from 'vue' | ||
import type { MaybeRefOrGetter } from 'vue' | ||
import { THEME_PROVIDER_KEY } from '~/composables/use-theme-provider' | ||
import appConfig from '~/app.config' | ||
|
||
export type Theme = (typeof appConfig)['themes'][number] | ||
|
||
export interface ThemeProps { | ||
theme?: Theme | false | null | ||
} | ||
|
||
export interface ThemeOptions { | ||
props?: ThemeProps | ||
preferredTheme?: MaybeRefOrGetter<Theme | false | null> | ||
} | ||
|
||
export function useTheme(options?: ThemeOptions) { | ||
const injectedTheme = inject(THEME_PROVIDER_KEY, null) | ||
const $style = useCssModule() | ||
|
||
const activeTheme = computed(() => { | ||
return toValue(options?.preferredTheme) || options?.props?.theme || injectedTheme?.value || 'light' | ||
}) | ||
|
||
const themeClass = computed(() => { | ||
return activeTheme.value && !!$style && $style['root--theme-' + activeTheme.value] | ||
}) | ||
|
||
return { activeTheme, themeClass } | ||
} |