-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeCssVars.ts
52 lines (47 loc) · 1.5 KB
/
mergeCssVars.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { type CssVarContext, makeCssVars } from "./makeCssVars";
import type { NonEmptyArray } from "../helpers/common";
/**
* Recursively merge all CssVarContext on an array into a single CssVarContext.
*/
export type MergeVars<T extends NonEmptyArray<CssVarContext<string>>> =
T extends [infer H, ...infer R]
? H extends CssVarContext<infer T2>
? R extends NonEmptyArray<CssVarContext<string>>
? MergeVars<R> extends CssVarContext<infer T3>
? CssVarContext<`${T2}${T3}`>
: never
: H
: never
: never;
/**
* Merges two or more `CssVarContext` object into a single context. Usefull to
* make the css variable definitions modular and use the all together from a
* single place.
*
* @example
* ```ts
* export const baseCssVars = makeCssVars(`
* --primary-color: red;
* --secondary-color: blue;
* `);
*
* export const navCssVars = makeCssVars(`
* --gap: 5%;
* --nav-width: 500;
* `);
*
* export const mainCssVars = mergeCssVars(baseCssVars, navCssVars);
* ```
*
* @param cssVars the `CssVarContext` objects to merge
* @returns a merged `CssVarContext` object
*/
export function mergeCssVars<T extends NonEmptyArray<CssVarContext<string>>>(...cssVars: T): MergeVars<T> {
const allDefinitions = cssVars.reduce<string>((acc, cssVar) => {
const separator = cssVar.definitions.startsWith("\n")
? ""
: "\n";
return `${acc}${separator}${cssVar.definitions}`;
}, "");
return makeCssVars(allDefinitions) as MergeVars<T>;
}