Skip to content

Commit

Permalink
feat(ui/react/styles): extractDynamicProps 함수 추가 (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukvvon authored Jul 19, 2024
1 parent 4751eb0 commit 2b2897f
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/ui/react/src/styles/extract-dynamic-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { cx, mergeStyles, toPx } from '@favolink-ui/utils';
import { assignInlineVars } from '@vanilla-extract/dynamic';
import { type CSSProperties } from 'react';

export function extractDynamicProps<
P extends {
style?: CSSProperties;
className?: string;
[key: string]: any;
},
S extends Record<string, string>,
>(props: P, dynamicVars: Record<string, string>, dynamicStyles: S) {
const { className, style, ...restProps } = props;

let composedClassName: string = '';
let willAssignInlineVars: Record<string, string | null | undefined> = {};

for (const prop in restProps) {
if (prop in dynamicStyles) {
composedClassName = cx(
composedClassName,
restProps[prop] && dynamicStyles[prop],
);

const regex = new RegExp(`${prop}Var`, 'g');

for (const dynamicVar in dynamicVars) {
if (regex.test(dynamicVar)) {
willAssignInlineVars = {
...willAssignInlineVars,
[dynamicVars[dynamicVar] as string]: toPx(restProps[prop]),
};

break;
}
}

delete restProps[prop];
}
}

const resultProps = {
...restProps,
className: cx(composedClassName, className),
style: mergeStyles(assignInlineVars(willAssignInlineVars), style),
} as Omit<P, keyof S>;

return resultProps;
}

0 comments on commit 2b2897f

Please sign in to comment.