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

Commit 82962cd

Browse files
committed
chore(theme): setup system providers
1 parent fc45b99 commit 82962cd

File tree

5 files changed

+71
-67
lines changed

5 files changed

+71
-67
lines changed

packages/c-theme-provider/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ const CThemeProvider = defineComponent({
2626
})
2727

2828
export default CThemeProvider
29+
export * from './theme.hooks'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ThemeProviderProps } from '@chakra-ui/vue-next'
2+
import { computed, inject } from 'vue'
3+
4+
/** Provides theme object in component context */
5+
export const useTheme = () => {
6+
const theme = inject('$chakraTheme') as ThemeProviderProps
7+
return theme
8+
}

packages/system/src/chakra.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { defineComponent, h } from 'vue'
2+
import { css } from '@chakra-ui/styled-system'
3+
import theme from '@chakra-ui/vue-theme'
4+
import { css as _css, cx } from '@emotion/css'
5+
import { extractStyleAttrs } from './system.attrs'
6+
import { domElements, DOMElements } from './system.utils'
7+
8+
/**
9+
* Creates a Chakra UI Vue component
10+
* @param tag Tag
11+
* @param componentName Component name
12+
*/
13+
// @ts-expect-error
14+
export const chakra: IChakraFactory = (
15+
tag: DOMElements,
16+
componentName?: string
17+
): any => {
18+
return defineComponent({
19+
inheritAttrs: false,
20+
setup(props, { slots, attrs }) {
21+
// Separate component style attributes from raw HTML attributes
22+
const { class: inheritedClass, ...rest } = attrs
23+
const { styles, attrs: _attrs } = extractStyleAttrs(rest)
24+
const className = _css(css(styles)({ theme }))
25+
26+
const _componentName = componentName
27+
? `chakra-${componentName}`
28+
: `chakra-component-${tag}`
29+
30+
return () =>
31+
h(
32+
tag,
33+
{
34+
class: cx(inheritedClass as string, _componentName, className),
35+
...props,
36+
..._attrs,
37+
},
38+
slots
39+
)
40+
},
41+
})
42+
}
43+
44+
type IChakraFactory = {
45+
[key in DOMElements]: any
46+
} & {
47+
(tag: DOMElements, componentName?: string): any
48+
}
49+
50+
domElements.forEach((tag) => {
51+
chakra[tag] = chakra(tag)
52+
})
53+
54+
export { domElements }

packages/system/src/index.ts

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,4 @@
1-
import { defineComponent, h } from 'vue'
2-
import { css } from '@chakra-ui/styled-system'
3-
import theme from '@chakra-ui/vue-theme'
4-
import { css as _css, cx } from '@emotion/css'
5-
import { extractStyleAttrs } from './system.attrs'
6-
import { domElements, DOMElements } from './system.utils'
7-
8-
let id = 0
9-
10-
/**
11-
* Creates a Chakra UI Vue component
12-
* @param tag Tag
13-
* @param componentName Component name
14-
*/
15-
// @ts-expect-error
16-
export const chakra: IChakraFactory = (
17-
tag: DOMElements,
18-
componentName?: string
19-
): any => {
20-
// Increment ids
21-
++id
22-
23-
return defineComponent({
24-
inheritAttrs: false,
25-
setup(props, { slots, attrs }) {
26-
// Separate component style attributes from raw HTML attributes
27-
const { class: inheritedClass, ...rest } = attrs
28-
const { styles, attrs: _attrs } = extractStyleAttrs(rest)
29-
const className = _css(css(styles)({ theme }))
30-
31-
const _componentName = componentName
32-
? `chakra-${componentName}`
33-
: `chakra-component-${id}`
34-
35-
return () =>
36-
h(
37-
tag,
38-
{
39-
class: cx(inheritedClass as string, _componentName, className),
40-
...props,
41-
..._attrs,
42-
},
43-
slots
44-
)
45-
},
46-
})
47-
}
48-
49-
type IChakraFactory = {
50-
[key in DOMElements]: any
51-
} & {
52-
(tag: DOMElements, componentName?: string): any
53-
}
54-
55-
domElements.forEach((tag) => {
56-
chakra[tag] = chakra(tag)
57-
})
58-
59-
export { domElements }
1+
export * from './chakra'
2+
export * from './system.attrs'
3+
export * from './system.types'
4+
export * from './system.utils'

playground/src/App.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,23 @@
33
<sidebar :stories="routes" />
44
<chakra.main w="full" border-left="1px solid" border-color="gray.400" padding="4">
55
<router-view />
6-
7-
<chakra.button :bg="['teal.500']" color="white" px="4" py="3">
8-
Button
9-
</chakra.button>
10-
116
</chakra.main>
127
</chakra.section>
138
</template>
149

1510
<script lang="ts">
16-
import { defineComponent, inject } from 'vue'
11+
import { defineComponent } from 'vue'
12+
import { useTheme } from '@chakra-ui/c-theme-provider'
1713
import Sidebar from './components/Sidebar.vue'
1814
import { routes } from './router'
1915
2016
export default defineComponent({
2117
components: { Sidebar },
2218
setup() {
23-
const chakraTheme = inject('$chakraTheme')
19+
const theme = useTheme()
2420
return {
2521
routes,
26-
chakraTheme
22+
theme
2723
}
2824
},
2925
})

0 commit comments

Comments
 (0)