Skip to content

Commit bdecd7c

Browse files
authored
feat(style): allow multiple strings in gale (#8)
1 parent 6fc53d4 commit bdecd7c

2 files changed

Lines changed: 11 additions & 19 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.2.1",
3+
"version": "0.2.2",
44
"type": "module",
55
"private": true,
66
"description": "In-house UI framework",

src/style/gale.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const gale = <T extends string>(
9595
componentStylesToGaleStringCache.set(STUB_COMPONENT_STYLES_KEY, c);
9696
galeStringCache = c;
9797
}
98-
const mFunction = (classes: string): string => {
98+
const mFunction = (...classes: string[]): string => {
9999
return computeClassesWithCache(
100100
projectStyles,
101101
STUB_COMPONENT_STYLES_KEY,
@@ -128,7 +128,7 @@ export const gale = <T extends string>(
128128
componentStylesToGaleStringCache.set(componentStyles, c);
129129
galeStringCache = c;
130130
}
131-
const mFunction = (classes: string): string => {
131+
const mFunction = (...classes: string[]): string => {
132132
return computeClassesWithCache(
133133
projectStyles,
134134
componentStyles,
@@ -144,14 +144,15 @@ export const gale = <T extends string>(
144144
projectStyles: Record<string, string>,
145145
componentStyles: Record<string, string>,
146146
cache: Map<string, string>,
147-
classes: string,
147+
classes: (string | undefined | null | false)[],
148148
): string => {
149-
const cached = cache.get(classes);
149+
const classesConcatenated = classes.filter(Boolean).join(" ");
150+
const cached = cache.get(classesConcatenated);
150151
if (cached !== undefined) {
151152
return cached;
152153
}
153154
const parsed: string[] = [];
154-
for (const p of classes.split(" ")) {
155+
for (const p of classesConcatenated.split(" ")) {
155156
const slotName = p.trim();
156157
if (!slotName) {
157158
continue;
@@ -166,7 +167,7 @@ export const gale = <T extends string>(
166167
}
167168

168169
const result = mergeClasses(...parsed);
169-
cache.set(classes, result);
170+
cache.set(classesConcatenated, result);
170171
return result;
171172
};
172173

@@ -188,7 +189,9 @@ export type GaleKeys<T extends string> = T | GaleBuiltinKey;
188189
/** Hook to be called inside a component to get the `m` function. See {@link gale} */
189190
export type GaleHook<T extends string> = () => GaleFn<T>;
190191
/** The `m` function that turns a style string into class names */
191-
export type GaleFn<T extends string> = <K extends string>(classes: GaleString<K, T, []>) => string;
192+
export type GaleFn<T extends string> = <K extends string>(
193+
...classes: (GaleString<K, T, []> | undefined | null | false)[]
194+
) => string;
192195

193196
/** Type-safe, space-separated style idents */
194197
export type GaleString<K extends string, T extends string, S extends string[]> = K extends S[number]
@@ -362,14 +365,3 @@ export const GALE_BUILTIN_STYLES = {
362365
} as const satisfies Record<string, GriffelStyle>;
363366

364367
type GaleBuiltinKey = keyof typeof GALE_BUILTIN_STYLES;
365-
// // type A = Validate<"margin-0", Keys>;
366-
// // type B = Validate<"foo", Keys>;
367-
// // type C = Validate<"foo bar", Keys>;
368-
// // type D = Validate<"margin-0 padding-0", Keys>;
369-
// // type E = Validate<"margin-0 0", Keys>;
370-
// // type F = Validate<"0 padding-0", Keys>;
371-
//
372-
// const testFunc = <T extends string>(exp: Validate<T, Keys>): void => {
373-
// }
374-
//
375-
// testFunc("margin-0");

0 commit comments

Comments
 (0)