-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
128 lines (111 loc) · 3.23 KB
/
index.tsx
File metadata and controls
128 lines (111 loc) · 3.23 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { useMemo } from "react";
import classNames, { ClassName } from "./classNames";
import BemFactory from "./BemFactory";
type TemplateArgs = [TemplateStringsArray, ...ClassName[]];
type TemplateArgsZipped = (string | ClassName)[];
type TemplateFn = (...args: TemplateArgs) => string;
type MixableTemplateFn = (...args: TemplateArgs) => Mixable;
type TemplateFnZipped = (args: TemplateArgsZipped) => string;
type Mixable = string & {
toString: () => string;
valueOf: () => string;
mix: TemplateFn;
};
function taggedLiteral(fn: TemplateFnZipped): TemplateFn {
function* zip(strings: string[], params: ClassName[]) {
yield strings.shift() as string;
while (strings.length) {
yield params.shift() as ClassName;
yield strings.shift() as string;
}
}
return (
modifiers: TemplateStringsArray | undefined = undefined,
...dynamic: ClassName[]
) => fn([...zip([...(modifiers || [])], [...dynamic])]);
}
function allowMixing(fn: TemplateFn) {
return (...args: TemplateArgs) => {
const base = fn(...args);
return {
toString: () => base,
valueOf: () => base,
mix: taggedLiteral((mixes) => classNames(base, ...mixes)),
};
};
}
type BemHelper = string & {
className: string;
toString: () => string;
valueOf: () => string;
block: MixableTemplateFn;
element: MixableTemplateFn;
mix: TemplateFn;
};
function helperFactory(
name: string,
autoMix: object | string | undefined,
): BemHelper {
const snakeName = name.replace(/([a-z])(?=[A-Z])/g, "$1-").toLowerCase();
const bemFactory = new BemFactory(snakeName, autoMix);
const className = bemFactory.toString();
const toString = () => {
return bemFactory.toString();
};
const valueOf = () => {
return bemFactory.toString();
};
const block = allowMixing(
taggedLiteral((modifiers) => bemFactory.block(...modifiers)),
);
const element = allowMixing((strings, ...modifiers) =>
classNames(
bemFactory.element(strings[0], ...modifiers),
...strings.slice(1),
),
);
const mix = (otherClasses: readonly string[]) => {
return [bemFactory.toString(), otherClasses].join(" ");
};
return {
className,
toString,
valueOf,
block,
element,
mix,
} as BemHelper & string;
}
type BemProps<P> = { bem: BemHelper } & P;
type OptionalClassName = {
className?: string;
};
function createWrappedComponent<P>(
name: string,
Component: React.ComponentType<BemProps<P>>,
): React.ComponentType<P & OptionalClassName> {
const WrappedComponent = (args: P) => {
const parentMix = (args as OptionalClassName)?.className;
const bem = useMemo(
() => helperFactory(name, parentMix),
[String(parentMix)],
);
return <Component {...args} bem={bem} />;
};
WrappedComponent.displayName = `Bem(${name})`;
return WrappedComponent;
}
export function withBem<P>(Component: React.ComponentType<BemProps<P>>) {
const name = Component.displayName;
if (!name) {
console.warn(
`withBem called on an anonymous component. Add .displayName to your component`,
Component,
);
}
return createWrappedComponent(name, Component);
}
withBem.named = createWrappedComponent;
export namespace withBem {
export type props<T = {}> = BemProps<T>;
}