generated from openscript-ch/react-vite-storybook-typescript-starter
-
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: make css properties configurable with props
- Loading branch information
1 parent
d291e3d
commit 425145f
Showing
3 changed files
with
56 additions
and
1 deletion.
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
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,17 @@ | ||
export const flattenObject = <O extends Record<string, T>, T extends O | string | undefined>(ob: Record<string, T>, delimiter?: string) => { | ||
const result: Record<string, string> = {}; | ||
|
||
Object.keys(ob).forEach((i) => { | ||
const value = ob[i]; | ||
if (typeof value === 'object') { | ||
const temp = flattenObject(value, delimiter); | ||
Object.keys(temp).forEach((j) => { | ||
result[`${i}${delimiter ?? '.'}${j}`] = temp[j]; | ||
}); | ||
} else if (value) { | ||
result[i] = value; | ||
} | ||
}); | ||
|
||
return result; | ||
}; |
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,31 @@ | ||
import { flattenObject } from '../helper/object'; | ||
|
||
export type StyleConfig = { | ||
line?: { | ||
width?: string; | ||
color?: string; | ||
}; | ||
marker?: { | ||
size?: string; | ||
color?: string; | ||
radius?: string; | ||
offset?: string; | ||
}; | ||
pointer?: { | ||
height?: string; | ||
width?: string; | ||
}; | ||
card?: { | ||
background?: string; | ||
radius?: string; | ||
offset?: string; | ||
shadow?: string; | ||
padding?: string; | ||
}; | ||
}; | ||
|
||
export const convertToCssVariable = (styleConfig: StyleConfig) => | ||
Object.entries(flattenObject(styleConfig, '-')).reduce( | ||
(prev: Record<string, string>, [key, value]) => ({ ...prev, [`--${key}`]: value }), | ||
{}, | ||
); |