-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathapi.tsx
More file actions
120 lines (102 loc) · 2.75 KB
/
api.tsx
File metadata and controls
120 lines (102 loc) · 2.75 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
import {
createElement,
useMemo,
type ComponentPropsWithRef,
type ComponentType,
type PropsWithChildren,
} from "react";
import { Appearance } from "react-native";
import type {
ColorScheme,
Props,
StyledConfiguration,
StyledOptions,
StyledProps,
} from "react-native-css";
import type { ReactComponent } from "../runtime.types";
import { assignStyle } from "./assign-style";
const defaultMapping: StyledConfiguration<ComponentType<{ style: unknown }>> = {
className: "style",
};
export const styled = <
const C extends ReactComponent,
const M extends StyledConfiguration<C>,
>(
baseComponent: C,
mapping: M = defaultMapping as M,
_options?: StyledOptions,
) => {
return (props: StyledProps<ComponentPropsWithRef<C>, M>) => {
return useCssElement(baseComponent, mapping, props);
};
};
export const useCssElement = <C extends ReactComponent>(
component: C,
incomingProps: Props,
mapping: StyledConfiguration<C>,
) => {
let props = { ...incomingProps };
for (const [key, value] of Object.entries(mapping)) {
const source: unknown = props[key];
if (!source) {
continue;
}
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete props[key];
let target: string | boolean =
typeof value === "object" ? value.target : value;
if (typeof target === "boolean") {
target = key;
}
props = assignStyle(
{ $$css: true, [key]: source },
target.split("."),
props,
);
}
return createElement(component, props);
};
export const colorScheme: ColorScheme = {
get() {
return Appearance.getColorScheme();
},
set(name) {
Appearance.setColorScheme(name);
},
};
/**
* @deprecated Use `<VariableContextProvider />` instead.
*/
export function vars(variables: Record<string, string | number>) {
const $variables: Record<string, string> = {};
for (const [key, value] of Object.entries(variables)) {
if (key.startsWith("--")) {
$variables[key] = value.toString();
} else {
$variables[`--${key}`] = value.toString();
}
}
return $variables;
}
export function VariableContextProvider(
props: PropsWithChildren<{ value: Record<`--${string}`, string | number> }>,
) {
const style = useMemo(() => {
return {
display: "contents",
...Object.fromEntries(
Object.entries(props.value).map(([key, value]) => [
key.startsWith("--") ? key : `--${key}`,
value,
]),
),
};
}, [props.value]);
return <div style={style}>{props.children}</div>;
}
export const useNativeVariable = () => {
throw new Error("useNativeVariable is not supported in web");
};
export const useUnstableNativeVariable = () => {
throw new Error("useUnstableNativeVariable is not supported in web");
};