-
Notifications
You must be signed in to change notification settings - Fork 1
/
shared.ts
42 lines (37 loc) · 973 Bytes
/
shared.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
import { options as preactOptions, VNode } from "preact";
import {
BaseTheme,
getSheet,
setup as twSetup,
Sheet,
tw,
TwindConfig,
} from "@twind/core";
export const STYLE_ELEMENT_ID = "__FRSH_TWIND";
export function setup<Theme extends BaseTheme>(
config: TwindConfig<Theme>,
sheet: Sheet = getSheet(),
) {
const instance = twSetup(config, sheet);
const originalHook = preactOptions.vnode;
preactOptions.vnode = (
vnode: VNode<{ class?: string; className?: string }>,
) => {
if (typeof vnode.type === "string" && typeof vnode.props === "object") {
const { props } = vnode;
const classes: string[] = [];
if (props.class) {
classes.push(tw(props.class));
props.class = undefined;
}
if (props.className) {
classes.push(tw(props.className));
}
if (classes.length) {
props.class = classes.join(" ");
}
}
originalHook?.(vnode);
};
return instance;
}