@proedis/react 2.1.0 → 2.2.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.
Files changed (53) hide show
  1. package/README.md +29 -1
  2. package/cjs/hooks/useAnnouncer.js +81 -0
  3. package/cjs/hooks/useAnnouncer.js.map +1 -0
  4. package/cjs/hooks/useClipboard.js +116 -0
  5. package/cjs/hooks/useClipboard.js.map +1 -0
  6. package/cjs/hooks/useDelayedBusy.js +80 -0
  7. package/cjs/hooks/useDelayedBusy.js.map +1 -0
  8. package/cjs/hooks/useForkRef.js +7 -2
  9. package/cjs/hooks/useForkRef.js.map +1 -1
  10. package/cjs/hooks/useMediaQuery.js +69 -0
  11. package/cjs/hooks/useMediaQuery.js.map +1 -0
  12. package/cjs/hooks/useOverflowItems.js +129 -0
  13. package/cjs/hooks/useOverflowItems.js.map +1 -0
  14. package/cjs/hooks/usePrefersReducedMotion.js +34 -0
  15. package/cjs/hooks/usePrefersReducedMotion.js.map +1 -0
  16. package/cjs/index.js +16 -0
  17. package/cjs/index.js.map +1 -1
  18. package/cjs/utils/contextBuilder.js +26 -0
  19. package/cjs/utils/contextBuilder.js.map +1 -1
  20. package/cjs/utils/inertValue.js +40 -0
  21. package/cjs/utils/inertValue.js.map +1 -0
  22. package/esm/hooks/useAnnouncer.js +60 -0
  23. package/esm/hooks/useAnnouncer.js.map +1 -0
  24. package/esm/hooks/useClipboard.js +95 -0
  25. package/esm/hooks/useClipboard.js.map +1 -0
  26. package/esm/hooks/useDelayedBusy.js +59 -0
  27. package/esm/hooks/useDelayedBusy.js.map +1 -0
  28. package/esm/hooks/useForkRef.js +7 -2
  29. package/esm/hooks/useForkRef.js.map +1 -1
  30. package/esm/hooks/useMediaQuery.js +48 -0
  31. package/esm/hooks/useMediaQuery.js.map +1 -0
  32. package/esm/hooks/useOverflowItems.js +107 -0
  33. package/esm/hooks/useOverflowItems.js.map +1 -0
  34. package/esm/hooks/usePrefersReducedMotion.js +32 -0
  35. package/esm/hooks/usePrefersReducedMotion.js.map +1 -0
  36. package/esm/index.js +8 -1
  37. package/esm/index.js.map +1 -1
  38. package/esm/utils/contextBuilder.js +26 -1
  39. package/esm/utils/contextBuilder.js.map +1 -1
  40. package/esm/utils/inertValue.js +19 -0
  41. package/esm/utils/inertValue.js.map +1 -0
  42. package/package.json +3 -3
  43. package/types/hooks/index.d.ts +6 -0
  44. package/types/hooks/useAnnouncer.d.ts +22 -0
  45. package/types/hooks/useClipboard.d.ts +28 -0
  46. package/types/hooks/useDelayedBusy.d.ts +28 -0
  47. package/types/hooks/useForkRef.d.ts +8 -2
  48. package/types/hooks/useMediaQuery.d.ts +25 -0
  49. package/types/hooks/useOverflowItems.d.ts +46 -0
  50. package/types/hooks/usePrefersReducedMotion.d.ts +18 -0
  51. package/types/utils/contextBuilder.d.ts +20 -0
  52. package/types/utils/index.d.ts +1 -0
  53. package/types/utils/inertValue.d.ts +9 -0
package/esm/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -25,6 +25,31 @@ function contextBuilder(name, initialContext) {
25
25
  BaseContext.displayName = name;
26
26
  return [BaseContext, useContextHook];
27
27
  }
