-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathapi.tsx
More file actions
139 lines (119 loc) · 3.66 KB
/
api.tsx
File metadata and controls
139 lines (119 loc) · 3.66 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
129
130
131
132
133
134
135
136
137
138
139
/* eslint-disable */
import { useContext, useMemo, useState, type PropsWithChildren } from "react";
import { Appearance } from "react-native";
import { VariableContext } from "react-native-css/style-collection";
import type { StyleDescriptor } from "../../compiler";
import type {
ColorScheme,
Props,
StyledConfiguration,
StyledOptions,
} from "../runtime.types";
import type { ReactComponent } from "../utils";
import { mappingToConfig, useNativeCss } from "./react/useNativeCss";
import { usePassthrough } from "./react/usePassthrough";
import {
colorScheme as colorSchemeObs,
VAR_SYMBOL,
type Effect,
type Getter,
type VariableContextValue,
} from "./reactivity";
import { resolveValue } from "./styles/resolve";
export {
StyleCollection,
VariableContext,
} from "react-native-css/style-collection";
export { useNativeCss };
/**
* Generates a new Higher-Order component the wraps the base component and applies the styles.
* This is added to the `interopComponents` map so that it can be used in the `wrapJSX` function
* @param baseComponent
* @param mapping
*/
export const styled = <
const C extends ReactComponent<any>,
const M extends StyledConfiguration<C>,
>(
baseComponent: C,
mapping: M,
options?: StyledOptions,
) => {
let component: any;
// const type = getComponentType(baseComponent);
const configs = mappingToConfig(mapping);
if (options?.passThrough) {
component = (props: Record<string, any>) => {
return usePassthrough(baseComponent, props, configs);
};
} else {
component = (props: Record<string, any>) => {
return useNativeCss(baseComponent, props, configs);
};
}
const name = baseComponent.displayName ?? baseComponent.name ?? "unknown";
component.displayName = `CssInterop.${name}`;
return component;
};
export const colorScheme: ColorScheme = {
get() {
return colorSchemeObs.get() ?? Appearance.getColorScheme() ?? "light";
},
set(value) {
return colorSchemeObs.set(value);
},
};
export const useUnstableNativeVariable = useNativeVariable;
export const useCssElement = <
const C extends ReactComponent<any>,
const M extends StyledConfiguration<C>,
>(
component: C,
incomingProps: Props,
mapping: M,
) => {
const [config] = useState(() => mappingToConfig(mapping));
return useNativeCss(component, incomingProps, config);
};
export function useNativeVariable(name: string) {
if (name.startsWith("--")) {
name = name.slice(2);
}
const inheritedVariables = useContext(VariableContext);
const [effect, setState] = useState(() => {
const effect: Effect = {
observers: new Set(),
run: () => setState((state) => ({ ...state })),
};
const get: Getter = (observable) => observable.get(effect);
return { ...effect, get };
});
return resolveValue([{}, "var", [name]], effect.get, { inheritedVariables });
}
/**
* @deprecated Use `<VariableContextProvider />` instead.
*/
export function vars(variables: Record<string, StyleDescriptor>) {
return Object.assign(
{ [VAR_SYMBOL]: "inline" },
Object.fromEntries(
Object.entries(variables).map(([k, v]) => [k.replace(/^--/, ""), v]),
),
);
}
export function VariableContextProvider(
props: PropsWithChildren<{ value: Record<`--${string}`, StyleDescriptor> }>,
) {
const inheritedVariables = useContext(VariableContext);
const value: VariableContextValue = useMemo(
() => ({
...inheritedVariables,
...Object.fromEntries(
Object.entries(props.value).map(([k, v]) => [k.replace(/^--/, ""), v]),
),
[VAR_SYMBOL]: true,
}),
[inheritedVariables, props.value],
);
return <VariableContext value={value}>{props.children}</VariableContext>;
}