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

Commit d7ce521

Browse files
committed
fix(aalert): fix colormode
1 parent ecfb0ce commit d7ce521

File tree

12 files changed

+786
-95
lines changed

12 files changed

+786
-95
lines changed

docs/guides/component-guide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ export function useHook(props: UseHookProps) {
114114
export type UseHookReturn = ReturnType<typeof useHook>
115115
```
116116

117+
### Compound components
118+
When building compound component in Vue, we may need to use the `StylesProvider` or component context provider provided by `createContext`.
119+
120+
**When providing component state, always make sure that you provide your component state in a computed variable**. This will ensure that your provided state is reactive. Otherwise it won't be if the user changes the component state dynamically.
121+
117122
### TypeScript
118123
119124
The end goal of this ensure all Chakra UI components are as strongly typed as

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"vitepress": "^0.12.0",
114114
"vue": ">=3.0.5",
115115
"vue-jest": "^5.0.0-alpha.7",
116-
"vue-router": "^4.0.4"
116+
"vue-router": "^4.0.4",
117+
"vue3-perfect-scrollbar": "^1.5.5"
117118
}
118119
}

packages/c-alert/src/alert.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { h, defineComponent, PropType, computed } from 'vue'
1+
import { h, defineComponent, PropType, computed, ComputedRef } from 'vue'
22
import {
33
chakra,
44
ThemingProps,
@@ -34,7 +34,7 @@ type AlertStatus = keyof typeof STATUSES
3434
export type AlertVariant = 'solid' | 'subtle' | 'left-accent' | 'top-accent'
3535

3636
interface AlertContext {
37-
status: AlertStatus
37+
status: ComputedRef<AlertStatus>
3838
}
3939

4040
const [AlertProvider, useAlertContext] = createContext<AlertContext>({
@@ -72,32 +72,34 @@ export const CAlert = defineComponent({
7272
},
7373
},
7474
setup(props, { slots, attrs }) {
75-
return () => {
76-
const colorScheme: string =
77-
props.colorScheme || STATUSES[props.status].colorScheme
75+
const colorScheme = computed<string>(
76+
() => props.colorScheme || STATUSES[props.status].colorScheme
77+
)
78+
79+
const themingProps = computed<ThemingProps>(() => ({
80+
colorScheme: colorScheme.value,
81+
variant: props.variant,
82+
}))
7883

79-
const themingProps: ThemingProps = {
80-
colorScheme,
81-
variant: props.variant,
82-
}
83-
const styles = useMultiStyleConfig('Alert', themingProps)
84-
const alertStyles: SystemStyleObject = {
85-
width: '100%',
86-
display: 'flex',
87-
alignItems: 'center',
88-
position: 'relative',
89-
overflow: 'hidden',
90-
...styles.value.container,
91-
}
84+
AlertProvider({ status: computed(() => props.status) })
85+
const styles = useMultiStyleConfig('Alert', themingProps.value)
86+
StylesProvider(styles)
9287

93-
StylesProvider(styles.value)
94-
AlertProvider({ status: props.status })
88+
const alertStyles = computed<SystemStyleObject>(() => ({
89+
width: '100%',
90+
display: 'flex',
91+
alignItems: 'center',
92+
position: 'relative',
93+
overflow: 'hidden',
94+
...styles.value.container,
95+
}))
9596

97+
return () => {
9698
return h(
9799
chakra(props.as, { label: 'alert' }),
98100
{
99101
role: 'alert',
100-
...alertStyles,
102+
...alertStyles.value,
101103
...attrs,
102104
},
103105
slots
@@ -120,7 +122,7 @@ export const CAlertTitle = defineComponent({
120122
return h(
121123
chakra('div', { label: 'alert__title' }),
122124
{
123-
...styles.title,
125+
...styles.value.title,
124126
...attrs,
125127
},
126128
slots
@@ -137,13 +139,12 @@ export const CAlertTitle = defineComponent({
137139
export const CAlertDescription = defineComponent({
138140
name: 'CAlertDescription',
139141
setup(_, { attrs, slots }) {
142+
const styles = useStyles()
140143
return () => {
141-
const styles = useStyles()
142-
143144
return h(
144145
chakra('div', { label: 'alert__description' }),
145146
{
146-
...styles.description,
147+
...styles.value.description,
147148
...attrs,
148149
},
149150
slots
@@ -165,17 +166,16 @@ export const CAlertIcon = defineComponent({
165166
},
166167
},
167168
setup(props, { attrs }) {
168-
return () => {
169-
const { status } = useAlertContext()
170-
const { icon } = STATUSES[status]
171-
const styles = useStyles()
172-
173-
const alertIcon = computed(() => props.icon || icon)
169+
const styles = useStyles()
170+
const { status } = useAlertContext()
171+
const { icon } = STATUSES[status.value]
172+
const alertIcon = computed(() => props.icon || icon)
174173

174+
return () => {
175175
return h(CIcon, {
176176
class: 'alert__icon',
177177
name: alertIcon.value,
178-
...styles.icon,
178+
...styles.value.icon,
179179
...attrs,
180180
})
181181
}

packages/c-button/src/button-group.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ const CButtonGroup = defineComponent({
5858
name: 'CButtonGroup',
5959
props,
6060
setup(props, { attrs, slots }) {
61+
ButtonGroupProvider(() => ({
62+
size: props.size,
63+
colorScheme: props.colorScheme,
64+
variant: props.variant,
65+
isDisabled: props.isDisabled,
66+
}))
6167
return () => {
62-
ButtonGroupProvider(() => ({
63-
size: props.size,
64-
colorScheme: props.colorScheme,
65-
variant: props.variant,
66-
isDisabled: props.isDisabled,
67-
}))
68-
6968
const styles = computed(() => {
7069
let groupStyles: SystemStyleObject = {
7170
display: 'inline-flex',

packages/c-button/src/button.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ const CButtonSpinner = defineComponent({
2121
>,
2222
},
2323
setup(props, { attrs }) {
24-
const spinnerStyles = computed<SystemStyleObject>(() => ({
25-
display: 'flex',
26-
alignItems: 'center',
27-
position: props.label ? 'relative' : 'absolute',
28-
marginEnd: props.label ? props.spacing : 0,
29-
}))
30-
return () =>
31-
h(
24+
return () => {
25+
const spinnerStyles = computed<SystemStyleObject>(() => ({
26+
display: 'flex',
27+
alignItems: 'center',
28+
position: props.label ? 'relative' : 'absolute',
29+
marginEnd: props.label ? props.spacing : 0,
30+
}))
31+
return h(
3232
chakra('div', {
3333
label: 'button__spinner',
3434
}),
@@ -44,6 +44,7 @@ const CButtonSpinner = defineComponent({
4444
}),
4545
]
4646
)
47+
}
4748
},
4849
})
4950

packages/c-button/src/icon-button.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const CIconButton = defineComponent({
3535
rounded: props.isRound ? 'rounded' : 'md',
3636
'aria-label': props.ariaLabel,
3737
...attrs,
38+
...props,
3839
},
3940
() => [
4041
h(CIcon, {

packages/c-spinner/src/spinner.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,22 @@ const CSpinner = defineComponent({
9696
styleConfig: props.styleConfig,
9797
}))
9898

99-
const styles = useStyleConfig('Spinner', { ...themingProps.value })
99+
return () => {
100+
const styles = useStyleConfig('Spinner', { ...themingProps.value })
101+
const spinnerStyles = {
102+
display: 'inline-block',
103+
borderColor: 'currentColor',
104+
borderStyle: 'solid',
105+
borderRadius: '99999px',
106+
borderWidth: props.thickness,
107+
borderBottomColor: props.emptyColor,
108+
borderLeftColor: props.emptyColor,
109+
color: props.color,
110+
animation: `${spin} ${props.speed} linear infinite`,
111+
...styles.value,
112+
}
100113

101-
const spinnerStyles = {
102-
display: 'inline-block',
103-
borderColor: 'currentColor',
104-
borderStyle: 'solid',
105-
borderRadius: '99999px',
106-
borderWidth: props.thickness,
107-
borderBottomColor: props.emptyColor,
108-
borderLeftColor: props.emptyColor,
109-
color: props.color,
110-
animation: `${spin} ${props.speed} linear infinite`,
111-
...styles.value,
112-
}
113-
114-
return () =>
115-
h(
114+
return h(
116115
chakra(props.as, {
117116
label: 'spinner',
118117
__css: spinnerStyles,
@@ -122,6 +121,7 @@ const CSpinner = defineComponent({
122121
},
123122
props.label && [h(CVisuallyHidden, props.label)]
124123
)
124+
}
125125
},
126126
})
127127

packages/system/src/composables/use-style-config.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ export function useStyleConfig(
2424
options: any = {},
2525
userStyleConfig?: any
2626
) {
27-
const { theme, colorMode } = useChakra()
28-
const themeStyleConfig = get(theme, `components.${themeKey}`)
27+
return computed(() => {
28+
const { theme, colorMode } = useChakra()
29+
const themeStyleConfig = get(theme, `components.${themeKey}`)
2930

30-
const styleConfig = userStyleConfig || themeStyleConfig
31+
const styleConfig = userStyleConfig || themeStyleConfig
3132

32-
const mergedProps = mergeWith(
33-
{ theme: theme, colorMode: colorMode.value },
34-
styleConfig?.defaultProps ?? {},
35-
filterUndefined(themingProps)
36-
)
33+
const mergedProps = mergeWith(
34+
{ theme: theme, colorMode: colorMode.value },
35+
styleConfig?.defaultProps ?? {},
36+
filterUndefined(themingProps)
37+
)
3738

38-
return computed(() => {
3939
const baseStyles = runIfFn(styleConfig.baseStyle ?? {}, mergedProps)
4040
const variants = runIfFn(
4141
styleConfig.variants?.[mergedProps.variant] ?? {},

packages/system/src/providers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { createContext } from '@chakra-ui/vue-utils'
22
import { Dict } from '@chakra-ui/utils'
33
import { SystemStyleObject } from '@chakra-ui/styled-system'
4+
import { ComputedRef } from '@vue/reactivity'
45

5-
const [StylesProvider, useStyles] = createContext<Dict<SystemStyleObject>>({
6+
const [StylesProvider, useStyles] = createContext<
7+
ComputedRef<Dict<SystemStyleObject>>
8+
>({
69
name: 'StylesContext',
710
errorMessage:
811
'useStyles: `styles` is undefined. Seems you forgot to provide `StylesProvider(...)` ',

playground/src/App.vue

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<template>
22
<c-reset />
33
<chakra.section d="flex" transition="all 0.2s" height="inherit" w="inherit" :__css="rootStyles">
4-
<sidebar :stories="routes" />
5-
<chakra.main w="full" pos="relative" border-left="1px solid" border-color="gray.400" padding="4">
4+
<perfect-scrollbar>
5+
<sidebar :stories="routes" />
6+
</perfect-scrollbar>
7+
<chakra.main w="full" pos="relative" border-left="1px solid" border-color="gray.200" padding="4">
68
<router-view v-slot="{ Component, route }">
79
<!-- <transition name="fade" mode="out-in"> -->
810
<component :is="Component" />
911
<!-- </transition> -->
1012
</router-view>
11-
<c-icon-button pos="absolute" color-scheme="whiteAlpha" @click="toggleColorMode" top="10" right="10" :aria-label="`Switch to ${colorMode === 'light' ? 'dark' : 'light'} mode`" :icon="colorMode === 'light' ? 'moon' : 'sun'" />
13+
<c-icon-button color="inherit" pos="absolute" @click="toggleColorMode" top="10" right="10" :aria-label="`Switch to ${colorMode === 'light' ? 'dark' : 'light'} mode`" :icon="colorMode === 'light' ? 'moon' : 'sun'" />
1214
</chakra.main>
1315
</chakra.section>
1416
</template>
@@ -39,7 +41,7 @@ export default defineComponent({
3941
},
4042
dark: {
4143
bg: 'gray.800',
42-
color: 'whiteAlpha.700',
44+
color: 'whiteAlpha.800',
4345
'a.router-link-active': {
4446
color: 'teal.200',
4547
fontSize: '0.9rem',
@@ -49,7 +51,10 @@ export default defineComponent({
4951
},
5052
}
5153
52-
return styles[colorMode.value]
54+
return {
55+
transition: 'all 0.2s ease-in',
56+
...styles[colorMode.value]
57+
}
5358
})
5459
5560
return {
@@ -93,4 +98,11 @@ a {
9398
.fade-leave-to {
9499
opacity: 0;
95100
}
101+
102+
.ps {
103+
height: 100vh;
104+
width: 275px;
105+
}
96106
</style>
107+
108+
<style src="vue3-perfect-scrollbar/dist/vue3-perfect-scrollbar.css"/>

0 commit comments

Comments
 (0)