Skip to content

Commit

Permalink
feat(shared/system): extractProps 함수 추가 (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukvvon authored Apr 23, 2024
1 parent bafb463 commit 467df01
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/shared/system/src/extract-props.ts
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,
};
}
1 change: 1 addition & 0 deletions packages/shared/system/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export * from './create-component';
export * from './create-context';
export * from './create-raw-style-props';
export * from './extract-props';
export * from './factory';
export * from './forward-ref';
export * from './types';

0 comments on commit 467df01

Please sign in to comment.