Skip to content

Commit

Permalink
feat: support group postfix, eg: _hover={["red300", "textXL"]}
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-leen committed Apr 18, 2021
1 parent 1782fae commit 808df99
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
4 changes: 3 additions & 1 deletion examples/example-react/src/pages/Dark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export default () => {
}
return (
<Box h-400 roundedLG bgGray800--dark bgAmber100 p4>
<Box red200>40 Lorem ipsum dolor sit amet</Box>
<Box _dark={['green200']} red200>
40 Lorem ipsum dolor sit amet
</Box>
<Box column toCenter spaceY3 gray800 white--dark textCenter>
<Box
as="button"
Expand Down
6 changes: 5 additions & 1 deletion examples/example-react/src/pages/TextSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ setTheme({

export default () => {
return (
<Box text2XL brand100>
<Box
text2XL
_hover={['bgAmber300', 'border']}
_md={['bgTeal400', 'text5XL']}
>
40 Lorem ipsum dolor sit amet
</Box>
);
Expand Down
36 changes: 33 additions & 3 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export class Parser {
traverseProps(props: Props): void {
if (isEmptyObj(props)) return

const { pseudos = [], theme } = this.config
const { breakpoints, modes } = theme || {}
const breakpointKeys = Object.keys(breakpoints)
const modeKeys: string[] = modes || []
const pseudoKeys: string[] = pseudos

const { excludedProps = [] } = props
const entries = Object.entries<any>(props)

Expand All @@ -77,10 +83,31 @@ export class Parser {

// parse css prop
if (propKey === 'css') {
this.parseCSSProp(propValue)
this.parseCSSObject(propValue)
continue
}

/** handle _hover, _sm, _dark... */
if (propKey.startsWith('_')) {
const postfix = propKey.replace(/^_/, '')
const obj = Array.isArray(propValue)
? propValue.reduce<any>((r, cur) => ({ ...r, [cur]: true }), {})
: propValue

if (modeKeys.includes(postfix)) {
this.parseCSSObject(obj, { mode: postfix })
continue
}
if (breakpointKeys.includes(postfix)) {
this.parseCSSObject(obj, { breakpoint: breakpoints[postfix] })
continue
}
if (pseudoKeys.includes(postfix)) {
this.parseCSSObject(obj, { pseudo: ':' + postfix })
continue
}
}

let atom = new Atom({ propKey, propValue })

try {
Expand Down Expand Up @@ -202,6 +229,9 @@ export class Parser {
const validTypes = ['string', 'boolean', 'number', 'undefined']
if (propKey === 'css') return true

// for _hover,_sm,_dark...
if (propKey.startsWith('_')) return true

const type = typeof propValue
if (validTypes.includes(type)) return true

Expand Down Expand Up @@ -246,15 +276,15 @@ export class Parser {
}
}

parseCSSProp(propValue: any) {
parseCSSObject(propValue: any, meta = {}) {
const parsed = parse(propValue)

const prefixClassName = objectToClassName(propValue)

for (const { selector, selectorType, style } of parsed) {
const [propKey, propValue] = Object.entries(style)[0]

let option: Options = { propKey, propValue, meta: {} }
let option: Options = { propKey, propValue, meta }

if (selectorType === 'pseudo' && option.meta) {
option.meta.pseudo = selector
Expand Down

0 comments on commit 808df99

Please sign in to comment.