-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathapi.tsx
More file actions
124 lines (106 loc) · 3.21 KB
/
api.tsx
File metadata and controls
124 lines (106 loc) · 3.21 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
/* eslint-disable */
import { useContext, useState, type ComponentType } from "react";
import { Appearance } from "react-native";
import type { StyleDescriptor } from "react-native-css/compiler";
import { VariableContext } from "react-native-css/native-internal";
import type {
ColorScheme,
Props,
ReactComponent,
StyledConfiguration,
StyledOptions,
} from "../runtime.types";
import { mappingToConfig, useNativeCss } from "./react/useNativeCss";
import { usePassthrough } from "./react/usePassthrough";
import {
colorScheme as colorSchemeObs,
VAR_SYMBOL,
type Effect,
type Getter,
} from "./reactivity";
import { resolveValue } from "./styles/resolve";
export {
StyleCollection,
VariableContext,
VariableContextProvider,
} from "react-native-css/native-internal";
export { useNativeCss };
const defaultMapping: StyledConfiguration<ComponentType<{ style: unknown }>> = {
className: "style",
};
/**
* 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 = defaultMapping as 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]),
),
);
}