@umber-style/react 0.1.0-alpha.0 → 0.1.0-alpha.2

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.
Files changed (69) hide show
  1. package/dist/CacheProvider.cjs +23 -0
  2. package/dist/CacheProvider.d.cts +10 -0
  3. package/dist/CacheProvider.d.mts +11 -0
  4. package/dist/CacheProvider.mjs +22 -0
  5. package/dist/Global.cjs +52 -0
  6. package/dist/Global.mjs +52 -0
  7. package/dist/Global.server.cjs +27 -0
  8. package/dist/Global.server.mjs +27 -0
  9. package/dist/GlobalRelease.cjs +32 -0
  10. package/dist/GlobalRelease.mjs +30 -0
  11. package/dist/ProviderScope.cjs +201 -0
  12. package/dist/ProviderScope.d.cts +14 -0
  13. package/dist/ProviderScope.d.mts +15 -0
  14. package/dist/ProviderScope.mjs +190 -0
  15. package/dist/ThemeProvider.cjs +94 -27
  16. package/dist/ThemeProvider.mjs +95 -28
  17. package/dist/ThemeProvider.server.cjs +130 -25
  18. package/dist/ThemeProvider.server.mjs +131 -26
  19. package/dist/ThemeValue.cjs +52 -20
  20. package/dist/ThemeValue.d.cts +3 -0
  21. package/dist/ThemeValue.d.mts +4 -0
  22. package/dist/ThemeValue.mjs +53 -23
  23. package/dist/_virtual/_rolldown/runtime.cjs +22 -0
  24. package/dist/createStyle.cjs +30 -20
  25. package/dist/createStyle.d.cts +4 -2
  26. package/dist/createStyle.d.mts +4 -2
  27. package/dist/createStyle.mjs +32 -22
  28. package/dist/createStyle.server.cjs +29 -22
  29. package/dist/createStyle.server.mjs +31 -24
  30. package/dist/defaultStyle.cjs +27 -3
  31. package/dist/defaultStyle.d.cts +18 -11
  32. package/dist/defaultStyle.d.mts +18 -11
  33. package/dist/defaultStyle.mjs +28 -4
  34. package/dist/defaultStyle.server.cjs +13 -5
  35. package/dist/defaultStyle.server.d.cts +16 -11
  36. package/dist/defaultStyle.server.d.mts +16 -11
  37. package/dist/defaultStyle.server.mjs +14 -6
  38. package/dist/domProps.cjs +27 -0
  39. package/dist/domProps.mjs +27 -0
  40. package/dist/index.cjs +12 -1
  41. package/dist/index.d.cts +17 -7
  42. package/dist/index.d.mts +17 -7
  43. package/dist/index.mjs +4 -2
  44. package/dist/index.server.cjs +6 -1
  45. package/dist/index.server.d.cts +15 -7
  46. package/dist/index.server.d.mts +15 -7
  47. package/dist/index.server.mjs +2 -2
  48. package/dist/plainTheme.cjs +49 -0
  49. package/dist/plainTheme.mjs +49 -0
  50. package/dist/serverContext.cjs +6 -24
  51. package/dist/serverContext.d.mts +0 -1
  52. package/dist/serverContext.mjs +6 -23
  53. package/dist/styleElements.cjs +75 -9
  54. package/dist/styleElements.d.cts +1 -0
  55. package/dist/styleElements.d.mts +2 -0
  56. package/dist/styleElements.mjs +73 -10
  57. package/dist/styled.cjs +32 -11
  58. package/dist/styled.mjs +33 -12
  59. package/dist/styled.server.cjs +6 -4
  60. package/dist/styled.server.mjs +6 -4
  61. package/dist/styledParts.cjs +36 -6
  62. package/dist/styledParts.mjs +37 -7
  63. package/dist/types.d.cts +225 -19
  64. package/dist/types.d.mts +225 -19
  65. package/dist/useMediaQuery.cjs +20 -0
  66. package/dist/useMediaQuery.d.cts +13 -0
  67. package/dist/useMediaQuery.d.mts +13 -0
  68. package/dist/useMediaQuery.mjs +20 -0
  69. package/package.json +3 -3
