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

Commit 07ae5c5

Browse files
committed
refactor(system): extract style attrs form isStyleProp func
1 parent a6f9e05 commit 07ae5c5

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

packages/system/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"watch:types": "cross-env tsc --emitDeclarationOnly --declaration --declarationDir dist/types --watch --incremental"
2121
},
2222
"dependencies": {
23-
"@chakra-ui/styled-system": "^1.4.1",
23+
"@chakra-ui/styled-system": "^1.9.0",
2424
"@chakra-ui/vue-theme": "*",
2525
"@chakra-ui/vue-utils": "*",
2626
"@emotion/css": "^11.1.3",

packages/system/src/system.attrs.ts

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,22 @@ import kebabCase from 'lodash.kebabcase'
22
import camelCase from 'lodash.camelcase'
33
import {
44
propNames,
5-
pseudoPropNames,
65
StyleObjectOrFn,
6+
isStyleProp,
77
} from '@chakra-ui/styled-system'
88
import { HTMLAttributes } from 'vue'
99

1010
export type StyleAndHTMLAttibutes = StyleObjectOrFn &
1111
Record<string, string | number | boolean | unknown> &
1212
HTMLAttributes
1313

14-
export const baseStylePropNames = propNames.reduce(
15-
(acc: StyleAndHTMLAttibutes, curr: string) => {
16-
acc[curr] = true
17-
acc[kebabCase(curr)] = true
18-
return acc
19-
},
20-
{}
21-
)
22-
23-
export const pseudoStylePropNames = pseudoPropNames.reduce(
24-
(acc: StyleAndHTMLAttibutes, curr: string) => {
25-
acc[curr] = curr
26-
return acc
27-
},
28-
{}
29-
)
30-
31-
export const allStylePropNames: StyleAndHTMLAttibutes = {
32-
...baseStylePropNames,
33-
...pseudoStylePropNames,
34-
}
35-
3614
interface ExtractedStyleAttrs {
3715
styles: Partial<StyleAndHTMLAttibutes>
3816
attrs: Partial<HTMLAttributes>
3917
}
4018

19+
const camelCaseCache: any = {}
20+
4121
/** Extracts CSS style properties and HTML attributes from merged component attributs */
4222
export const extractStyleAttrs = <
4323
T extends StyleAndHTMLAttibutes,
@@ -49,10 +29,15 @@ export const extractStyleAttrs = <
4929
const attrs = {} as U
5030

5131
for (const prop in styleProps) {
52-
// TODO: Cache style prop names so that that camelCases is only computed once for each property
53-
// TODO: Is there another way to detect
54-
const _attr = (pseudoStylePropNames[prop] as string) || camelCase(prop)
55-
if (_attr in allStylePropNames) {
32+
const _isStyledProp = isStyleProp(prop)
33+
if (_isStyledProp) {
34+
let _attr: string
35+
if (camelCaseCache[prop]) {
36+
_attr = camelCaseCache[prop]
37+
} else {
38+
_attr = camelCase(prop)
39+
camelCaseCache[prop] = _attr
40+
}
5641
// @ts-expect-error Not sure how to cast returned string into typeof key of U
5742
styles[_attr] = styleProps[prop]
5843
} else {
@@ -61,6 +46,11 @@ export const extractStyleAttrs = <
6146
}
6247
}
6348

49+
console.log({
50+
styles,
51+
attrs,
52+
})
53+
6454
return {
6555
styles,
6656
attrs,

0 commit comments

Comments
 (0)