28
+ /**
29
+ * The same pair for a context whose provider is **optional**: outside it the hook returns the default
30
+ * value instead of throwing.
31
+ *
32
+ * ```tsx
33
+ * const [ FieldContext, useFieldContext ] = optionalContextBuilder<FieldState>('Field', NO_FIELD);
34
+ *
35
+ * const field = useFieldContext(); // NO_FIELD outside a Field: the control works on its own
36
+ * ```
37
+ *
38
+ * `contextBuilder` is right when a missing provider is a mistake. This one is for the other case, a
39
+ * component that also works alone and only reads the context when something above provides it: a
40
+ * control inside or outside a field, a dialog nested or not. The value may be anything, a primitive
41
+ * included (`optionalContextBuilder('OverlayLevel', 0)`), which `contextBuilder` cannot take because
42
+ * it tells a missing provider apart by a falsy value.
43
+ *
44
+ * @param name The name shown by React DevTools
45
+ * @param defaultValue What the hook returns with no provider above
46
+ */
47
+ function optionalContextBuilder(name, defaultValue) {
48
+ const BaseContext = React.createContext(defaultValue);
49
+ const useContextHook = () => React.useContext(BaseContext);
50
+ BaseContext.displayName = name;
51
+ return [BaseContext, useContextHook];
52
+ }
28
53
 
29
- export { contextBuilder };
54
+ export { contextBuilder, optionalContextBuilder };
30
55
  //# sourceMappingURL=contextBuilder.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"contextBuilder.js","sources":["../../../src/utils/contextBuilder.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAWA;;;;;;AAMG;AACG,SAAU,cAAc,CAAsB,IAAY,EAAE,cAAkB,EAAA;;IAGlF,MAAM,WAAW,GAAG,KAAK,CAAC,aAAa,CAAgB,cAAc,CAAC;;IAGtE,MAAM,cAAc,GAAY,MAAK;;QAEnC,MAAM,YAAY,GAAG,KAAK,CAAC,UAAU,CAAC,WAAW,CAAC;;QAElD,IAAI,CAAC,YAAY,EAAE;AACjB,YAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,CAAA,yCAAA,CAA2C,CAAC;QAC3F;;AAEA,QAAA,OAAO,YAAY;AACrB,IAAA,CAAC;;AAGD,IAAA,WAAW,CAAC,WAAW,GAAG,IAAI;AAE9B,IAAA,OAAO,CAAE,WAAW,EAAE,cAAc,CAAE;AACxC;;;;"}
