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> = { className: "style", }; export const styled = < const C extends ReactComponent, const M extends StyledConfiguration, >( baseComponent: C, mapping: M = defaultMapping as M, _options?: StyledOptions, ) => { return (props: StyledProps, M>) => { return useCssElement(baseComponent, props, mapping); }; }; export const useCssElement = ( component: C, incomingProps: Props, mapping: StyledConfiguration, ) => { 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 `` instead. */ export function vars(variables: Record) { const $variables: Record = {}; 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
{props.children}
; } export const useNativeVariable = () => { throw new Error("useNativeVariable is not supported in web"); }; export const useUnstableNativeVariable = () => { throw new Error("useUnstableNativeVariable is not supported in web"); };