-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
43 lines (40 loc) · 1.57 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React, {Ref} from 'react'
import {MSA} from '@stylin/msa-loader/index'
import {CreateComponent, ApplyStyle} from './types'
export const url = (value: string) => `url(${value})`
export const createComponent: CreateComponent = css => (msa, component) => {
const Component = <T>({className: firstClassName = ``, ...props}, ref: Ref<T>) => {
const className = `${firstClassName} ${css[msa.className] || ``}`
const {tagName, properties, variables} = msa
const finalProps = Object
.entries(props)
.reduce((acc, [name, value]: [string, string]) => {
if (properties[name]) {
if (value === undefined) return acc
const cssName = properties[name][value] || properties[name][`@default`]
const hashName = css[cssName]
if (hashName) {
acc.className += ` ${hashName}`
}
return acc
}
if (variables[name]) {
const [defaultValue, variable] = variables[name]
if (defaultValue !== value) {
acc.style[variable] = value
}
return acc
}
acc[name] = value
return acc
}, {ref, className, style: {}}) as any
return React.createElement(component || tagName, finalProps)
}
Component.displayName = msa.componentName
return React.forwardRef(Component)
}
export const applyStyle: ApplyStyle = css => msaList => (style, component) => {
const msa = msaList.find(({className}) => className === style)
|| ({className: style, properties: {}, variables: {}} as MSA)
return createComponent(css)(msa, component)
}