@umber-style/react-native 0.0.0 → 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ let _umber_style_core = require("@umber-style/core");
2
+ const require_createStyle = require("./createStyle.cjs");
3
+ let react_native = require("react-native");
4
+ // src/defaultStyle.ts
5
+ /** Makes the default styling: one holder of the base theme, behind `styled`, `ThemeProvider` and the rest. */
6
+ function createDefaultStyle() {
7
+ const holder = { baseTheme: void 0 };
8
+ let bound;
9
+ const getStyle = () => {
10
+ bound ??= require_createStyle.bindHolder(holder);
11
+ return bound;
12
+ };
13
+ /** Gives the default style a base theme. Throws once the default style was used. */
14
+ function configure(options) {
15
+ if (bound) throw new Error("configure must be called before the default style is used.");
16
+ holder.baseTheme = options.theme;
17
+ }
18
+ const styled = ((tag, options) => getStyle().styled(tag, options));
19
+ const StyleSheet = {
20
+ ...react_native.StyleSheet,
21
+ create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : (0, _umber_style_core.createDeferredRecord)(styles, () => getStyle().StyleSheet.create(styles)))
22
+ };
23
+ const useStyle = ((sheet) => getStyle().useStyle(sheet));
24
+ function useTheme() {
25
+ return getStyle().useTheme();
26
+ }
27
+ function ThemeProvider(props) {
28
+ return getStyle().ThemeProvider(props);
29
+ }
30
+ return {
31
+ configure,
32
+ styled,
33
+ StyleSheet,
34
+ useStyle,
35
+ useTheme,
36
+ ThemeProvider,
37
+ apply: ((...entries) => getStyle().apply(...entries)),
38
+ when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
39
+ whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
40
+ themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme))
41
+ };
42
+ }
43
+ exports.createDefaultStyle = createDefaultStyle;
@@ -0,0 +1,23 @@
1
+ import { NativeStyle, NativeStyleSheet, SheetDefinition, StyleOptions, Styled, ThemeProviderProps } from "./types.cjs";
2
+ import { NativeSheetStyles, NativeStyleObject } from "./nativeStyle.cjs";
3
+ import { Theme } from "@umber-style/core";
4
+ export interface DefaultStyleOptions extends StyleOptions<Theme> {
5
+ /** The base theme, for an app that has no provider at all. */
6
+ theme?: Theme;
7
+ }
8
+ /** Makes the default styling: one holder of the base theme, behind `styled`, `ThemeProvider` and the rest. */
9
+ export declare function createDefaultStyle(): {
10
+ configure: (options: DefaultStyleOptions) => void;
11
+ styled: Styled<Theme>;
12
+ StyleSheet: NativeStyleSheet<Theme>;
13
+ useStyle: {
14
+ <K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
15
+ <S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
16
+ };
17
+ useTheme: () => Theme;
18
+ ThemeProvider: (props: ThemeProviderProps<Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
19
+ apply: NativeStyle<Theme>['apply'];
20
+ when: NativeStyle<Theme>['when'];
21
+ whenScheme: NativeStyle<Theme>['whenScheme'];
22
+ themeClass: NativeStyle<Theme>['themeClass'];
23
+ };
@@ -0,0 +1,23 @@
1
+ import { NativeStyle, NativeStyleSheet, SheetDefinition, StyleOptions, Styled, ThemeProviderProps } from "./types.mjs";
2
+ import { NativeSheetStyles, NativeStyleObject } from "./nativeStyle.mjs";
3
+ import { Theme } from "@umber-style/core";
4
+ export interface DefaultStyleOptions extends StyleOptions<Theme> {
5
+ /** The base theme, for an app that has no provider at all. */
6
+ theme?: Theme;
7
+ }
8
+ /** Makes the default styling: one holder of the base theme, behind `styled`, `ThemeProvider` and the rest. */
9
+ export declare function createDefaultStyle(): {
10
+ configure: (options: DefaultStyleOptions) => void;
11
+ styled: Styled<Theme>;
12
+ StyleSheet: NativeStyleSheet<Theme>;
13
+ useStyle: {
14
+ <K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
15
+ <S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
16
+ };
17
+ useTheme: () => Theme;
18
+ ThemeProvider: (props: ThemeProviderProps<Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
19
+ apply: NativeStyle<Theme>['apply'];
20
+ when: NativeStyle<Theme>['when'];
21
+ whenScheme: NativeStyle<Theme>['whenScheme'];
22
+ themeClass: NativeStyle<Theme>['themeClass'];
23
+ };
@@ -0,0 +1,43 @@
1
+ import { createDeferredRecord } from "@umber-style/core";
2
+ import { bindHolder } from "./createStyle.mjs";
3
+ import { platformStyleSheet } from "./platform.mjs";
4
+ // src/defaultStyle.ts
5
+ /** Makes the default styling: one holder of the base theme, behind `styled`, `ThemeProvider` and the rest. */
6
+ function createDefaultStyle() {
7
+ const holder = { baseTheme: void 0 };
8
+ let bound;
9
+ const getStyle = () => {
10
+ bound ??= bindHolder(holder);
11
+ return bound;
12
+ };
13
+ /** Gives the default style a base theme. Throws once the default style was used. */
14
+ function configure(options) {
15
+ if (bound) throw new Error("configure must be called before the default style is used.");
16
+ holder.baseTheme = options.theme;
17
+ }
18
+ const styled = ((tag, options) => getStyle().styled(tag, options));
19
+ const StyleSheet = {
20
+ ...platformStyleSheet,
21
+ create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : createDeferredRecord(styles, () => getStyle().StyleSheet.create(styles)))
22
+ };
23
+ const useStyle = ((sheet) => getStyle().useStyle(sheet));
24
+ function useTheme() {
25
+ return getStyle().useTheme();
26
+ }
27
+ function ThemeProvider(props) {
28
+ return getStyle().ThemeProvider(props);
29
+ }
30
+ return {
31
+ configure,
32
+ styled,
33
+ StyleSheet,
34
+ useStyle,
35
+ useTheme,
36
+ ThemeProvider,
37
+ apply: ((...entries) => getStyle().apply(...entries)),
38
+ when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
39
+ whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
40
+ themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme))
41
+ };
42
+ }
43
+ export { createDefaultStyle };
package/dist/index.cjs ADDED
@@ -0,0 +1,32 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_defaultStyle = require("./defaultStyle.cjs");
3
+ let _umber_style_core = require("@umber-style/core");
4
+ const require_alpha = require("./alpha.cjs");
5
+ const require_calc = require("./calc.cjs");
6
+ const require_createStyle = require("./createStyle.cjs");
7
+ const { configure, styled, StyleSheet, useStyle, useTheme, ThemeProvider, apply, themeClass, when, whenScheme } = require_defaultStyle.createDefaultStyle();
8
+ exports.StyleSheet = StyleSheet;
9
+ exports.ThemeProvider = ThemeProvider;
10
+ exports.add = require_calc.add;
11
+ exports.alpha = require_alpha.alpha;
12
+ exports.apply = apply;
13
+ Object.defineProperty(exports, "at", {
14
+ enumerable: true,
15
+ get: function() {
16
+ return _umber_style_core.at;
17
+ }
18
+ });
19
+ exports.bindHolder = require_createStyle.bindHolder;
20
+ exports.configure = configure;
21
+ exports.createDefaultStyle = require_defaultStyle.createDefaultStyle;
22
+ exports.createStyle = require_createStyle.createStyle;
23
+ exports.divide = require_calc.divide;
24
+ exports.multiply = require_calc.multiply;
25
+ exports.negate = require_calc.negate;
26
+ exports.styled = styled;
27
+ exports.subtract = require_calc.subtract;
28
+ exports.themeClass = themeClass;
29
+ exports.useStyle = useStyle;
30
+ exports.useTheme = useTheme;
31
+ exports.when = when;
32
+ exports.whenScheme = whenScheme;
@@ -0,0 +1,18 @@
1
+ import { at } from "@umber-style/core";
2
+ import { DefaultStyleOptions, createDefaultStyle } from "./defaultStyle.cjs";
3
+ import { NativeStyle, NativeStyleSheet, SheetDefinition, StyleOptions, StyleProp, Styled, StyledComponent, StyledOptions, ThemeProviderProps } from "./types.cjs";
4
+ import { NativeObjectSheet, NativeSheetInput, NativeSheetStyles, NativeStyleFunction, NativeStyleInput, NativeStyleObject, NativeStyleResult } from "./nativeStyle.cjs";
5
+ import { alpha } from "./alpha.cjs";
6
+ import { Operand, Percent, add, divide, multiply, negate, subtract } from "./calc.cjs";
7
+ import { NativeHolder, bindHolder, createStyle } from "./createStyle.cjs";
8
+ export declare const configure: (options: DefaultStyleOptions) => void, styled: Styled<import("@umber-style/core").Theme>, StyleSheet: NativeStyleSheet<import("@umber-style/core").Theme>, useStyle: {
9
+ <K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
10
+ <S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
11
+ }, useTheme: () => import("@umber-style/core").Theme, ThemeProvider: (props: ThemeProviderProps<import("@umber-style/core").Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>, apply: (...entries: (NativeStyleObject | null | undefined | false)[]) => {
12
+ style: NativeStyleObject[];
13
+ }, themeClass: (theme: {
14
+ readonly _theme?: never;
15
+ }, colorScheme?: import("@umber-style/core").ColorScheme) => string, when: (theme: {
16
+ readonly _theme?: never;
17
+ }, colorScheme?: import("@umber-style/core").ColorScheme) => import("@umber-style/core").ThemeKey, whenScheme: (scheme: import("@umber-style/core").SchemeName) => import("@umber-style/core").SchemeKey;
18
+ export { DefaultStyleOptions, NativeHolder, NativeObjectSheet, NativeSheetInput, NativeSheetStyles, NativeStyle, NativeStyleFunction, NativeStyleInput, NativeStyleObject, NativeStyleResult, NativeStyleSheet, Operand, Percent, SheetDefinition, StyleOptions, StyleProp, Styled, StyledComponent, StyledOptions, ThemeProviderProps, add, alpha, at, bindHolder, createDefaultStyle, createStyle, divide, multiply, negate, subtract };
@@ -0,0 +1,18 @@
1
+ import { DefaultStyleOptions, createDefaultStyle } from "./defaultStyle.mjs";
2
+ import { NativeStyle, NativeStyleSheet, SheetDefinition, StyleOptions, StyleProp, Styled, StyledComponent, StyledOptions, ThemeProviderProps } from "./types.mjs";
3
+ import { NativeObjectSheet, NativeSheetInput, NativeSheetStyles, NativeStyleFunction, NativeStyleInput, NativeStyleObject, NativeStyleResult } from "./nativeStyle.mjs";
4
+ import { at } from "@umber-style/core";
5
+ import { alpha } from "./alpha.mjs";
6
+ import { Operand, Percent, add, divide, multiply, negate, subtract } from "./calc.mjs";
7
+ import { NativeHolder, bindHolder, createStyle } from "./createStyle.mjs";
8
+ export declare const configure: (options: DefaultStyleOptions) => void, styled: Styled<import("@umber-style/core").Theme>, StyleSheet: NativeStyleSheet<import("@umber-style/core").Theme>, useStyle: {
9
+ <K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
10
+ <S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
11
+ }, useTheme: () => import("@umber-style/core").Theme, ThemeProvider: (props: ThemeProviderProps<import("@umber-style/core").Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>, apply: (...entries: (NativeStyleObject | null | undefined | false)[]) => {
12
+ style: NativeStyleObject[];
13
+ }, themeClass: (theme: {
14
+ readonly _theme?: never;
15
+ }, colorScheme?: import("@umber-style/core").ColorScheme) => string, when: (theme: {
16
+ readonly _theme?: never;
17
+ }, colorScheme?: import("@umber-style/core").ColorScheme) => import("@umber-style/core").ThemeKey, whenScheme: (scheme: import("@umber-style/core").SchemeName) => import("@umber-style/core").SchemeKey;
18
+ export { DefaultStyleOptions, NativeHolder, NativeObjectSheet, NativeSheetInput, NativeSheetStyles, NativeStyle, NativeStyleFunction, NativeStyleInput, NativeStyleObject, NativeStyleResult, NativeStyleSheet, Operand, Percent, SheetDefinition, StyleOptions, StyleProp, Styled, StyledComponent, StyledOptions, ThemeProviderProps, add, alpha, at, bindHolder, createDefaultStyle, createStyle, divide, multiply, negate, subtract };
package/dist/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ import { createDefaultStyle } from "./defaultStyle.mjs";
2
+ import { at } from "@umber-style/core";
3
+ import { alpha } from "./alpha.mjs";
4
+ import { add, divide, multiply, negate, subtract } from "./calc.mjs";
5
+ import { bindHolder, createStyle } from "./createStyle.mjs";
6
+ const { configure, styled, StyleSheet, useStyle, useTheme, ThemeProvider, apply, themeClass, when, whenScheme } = createDefaultStyle();
7
+ export { StyleSheet, ThemeProvider, add, alpha, apply, at, bindHolder, configure, createDefaultStyle, createStyle, divide, multiply, negate, styled, subtract, themeClass, useStyle, useTheme, when, whenScheme };
@@ -0,0 +1,32 @@
1
+ import { ImageStyle, TextStyle, ViewStyle } from "react-native";
2
+ /** What a style of any one component takes: a view, a text or an image. */
3
+ type ComponentStyle = ViewStyle | TextStyle | ImageStyle;
4
+ type KeyOf<S> = S extends unknown ? keyof S : never;
5
+ /** The type of `K` in the members of `S` that have it. */
6
+ type ValueOf<S, K extends PropertyKey> = S extends unknown ? K extends keyof S ? S[K] : never : never;
7
+ /**
8
+ * A style for React Native: the keys of the platform's view, text and image styles, each with the value
9
+ * type of the platform. Nothing is nested, and there is no `label`: a key that is not the platform's is
10
+ * an error in an object literal.
11
+ */
12
+ export type NativeStyleObject = { [K in KeyOf<ComponentStyle>]?: ValueOf<ComponentStyle, K>; };
13
+ /** A set of named native styles. */
14
+ export type NativeSheetStyles = Record<string, NativeStyleObject>;
15
+ /** What a style function may return on React Native. */
16
+ export type NativeStyleResult = NativeStyleObject | null | undefined | false;
17
+ /** A native style that is computed from the props and the theme. */
18
+ export type NativeStyleFunction<T, P> = (props: P & {
19
+ theme: T;
20
+ }) => NativeStyleResult;
21
+ /** A native style object, or a function of the props and the theme; the defaults accept any function. */
22
+ export type NativeStyleInput<T = never, P = never> = NativeStyleObject | NativeStyleFunction<T, P>;
23
+ /**
24
+ * A native sheet given as an object: one style for each name. It is a record of styles with a function
25
+ * left out, so that a function of the theme is not taken for one (the compiler tries the object form first).
26
+ */
27
+ export type NativeObjectSheet<K extends string> = Record<K, NativeStyleObject> & {
28
+ call?: never;
29
+ };
30
+ /** What a native sheet is made from: one style for each name, or a function of the theme that returns them. */
31
+ export type NativeSheetInput<K extends string, T> = Record<K, NativeStyleObject> | ((theme: T) => Record<K, NativeStyleObject>);
32
+ export {};
@@ -0,0 +1,32 @@
1
+ import { ImageStyle, TextStyle, ViewStyle } from "react-native";
2
+ /** What a style of any one component takes: a view, a text or an image. */
3
+ type ComponentStyle = ViewStyle | TextStyle | ImageStyle;
4
+ type KeyOf<S> = S extends unknown ? keyof S : never;
5
+ /** The type of `K` in the members of `S` that have it. */
6
+ type ValueOf<S, K extends PropertyKey> = S extends unknown ? K extends keyof S ? S[K] : never : never;
7
+ /**
8
+ * A style for React Native: the keys of the platform's view, text and image styles, each with the value
9
+ * type of the platform. Nothing is nested, and there is no `label`: a key that is not the platform's is
10
+ * an error in an object literal.
11
+ */
12
+ export type NativeStyleObject = { [K in KeyOf<ComponentStyle>]?: ValueOf<ComponentStyle, K>; };
13
+ /** A set of named native styles. */
14
+ export type NativeSheetStyles = Record<string, NativeStyleObject>;
15
+ /** What a style function may return on React Native. */
16
+ export type NativeStyleResult = NativeStyleObject | null | undefined | false;
17
+ /** A native style that is computed from the props and the theme. */
18
+ export type NativeStyleFunction<T, P> = (props: P & {
19
+ theme: T;
20
+ }) => NativeStyleResult;
21
+ /** A native style object, or a function of the props and the theme; the defaults accept any function. */
22
+ export type NativeStyleInput<T = never, P = never> = NativeStyleObject | NativeStyleFunction<T, P>;
23
+ /**
24
+ * A native sheet given as an object: one style for each name. It is a record of styles with a function
25
+ * left out, so that a function of the theme is not taken for one (the compiler tries the object form first).
26
+ */
27
+ export type NativeObjectSheet<K extends string> = Record<K, NativeStyleObject> & {
28
+ call?: never;
29
+ };
30
+ /** What a native sheet is made from: one style for each name, or a function of the theme that returns them. */
31
+ export type NativeSheetInput<K extends string, T> = Record<K, NativeStyleObject> | ((theme: T) => Record<K, NativeStyleObject>);
32
+ export {};
@@ -0,0 +1,2 @@
1
+ import { StyleSheet as platformStyleSheet } from "react-native";
2
+ export { platformStyleSheet };
@@ -0,0 +1,2 @@
1
+ import { StyleSheet as platformStyleSheet } from "react-native";
2
+ export { platformStyleSheet };
@@ -0,0 +1 @@
1
+ export { Appearance as platformAppearance, StyleSheet as platformStyleSheet } from "react-native";
@@ -0,0 +1,44 @@
1
+ let react = require("react");
2
+ const require_toNativeStyle = require("./toNativeStyle.cjs");
3
+ let react_native = require("react-native");
4
+ // src/styled.ts
5
+ /**
6
+ * Makes `styled` for React Native. Its object styles are registered with the platform StyleSheet
7
+ * once, when the component is made; its function styles are computed with the real theme, which
8
+ * `useTheme` reads, on each render. A style with a `when()` or `whenScheme()` key is resolved on each
9
+ * render against the nearest provider, which `useCondition` reads; it is not registered. A component
10
+ * without function styles or condition keys reads no context.
11
+ */
12
+ function createStyled(useTheme, useCondition) {
13
+ const styled = (tag, options = {}) => {
14
+ if (typeof tag === "string") throw new TypeError(`styled needs a component on React Native, not the tag "${tag}".`);
15
+ const { shouldForwardProp: shouldForward } = options;
16
+ return (...styles) => {
17
+ const isDynamic = styles.some((style) => typeof style === "function");
18
+ const isConditional = styles.some((style) => typeof style !== "function" && !!style && require_toNativeStyle.hasConditions(style));
19
+ const fixed = Object.fromEntries(styles.flatMap((style, index) => typeof style === "function" || !style || require_toNativeStyle.hasConditions(style) ? [] : [[`s${index}`, require_toNativeStyle.toNativeStyle(style)]]));
20
+ const registered = Object.keys(fixed).length > 0 ? react_native.StyleSheet.create(fixed) : fixed;
21
+ const render = (props, theme, condition) => {
22
+ const resolved = styles.flatMap((style, index) => {
23
+ if (typeof style !== "function") return [style && require_toNativeStyle.hasConditions(style) ? require_toNativeStyle.toNativeStyle(style, condition) : registered[`s${index}`]];
24
+ const result = style({
25
+ ...props,
26
+ theme
27
+ });
28
+ return result ? [require_toNativeStyle.toNativeStyle(result, condition)] : [];
29
+ });
30
+ const forwarded = {};
31
+ for (const [key, value] of Object.entries(props)) if (!(key === "as" || key === "theme" || key === "style") && (!shouldForward || shouldForward(key))) forwarded[key] = value;
32
+ forwarded.style = [...resolved, props.style].flat(1e400).filter(Boolean);
33
+ return (0, react.createElement)(props.as ?? tag, forwarded);
34
+ };
35
+ const StaticStyled = (props) => render(props);
36
+ const DynamicStyled = (props) => render(props, useTheme(), useCondition());
37
+ const ConditionalStyled = (props) => render(props, void 0, useCondition());
38
+ if (isDynamic) return DynamicStyled;
39
+ return isConditional ? ConditionalStyled : StaticStyled;
40
+ };
41
+ };
42
+ return styled;
43
+ }
44
+ exports.createStyled = createStyled;
@@ -0,0 +1,44 @@
1
+ import { createElement } from "react";
2
+ import { platformStyleSheet } from "./platform.mjs";
3
+ import { hasConditions, toNativeStyle } from "./toNativeStyle.mjs";
4
+ // src/styled.ts
5
+ /**
6
+ * Makes `styled` for React Native. Its object styles are registered with the platform StyleSheet
7
+ * once, when the component is made; its function styles are computed with the real theme, which
8
+ * `useTheme` reads, on each render. A style with a `when()` or `whenScheme()` key is resolved on each
9
+ * render against the nearest provider, which `useCondition` reads; it is not registered. A component
10
+ * without function styles or condition keys reads no context.
11
+ */
12
+ function createStyled(useTheme, useCondition) {
13
+ const styled = (tag, options = {}) => {
14
+ if (typeof tag === "string") throw new TypeError(`styled needs a component on React Native, not the tag "${tag}".`);
15
+ const { shouldForwardProp: shouldForward } = options;
16
+ return (...styles) => {
17
+ const isDynamic = styles.some((style) => typeof style === "function");
18
+ const isConditional = styles.some((style) => typeof style !== "function" && !!style && hasConditions(style));
19
+ const fixed = Object.fromEntries(styles.flatMap((style, index) => typeof style === "function" || !style || hasConditions(style) ? [] : [[`s${index}`, toNativeStyle(style)]]));
20
+ const registered = Object.keys(fixed).length > 0 ? platformStyleSheet.create(fixed) : fixed;
21
+ const render = (props, theme, condition) => {
22
+ const resolved = styles.flatMap((style, index) => {
23
+ if (typeof style !== "function") return [style && hasConditions(style) ? toNativeStyle(style, condition) : registered[`s${index}`]];
24
+ const result = style({
25
+ ...props,
26
+ theme
27
+ });
28
+ return result ? [toNativeStyle(result, condition)] : [];
29
+ });
30
+ const forwarded = {};
31
+ for (const [key, value] of Object.entries(props)) if (!(key === "as" || key === "theme" || key === "style") && (!shouldForward || shouldForward(key))) forwarded[key] = value;
32
+ forwarded.style = [...resolved, props.style].flat(1e400).filter(Boolean);
33
+ return createElement(props.as ?? tag, forwarded);
34
+ };
35
+ const StaticStyled = (props) => render(props);
36
+ const DynamicStyled = (props) => render(props, useTheme(), useCondition());
37
+ const ConditionalStyled = (props) => render(props, void 0, useCondition());
38
+ if (isDynamic) return DynamicStyled;
39
+ return isConditional ? ConditionalStyled : StaticStyled;
40
+ };
41
+ };
42
+ return styled;
43
+ }
44
+ export { createStyled };
@@ -0,0 +1,39 @@
1
+ let _umber_style_core = require("@umber-style/core");
2
+ const require_conditions = require("./conditions.cjs");
3
+ const require_warnStyle = require("./warnStyle.cjs");
4
+ // src/toNativeStyle.ts
5
+ /** Whether `value` is a plain object: the `native` part of a style. */
6
+ function isNested(value) {
7
+ return typeof value === "object" && value !== null && !Array.isArray(value);
8
+ }
9
+ /** Whether the key `name` exists only on the web: a selector, an at-rule, a custom property or `label`. */
10
+ function isWebOnlyKey(name) {
11
+ return name === "label" || name.includes("&") || /^(?::|@|--)/.test(name);
12
+ }
13
+ /** Whether `style`, or its native part, has a `when()` or `whenScheme()` key: then the result depends on the providers above. */
14
+ function hasConditions(style) {
15
+ const { native } = style;
16
+ return [...Object.keys(style), ...isNested(native) ? Object.keys(native) : []].some((key) => (0, _umber_style_core.readCondition)(key) !== void 0);
17
+ }
18
+ /**
19
+ * `style` without the web-only keys: `label`, selectors (a key with `&` or one that starts with `:`),
20
+ * at-rules and custom properties, each dropped whole. Objects and arrays under any other key are kept
21
+ * (`shadowOffset`, `transform`). A style written for both platforms has a `native` part, which is merged
22
+ * over the rest, and a `web` part, which is dropped. A `when()` or `whenScheme()` key is a part that
23
+ * applies when it matches `condition` (the nearest provider): it is merged over the rest, in order; with no
24
+ * `condition` it is dropped.
25
+ */
26
+ function toNativeStyle(style, condition) {
27
+ const { web: _web, native, ...rest } = style;
28
+ const merged = isNested(native) ? {
29
+ ...rest,
30
+ ...native
31
+ } : rest;
32
+ const resolved = Object.fromEntries(Object.entries(merged).filter(([key]) => !isWebOnlyKey(key)));
33
+ const matching = Object.entries(merged).filter(([key, part]) => isNested(part) && require_conditions.matchesCondition(key, condition));
34
+ for (const [, part] of matching) Object.assign(resolved, toNativeStyle(part, condition));
35
+ require_warnStyle.warnAboutValues(resolved);
36
+ return resolved;
37
+ }
38
+ exports.hasConditions = hasConditions;
39
+ exports.toNativeStyle = toNativeStyle;
@@ -0,0 +1,38 @@
1
+ import { readCondition } from "@umber-style/core";
2
+ import { matchesCondition } from "./conditions.mjs";
3
+ import { warnAboutValues } from "./warnStyle.mjs";
4
+ // src/toNativeStyle.ts
5
+ /** Whether `value` is a plain object: the `native` part of a style. */
6
+ function isNested(value) {
7
+ return typeof value === "object" && value !== null && !Array.isArray(value);
8
+ }
9
+ /** Whether the key `name` exists only on the web: a selector, an at-rule, a custom property or `label`. */
10
+ function isWebOnlyKey(name) {
11
+ return name === "label" || name.includes("&") || /^(?::|@|--)/.test(name);
12
+ }
13
+ /** Whether `style`, or its native part, has a `when()` or `whenScheme()` key: then the result depends on the providers above. */
14
+ function hasConditions(style) {
15
+ const { native } = style;
16
+ return [...Object.keys(style), ...isNested(native) ? Object.keys(native) : []].some((key) => readCondition(key) !== void 0);
17
+ }
18
+ /**
19
+ * `style` without the web-only keys: `label`, selectors (a key with `&` or one that starts with `:`),
20
+ * at-rules and custom properties, each dropped whole. Objects and arrays under any other key are kept
21
+ * (`shadowOffset`, `transform`). A style written for both platforms has a `native` part, which is merged
22
+ * over the rest, and a `web` part, which is dropped. A `when()` or `whenScheme()` key is a part that
23
+ * applies when it matches `condition` (the nearest provider): it is merged over the rest, in order; with no
24
+ * `condition` it is dropped.
25
+ */
26
+ function toNativeStyle(style, condition) {
27
+ const { web: _web, native, ...rest } = style;
28
+ const merged = isNested(native) ? {
29
+ ...rest,
30
+ ...native
31
+ } : rest;
32
+ const resolved = Object.fromEntries(Object.entries(merged).filter(([key]) => !isWebOnlyKey(key)));
33
+ const matching = Object.entries(merged).filter(([key, part]) => isNested(part) && matchesCondition(key, condition));
34
+ for (const [, part] of matching) Object.assign(resolved, toNativeStyle(part, condition));
35
+ warnAboutValues(resolved);
36
+ return resolved;
37
+ }
38
+ export { hasConditions, toNativeStyle };
@@ -0,0 +1,62 @@
1
+ import { BindingOptions, ColorScheme, DeepPartial, SchemeKey, SchemeName, StylerOptions, ThemeKey } from "@umber-style/core";
2
+ import { NativeObjectSheet, NativeSheetStyles, NativeStyleInput, NativeStyleObject } from "./nativeStyle.cjs";
3
+ import { platformStyleSheet } from "./platform.cjs";
4
+ import { ComponentProps, ElementType, FunctionComponent, ReactElement, ReactNode } from "react";
5
+ /** The options of `createStyle`: the same as on the web, so one call runs on both; they are not used. */
6
+ export type StyleOptions<T = never> = BindingOptions<T>;
7
+ export interface StyledOptions extends StylerOptions {
8
+ /** Whether a prop is forwarded. Default: every prop but `as`, `theme` and `style`. */
9
+ shouldForwardProp?: (prop: string) => boolean;
10
+ }
11
+ /** What the `style` prop of a styled component takes: style objects (sheet entries are such objects), in arrays, with falsy values dropped. */
12
+ type StyleValue = NativeStyleObject | false | null | readonly StyleProp[];
13
+ export type StyleProp = StyleValue | undefined;
14
+ export type StyledComponent<C extends ElementType, P extends object> = FunctionComponent<Omit<ComponentProps<C>, keyof P | 'as' | 'style'> & P & {
15
+ as?: ElementType;
16
+ style?: StyleValue;
17
+ }>;
18
+ export type Styled<T> = <C extends ElementType>(tag: C, options?: StyledOptions) => <P extends object = object>(...styles: NativeStyleInput<T, P>[]) => StyledComponent<C, P>;
19
+ /** A set of named styles; `E` is what one of them becomes. */
20
+ export interface SheetDefinition<S extends NativeSheetStyles, E> {
21
+ /** Never set at run time: it only carries the types. */
22
+ readonly entries?: { [K in keyof S]: E; };
23
+ }
24
+ export interface ThemeProviderProps<T> {
25
+ /** The theme, or the part of it that this subtree changes. */
26
+ readonly theme: DeepPartial<T>;
27
+ /** The color scheme of this subtree, for `whenScheme`: `light` or `dark`; `light dark` keeps the one above. */
28
+ readonly colorScheme?: ColorScheme;
29
+ /** Ignored on React Native. */
30
+ readonly className?: string;
31
+ /** Accepted and ignored: there are no classes on React Native, so the provider never renders an element. */
32
+ readonly isRoot?: boolean;
33
+ readonly children?: ReactNode;
34
+ }
35
+ /** The platform StyleSheet, with a `create` that registers the styles, and resolves them with the theme when they are a function of it. */
36
+ export type NativeStyleSheet<T> = Omit<typeof platformStyleSheet, 'create'> & {
37
+ /** A sheet given as an object is registered with the platform on first read and is its registered styles, usable at once, with no hook and no theme. */
38
+ create<K extends string>(styles: NativeObjectSheet<K>): { readonly [P in K]: NativeStyleObject; };
39
+ /** A sheet given as a function of the theme is for `useStyle`, which resolves it against the theme. */
40
+ create<K extends string>(styles: (theme: T) => Record<K, NativeStyleObject>): SheetDefinition<Record<K, NativeStyleObject>, NativeStyleObject>;
41
+ };
42
+ export interface NativeStyle<T> {
43
+ styled: Styled<T>;
44
+ StyleSheet: NativeStyleSheet<T>;
45
+ /** The styles of a sheet; an object sheet is its own registered styles and is returned as it is. */
46
+ useStyle<K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
47
+ useStyle<S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
48
+ /** The props that apply entries of a sheet: a style array. */
49
+ apply(...entries: (NativeStyleObject | null | undefined | false)[]): {
50
+ style: NativeStyleObject[];
51
+ };
52
+ ThemeProvider(props: ThemeProviderProps<T>): ReactElement;
53
+ /** The real theme values, for JavaScript that needs a number or a name. */
54
+ useTheme(): T;
55
+ /** Always '': there are no classes on React Native. */
56
+ themeClass(theme: DeepPartial<T>, colorScheme?: ColorScheme): string;
57
+ /** A style key for "the nearest provider has this theme object" (compared by identity; `colorScheme` is not used here). */
58
+ when(theme: DeepPartial<T>, colorScheme?: ColorScheme): ThemeKey;
59
+ /** A style key for "the nearest provider's `colorScheme` is this one"; with none above, the platform's color scheme. */
60
+ whenScheme(scheme: SchemeName): SchemeKey;
61
+ }
62
+ export {};
@@ -0,0 +1,62 @@
1
+ import { BindingOptions, ColorScheme, DeepPartial, SchemeKey, SchemeName, StylerOptions, ThemeKey } from "@umber-style/core";
2
+ import { NativeObjectSheet, NativeSheetStyles, NativeStyleInput, NativeStyleObject } from "./nativeStyle.mjs";
3
+ import { platformStyleSheet } from "./platform.mjs";
4
+ import { ComponentProps, ElementType, FunctionComponent, ReactElement, ReactNode } from "react";
5
+ /** The options of `createStyle`: the same as on the web, so one call runs on both; they are not used. */
6
+ export type StyleOptions<T = never> = BindingOptions<T>;
7
+ export interface StyledOptions extends StylerOptions {
8
+ /** Whether a prop is forwarded. Default: every prop but `as`, `theme` and `style`. */
9
+ shouldForwardProp?: (prop: string) => boolean;
10
+ }
11
+ /** What the `style` prop of a styled component takes: style objects (sheet entries are such objects), in arrays, with falsy values dropped. */
12
+ type StyleValue = NativeStyleObject | false | null | readonly StyleProp[];
13
+ export type StyleProp = StyleValue | undefined;
14
+ export type StyledComponent<C extends ElementType, P extends object> = FunctionComponent<Omit<ComponentProps<C>, keyof P | 'as' | 'style'> & P & {
15
+ as?: ElementType;
16
+ style?: StyleValue;
17
+ }>;
18
+ export type Styled<T> = <C extends ElementType>(tag: C, options?: StyledOptions) => <P extends object = object>(...styles: NativeStyleInput<T, P>[]) => StyledComponent<C, P>;
19
+ /** A set of named styles; `E` is what one of them becomes. */
20
+ export interface SheetDefinition<S extends NativeSheetStyles, E> {
21
+ /** Never set at run time: it only carries the types. */
22
+ readonly entries?: { [K in keyof S]: E; };
23
+ }
24
+ export interface ThemeProviderProps<T> {
25
+ /** The theme, or the part of it that this subtree changes. */
26
+ readonly theme: DeepPartial<T>;
27
+ /** The color scheme of this subtree, for `whenScheme`: `light` or `dark`; `light dark` keeps the one above. */
28
+ readonly colorScheme?: ColorScheme;
29
+ /** Ignored on React Native. */
30
+ readonly className?: string;
31
+ /** Accepted and ignored: there are no classes on React Native, so the provider never renders an element. */
32
+ readonly isRoot?: boolean;
33
+ readonly children?: ReactNode;
34
+ }
35
+ /** The platform StyleSheet, with a `create` that registers the styles, and resolves them with the theme when they are a function of it. */
36
+ export type NativeStyleSheet<T> = Omit<typeof platformStyleSheet, 'create'> & {
37
+ /** A sheet given as an object is registered with the platform on first read and is its registered styles, usable at once, with no hook and no theme. */
38
+ create<K extends string>(styles: NativeObjectSheet<K>): { readonly [P in K]: NativeStyleObject; };
39
+ /** A sheet given as a function of the theme is for `useStyle`, which resolves it against the theme. */
40
+ create<K extends string>(styles: (theme: T) => Record<K, NativeStyleObject>): SheetDefinition<Record<K, NativeStyleObject>, NativeStyleObject>;
41
+ };
42
+ export interface NativeStyle<T> {
43
+ styled: Styled<T>;
44
+ StyleSheet: NativeStyleSheet<T>;
45
+ /** The styles of a sheet; an object sheet is its own registered styles and is returned as it is. */
46
+ useStyle<K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
47
+ useStyle<S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
48
+ /** The props that apply entries of a sheet: a style array. */
49
+ apply(...entries: (NativeStyleObject | null | undefined | false)[]): {
50
+ style: NativeStyleObject[];
51
+ };
52
+ ThemeProvider(props: ThemeProviderProps<T>): ReactElement;
53
+ /** The real theme values, for JavaScript that needs a number or a name. */
54
+ useTheme(): T;
55
+ /** Always '': there are no classes on React Native. */
56
+ themeClass(theme: DeepPartial<T>, colorScheme?: ColorScheme): string;
57
+ /** A style key for "the nearest provider has this theme object" (compared by identity; `colorScheme` is not used here). */
58
+ when(theme: DeepPartial<T>, colorScheme?: ColorScheme): ThemeKey;
59
+ /** A style key for "the nearest provider's `colorScheme` is this one"; with none above, the platform's color scheme. */
60
+ whenScheme(scheme: SchemeName): SchemeKey;
61
+ }
62
+ export {};