Skip to content

Commit 7eec17a

Browse files
authored
feat(styles): apply c- prefix to component level styles (#6)
1 parent 814bc9b commit 7eec17a

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pistonite/celera",
3-
"version": "0.1.5",
3+
"version": "0.2.0",
44
"type": "module",
55
"private": true,
66
"description": "In-house UI framework",

src/style/gale.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ import { log } from "#util";
4646
* return <div className={m("wh-100 padding-0")}>my component</div>;
4747
* };
4848
*
49+
* const useStyles = useStyleEngine.extend({
50+
* container: {
51+
* // not shown
52+
* }
53+
* });
54+
*
4955
* export const MyComponent2 = () => {
5056
* const m = useStyles(); // component-level style idents
51-
* return <div className={m("h-100 my-container")}>my component</div>;
57+
* // note component-level styles are c- prefixed
58+
* return <div className={m("h-100 c-my-container")}>my component</div>;
5259
* };
5360
*
5461
* const useExtraStyles = makeStyles({
5562
* extra: {
5663
* overflow: "hidden"
5764
* }
5865
* });
59-
*
60-
* export const MyComponent3 = () => {
61-
* const m = useStyles(); // component-level style idents
62-
* // if needed (should be rare), you can interop with hooks from griffel
63-
* const c = useExtraStyles();
64-
* return <div className={m("h-100 my-container", c.extra)}>my component</div>;
65-
* };
6666
* ```
6767
*
6868
*/
@@ -107,9 +107,12 @@ export const gale = <T extends string>(
107107
};
108108
const makeComponentLevelStyleEngine = <K extends string>(
109109
componentStyles: Record<K, GriffelStyle>,
110-
): GaleHook<K | GaleKeys<T>> => {
111-
const useComponentStyles = makeStyles(componentStyles);
112-
const useStyles = (): GaleFn<K | GaleKeys<T>> => {
110+
): GaleHook<`c-${K}` | GaleKeys<T>> => {
111+
const mappedComponentStyles = Object.fromEntries(
112+
Object.entries(componentStyles).map(([k, v]) => ["c-" + k, v]),
113+
) as Record<`c-${K}`, GriffelStyle>;
114+
const useComponentStyles = makeStyles(mappedComponentStyles);
115+
const useStyles = (): GaleFn<`c-${K}` | GaleKeys<T>> => {
113116
// griffel makeStyles hook return cached stable reference
114117
const projectStyles = useProjectLevelGriffelStyles();
115118
let componentStylesToGaleStringCache = cache.get(projectStyles);
@@ -133,7 +136,7 @@ export const gale = <T extends string>(
133136
classes,
134137
);
135138
};
136-
return mFunction as GaleFn<K | GaleKeys<T>>;
139+
return mFunction as GaleFn<`c-${K}` | GaleKeys<T>>;
137140
};
138141
return useStyles;
139142
};
@@ -178,7 +181,7 @@ export const gale = <T extends string>(
178181
*/
179182
export interface GaleEngine<T extends string> extends GaleHook<T> {
180183
/** Extend the styles to get a component-specific hook */
181-
extend: <K extends string>(componentStyles: Record<K, GriffelStyle>) => GaleHook<K | T>;
184+
extend: <K extends string>(componentStyles: Record<K, GriffelStyle>) => GaleHook<`c-${K}` | T>;
182185
}
183186
/** Wrapper to concatenate built-in style keys with project-specific style keys */
184187
export type GaleKeys<T extends string> = T | GaleBuiltinKey;

0 commit comments

Comments
 (0)