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

Commit b874a88

Browse files
committed
fix: update types
1 parent 077248e commit b874a88

File tree

10 files changed

+146
-22
lines changed

10 files changed

+146
-22
lines changed

components.d.ts

Lines changed: 1 addition & 2 deletions
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 2022-03-03T13:59:03.450Z
9+
* This file was generated on 2022-03-05T09:54:15.061Z
1010
*/
1111

1212
import { ChakraProps, chakra } from "@chakra-ui/vue-system"
@@ -191,7 +191,6 @@ declare module "@vue/runtime-core" {
191191
/* Component custom props types for JSX and TSX auto complete */
192192
export interface ComponentCustomProps
193193
extends JsxComponentCustomProps,
194-
ComponentCustomProps,
195194
ChakraProps {
196195
vSlots?: {
197196
[eleName: string]: JSX.Element

packages/core/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
"@chakra-ui/vue-system": "0.1.0-alpha.10",
5151
"@chakra-ui/vue-theme": "1.0.0-alpha.10",
5252
"@chakra-ui/vue-theme-tools": "0.1.0-alpha.10",
53-
"@emotion/cache": "^11.7.1"
53+
"@emotion/cache": "^11.7.1",
54+
"@emotion/serialize": "^1.0.2",
55+
"@emotion/sheet": "^1.1.0",
56+
"@emotion/utils": "^1.1.0"
5457
},
5558
"devDependencies": {
5659
"vue": "^3.2.29"

packages/core/src/extend-theme.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ type DeepThemeExtension<ThemeObject, TypeHints> = {
1717
| (ThemeObject[Key] extends (...args: any[]) => any
1818
? Partial<ReturnType<ThemeObject[Key]>>
1919
: Partial<ThemeObject[Key]>) // allow function or object
20-
} &
21-
Partial<TypeHints> &
20+
} & Partial<TypeHints> &
2221
Record<string, any> // escape hatch
2322

2423
export type ThemeOverride = DeepThemeExtension<Theme, ThemeExtensionTypeHints>

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

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { cssResetStyles } from "@chakra-ui/c-reset"
33
import { ThemeOverride } from "../extend-theme"
44
import { get, runIfFn } from "@chakra-ui/utils"
55
import { ColorModeRef } from "@chakra-ui/c-color-mode"
6-
import { computed, ref, watch } from "vue"
6+
import { serializeStyles } from "@emotion/serialize"
7+
import { StyleSheet } from "@emotion/sheet"
8+
import { computed, getCurrentInstance, ref, watch, watchEffect } from "vue"
9+
import createCache, { EmotionCache } from "@emotion/cache"
10+
import { insertStyles, SerializedStyles } from "@emotion/utils"
711

812
/** Injects CSS reset styles */
913
export function injectResetStyles() {
@@ -13,6 +17,7 @@ export function injectResetStyles() {
1317
/** Injects styles from `theme.styles.global` property */
1418
export function injectThemeGlobalStyles(
1519
theme: ThemeOverride,
20+
_cache: EmotionCache,
1621
colorMode: ColorModeRef
1722
) {
1823
const styleObjectOrFn = get(theme, "styles.global")
@@ -23,15 +28,83 @@ export function injectThemeGlobalStyles(
2328
})
2429
)
2530
if (!globalStyles.value) return undefined
31+
32+
const cache = createCache({
33+
key: "chakra-global",
34+
nonce: "__chakra__",
35+
})
36+
37+
const _globalStyles = computed(() => css(globalStyles.value)(theme))
38+
39+
const serializedStyles = computed<SerializedStyles>(() =>
40+
serializeStyles([_globalStyles.value], cache.registered, theme)
41+
)
42+
2643
const id = ref(0)
44+
const sheetRef = ref<[sheet: StyleSheet, hydrating: boolean]>()
45+
46+
console.log(cache)
47+
2748
watch(
28-
colorMode,
49+
() => cache,
2950
() => {
30-
const styles = css(globalStyles.value)(theme)
31-
injectGlobal(styles)
51+
const key = `${cache.key}`
52+
53+
let sheet = new StyleSheet({
54+
key: `${key} ${serializedStyles.value.name}`,
55+
nonce: cache.sheet.nonce,
56+
container: cache.sheet.container,
57+
// @ts-expect-error `isSpeedy` is not typed
58+
speedy: cache.sheet.isSpeedy,
59+
})
60+
61+
let rehydrating = false
62+
let node: HTMLStyleElement | null = document.querySelector(
63+
`style[data-emotion="${key} ${serializedStyles.value.name}"]`
64+
)
65+
66+
if (cache.sheet.tags.length) {
67+
sheet.before = cache.sheet.tags[0]
68+
}
69+
70+
if (node !== null) {
71+
rehydrating = true
72+
// clear the hash so this node won't be recognizable as rehydratable by other <Global/>s
73+
node.setAttribute(
74+
"data-emotion",
75+
`${key} ${serializedStyles.value.name}`
76+
)
77+
sheet.hydrate([node])
78+
}
79+
80+
sheetRef.value = [sheet, rehydrating]
3281
},
3382
{
3483
immediate: true,
3584
}
3685
)
86+
87+
watchEffect((_) => {
88+
const [sheet, rehydrating] = sheetRef.value!
89+
90+
if (rehydrating) {
91+
sheetRef.value![1] = false
92+
}
93+
94+
if (serializedStyles.value.next !== undefined) {
95+
// insert keyframes
96+
insertStyles(cache, serializedStyles.value.next, true)
97+
}
98+
99+
if (sheet.tags.length) {
100+
// if this doesn't exist then it will be
101+
// null so the style element will be appended
102+
let element = sheet.tags[sheet.tags.length - 1].nextElementSibling
103+
console.log({ element })
104+
sheet.before = element
105+
sheet.flush()
106+
}
107+
108+
cache.insert(``, serializedStyles.value, sheet, false)
109+
})
37110
}

packages/core/src/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
EmotionThemeContextSymbol,
88
EmotionCacheInjectionSymbol,
99
} from "@chakra-ui/vue-styled"
10-
import createCache from "@emotion/cache"
10+
import createCache, { EmotionCache } from "@emotion/cache"
1111
import internalIcons from "./icon.internals"
1212
import { extendTheme, ThemeOverride } from "./extend-theme"
1313
import { MergedIcons, parseIcons } from "./parse-icons"
@@ -60,8 +60,6 @@ const ChakraUIVuePlugin: Plugin = {
6060
if (options.cssReset) {
6161
injectResetStyles()
6262
}
63-
// Inject `styles.global` in document
64-
injectThemeGlobalStyles(computedTheme.value, colorModeRef)
6563

6664
let libraryIcons = options.icons?.library || {}
6765
let extendedIcons = options.icons?.extend || {}
@@ -72,12 +70,23 @@ const ChakraUIVuePlugin: Plugin = {
7270
app.provide(EmotionThemeContextSymbol, computedTheme.value)
7371
app.provide("$chakraTheme", computedTheme.value as ThemeOverride)
7472

73+
let emotionCache: EmotionCache | null = null
7574
// Provide emotion cache
7675
if (options.emotionCacheOptions) {
77-
const emotionCache = createCache(options.emotionCacheOptions)
76+
emotionCache = createCache(options.emotionCacheOptions)
7877
app.provide(EmotionCacheInjectionSymbol, emotionCache)
7978
}
8079

80+
if (!emotionCache) {
81+
emotionCache = createCache({
82+
key: "chakra",
83+
nonce: `chakra-global-cache-${Date.now()}`,
84+
})
85+
}
86+
87+
// Inject `styles.global` in document
88+
injectThemeGlobalStyles(computedTheme.value, emotionCache, colorModeRef)
89+
8190
libraryIcons = parseIcons(libraryIcons)
8291

8392
// Merge internal icons and library icons

packages/test-utils/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
"clean": "rimraf dist"
2121
},
2222
"dependencies": {
23+
"@chakra-ui/vue-styled": "^0.0.0-alpha.2",
2324
"@chakra-ui/vue-theme": "1.0.0-alpha.10",
2425
"@testing-library/jest-dom": "^5.14.1",
2526
"@testing-library/user-event": "^13.2.1",
2627
"@testing-library/vue": "^6.4.2",
2728
"jest-axe": "^5.0.1",
2829
"vue": "^3.2.29"
2930
}
30-
}
31+
}

