-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shared/system): extractProps 함수 추가 (#157)
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { mergeRecords } from '@favolink/utils'; | ||
import { type createVar } from '@vanilla-extract/css'; | ||
import { assignInlineVars } from '@vanilla-extract/dynamic'; | ||
import { type CSSProperties } from 'react'; | ||
|
||
export function extractProps< | ||
Props extends { | ||
className?: string; | ||
style?: CSSProperties; | ||
[key: string]: any; | ||
}, | ||
CSSVarFuntion extends ReturnType<typeof createVar>, | ||
Styles extends ReturnType<typeof assignInlineVars>, | ||
>( | ||
props: Props, | ||
vars: Record<string, CSSVarFuntion>, | ||
...styleProps: Record<string, undefined>[] | ||
) { | ||
const { style, ...restProps } = props; | ||
const copiedRestProps = { ...restProps }; | ||
const copiedVars = { ...vars }; | ||
const allStyleProps = mergeRecords(...styleProps); | ||
const willAssignInlineStyle = {} as Record<CSSVarFuntion, string>; | ||
|
||
for (const styleProp in allStyleProps) { | ||
if (styleProp in copiedRestProps) { | ||
delete copiedRestProps[styleProp]; | ||
|
||
const regex = new RegExp(`\\w*${styleProp}\\w*`, 'i'); | ||
|
||
for (const _var in copiedVars) { | ||
if (regex.test(copiedVars[_var] as CSSVarFuntion)) { | ||
delete copiedVars[_var]; | ||
|
||
willAssignInlineStyle[vars[_var] as CSSVarFuntion] = props[ | ||
styleProp | ||
] as string; | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { | ||
...copiedRestProps, | ||
style: { | ||
...assignInlineVars(willAssignInlineStyle), | ||
...style, | ||
} as CSSProperties & Styles, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters