-
Notifications
You must be signed in to change notification settings - Fork 59
/
makeStyles.ts
63 lines (49 loc) · 2.07 KB
/
makeStyles.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
53
54
55
56
57
58
59
60
61
62
63
import { debugData, isDevToolsEnabled, getSourceURLfromError } from './devtools';
import { insertionFactory } from './insertionFactory';
import { resolveStyleRulesForSlots } from './resolveStyleRulesForSlots';
import { reduceToClassNameForSlots } from './runtime/reduceToClassNameForSlots';
import type { CSSClassesMapBySlot, CSSRulesByBucket, GriffelRenderer, StylesBySlots } from './types';
import type { GriffelInsertionFactory } from './types';
export interface MakeStylesOptions {
dir: 'ltr' | 'rtl';
renderer: GriffelRenderer;
}
export function makeStyles<Slots extends string | number>(
stylesBySlots: StylesBySlots<Slots>,
factory: GriffelInsertionFactory = insertionFactory,
) {
const insertStyles = factory();
let classesMapBySlot: CSSClassesMapBySlot<Slots> | null = null;
let cssRules: CSSRulesByBucket | null = null;
let ltrClassNamesForSlots: Record<Slots, string> | null = null;
let rtlClassNamesForSlots: Record<Slots, string> | null = null;
let sourceURL: string | undefined;
if (process.env.NODE_ENV !== 'production' && isDevToolsEnabled) {
sourceURL = getSourceURLfromError();
}
function computeClasses(options: MakeStylesOptions): Record<Slots, string> {
const { dir, renderer } = options;
if (classesMapBySlot === null) {
[classesMapBySlot, cssRules] = resolveStyleRulesForSlots(stylesBySlots);
}
const isLTR = dir === 'ltr';
if (isLTR) {
if (ltrClassNamesForSlots === null) {
ltrClassNamesForSlots = reduceToClassNameForSlots(classesMapBySlot, dir);
}
} else {
if (rtlClassNamesForSlots === null) {
rtlClassNamesForSlots = reduceToClassNameForSlots(classesMapBySlot, dir);
}
}
insertStyles(renderer, dir, cssRules!);
const classNamesForSlots = isLTR
? (ltrClassNamesForSlots as Record<Slots, string>)
: (rtlClassNamesForSlots as Record<Slots, string>);
if (process.env.NODE_ENV !== 'production' && isDevToolsEnabled) {
debugData.addSequenceDetails(classNamesForSlots!, sourceURL);
}
return classNamesForSlots;
}
return computeClasses;
}