package/dist/types.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { ComponentProps, ElementType, FunctionComponent, ReactElement, ReactNode } from "react";
2
- import { AlphaColor, BindingOptions, Cache, ColorScheme, DeepPartial, Engine, NestedKey, ObjectSheet, SchemeKey, SchemeKey as SchemeKey$1, SchemeName, SchemeName as SchemeName$1, SheetEntry, SheetEntry as SheetEntry$1, SheetStyles, StyleInput, StyleObject, StylerOptions, Theme, ThemeKey, ThemeKey as ThemeKey$1, WebNested, WebProperties, WebStyleObject, WebStyleObject as WebStyleObject$1 } from "@umber-style/core";
1
+ import { ComponentProps, Context, ElementType, FunctionComponent, JSX, ReactElement, ReactNode } from "react";
2
+ import { AlphaColor, BindingOptions, CSSObject, Cache, ColorScheme, ComponentSelector, CssArgument, CssArgument as CssArgument$1, DeepPartial, Engine, GlobalInput, GlobalInput as GlobalInput$1, GlobalStyleObject, GlobalStyleObject as GlobalStyleObject$1, GlobalStyles, GlobalStyles as GlobalStyles$1, GlobalStylesInput, Interpolation as Interpolation$1, InterpolationResult, InterpolationResult as InterpolationResult$1, Keyframes, Keyframes as Keyframes$1, KeyframesInput, KeyframesOptions, KeyframesSteps, KeyframesSteps as KeyframesSteps$1, NestedKey, ObjectSheet, SchemeKey, SchemeKey as SchemeKey$1, SchemeName, SchemeName as SchemeName$1, SerializedStyles, SerializedStyles as SerializedStyles$1, SheetEntry, SheetEntry as SheetEntry$1, SheetStyles, StaticTemplateValue, StaticTemplateValue as StaticTemplateValue$1, StyleInput, StyleObject, StylerOptions, TemplateValue, TemplateValue as TemplateValue$1, Theme as Theme$1, ThemeInput, ThemeInput as ThemeInput$1, ThemeKey, ThemeKey as ThemeKey$1, UmberCache, WebNested, WebProperties, WebStyleObject, WebStyleObject as WebStyleObject$1 } from "@umber-style/core";
3
3
  /** What only the client build of the React binding takes: it can render the style of its classes on a server. */
4
4
  export interface ServerStyleOptions {
5
5
  /**
@@ -12,15 +12,59 @@ export interface ServerStyleOptions {
12
12
  */
13
13
  hasServerStyleElements?: boolean;
14
14
  }
15
+ /**
16
+ * The theme of the default style of this binding: what `styled`, `useTheme`, `StyleSheet` and
17
+ * `ThemeProvider` of the default style read. It extends the core's `Theme`, so an app gives it members
18
+ * from either module: `declare module '@umber-style/react' { interface Theme extends MyTheme {} }` for
19
+ * React alone, or `declare module '@umber-style/core'` for a theme that every binding shares. The
20
+ * optional member below only keeps it from being empty.
21
+ */
22
+ export interface Theme extends Theme$1 {
23
+ readonly _theme?: never;
24
+ }
25
+ /**
26
+ * A style in any form that `styled` takes: a style object, a `css()` result or a sheet entry, a function
27
+ * of the props and the theme, nothing, or a list of them. The core's `Interpolation`, with this binding's
28
+ * `Theme` unless another is given.
29
+ */
30
+ export type Interpolation<Props = unknown, T = Theme> = Interpolation$1<Props, T>;
31
+ /** The options of `createStyle(theme, options)`: those of the engine it makes, or the engine to use, and the server options. */
15
32
  export interface StyleOptions<T = never> extends BindingOptions<T>, ServerStyleOptions {}
33
+ /** The options of `configure()`: those of `StyleOptions` for the engine of the default style, and its base theme. */
16
34
  export interface DefaultStyleOptions extends BindingOptions<Theme>, ServerStyleOptions {
17
35
  /** The base theme, for an app that has no provider at all. */
18
36
  theme?: Theme;
19
37
  }