playground/src/App.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<c-reset />
3-
<chakra.section d="flex" transition="all 0.2s" height="inherit" w="inherit" :sx="rootStyles">
3+
<chakra.section d="flex" transition="all 0.2s" height="inherit" w="inherit">
44
<perfect-scrollbar>
55
<chakra.div pb="12">
66
<sidebar :stories="routes" />
@@ -33,11 +33,10 @@ export default defineComponent({
3333
const { colorMode, toggleColorMode } = useColorMode()
3434
3535
const rootStyles = computed(() => {
36-
3736
const styles = {
3837
light: {
3938
bg: 'white',
40-
// color: 'blackAlpha.800',
39+
color: 'blackAlpha.800',
4140
'a.router-link-active': {
4241
color: 'teal.500',
4342
fontSize: '0.9rem',
@@ -47,12 +46,12 @@ export default defineComponent({
4746
},
4847
dark: {
4948
bg: 'gray.800',
50-
// color: 'whiteAlpha.800',
49+
color: 'whiteAlpha.800',
5150
'a.router-link-active': {
5251
color: 'teal.200',
5352
fontSize: '0.9rem',
5453
fontWeight: 'bold',
55-
textDecoration: 'underline'
54+
textDecoration: 'underline',
5655
}
5756
},
5857
}
@@ -63,6 +62,10 @@ export default defineComponent({
6362
}
6463
})
6564
65+
watchEffect(() => {
66+
console.log("rootStyles", rootStyles.value)
67+
})
68+
6669
return {
6770
routes,
6871
colorMode,

playground/src/main.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import PerfectScrollbar from "vue3-perfect-scrollbar"
1111
import { MotionPlugin } from "@vueuse/motion"
1212
import App from "./App.vue"
1313
import router from "./router"
14+
import { mode } from "@chakra-ui/vue-theme-tools"
1415

1516
console.log({ router })
1617

@@ -34,7 +35,7 @@ const app = createApp(App)
3435
},
3536
},
3637
emotionCacheOptions: {
37-
key: "css",
38+
key: "chakra",
3839
},
3940
extendTheme: extendTheme({
4041
config: {},
@@ -52,7 +53,14 @@ const app = createApp(App)
5253
global: (props: any) => {
5354
return {
5455
body: {
55-
// color: mode('gray.700', 'whiteAlpha.900')(props),
56+
bg: mode("white", "gray.800")(props),
57+
color: mode("blackAlpha.800", "whiteAlpha.800")(props),
58+
"a.router-link-active": {
59+
color: mode("teal.500", "teal.200")(props),
60+
fontSize: mode("0.9rem", "0.9rem")(props),
61+
fontWeight: mode("bold", "bold")(props),
62+
textDecoration: mode("underline", "underline")(props),
63+
},
5664
},
5765
}
5866
},

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
2727
},
2828
"include": [
29-
"components.d.ts",
29+
"./components.d.ts",
3030
"packages",
3131
"playground/**/*.ts",
3232
"playground/**/*.d.ts",

yarn.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,30 @@
12681268
framesync "5.3.0"
12691269
lodash.mergewith "4.6.2"
12701270

1271+
"@chakra-ui/vue-styled@^0.0.0-alpha.2":
1272+
version "0.0.0-alpha.2"
1273+
resolved "https://registry.yarnpkg.com/@chakra-ui/vue-styled/-/vue-styled-0.0.0-alpha.2.tgz#046aeff8f521d4da0bc33964f6728f64a0784b3d"
1274+
integrity sha512-iB6+hl60knaASd4HuV7HcHcZ2GUMAcyN9eFjtbpRCxdhMQWDVbvMZM0AWNsgi2F5ReRmUsvwwqyMaF5T6+V0aQ==
1275+
dependencies:
1276+
"@chakra-ui/vue-utils" "0.1.0-alpha.12"
1277+
"@emotion/cache" "^11.7.1"
1278+
"@emotion/css" "^11.7.1"
1279+
"@emotion/serialize" "^1.0.2"
1280+
"@emotion/utils" "^1.0.0"
1281+
"@emotion/weak-memoize" "^0.2.5"
1282+
clsx "^1.1.1"
1283+
lodash.camelcase "^4.3.0"
1284+
1285+
"@chakra-ui/[email protected]":
1286+
version "0.1.0-alpha.12"
1287+
resolved "https://registry.yarnpkg.com/@chakra-ui/vue-utils/-/vue-utils-0.1.0-alpha.12.tgz#d0cd4de1688d07452d82e7055891a9fefbbe0f11"
1288+
integrity sha512-nANxYyZbA3lbSlGVZJNLb1T6O8nlSa6tgk9N31xvCzKvttFgDp/KOXxodDQaK+YwwpzU1qrSHzy6IDf6VKP/wQ==
1289+
dependencies:
1290+
"@chakra-ui/utils" "^1.9.1"
1291+
css-box-model "1.2.1"
1292+
lodash.mergewith "^4.6.2"
1293+
object-assign "^4.1.1"
1294+
12711295
"@changesets/apply-release-plan@^5.0.4":
12721296
version "5.0.4"
12731297
resolved "https://registry.yarnpkg.com/@changesets/apply-release-plan/-/apply-release-plan-5.0.4.tgz#fefffecf73b59dbee7ae905b3c6a2e64c489f0cb"
@@ -1848,6 +1872,11 @@
18481872
resolved "https://registry.yarnpkg.com/@emotion/utils/-/utils-1.0.0.tgz#abe06a83160b10570816c913990245813a2fd6af"
18491873
integrity sha512-mQC2b3XLDs6QCW+pDQDiyO/EdGZYOygE8s5N5rrzjSI4M3IejPE/JPndCBwRT9z982aqQNi6beWs1UeayrQxxA==
18501874

1875+
"@emotion/utils@^1.1.0":
1876+
version "1.1.0"
1877+
resolved "https://registry.yarnpkg.com/@emotion/utils/-/utils-1.1.0.tgz#86b0b297f3f1a0f2bdb08eeac9a2f49afd40d0cf"
1878+
integrity sha512-iRLa/Y4Rs5H/f2nimczYmS5kFJEbpiVvgN3XVfZ022IYhuNA1IRSHEizcof88LtCTXtl9S2Cxt32KgaXEu72JQ==
1879+
18511880
"@emotion/weak-memoize@^0.2.5":
18521881
version "0.2.5"
18531882
resolved "https://registry.yarnpkg.com/@emotion/weak-memoize/-/weak-memoize-0.2.5.tgz#8eed982e2ee6f7f4e44c253e12962980791efd46"

0 commit comments

Comments
 (0)