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

Commit deca8b6

Browse files
committed
feat(system): add system architecture
1 parent 8b11589 commit deca8b6

File tree

7 files changed

+70
-32
lines changed

7 files changed

+70
-32
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"c-alert": "yarn workspace @chakra-ui/c-alert",
2020
"c-box": "yarn workspace @chakra-ui/c-box",
2121
"c-button": "yarn workspace @chakra-ui/c-button",
22+
23+
"system": "yarn workspace @chakra-ui/system-vue",
2224
"nuxt": "yarn workspace @chakra-ui/nuxt-next"
2325
},
2426
"license": "MIT",

packages/c-alert/examples/base-alert.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import CAlert from '@chakra-ui/c-alert/src'
88
import { defineComponent } from 'vue'
99
1010
export default defineComponent({
11+
name: 'BaseAlertExample',
1112
components: { CAlert },
1213
})
1314
</script>

packages/c-alert/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
"build": "concurrently yarn:build:*",
1717
"build:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts -d dist/esm --source-maps",
1818
"build:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts -d dist/cjs --source-maps",
19-
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types"
19+
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types",
20+
"watch": "concurrently yarn:watch:*",
21+
"watch:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts -d dist/esm --source-maps --watch",
22+
"watch:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts -d dist/cjs --source-maps --watch",
23+
"watch:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types --watch"
2024
},
2125
"dependencies": {
2226
"@chakra-ui/vue-styled-system": "1.0.0",

packages/c-alert/src/index.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import { h, defineComponent } from 'vue'
1+
import { h, defineComponent, PropType } from 'vue'
22

33
import { css } from '@chakra-ui/styled-system'
44
import { css as emotionCss, cx } from '@emotion/css'
55
import theme from '@chakra-ui/vue-theme'
6+
import { chakra } from '@chakra-ui/system-vue'
7+
import { DOMElements } from '@chakra-ui/system-vue/dist/types/system.utils'
68

79
const CAlert = defineComponent({
10+
name: 'CAlert',
811
props: {
912
as: {
10-
type: [String, Object],
13+
type: [String, Object] as PropType<DOMElements>,
1114
default: 'div',
1215
},
1316
},
14-
render() {
17+
setup(props, { slots, attrs }) {
1518
const alertStyles = css({
1619
bg: 'blue.400',
1720
p: 4,
@@ -23,15 +26,16 @@ const CAlert = defineComponent({
2326

2427
const className = emotionCss(alertStyles)
2528

26-
return h(
27-
this.as,
28-
{
29-
...this.$attrs,
30-
class: cx('chakra-alert', className),
31-
role: 'alert',
32-
},
33-
this.$slots.default && this.$slots.default()
34-
)
29+
return () =>
30+
h(
31+
chakra[props.as],
32+
{
33+
...attrs,
34+
class: cx('chakra-alert', className),
35+
role: 'alert',
36+
},
37+
slots
38+
)
3539
},
3640
})
3741

packages/system/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
"build": "concurrently yarn:build:*",
1414
"build:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts -d dist/esm --source-maps",
1515
"build:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts -d dist/cjs --source-maps",
16-
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types"
16+
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types",
17+
"watch": "concurrently yarn:watch:*",
18+
"watch:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts -d dist/esm --source-maps --watch",
19+
"watch:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts -d dist/cjs --source-maps --watch",
20+
"watch:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types --watch"
1721
},
1822
"dependencies": {
1923
"@chakra-ui/vue-styled-system": "^1.0.0"

packages/system/src/index.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
1-
import {
2-
Component,
3-
defineComponent,
4-
Fragment,
5-
Suspense,
6-
Teleport,
7-
h,
8-
} from 'vue'
9-
import { DOMElements } from './system.utils'
1+
import { defineComponent, h } from 'vue'
2+
import { domElements, DOMElements } from './system.utils'
103

11-
type Tag =
12-
| string
13-
| typeof Fragment
14-
| typeof Teleport
15-
| typeof Suspense
16-
| Component
17-
18-
export const chakra = (tag: DOMElements, componentProps = {}) => {
4+
/**
5+
* Creates a Chakra UI Vue component
6+
* @param tag Tag
7+
* @param componentProps Component Props
8+
*/
9+
// @ts-ignore
10+
export const chakra: IChakraFactory = (
11+
tag: DOMElements,
12+
componentProps = {}
13+
): any => {
1914
return defineComponent({
2015
inheritAttrs: false,
2116
props: componentProps,
2217
setup(props, { slots, attrs }) {
23-
return () => h(tag, {}, slots)
18+
return () =>
19+
h(
20+
tag,
21+
{
22+
...props,
23+
...attrs,
24+
},
25+
slots
26+
)
2427
},
2528
})
2629
}
30+
31+
type IChakraFactory = {
32+
[key in DOMElements]: any
33+
} & {
34+
(tag: DOMElements): any
35+
}
36+
37+
domElements.forEach((tag) => {
38+
chakra[tag] = chakra(tag)
39+
})

packages/system/src/system.types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Component, Fragment, Suspense, Teleport } from 'vue'
2+
3+
export type Tag =
4+
| string
5+
| typeof Fragment
6+
| typeof Teleport
7+
| typeof Suspense
8+
| Component
9+
10+
export type As<Props = any> = Tag

0 commit comments

Comments
 (0)