Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 1c68336

Browse files
committed
feat(colormode): add injection of global styles
1 parent b98fd81 commit 1c68336

File tree

7 files changed

+49
-37
lines changed

7 files changed

+49
-37
lines changed

components.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* This is a generated file. Do not edit it's contents.
88
*
9-
* This file was generated on 2021-07-16T08:32:18.292Z
9+
* This file was generated on 2021-07-16T10:09:49.527Z
1010
*/
1111

1212
import { ChakraProps } from '@chakra-ui/vue-system'

packages/c-color-mode/src/color-mode-provider.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { __DEV__ } from '@chakra-ui/utils'
22
import { inject, isRef, ref, watchEffect } from 'vue'
3-
import { ColorMode } from './color-mode.utils'
3+
import { ColorModeRef } from './color-mode.utils'
44

5-
export type { ColorMode }
5+
export type { ColorModeRef }
66

77
export interface ColorModeOptions {
8-
initialColorMode?: ColorMode
8+
initialColorMode?: ColorModeRef
99
useSystemColorMode?: boolean
1010
}
1111

1212
export type ColorModeContext = {
13-
colorMode: ColorMode
13+
colorMode: ColorModeRef
1414
toggleColorMode: () => void
1515
}
1616

1717
/** Injects color mode into component instance */
1818
export const useColorMode = (): ColorModeContext => {
19-
const _colorMode = inject('$chakraColorMode') as ColorMode
19+
const _colorMode = inject('$chakraColorMode') as ColorModeRef
2020
const colorMode = isRef(_colorMode) ? _colorMode : ref(_colorMode)
2121

2222
const toggleColorMode = () => {
@@ -36,8 +36,8 @@ export const useColorMode = (): ColorModeContext => {
3636
/**
3737
* Change value based on color mode.
3838
*
39-
* @param light the light mode value
40-
* @param dark the dark mode value
39+
* @param lightValue the light mode value
40+
* @param darkValue the dark mode value
4141
*
4242
* @example
4343
*
@@ -46,14 +46,14 @@ export const useColorMode = (): ColorModeContext => {
4646
* ```
4747
*/
4848
export function useColorModeValue<TLight = unknown, TDark = unknown>(
49-
light: TLight,
50-
dark: TDark
49+
lightValue: TLight,
50+
darkValue: TDark
5151
) {
5252
const { colorMode } = useColorMode()
5353
const modeValue = ref()
5454

5555
watchEffect(() => {
56-
modeValue.value = colorMode.value === 'dark' ? dark : light
56+
modeValue.value = colorMode.value === 'dark' ? darkValue : lightValue
5757
})
5858

5959
return modeValue

packages/c-color-mode/src/color-mode.utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const classNames = {
66
dark: 'chakra-ui-dark',
77
}
88

9-
export type ColorMode = Ref<'light' | 'dark'>
9+
export type ColorModeRef = Ref<'light' | 'dark'>
1010

1111
/**
1212
* SSR: Graceful fallback for the `body` element
@@ -45,7 +45,7 @@ export const queries = {
4545
export const lightQuery = queries.light
4646
export const darkQuery = queries.dark
4747

48-
export function getColorScheme(fallback?: ColorMode) {
48+
export function getColorScheme(fallback?: ColorModeRef) {
4949
const isDark = getMediaQuery(queries.dark) ?? fallback?.value === 'dark'
5050
return isDark ? 'dark' : 'light'
5151
}
@@ -77,8 +77,8 @@ export const root = {
7777
get: () =>
7878
document.documentElement.style.getPropertyValue(
7979
'--chakra-ui-color-mode'
80-
) as ColorMode['value'],
81-
set: (mode: ColorMode) => {
80+
) as ColorModeRef['value'],
81+
set: (mode: ColorModeRef) => {
8282
if (isBrowser) {
8383
document.documentElement.style.setProperty(
8484
'--chakra-ui-color-mode',

packages/c-color-mode/src/storage-manager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { __DEV__ } from '@chakra-ui/utils'
2-
import { ColorMode } from './color-mode.utils'
2+
import { ColorModeRef } from './color-mode.utils'
33

44
const hasSupport = () => typeof Storage !== 'undefined'
55
export const storageKey = 'chakra-ui-color-mode'
66

7-
type MaybeColorMode = ColorMode['value'] | undefined
7+
type MaybeColorMode = ColorModeRef['value'] | undefined
88

99
export interface StorageManager {
10-
get(init?: ColorMode['value']): MaybeColorMode
11-
set(value: ColorMode['value']): void
10+
get(init?: ColorModeRef['value']): MaybeColorMode
11+
set(value: ColorModeRef['value']): void
1212
type: 'cookie' | 'localStorage'
1313
}
1414

@@ -49,7 +49,7 @@ export const cookieStorageManager = (cookies = ''): StorageManager => ({
4949
const match = cookies.match(new RegExp(`(^| )${storageKey}=([^;]+)`))
5050

5151
if (match) {
52-
return match[2] as ColorMode['value']
52+
return match[2] as ColorModeRef['value']
5353
}
5454

5555
return init

packages/core/src/helpers/css-reset.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { injectGlobal } from '@chakra-ui/vue-system'
22
import { cssResetStyles } from '@chakra-ui/c-reset'
33
import { ThemeOverride } from '../extend-theme'
44
import { get, runIfFn } from '@chakra-ui/utils'
5-
import { ColorMode } from '@chakra-ui/c-color-mode'
5+
import { ColorModeRef } from '@chakra-ui/c-color-mode'
66
import { css } from '@chakra-ui/styled-system'
7-
import { Ref } from 'vue'
87

98
/** Injects CSS reset styles */
109
export function injectResetStyles() {
@@ -14,7 +13,7 @@ export function injectResetStyles() {
1413
/** Injects styles from `theme.styles.global` property */
1514
export function injectThemeGlobalStyles(
1615
theme: ThemeOverride,
17-
colorMode: Ref<ColorMode>
16+
colorMode: ColorModeRef
1817
) {
1918
const styleObjectOrFn = get(theme, 'styles.global')
2019
const globalStyles = runIfFn(styleObjectOrFn, {
@@ -23,4 +22,5 @@ export function injectThemeGlobalStyles(
2322
})
2423
if (!globalStyles) return undefined
2524
const styles = css(globalStyles)(theme)
25+
injectGlobal(styles)
2626
}

packages/core/src/helpers/plugin.types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ColorMode } from '@chakra-ui/c-color-mode'
1+
import { ColorModeRef } from '@chakra-ui/c-color-mode'
2+
import { UnwrapRef } from 'vue-demi'
23
import { ThemeOverride } from '../extend-theme'
34

45
interface ExtendIconsPath {
@@ -15,5 +16,5 @@ export interface ChakraPluginOptions {
1516
cssReset?: boolean
1617
extendTheme?: ThemeOverride
1718
icons?: IconsOptions
18-
defaultColorMode?: ColorMode
19+
defaultColorMode?: UnwrapRef<ColorModeRef>
1920
}

packages/core/src/index.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { computed, Plugin, Ref, ref, watch } from 'vue'
2-
import defaultTheme, { ColorMode } from '@chakra-ui/vue-theme'
1+
import { computed, Plugin, Ref, ref, UnwrapRef } from 'vue'
2+
import defaultTheme from '@chakra-ui/vue-theme'
3+
import type { ColorModeRef } from '@chakra-ui/c-color-mode'
34
import { toCSSVar, WithCSSVar } from '@chakra-ui/styled-system'
45
import { chakra, injectGlobal, css } from '@chakra-ui/vue-system'
56
import internalIcons from './icon.internals'
67
import { extendTheme, ThemeOverride } from './extend-theme'
78
import { MergedIcons, parseIcons } from './parse-icons'
9+
import { injectResetStyles, injectThemeGlobalStyles } from './helpers/css-reset'
810
import { mode } from '@chakra-ui/vue-theme-tools'
911
import { ChakraPluginOptions } from './helpers/plugin.types'
1012

1113
/**
1214
* Helper function to extend Chakra plugin with options
13-
* It just returns its arguments
15+
* It just returns its arguments with typescript types added
1416
*/
1517
export function extendChakra(
1618
options: ChakraPluginOptions = { cssReset: true }
@@ -27,25 +29,34 @@ const ChakraUIVuePlugin: Plugin = {
2729
const computedTheme = computed<WithCSSVar<ThemeOverride>>(() =>
2830
toCSSVar(theme)
2931
)
32+
33+
// Inject Chakra CSS variables
3034
injectGlobal({
3135
':root': computedTheme.value.__cssVars,
3236
})
3337

34-
let libraryIcons = options.icons?.library || {}
35-
let extendedIcons = options.icons?.extend || {}
36-
3738
// Initialize colormode
38-
const colorMode: ColorMode = theme.config?.initialColorMode || 'light'
39+
const colorMode: UnwrapRef<ColorModeRef> =
40+
theme.config?.initialColorMode || 'light'
41+
42+
// Provide initial colormode
43+
app.config.globalProperties.$initialColorMode = colorMode
44+
45+
const colorModeRef = ref(colorMode) as ColorModeRef
46+
app.provide<ColorModeRef>('$chakraColorMode', colorModeRef)
3947

48+
if (options.cssReset) {
49+
injectResetStyles()
50+
}
51+
// Inject `styles.global` in document
52+
injectThemeGlobalStyles(computedTheme.value, colorModeRef)
53+
54+
let libraryIcons = options.icons?.library || {}
55+
let extendedIcons = options.icons?.extend || {}
4056
// Bind theme to application global properties and provide to application
4157
app.config.globalProperties.$chakraTheme = computedTheme.value
4258
app.provide('$chakraTheme', computedTheme.value as ThemeOverride)
4359

44-
// Provide initial colormode
45-
app.config.globalProperties.$initialColorMode = colorMode
46-
const colorModeRef = ref<ColorMode>(colorMode)
47-
app.provide<Ref<ColorMode>>('$chakraColorMode', colorModeRef)
48-
4960
libraryIcons = parseIcons(libraryIcons)
5061

5162
// Merge internal icons and library icons

0 commit comments

Comments
 (0)