1
+ {"version":3,"file":"contextBuilder.js","sources":["../../../src/utils/contextBuilder.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAWA;;;;;;AAMG;AACG,SAAU,cAAc,CAAsB,IAAY,EAAE,cAAkB,EAAA;;IAGlF,MAAM,WAAW,GAAG,KAAK,CAAC,aAAa,CAAgB,cAAc,CAAC;;IAGtE,MAAM,cAAc,GAAY,MAAK;;QAEnC,MAAM,YAAY,GAAG,KAAK,CAAC,UAAU,CAAC,WAAW,CAAC;;QAElD,IAAI,CAAC,YAAY,EAAE;AACjB,YAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,CAAA,yCAAA,CAA2C,CAAC;QAC3F;;AAEA,QAAA,OAAO,YAAY;AACrB,IAAA,CAAC;;AAGD,IAAA,WAAW,CAAC,WAAW,GAAG,IAAI;AAE9B,IAAA,OAAO,CAAE,WAAW,EAAE,cAAc,CAAE;AACxC;AAGA;;;;;;;;;;;;;;;;;;AAkBG;AACG,SAAU,sBAAsB,CAAI,IAAY,EAAE,YAAe,EAAA;IACrE,MAAM,WAAW,GAAG,KAAK,CAAC,aAAa,CAAI,YAAY,CAAC;IAExD,MAAM,cAAc,GAAG,MAAS,KAAK,CAAC,UAAU,CAAC,WAAW,CAAC;AAE7D,IAAA,WAAW,CAAC,WAAW,GAAG,IAAI;AAE9B,IAAA,OAAO,CAAE,WAAW,EAAE,cAAc,CAAE;AACxC;;;;"}
@@ -0,0 +1,19 @@
1
+ import * as React from 'react';
2
+
3
+ /**
4
+ * The value of the `inert` attribute for the React running: a boolean on 19, an empty string on 18,
5
+ * which drops a boolean with a warning and leaves the element live.
6
+ *
7
+ * ```tsx
8
+ * <div inert={inertValue(wait.busy)}>…</div>
9
+ * ```
10
+ */
11
+ function inertValue(value) {
12
+ if (!value) {
13
+ return undefined;
14
+ }
15
+ return (Number.parseInt(React.version, 10) >= 19 ? true : '');
16
+ }
17
+
18
+ export { inertValue };
19
+ //# sourceMappingURL=inertValue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inertValue.js","sources":["../../../src/utils/inertValue.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAGA;;;;;;;AAOG;AACG,SAAU,UAAU,CAAC,KAA0B,EAAA;IACnD,IAAI,CAAC,KAAK,EAAE;AACV,QAAA,OAAO,SAAS;IAClB;IAEA,QAAQ,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,EAAE,GAAG,IAAI,GAAG,EAAE;AAC9D;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proedis/react",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "A set of React Hooks, that add UI or Core functionality to your Apps",
5
5
  "keywords": [
6
6
  "proedis",
@@ -10,8 +10,8 @@
10
10
  "license": "MIT",
11
11
  "main": "./cjs/index.js",
12
12
  "dependencies": {
13
- "@proedis/types": "^2.1.0",
14
- "@proedis/utils": "^2.1.0",
13
+ "@proedis/types": "^2.2.0",
14
+ "@proedis/utils": "^2.2.0",
15
15
  "clsx": "^2.1.1",
16
16
  "kind-of": "^6.0.3"
17
17
  },
@@ -1,15 +1,21 @@
1
+ export * from './useAnnouncer';
1
2
  export * from './useAutoControlledState';
2
3
  export * from './useAutoSizer';
3
4
  export * from './useClickOutside';
5
+ export * from './useClipboard';
4
6
  export * from './useDataManager';
5
7
  export * from './useDataSelector';
6
8
  export * from './useDebouncedCallback';
9
+ export * from './useDelayedBusy';
7
10
  export * from './useElementType';
8
11
  export * from './useEnhancedEffect';
9
12
  export * from './useEvent';
10
13
  export * from './useForkRef';
11
14
  export * from './useInputValue';
15
+ export * from './useMediaQuery';
12
16
  export * from './useObjectState';
17
+ export * from './useOverflowItems';
18
+ export * from './usePrefersReducedMotion';
13
19
  export * from './useSafeState';
14
20
  export * from './useSyncedRef';
15
21
  export * from './useUnmountEffect';
@@ -0,0 +1,22 @@
1
+ /** The text to render in a live region, and the function that announces a new one */
2
+ export type Announcer = readonly [message: string, announce: (text: string | null | undefined) => void];
3
+ /**
4
+ * The text of a live region, announced again even when it repeats.
5
+ *
6
+ * ```tsx
7
+ * const [ message, announce ] = useAnnouncer();
8
+ *
9
+ * <button onClick={() => { remove(file); announce(`${file.name} removed`); }}>Remove</button>
10
+ * <span role={'status'} className={'sr-only'}>{message}</span>
11
+ * ```
12
+ *
13
+ * A screen reader speaks a live region when its content changes, so setting the same text twice in
14
+ * a row says nothing the second time: two files with the same name removed one after the other, the
15
+ * same sort applied again. `announce` empties the region first and writes the text on the next task,
16
+ * which makes every call a change.
17
+ *
18
+ * A call replaces one still pending, so only the last text is spoken; an empty text is ignored, so
19
+ * an optional label can be passed as it is. The region itself is the caller's: the hook renders
20
+ * nothing, and the element, its role and its slot stay where the component decides.
21
+ */
22
+ export declare function useAnnouncer(): Announcer;
@@ -0,0 +1,28 @@
1
+ export interface ClipboardOptions {
2
+ /** How long `copied` or `failed` lasts before going back to `idle`. Defaults to 1500ms */
3
+ resetAfter?: number;
4
+ }
5
+ /** `copied` and `failed` last `resetAfter`, then the status is `idle` again */
6
+ export type ClipboardStatus = 'idle' | 'copied' | 'failed';
7
+ /** What to copy: the text, or a function that gives it, read only when the copy happens */
8
+ export type ClipboardValue = string | (() => string | Promise<string>);
9
+ export interface Clipboard {
10
+ readonly status: ClipboardStatus;
11
+ /** Copies, and tells whether it worked. Never throws: a refusal is `false` and the status `failed` */
12
+ copy: (value: ClipboardValue) => Promise<boolean>;
13
+ }
14
+ /**
15
+ * Copies a text to the clipboard and says how it went, for a button that turns into a tick for a moment.
16
+ *
17
+ * ```tsx
18
+ * const clipboard = useClipboard();
19
+ *
20
+ * <button onClick={() => clipboard.copy(apiKey)}>
21
+ * {clipboard.status === 'copied' ? 'Copied' : 'Copy'}
22
+ * </button>
23
+ * ```
24
+ *
25
+ * The value may be a function, read at the moment of the copy, and it may be asynchronous: a link
26
+ * created on demand. A second copy before the reset starts the time again.
27
+ */
28
+ export declare function useClipboard(options?: ClipboardOptions): Clipboard;
@@ -0,0 +1,28 @@
1
+ export interface DelayedBusyOptions {
2
+ /** How long a wait lasts before it shows. Defaults to 300ms: a fast answer flashes nothing */
3
+ delay?: number;
4
+ /** How long, once shown, the indicator stays at least. Defaults to 400ms: it never blinks */
5
+ minDuration?: number;
6
+ }
7
+ export interface DelayedBusy {
8
+ /** At once: block the clicks, set `aria-busy`, make a dialog `inert`. No double submit in the first 300ms */
9
+ busy: boolean;
10
+ /** Later: the look of the wait, a skeleton, a bar, a veil, a spinner */
11
+ visible: boolean;
12
+ }
13
+ /**
14
+ * The timing every wait shares, in two values: the block comes at once, the look after 300ms, and
15
+ * once shown it stays at least 400ms.
16
+ *
17
+ * ```tsx
18
+ * const wait = useDelayedBusy(query.isFetching);
19
+ *
20
+ * <div aria-busy={wait.busy}>
21
+ * {wait.visible && <LoadingBar />}
22
+ * </div>
23
+ * ```
24
+ *
25
+ * Data that arrives is shown at once and replaces a skeleton: the minimum holds only the indicator
26
+ * (a bar, a veil, a spinner), which is what `visible` drives.
27
+ */
28
+ export declare function useDelayedBusy(busy: boolean, options?: DelayedBusyOptions): DelayedBusy;
@@ -10,6 +10,12 @@ import * as React from 'react';
10
10
  interface MutableRefLike<T> {
11
11
  current: T | null | undefined;
12
12
  }
13
+ /**
14
+ * What a forked ref may be handed: a container, a callback, or nothing at all, which is what a
15
+ * forwarded ref is when the parent passed none. A callback ref is what `render` props and most
16
+ * libraries hand down, so a fork that only wrote `current` lost them silently.
17
+ */
18
+ type ForkableRef<T> = MutableRefLike<T> | ((instance: T | null) => void) | null | undefined;
13
19
  /**
14
20
  * Merge several refs into the single ref callback a JSX element can accept.
15
21
  *
@@ -26,10 +32,10 @@ interface MutableRefLike<T> {
26
32
  * @example
27
33
  * const Input = React.forwardRef<HTMLInputElement, Props>((props, ref) => {
28
34
  * const innerRef = React.useRef<HTMLInputElement>(null);
29
- * const handleRef = useForkRef(innerRef, ref as MutableRefLike<HTMLInputElement>);
35
+ * const handleRef = useForkRef(innerRef, ref);
30
36
  *
31
37
  * return <input {...props} ref={handleRef} />;
32
38
  * });
33
39
  */
34
- export declare function useForkRef<T>(...refs: MutableRefLike<T>[]): React.RefCallback<T>;
40
+ export declare function useForkRef<T>(...refs: ForkableRef<T>[]): React.RefCallback<T>;
35
41
  export {};
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Whether a media query matches, kept in sync with the browser.
3
+ *
4
+ * The primitive `usePrefersReducedMotion` is built on, here, and `useBreakpoint` / `useIsMobile` in
5
+ * `@proedis/ui`, which add the vocabulary of the Tailwind breakpoints on top. Exported because a named
6
+ * breakpoint is not the only question worth asking: `(prefers-reduced-motion: reduce)`,
7
+ * `(prefers-contrast: more)`, `print`, `(orientation: landscape)`, `(hover: hover)` and a width outside
8
+ * the theme's scale all end here.
9
+ *
10
+ * ```tsx
11
+ * const isLandscape = useMediaQuery('(orientation: landscape)');
12
+ * const canHover = useMediaQuery('(hover: hover)');
13
+ * ```
14
+ *
15
+ * `useSyncExternalStore` rather than an effect with a `setState`: it takes a server snapshot as an
16
+ * argument, which is what turns a hydration-safe read into one line instead of the mounted-guard
17
+ * dance. The same snapshot is used while **hydrating** what the server rendered, so it has to be the
18
+ * answer that is safe to be wrong about for one frame; a tree rendered on the client only reads the
19
+ * real value from its first render.
20
+ *
21
+ * @param query - Any CSS media query, as `window.matchMedia` accepts it.
22
+ * @param serverSnapshot - What to answer with no `window` and while hydrating. Defaults
23
+ * to `false`, i.e. "does not match".
24
+ */
25
+ export declare function useMediaQuery(query: string, serverSnapshot?: boolean): boolean;
@@ -0,0 +1,46 @@
1
+ export interface OverflowItemsOptions {
2
+ /** How many items: before the first measure they are all shown */
3
+ count: number;
4
+ /**
5
+ * The space between two items, in pixels. Leave it out to read it from the copy (its `column-gap`),
6
+ * which is what keeps the fit right when a product rescales `--spacing` or restyles the row
7
+ */
8
+ gap?: number;
9
+ /**
10
+ * The room the "More" control takes once something overflows, its gap and divider included. Leave it
11
+ * out to measure it: a child of the copy marked `data-overflow-reserve` is the control, not an item
12
+ */
13
+ reserve?: number;
14
+ /** An item that never goes into the overflow: the open tab. It takes the last visible place */
15
+ pinned?: number;
16
+ /** The width the items may use, read from the container. Defaults to its content width */
17
+ available?: (container: HTMLElement) => number;
18
+ }
19
+ export interface OverflowItems {
20
+ /** The indexes shown in the row, in order: the pinned one may come last, out of its place */
21
+ visible: readonly number[];
22
+ /** The indexes in the overflow, in their original order */
23
+ overflow: readonly number[];
24
+ }
25
+ /** Which items fit, as a pure function of the measures: no frame where the pinned item is missing */
26
+ export declare function fitItems(widths: readonly number[], available: number, options: {
27
+ gap: number;
28
+ reserve: number;
29
+ pinned?: number;
30
+ }): OverflowItems;
31
+ /**
32
+ * Which items of a row fit, and which go into a "More" menu: the tabs of a detail, the tabs of a
33
+ * header. The widths come from a hidden copy of the row (`measureRef`, `aria-hidden` and `inert`),
34
+ * one child per item, so measuring never moves what the user sees.
35
+ *
36
+ * ```tsx
37
+ * const { visible, overflow } = useOverflowItems(rowRef, copyRef, { count: tabs.length, pinned: active });
38
+ * ```
39
+ *
40
+ * It measures again when the row or the copy changes size, so a count that arrives late is taken in.
41
+ */
42
+ export declare function useOverflowItems(containerRef: {
43
+ readonly current: HTMLElement | null;
44
+ }, measureRef: {
45
+ readonly current: HTMLElement | null;
46
+ }, options: OverflowItemsOptions): OverflowItems;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Whether the user asked the system to reduce motion.
3
+ *
4
+ * For animation driven from JavaScript — a spring, a scroll interpolation, an autoplaying carousel.
5
+ * A transition written in CSS does **not** need this: `@media (prefers-reduced-motion: reduce)`
6
+ * answers the same question where the animation lives, without shipping a hook to the client.
7
+ *
8
+ * ```tsx
9
+ * const still = usePrefersReducedMotion();
10
+ * <motion.div animate={still ? {} : { y: 0 }} />
11
+ * ```
12
+ *
13
+ * ⚠️ It answers **`true`** with no `window` and on the first client render, unlike the other media
14
+ * query hooks, and the asymmetry is on purpose: the two possible mistakes are not equivalent. Guessing
15
+ * "reduce" and correcting to "animate" costs one frame of stillness; guessing the other way plays
16
+ * the animation this preference exists to prevent, to the person who asked for it not to.
17
+ */
18
+ export declare function usePrefersReducedMotion(): boolean;
@@ -9,4 +9,24 @@ type BuiltContext<C extends AnyObject> = [React.Context<C | undefined>, () => C]
9
9
  * @param initialContext
10
10
  */
11
11
  export declare function contextBuilder<C extends AnyObject>(name: string, initialContext?: C): BuiltContext<C>;
12
+ /**
13
+ * The same pair for a context whose provider is **optional**: outside it the hook returns the default
14
+ * value instead of throwing.
15
+ *
16
+ * ```tsx
17
+ * const [ FieldContext, useFieldContext ] = optionalContextBuilder<FieldState>('Field', NO_FIELD);
18
+ *
19
+ * const field = useFieldContext(); // NO_FIELD outside a Field: the control works on its own
20
+ * ```
21
+ *
22
+ * `contextBuilder` is right when a missing provider is a mistake. This one is for the other case, a
23
+ * component that also works alone and only reads the context when something above provides it: a
24
+ * control inside or outside a field, a dialog nested or not. The value may be anything, a primitive
25
+ * included (`optionalContextBuilder('OverlayLevel', 0)`), which `contextBuilder` cannot take because
26
+ * it tells a missing provider apart by a falsy value.
27
+ *
28
+ * @param name The name shown by React DevTools
29
+ * @param defaultValue What the hook returns with no provider above
30
+ */
31
+ export declare function optionalContextBuilder<C>(name: string, defaultValue: C): [React.Context<C>, () => C];
12
32
  export {};
@@ -1,3 +1,4 @@
1
1
  export * from './compose';
2
2
  export * from './contextBuilder';
3
+ export * from './inertValue';
3
4
  export * from './shorthands';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The value of the `inert` attribute for the React running: a boolean on 19, an empty string on 18,
3
+ * which drops a boolean with a warning and leaves the element live.
4
+ *
5
+ * ```tsx
6
+ * <div inert={inertValue(wait.busy)}>…</div>
7
+ * ```
8
+ */
9
+ export declare function inertValue(value: boolean | undefined): boolean | undefined;