20
- export interface StyledOptions extends StylerOptions {
21
- /** Whether a prop is forwarded to a string tag. Default: every prop but `as`, `theme`, `className` and `style`, which the component handles itself. */
38
+ /**
39
+ * The options of `styled(tag, options)`: the label of the class, which props reach the element, and a class
40
+ * of your own. `_Props`, the props of the tag, changes nothing: it lets a wrapper of `styled` write
41
+ * `StyledOptions<ComponentProps<C>>` next to `FilteringStyledOptions<ComponentProps<C>, Forwarded>`.
42
+ */
43
+ export interface StyledOptions<_Props = unknown> extends StylerOptions {
44
+ /**
45
+ * Whether a prop is forwarded to a string tag. It replaces the default, which forwards the attributes
46
+ * of HTML and SVG (by their React names), `data-*`, `aria-*`, event handlers and React's own props, and
47
+ * every prop to a custom element (a tag with a hyphen). `as`, `theme`, `className` and `style` are the
48
+ * component's own and are never forwarded. A component tag receives every prop but those four.
49
+ */
22
50
  shouldForwardProp?: (prop: string) => boolean;
51
+ /**
52
+ * A class name of your own that the element always has, after the engine's classes: a stable name
53
+ * that a parent's selector or a test can find. It is not a style and has no rule. A component that
54
+ * wraps this one keeps it, and adds its own.
55
+ */
56
+ target?: string;
23
57
  }
58
+ /**
59
+ * `StyledOptions` whose `shouldForwardProp` is a type guard: the props of the tag that a styled component
60
+ * then takes are only the ones it names, `Forwarded`.
61
+ */
62
+ export interface FilteringStyledOptions<Props = Record<string, unknown>, Forwarded extends keyof Props & string = keyof Props & string> extends StyledOptions<Props> {
63
+ /** A type guard of the props that are forwarded to a string tag: the component takes only the props of the tag that it names. */
64
+ shouldForwardProp?: (prop: string) => prop is Forwarded;
65
+ }
66
+ /** The props of a component or a tag, as an element of it takes them. */
67
+ export type PropsOf<C extends ElementType> = JSX.LibraryManagedAttributes<C, ComponentProps<C>>;
24
68
  /** What a styled component keeps, so that styling it again flattens into one element. */
25
69
  export interface StyledStatics {
26
70
  /** Every style of the component, the styles of the components it wraps first. */
@@ -28,43 +72,147 @@ export interface StyledStatics {
28
72
  /** The tag or component that is finally rendered. */
29
73
  _tag: ElementType;
30
74
  _shouldForwardProp?: (prop: string) => boolean;
75
+ /** The `target` classes of the component and of the components it wraps. */
76
+ _target?: string;
77
+ /** The component as a selector: `.` and a class that every element of it has. Throws when it has none. */
78
+ toString(): string;
31
79
  }
32
80
  /**
33
81
  * What the `style` prop of a styled component takes on the web: sheet entries and plain style
34
82
  * objects, in arrays, with falsy values dropped. Nothing is rendered as a style attribute: all of it
35
83
  * is merged, in order, into the one class of the element, after its own styles and before
36
- * `className`. A value that changes often (an animated or measured number) makes a new class for
37
- * each value, so give such a value to a class as a CSS custom property, or drive it another way. A
38
- * string is an error.
84
+ * `className`. In the browser, a class made with a plain object is removed once no element uses it, so
85
+ * a value that changes often leaves no class behind; each new value still writes a rule. A string is
86
+ * an error.
39
87
  */
40
88
  export type StyleProp = SheetEntry$1 | WebStyleObject$1 | false | null | undefined | readonly StyleProp[];
41
- export type StyledComponent<C extends ElementType, P extends object> = FunctionComponent<Omit<ComponentProps<C>, keyof P | 'as' | 'style'> & P & {
89
+ /**
90
+ * A component made by `styled`: it takes the props of the tag (`Base`: all of them, unless a
91
+ * `shouldForwardProp` type guard named some), its own props `P`, `as` and `style`. As text it is a
92
+ * selector: `.` and a class that every element of it has (its `target`, else its class when none of its
93
+ * styles is a function).
94
+ */
95
+ export type StyledComponent<C extends ElementType, P extends object, Base extends object = ComponentProps<C>> = FunctionComponent<Omit<Base, keyof P | 'as' | 'style'> & P & {
42
96
  as?: ElementType;
43
97
  style?: StyleProp;
44
98
  }> & StyledStatics;
45
- export type Styled<T> = <C extends ElementType>(tag: C, options?: StyledOptions) => <P extends object = object>(...styles: StyleInput<T, P>[]) => StyledComponent<C, P>;
99
+ /** The props that a function style reads: the props of the tag `Base`, with the component's own `P` over them. */
100
+ type StyleFunctionProps<Base extends object, P extends object> = Omit<Base, keyof P> & P;
101
+ /** The props that the element of a styled component takes: those of the component, of the tag, and the ones only the element takes. */
102
+ type ElementProps<ComponentProps extends object, SpecificComponentProps extends object, JSXProps extends object> = ComponentProps & SpecificComponentProps & JSXProps;
103
+ /**
104
+ * What `styled(tag, options)` returns, in the form that a wrapper of `styled` names it: the props of the
105
+ * component are `ComponentProps` and `SpecificComponentProps` (a wrapper puts its own, such as `theme`, in
106
+ * the first, and the props of the tag in the second), and `JSXProps` are props that only the element takes
107
+ * (`ref`). It takes the styles and makes the component, or is used as a tag, whose template is one style.
108
+ * The props of the component's own are a type argument, `AdditionalProps`. A function style reads all of
109
+ * them, the component's own over the others, and `theme`, the theme `T`.
110
+ */
111
+ export interface CreateStyledComponent<ComponentProps extends object, SpecificComponentProps extends object = object, JSXProps extends object = object, T = Theme> {
112
+ <AdditionalProps extends object = object>(style: (props: StyleFunctionProps<ComponentProps & SpecificComponentProps, AdditionalProps> & {
113
+ theme: T;
114
+ }) => InterpolationResult$1, ...styles: Interpolation<StyleFunctionProps<ComponentProps & SpecificComponentProps, AdditionalProps>, T>[]): StyledComponent<ElementType, AdditionalProps, ElementProps<ComponentProps, SpecificComponentProps, JSXProps>>;
115
+ <AdditionalProps extends object = object>(template: TemplateStringsArray, ...values: TemplateValue$1<NoInfer<StyleFunctionProps<ComponentProps & SpecificComponentProps, AdditionalProps>>, T>[]): StyledComponent<ElementType, AdditionalProps, ElementProps<ComponentProps, SpecificComponentProps, JSXProps>>;
116
+ <AdditionalProps extends object = object>(...styles: Interpolation<StyleFunctionProps<ComponentProps & SpecificComponentProps, AdditionalProps>, T>[]): StyledComponent<ElementType, AdditionalProps, ElementProps<ComponentProps, SpecificComponentProps, JSXProps>>;
117
+ }
118
+ /**
119
+ * What `styled(tag, options)` returns for the theme `T` and the tag `C`, whose props the component takes
120
+ * (`Base`: all of them, unless a `shouldForwardProp` type guard named some): `CreateStyledComponent` with
121
+ * the props of the tag.
122
+ */
123
+ export type StyledFactory<T, C extends ElementType, Base extends object = ComponentProps<C>> = CreateStyledComponent<object, Base, object, T>;
124
+ /** The shorthand of each HTML and SVG tag: `styled.div(...styles)` is `styled('div')(...styles)`. */
125
+ export type StyledTags<T> = { readonly [K in keyof JSX.IntrinsicElements]: StyledFactory<T, K>; };
126
+ /** `styled(tag, options)`: with a `shouldForwardProp` type guard, the component takes only the props of `tag` that it names. */
127
+ export interface StyledCall<T> {
128
+ <C extends ElementType, Forwarded extends keyof ComponentProps<C> & string>(tag: C, options: FilteringStyledOptions<ComponentProps<C>, Forwarded> & {
129
+ shouldForwardProp: (prop: string) => prop is Forwarded;
130
+ }): StyledFactory<T, C, Pick<ComponentProps<C>, Forwarded>>;
131
+ <C extends ElementType>(tag: C, options?: StyledOptions): StyledFactory<T, C>;
132
+ }
133
+ /** `styled` of a theme `T`: a call with a tag and options, and the shorthand of each tag. */
134
+ export type Styled<T> = StyledCall<T> & StyledTags<T>;
135
+ /** The type of `styled` itself, with the tag shorthands: `Styled` of this binding's `Theme` unless another is given. */
136
+ export type CreateStyled<T = Theme> = Styled<T>;
137
+ /** `css()`: one style made once and passed around, from styles or from a tagged template. */
138
+ export interface CssFunction {
139
+ (template: TemplateStringsArray, ...values: StaticTemplateValue$1[]): SerializedStyles$1;
140
+ (...styles: CssArgument$1[]): SerializedStyles$1;
141
+ }
46
142
  /** A set of named styles; `E` is what one of them becomes: a class name on the web, a style object on React Native. */
47
143
  export interface SheetDefinition<S extends SheetStyles, E> {
48
144
  /** Never set at run time: it only carries the types. */
49
145
  readonly entries?: { [K in keyof S]: E; };
50
146
  }
147
+ /**
148
+ * What the `theme` of a provider of the theme `T` takes: `ThemeInput<T>`. The `Theme` of the default style
149
+ * has no member of its own until an app declares its members in it, and until then no theme of the app
150
+ * would match it (TypeScript rejects an object that shares no member with a type whose members are all
151
+ * optional); so while it has none, the provider takes any theme object, or a function of the theme above
152
+ * whose parameter the app types (`(outer: AppTheme) => AppTheme`).
153
+ */
154
+ type ProviderThemeInput<T> = [Exclude<keyof T, '_theme'>] extends [never] ? object | UntypedThemeFunction['derive'] : ThemeInput$1<T>;
155
+ /**
156
+ * The function form of `ProviderThemeInput` for a theme with no members: its parameter is compared both
157
+ * ways, as a method's is, so a function whose parameter the app types as its own theme is taken.
158
+ */
159
+ interface UntypedThemeFunction {
160
+ derive(outer: object): object;
161
+ }
162
+ /** The props of `ThemeProvider`: the theme of the subtree, and on the web its color scheme and wrapper. */
51
163
  export interface ThemeProviderProps<T> {
52
- /** The theme, or the part of it that this subtree changes. */
53
- readonly theme: DeepPartial<T>;
164
+ /**
165
+ * The theme, or the part of it that this subtree changes; or a function of the theme above (the base
166
+ * theme, or an empty object, for the outermost provider) that returns it. It is merged over the theme above.
167
+ */
168
+ readonly theme: ProviderThemeInput<T>;
54
169
  /** Web only. */
55
170
  readonly colorScheme?: ColorScheme;
56
- /** Web only. */
171
+ /** Web only: a class of the wrapper of a nested provider. */
57
172
  readonly className?: string;
58
- /** Web only. Renders no element and no class: the app puts `themeClass(theme)` on an ancestor, usually `<html>`. The theme still reaches the engine and the context. */
173
+ /**
174
+ * Web only. Renders no element, no class and no rule: the app puts `themeClass(theme)` on an ancestor,
175
+ * usually `<html>`. The theme still reaches the engine and the context. Without it the outermost provider
176
+ * declares its theme on `:root`, and renders no element either.
177
+ */
59
178
  readonly isRoot?: boolean;
179
+ /** The subtree that the theme applies to. */
180
+ readonly children?: ReactNode;
181
+ }
182
+ /** The props of `ThemeScope`. */
183
+ export interface ThemeScopeProps {
184
+ /** The content to wrap in the wrapper of the nearest nested provider: what a portal renders elsewhere. */
185
+ readonly children?: ReactNode;
186
+ }
187
+ /** The props of `CacheProvider`. */
188
+ export interface CacheProviderProps {
189
+ /** The cache of the subtree: one that `createCache` of `@umber-style/cache` (or of the core) made. */
190
+ readonly value: Cache;
191
+ /** The subtree whose rules go to `value`. */
60
192
  readonly children?: ReactNode;
61
193
  }
194
+ /** The props of `Global`. */
195
+ export interface GlobalProps<T> {
196
+ /**
197
+ * The global styles: a handle that `globalStyles` made, a style object, a function of the theme, or a
198
+ * list of them, written in the order of the list. A function of the theme reads the nearest provider:
199
+ * tokens are references, raw keys (`breakpoints`) are its real values.
200
+ */
201
+ readonly styles: GlobalInput$1<T>;
202
+ }
203
+ /** The props of `ThemeProvider` on the web: those of every platform, and the element of the wrapper. */
62
204
  export interface WebThemeProviderProps<T> extends ThemeProviderProps<T> {
63
- /** The element that carries the theme class. Default 'div'. */
205
+ /** The element of the wrapper of a nested provider. Default 'div'. */
64
206
  readonly as?: string;
65
207
  }
208
+ /**
209
+ * What every platform's styling has, with `E` the entry of a sheet (a class name on the web, a style
210
+ * object on React Native): the exports of the binding of the same names.
211
+ */
66
212
  export interface Style<T, E> {
213
+ /** Makes a styled component from a tag or a component and its styles. */
67
214
  styled: Styled<T>;
215
+ /** Makes sheets: named styles, from an object or from a function of the theme. */
68
216
  StyleSheet: {
69
217
  /** A sheet given as an object is its entries, usable at once, with no hook and no theme. */
70
218
  create<K extends string>(styles: ObjectSheet<K>): { readonly [P in K]: E; };
@@ -74,8 +222,13 @@ export interface Style<T, E> {
74
222
  /** The entries of a sheet; an object sheet is its own entries and is returned as it is. */
75
223
  useStyle<K extends string>(sheet: { readonly [P in K]: E; }): { readonly [P in K]: E; };
76
224
  useStyle<S extends SheetStyles>(sheet: SheetDefinition<S, E>): { [K in keyof S]: E; };
225
+ /** Gives the subtree below it its theme, merged over the theme above. */
77
226
  ThemeProvider(props: ThemeProviderProps<T>): ReactElement;
78
- /** The real theme values, for JavaScript that needs a number or a name. */
227
+ /**
228
+ * The real theme values, for JavaScript that needs a number or a name. In a client component: of the
229
+ * nearest provider, else the base theme, else an empty object. In a server component (the server
230
+ * components build): the base theme, the same in every render, else an empty object.
231
+ */
79
232
  useTheme(): T;
80
233
  /** The class that declares the variables of `theme`; '' on React Native. */
81
234
  themeClass(theme: DeepPartial<T>, colorScheme?: ColorScheme): string;
@@ -83,19 +236,72 @@ export interface Style<T, E> {
83
236
  when(theme: DeepPartial<T>, colorScheme?: ColorScheme): ThemeKey$1;
84
237
  /** A style key for "the nearest color scheme is this one": the nearest provider given that `colorScheme`, else the system preference. */
85
238
  whenScheme(scheme: SchemeName$1): SchemeKey$1;
239
+ /**
240
+ * Registers an animation and returns its name, `<label>-<hash>`, for `animation` or `animationName`.
241
+ * It writes nothing and needs no engine, so a module can call it before `configure()`: the
242
+ * `@keyframes` rule is written once, when a class that uses the name is first written. On React Native
243
+ * it only returns the name.
244
+ */
245
+ keyframes(steps: (theme: T) => KeyframesSteps$1, options?: KeyframesOptions): Keyframes$1;
246
+ /** As a tag: the steps written as CSS text (`from { ... } to { ... }`); a function among the interpolations is called with `{ theme }`. */
247
+ keyframes(template: TemplateStringsArray, ...values: TemplateValue$1<unknown, T>[]): Keyframes$1;
248
+ keyframes(steps: KeyframesInput<T>, options?: KeyframesOptions): Keyframes$1;
249
+ /** Makes global styles (selectors and at-rules as keys) and returns their handle, for `Global`. It writes nothing and needs no engine. */
250
+ globalStyles(styles: (theme: T) => GlobalStyleObject$1): GlobalStyles$1<T>;
251
+ /** As a tag: the styles written as CSS text (`body { margin: 0; }`); a function among the interpolations is called with `{ theme }`. */
252
+ globalStyles(template: TemplateStringsArray, ...values: TemplateValue$1<unknown, T>[]): GlobalStyles$1<T>;
253
+ globalStyles(styles: GlobalStylesInput<T>): GlobalStyles$1<T>;
254
+ /**
255
+ * Writes global styles into the `global` layer while it is rendered, and renders nothing of its own.
256
+ * In the browser the rules are removed when it unmounts, and replaced when its styles, or what they
257
+ * read of the theme, change. On a server they are written for good, and sent as style elements where
258
+ * the build sends them. Nothing on React Native.
259
+ */
260
+ Global(props: GlobalProps<T>): ReactElement | null;
86
261
  }
87
- /** The styling for server components: the root theme of the request is read with readTheme, not with a hook. */
88
- export interface ServerStyle<T> extends Omit<WebStyle<T>, 'useTheme'> {
89
- /** The root theme of the request, or the base theme of the engine; throws when there is neither. */
262
+ /**
263
+ * The styling for server components. `useTheme` is a plain function there, not a hook: it returns the base
264
+ * theme. There is no context, and so no `ThemeContext`, `ThemeScope` or `useThemeClass`.
265
+ */
266
+ export interface ServerStyle<T> extends Omit<WebStyle<T>, 'useTheme' | 'ThemeContext' | 'ThemeScope' | 'useThemeClass'> {
267
+ /**
268
+ * The base theme: the one given to `createStyle(theme)` or `configure({ theme })`, the same in every
269
+ * render, whatever providers render; never the theme of a provider. With none, an empty object, and a
270
+ * warning in development. A component that needs the nearest provider's values is a client component.
271
+ */
272
+ useTheme(): T;
273
+ /** Another name of `useTheme` in this build. */
90
274
  readTheme(): T;
91
275
  }
276
+ /** The styling of the client build of the web binding: what `createStyle` returns, and the default style. */
92
277
  export interface WebStyle<T> extends Omit<Style<T, SheetEntry$1>, 'ThemeProvider' | 'apply'> {
93
278
  /** The props that apply sheet entries (or class names) to a plain element: a class name. */
94
279
  apply(...entries: (SheetEntry$1 | string | null | undefined | false)[]): {
95
280
  className?: string;
96
281
  };
282
+ /**
283
+ * One style made once and passed around: style objects, `css()` results and sheet entries (lists too,
284
+ * falsy values dropped) merged in order, the last winning; or, as a tag, the template read as CSS text.
285
+ * It returns a sheet entry: its text is its class name, and it goes in the `style` prop of a styled
286
+ * component, in `apply`, in another `css()`, among the styles of `styled` and under a selector of
287
+ * global styles. Equal styles give the same class. A function is a `TypeError`: there are no props.
288
+ */
289
+ css: CssFunction;
290
+ /** Gives the subtree below it its theme: the outermost one declares it on `:root`, a nested one renders a wrapper with its theme class. */
97
291
  ThemeProvider(props: WebThemeProviderProps<T>): ReactElement;
292
+ /**
293
+ * Renders the wrapper of the nearest nested provider again (the same element and classes) around its
294
+ * children, for content that a portal puts elsewhere in the document; under the outermost provider, or
295
+ * none, its children alone.
296
+ */
297
+ ThemeScope(props: ThemeScopeProps): ReactElement;
298
+ /** The class string of the wrapper of the nearest nested provider, for an element of your own; undefined under the outermost provider, or none. */
299
+ useThemeClass(): string | undefined;
300
+ /** The context that holds the real theme of the nearest provider (merged over the ones above); undefined with no provider above. */
301
+ ThemeContext: Context<T | undefined>;
302
+ /** The engine that writes the rules of this style. */
98
303
  engine: Engine<T>;
304
+ /** The cache of the engine, which holds the rules. */
99
305
  cache: Cache;
100
306
  }
101
- export type { AlphaColor, NestedKey, SchemeKey, SchemeName, SheetEntry, StyleObject, ThemeKey, WebNested, WebProperties, WebStyleObject };
307
+ export type { AlphaColor, CSSObject, ComponentSelector, CssArgument, GlobalInput, GlobalStyleObject, GlobalStyles, InterpolationResult, Keyframes, KeyframesSteps, NestedKey, SchemeKey, SchemeName, SerializedStyles, SheetEntry, StaticTemplateValue, StyleObject, TemplateValue, ThemeInput, ThemeKey, UmberCache, WebNested, WebProperties, WebStyleObject };
@@ -0,0 +1,20 @@
1
+ "use client";
2
+ let react = require("react");
3
+ // src/useMediaQuery.ts
4
+ /**
5
+ * Whether the media query `query` (as `matchMedia` takes it: `(min-width: 600px)`) matches, for what a
6
+ * style cannot do: render one structure or another. The component renders again when the answer
7
+ * changes. On the server, and in the first render of a hydration, it is `defaultMatches` (false unless
8
+ * given), so that the HTML of the server and the hydration agree; the browser's answer follows at once.
9
+ * It uses a hook, so it is not in the server-components build.
10
+ */
11
+ function useMediaQuery(query, options = {}) {
12
+ const { defaultMatches = false } = options;
13
+ const subscribe = (0, react.useCallback)((onChange) => {
14
+ const list = window.matchMedia(query);
15
+ list.addEventListener("change", onChange);
16
+ return () => list.removeEventListener("change", onChange);
17
+ }, [query]);
18
+ return (0, react.useSyncExternalStore)(subscribe, () => window.matchMedia(query).matches, () => defaultMatches);
19
+ }
20
+ exports.useMediaQuery = useMediaQuery;
@@ -0,0 +1,13 @@
1
+ /** The options of `useMediaQuery`. */
2
+ export interface MediaQueryOptions {
3
+ /** The answer on the server and in the first render of a hydration, where there is no window. Default false. */
4
+ defaultMatches?: boolean;
5
+ }
6
+ /**
7
+ * Whether the media query `query` (as `matchMedia` takes it: `(min-width: 600px)`) matches, for what a
8
+ * style cannot do: render one structure or another. The component renders again when the answer
9
+ * changes. On the server, and in the first render of a hydration, it is `defaultMatches` (false unless
10
+ * given), so that the HTML of the server and the hydration agree; the browser's answer follows at once.
11
+ * It uses a hook, so it is not in the server-components build.
12
+ */
13
+ export declare function useMediaQuery(query: string, options?: MediaQueryOptions): boolean;
@@ -0,0 +1,13 @@
1
+ /** The options of `useMediaQuery`. */
2
+ export interface MediaQueryOptions {
3
+ /** The answer on the server and in the first render of a hydration, where there is no window. Default false. */
4
+ defaultMatches?: boolean;
5
+ }
6
+ /**
7
+ * Whether the media query `query` (as `matchMedia` takes it: `(min-width: 600px)`) matches, for what a
8
+ * style cannot do: render one structure or another. The component renders again when the answer
9
+ * changes. On the server, and in the first render of a hydration, it is `defaultMatches` (false unless
10
+ * given), so that the HTML of the server and the hydration agree; the browser's answer follows at once.
11
+ * It uses a hook, so it is not in the server-components build.
12
+ */
13
+ export declare function useMediaQuery(query: string, options?: MediaQueryOptions): boolean;
@@ -0,0 +1,20 @@
1
+ "use client";
2
+ import { useCallback, useSyncExternalStore } from "react";
3
+ // src/useMediaQuery.ts
4
+ /**
5
+ * Whether the media query `query` (as `matchMedia` takes it: `(min-width: 600px)`) matches, for what a
6
+ * style cannot do: render one structure or another. The component renders again when the answer
7
+ * changes. On the server, and in the first render of a hydration, it is `defaultMatches` (false unless
8
+ * given), so that the HTML of the server and the hydration agree; the browser's answer follows at once.
9
+ * It uses a hook, so it is not in the server-components build.
10
+ */
11
+ function useMediaQuery(query, options = {}) {
12
+ const { defaultMatches = false } = options;
13
+ const subscribe = useCallback((onChange) => {
14
+ const list = window.matchMedia(query);
15
+ list.addEventListener("change", onChange);
16
+ return () => list.removeEventListener("change", onChange);
17
+ }, [query]);
18
+ return useSyncExternalStore(subscribe, () => window.matchMedia(query).matches, () => defaultMatches);
19
+ }
20
+ export { useMediaQuery };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umber-style/react",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.2",
4
4
  "description": "UmberStyle for React: styled, ThemeProvider, useTheme, StyleSheet, useStyle and apply, with a server build for server components.",
5
5
  "keywords": [
6
6
  "styling",
@@ -54,12 +54,13 @@
54
54
  "access": "public"
55
55
  },
56
56
  "dependencies": {
57
- "@umber-style/core": "0.1.0-alpha.0"
57
+ "@umber-style/core": "0.1.0-alpha.5"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "react": "^19.2.8"
61
61
  },
62
62
  "devDependencies": {
63
+ "@soroush.tech/bench": "^2.2.3",
63
64
  "@emotion/is-prop-valid": "^1.4.0",
64
65
  "@soroush.tech/styled-system": "^6.0.1",
65
66
  "@testing-library/react": "^16.3.3",
@@ -76,7 +77,6 @@
76
77
  "tsdown": "^0.23.7",
77
78
  "typescript": "~7.0.2",
78
79
  "vitest": "^5.1.0",
79
- "@umber-bench/shared": "0.1.0",
80
80
  "@umber-style/eslint-config": "0.0.0"
81
81
  },
82
82
  "scripts": {