@venizia/ardor-react 0.4.8-0 → 0.4.8-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 (85) hide show
  1. package/README.md +5 -4
  2. package/dist/contexts/application.d.ts +4 -0
  3. package/dist/contexts/application.js +1 -0
  4. package/dist/hooks/element-target.d.ts +9 -0
  5. package/dist/hooks/element-target.js +7 -0
  6. package/dist/hooks/index.d.ts +6 -1
  7. package/dist/hooks/index.js +5 -1
  8. package/dist/hooks/redux/use-app-dispatch.d.ts +5 -1
  9. package/dist/hooks/redux/use-app-dispatch.js +5 -1
  10. package/dist/hooks/redux/use-app-selector.js +27 -2
  11. package/dist/hooks/resize-observation.d.ts +19 -0
  12. package/dist/hooks/resize-observation.js +67 -0
  13. package/dist/hooks/use-application-context.d.ts +2 -0
  14. package/dist/hooks/use-application-context.js +2 -0
  15. package/dist/hooks/use-artifact.d.ts +26 -0
  16. package/dist/hooks/use-artifact.js +24 -2
  17. package/dist/hooks/use-autosave.d.ts +7 -0
  18. package/dist/hooks/use-autosave.js +4 -0
  19. package/dist/hooks/use-before-unload.d.ts +2 -0
  20. package/dist/hooks/use-before-unload.js +1 -0
  21. package/dist/hooks/use-confirm.d.ts +6 -0
  22. package/dist/hooks/use-confirm.js +1 -0
  23. package/dist/hooks/use-copy-to-clipboard.d.ts +21 -1
  24. package/dist/hooks/use-copy-to-clipboard.js +39 -15
  25. package/dist/hooks/use-debounce.d.ts +4 -0
  26. package/dist/hooks/use-debounce.js +1 -0
  27. package/dist/hooks/use-document-visibility.d.ts +3 -0
  28. package/dist/hooks/use-document-visibility.js +10 -0
  29. package/dist/hooks/use-element-size.d.ts +13 -1
  30. package/dist/hooks/use-element-size.js +11 -20
  31. package/dist/hooks/use-injectable.d.ts +32 -1
  32. package/dist/hooks/use-injectable.js +17 -3
  33. package/dist/hooks/use-intersection.d.ts +28 -0
  34. package/dist/hooks/use-intersection.js +50 -0
  35. package/dist/hooks/use-media-query.d.ts +5 -0
  36. package/dist/hooks/use-media-query.js +14 -0
  37. package/dist/hooks/use-online-status.d.ts +3 -0
  38. package/dist/hooks/use-online-status.js +14 -0
  39. package/dist/hooks/use-resize-effect.d.ts +31 -0
  40. package/dist/hooks/use-resize-effect.js +42 -0
  41. package/dist/hooks/use-stored-state.d.ts +28 -1
  42. package/dist/hooks/use-stored-state.js +38 -11
  43. package/dist/hooks/use-window-dimensions.d.ts +1 -0
  44. package/dist/hooks/use-window-dimensions.js +1 -0
  45. package/package.json +16 -16
  46. package/dist/contexts/application.d.ts.map +0 -1
  47. package/dist/contexts/application.js.map +0 -1
  48. package/dist/contexts/index.d.ts.map +0 -1
  49. package/dist/contexts/index.js.map +0 -1
  50. package/dist/hooks/index.d.ts.map +0 -1
  51. package/dist/hooks/index.js.map +0 -1
  52. package/dist/hooks/redux/index.d.ts.map +0 -1
  53. package/dist/hooks/redux/index.js.map +0 -1
  54. package/dist/hooks/redux/use-app-dispatch.d.ts.map +0 -1
  55. package/dist/hooks/redux/use-app-dispatch.js.map +0 -1
  56. package/dist/hooks/redux/use-app-selector.d.ts.map +0 -1
  57. package/dist/hooks/redux/use-app-selector.js.map +0 -1
  58. package/dist/hooks/use-application-context.d.ts.map +0 -1
  59. package/dist/hooks/use-application-context.js.map +0 -1
  60. package/dist/hooks/use-artifact.d.ts.map +0 -1
  61. package/dist/hooks/use-artifact.js.map +0 -1
  62. package/dist/hooks/use-autosave.d.ts.map +0 -1
  63. package/dist/hooks/use-autosave.js.map +0 -1
  64. package/dist/hooks/use-before-unload.d.ts.map +0 -1
  65. package/dist/hooks/use-before-unload.js.map +0 -1
  66. package/dist/hooks/use-confirm.d.ts.map +0 -1
  67. package/dist/hooks/use-confirm.js.map +0 -1
  68. package/dist/hooks/use-copy-to-clipboard.d.ts.map +0 -1
  69. package/dist/hooks/use-copy-to-clipboard.js.map +0 -1
  70. package/dist/hooks/use-debounce.d.ts.map +0 -1
  71. package/dist/hooks/use-debounce.js.map +0 -1
  72. package/dist/hooks/use-element-size.d.ts.map +0 -1
  73. package/dist/hooks/use-element-size.js.map +0 -1
  74. package/dist/hooks/use-injectable.d.ts.map +0 -1
  75. package/dist/hooks/use-injectable.js.map +0 -1
  76. package/dist/hooks/use-sizer.d.ts +0 -10
  77. package/dist/hooks/use-sizer.d.ts.map +0 -1
  78. package/dist/hooks/use-sizer.js +0 -23
  79. package/dist/hooks/use-sizer.js.map +0 -1
  80. package/dist/hooks/use-stored-state.d.ts.map +0 -1
  81. package/dist/hooks/use-stored-state.js.map +0 -1
  82. package/dist/hooks/use-window-dimensions.d.ts.map +0 -1
  83. package/dist/hooks/use-window-dimensions.js.map +0 -1
  84. package/dist/index.d.ts.map +0 -1
  85. package/dist/index.js.map +0 -1
package/README.md CHANGED
@@ -15,15 +15,14 @@ which re-exports this package.
15
15
  | Area | Exports |
16
16
  |---|---|
17
17
  | Context | `ApplicationContext`, `useApplicationContext`, `useApplicationLogger` |
18
- | Injection | `useInjectable` - resolve a binding by key or by class |
18
+ | Injection | `useInjectable` - resolve a binding by key or by class; the artifact hooks `useApplicationContainer`, `useService`, `useProvider`, `useComponent`, `useConfiguration`, `useRepository` |
19
19
  | Redux | `DispatchHooks.create`, `SelectorHooks.create` - factories that bind your `RootState` and `AppDispatch` (the old `createAppDispatch` and `createAppSelectors` are removed; `bun run ardor codemod namespace` moves your code) |
20
- | UI hooks | `useDebounce`, `useAutosave`, `useConfirm`, `useBeforeUnload`, `useCopyToClipboard`, `useSizer`, `useWindowDimensions` |
20
+ | UI hooks | `useDebounce`, `useAutosave`, `useConfirm`, `useBeforeUnload`, `useCopyToClipboard`, `useWindowDimensions`, `useElementSize`, `useResizeEffect`, `useIntersection`, `useStoredState`, `useMediaQuery`, `useDocumentVisibility`, `useOnlineStatus` |
21
21
  | Types | `TUseInjectableKeys` |
22
22
 
23
23
  ## Typing your own binding keys
24
24
 
25
- `IUseInjectableKeysOverrides` is declared here, so augment this package - an interface merges only
26
- into the module that declares it, never through a re-export:
25
+ `IUseInjectableKeysOverrides` is declared here, so augment this package when it is the one you depend on:
27
26
 
28
27
  ```typescript
29
28
  declare module '@venizia/ardor-react' {
@@ -33,6 +32,8 @@ declare module '@venizia/ardor-react' {
33
32
  }
34
33
  ```
35
34
 
35
+ With `@venizia/ardor`, write `declare module '@venizia/ardor'` instead.
36
+
36
37
  ## License
37
38
 
38
39
  MIT - see [LICENSE.md](./LICENSE.md).
@@ -1,9 +1,13 @@
1
1
  import React from 'react';
2
2
  import type { Container } from '@venizia/ignis-inversion';
3
3
  import type { ILogger } from '@venizia/ignis-helpers/core';
4
+ /** What `ApplicationContext` carries; both are `null` outside a provider. */
4
5
  export interface IApplicationContextValue {
6
+ /** The application's container. */
5
7
  container: Container | null;
8
+ /** The application's logger. */
6
9
  logger: ILogger | null;
7
10
  }
11
+ /** Read through `useApplicationContext` and `useApplicationLogger`; the default value holds `null`s. */
8
12
  export declare const ApplicationContext: React.Context<IApplicationContextValue>;
9
13
  //# sourceMappingURL=application.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ /** Read through `useApplicationContext` and `useApplicationLogger`; the default value holds `null`s. */
2
3
  export const ApplicationContext = React.createContext({
3
4
  container: null,
4
5
  logger: null,
@@ -0,0 +1,9 @@
1
+ import type React from 'react';
2
+ /** An element, a ref to one, or `null` for none yet. */
3
+ export type TElementTarget = React.RefObject<Element | null> | Element | null;
4
+ export declare class ElementTargets {
5
+ static resolve(opts: {
6
+ target: TElementTarget;
7
+ }): Element | null;
8
+ }
9
+ //# sourceMappingURL=element-target.d.ts.map
@@ -0,0 +1,7 @@
1
+ export class ElementTargets {
2
+ static resolve(opts) {
3
+ const { target } = opts;
4
+ return target && 'current' in target ? target.current : target;
5
+ }
6
+ }
7
+ //# sourceMappingURL=element-target.js.map
@@ -1,3 +1,4 @@
1
+ export type { TElementTarget } from './element-target.js';
1
2
  export * from './redux/index.js';
2
3
  export * from './use-application-context.js';
3
4
  export * from './use-artifact.js';
@@ -6,9 +7,13 @@ export * from './use-before-unload.js';
6
7
  export * from './use-confirm.js';
7
8
  export * from './use-copy-to-clipboard.js';
8
9
  export * from './use-debounce.js';
10
+ export * from './use-document-visibility.js';
9
11
  export * from './use-element-size.js';
10
12
  export * from './use-injectable.js';
11
- export * from './use-sizer.js';
13
+ export * from './use-intersection.js';
14
+ export * from './use-media-query.js';
15
+ export * from './use-online-status.js';
16
+ export * from './use-resize-effect.js';
12
17
  export * from './use-stored-state.js';
13
18
  export * from './use-window-dimensions.js';
14
19
  //# sourceMappingURL=index.d.ts.map
@@ -6,9 +6,13 @@ export * from './use-before-unload.js';
6
6
  export * from './use-confirm.js';
7
7
  export * from './use-copy-to-clipboard.js';
8
8
  export * from './use-debounce.js';
9
+ export * from './use-document-visibility.js';
9
10
  export * from './use-element-size.js';
10
11
  export * from './use-injectable.js';
11
- export * from './use-sizer.js';
12
+ export * from './use-intersection.js';
13
+ export * from './use-media-query.js';
14
+ export * from './use-online-status.js';
15
+ export * from './use-resize-effect.js';
12
16
  export * from './use-stored-state.js';
13
17
  export * from './use-window-dimensions.js';
14
18
  //# sourceMappingURL=index.js.map
@@ -1,6 +1,10 @@
1
1
  import type { Dispatch, UnknownAction } from '@reduxjs/toolkit';
2
+ /** Builds the typed dispatch hooks of a store. */
2
3
  export declare class DispatchHooks {
3
- /** The typed dispatch hooks of a store: bind your `AppDispatch` once, and export what comes back. */
4
+ /**
5
+ * The typed dispatch hooks of a store: bind your `AppDispatch` once, and export what comes back.
6
+ * `useMultipleAppDispatch` returns a function that dispatches each action in order and skips falsy ones.
7
+ */
4
8
  static create<TDispatch extends Dispatch<UnknownAction> = Dispatch<UnknownAction>>(): {
5
9
  useAppDispatch: import("react-redux").UseDispatch<TDispatch>;
6
10
  useMultipleAppDispatch: () => (...actions: Parameters<TDispatch>[0][]) => void;
@@ -1,7 +1,11 @@
1
1
  import React from 'react';
2
2
  import { useDispatch } from 'react-redux';
3
+ /** Builds the typed dispatch hooks of a store. */
3
4
  export class DispatchHooks {
4
- /** The typed dispatch hooks of a store: bind your `AppDispatch` once, and export what comes back. */
5
+ /**
6
+ * The typed dispatch hooks of a store: bind your `AppDispatch` once, and export what comes back.
7
+ * `useMultipleAppDispatch` returns a function that dispatches each action in order and skips falsy ones.
8
+ */
5
9
  static create() {
6
10
  const useAppDispatch = useDispatch.withTypes();
7
11
  const useMultipleAppDispatch = () => {
@@ -1,5 +1,30 @@
1
- import isEqual from 'lodash/isEqual.js';
2
1
  import { shallowEqual, useSelector } from 'react-redux';
2
+ const isPlain = (value) => {
3
+ return (typeof value === 'object' &&
4
+ value !== null &&
5
+ (Array.isArray(value) || [Object.prototype, null].includes(Object.getPrototypeOf(value))));
6
+ };
7
+ /**
8
+ * Equality by value for what a selector returns: primitives, plain objects, arrays and dates, none
9
+ * of them cyclic; `0` equals `-0`, and `NaN` equals `NaN`. Any other object (a `Map`, a class
10
+ * instance) is equal only to itself.
11
+ */
12
+ const deepEqual = (a, b) => {
13
+ if (a === b || Object.is(a, b)) {
14
+ return true;
15
+ }
16
+ if (a instanceof Date && b instanceof Date) {
17
+ return Object.is(a.getTime(), b.getTime());
18
+ }
19
+ if (!isPlain(a) || !isPlain(b) || Array.isArray(a) !== Array.isArray(b)) {
20
+ return false;
21
+ }
22
+ const keys = Object.keys(a);
23
+ return (keys.length === Object.keys(b).length &&
24
+ keys.every((key) => {
25
+ return Object.hasOwn(b, key) && deepEqual(Reflect.get(a, key), Reflect.get(b, key));
26
+ }));
27
+ };
3
28
  export class SelectorHooks {
4
29
  /** The three typed selector hooks of a store: bind your `RootState` once, and export what comes back. */
5
30
  static create() {
@@ -8,7 +33,7 @@ export class SelectorHooks {
8
33
  return useAppSelector(selector, shallowEqual);
9
34
  };
10
35
  const useDeepEqualSelector = (selector) => {
11
- return useAppSelector(selector, isEqual);
36
+ return useAppSelector(selector, deepEqual);
12
37
  };
13
38
  return {
14
39
  useAppSelector,
@@ -0,0 +1,19 @@
1
+ /** The boxes `useElementSize` and `useResizeEffect` read. */
2
+ export type TResizeBox = Exclude<ResizeObserverBoxOptions, 'device-pixel-content-box'>;
3
+ export interface IResizeObservationOptions {
4
+ box: TResizeBox;
5
+ /** Also observe the direct children of each target, and again whenever they change. @default false */
6
+ children?: boolean;
7
+ /** Called at most once per animation frame, with the latest entry of each element that resized. */
8
+ onFrame: (entries: ResizeObserverEntry[]) => void;
9
+ }
10
+ export interface IResizeObservation {
11
+ /** Observes these elements and stops observing the ones no longer listed. */
12
+ sync: (targets: Element[]) => void;
13
+ stop: () => void;
14
+ }
15
+ export declare class ResizeObservation {
16
+ /** Does nothing without `ResizeObserver`. */
17
+ static observe(opts: IResizeObservationOptions): IResizeObservation;
18
+ }
19
+ //# sourceMappingURL=resize-observation.d.ts.map
@@ -0,0 +1,67 @@
1
+ export class ResizeObservation {
2
+ /** Does nothing without `ResizeObserver`. */
3
+ static observe(opts) {
4
+ const { box, children = false, onFrame } = opts;
5
+ if (typeof ResizeObserver === 'undefined') {
6
+ return { sync: () => { }, stop: () => { } };
7
+ }
8
+ const latest = new Map();
9
+ let watched = new Set();
10
+ let current = [];
11
+ let frame;
12
+ const observer = new ResizeObserver((entries) => {
13
+ for (const entry of entries) {
14
+ latest.set(entry.target, entry);
15
+ }
16
+ if (frame !== undefined) {
17
+ return;
18
+ }
19
+ frame = requestAnimationFrame(() => {
20
+ frame = undefined;
21
+ const batch = [...latest.values()];
22
+ latest.clear();
23
+ if (batch.length > 0) {
24
+ onFrame(batch);
25
+ }
26
+ });
27
+ });
28
+ const update = (targets) => {
29
+ const wanted = new Set(children ? targets.flatMap((target) => [target, ...target.children]) : targets);
30
+ for (const element of watched) {
31
+ if (!wanted.has(element)) {
32
+ observer.unobserve(element);
33
+ latest.delete(element);
34
+ }
35
+ }
36
+ for (const element of wanted) {
37
+ if (!watched.has(element)) {
38
+ observer.observe(element, { box });
39
+ }
40
+ }
41
+ watched = wanted;
42
+ };
43
+ const mutations = children ? new MutationObserver(() => update(current)) : undefined;
44
+ return {
45
+ sync: (targets) => {
46
+ if (targets.length === current.length &&
47
+ targets.every((target, index) => target === current[index])) {
48
+ return;
49
+ }
50
+ current = targets;
51
+ update(targets);
52
+ // A removed target stays registered on `mutations` until `stop`; its callback only updates the current targets.
53
+ for (const target of targets) {
54
+ mutations?.observe(target, { childList: true });
55
+ }
56
+ },
57
+ stop: () => {
58
+ mutations?.disconnect();
59
+ observer.disconnect();
60
+ if (frame !== undefined) {
61
+ cancelAnimationFrame(frame);
62
+ }
63
+ },
64
+ };
65
+ }
66
+ }
67
+ //# sourceMappingURL=resize-observation.js.map
@@ -1,3 +1,5 @@
1
+ /** The application's `Container`. Throws when there is no `ApplicationContext` provider above. */
1
2
  export declare const useApplicationContext: () => import("@venizia/ignis-inversion").Container;
3
+ /** The application's logger. Throws when there is no `ApplicationContext` provider above. */
2
4
  export declare const useApplicationLogger: () => import("@venizia/ignis-helpers/core").ILogger;
3
5
  //# sourceMappingURL=use-application-context.d.ts.map
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  import { getError } from '@venizia/ignis-inversion';
3
3
  import { ApplicationContext } from '../contexts/application.js';
4
+ /** The application's `Container`. Throws when there is no `ApplicationContext` provider above. */
4
5
  export const useApplicationContext = () => {
5
6
  const context = React.useContext(ApplicationContext);
6
7
  if (!context?.container) {
@@ -10,6 +11,7 @@ export const useApplicationContext = () => {
10
11
  }
11
12
  return context.container;
12
13
  };
14
+ /** The application's logger. Throws when there is no `ApplicationContext` provider above. */
13
15
  export const useApplicationLogger = () => {
14
16
  const context = React.useContext(ApplicationContext);
15
17
  if (!context?.logger) {
@@ -2,19 +2,45 @@ import { type Container, type TClass } from '@venizia/ignis-inversion';
2
2
  import { type TUseInjectableKeys } from './use-injectable.js';
3
3
  /** By class: the return type is inferred from the target. */
4
4
  export interface IUseArtifactByTarget<TInstance> {
5
+ /** The container to resolve from. @default The application's container (`ApplicationContext`) */
5
6
  container?: Container;
7
+ /** The class to resolve; its recorded binding key is used. */
6
8
  target: TClass<TInstance>;
7
9
  key?: never;
8
10
  }
9
11
  /** By key: the caller supplies the type. */
10
12
  export interface IUseArtifactByKey {
13
+ /** The container to resolve from. @default The application's container (`ApplicationContext`) */
11
14
  container?: Container;
15
+ /** The binding key to resolve. */
12
16
  key: TUseInjectableKeys;
13
17
  target?: never;
14
18
  }
19
+ /** Resolves the instance bound under the `service` namespace by `target` or `key`. A `target` bound under another namespace throws. */
15
20
  export declare const useService: <TInstance = unknown>(opts: IUseArtifactByTarget<TInstance> | IUseArtifactByKey) => TInstance;
21
+ /** Resolves the instance bound under the `provider` namespace by `target` or `key`. A `target` bound under another namespace throws. */
16
22
  export declare const useProvider: <TInstance = unknown>(opts: IUseArtifactByTarget<TInstance> | IUseArtifactByKey) => TInstance;
23
+ /** Resolves the instance bound under the `component` namespace by `target` or `key`. A `target` bound under another namespace throws. */
17
24
  export declare const useComponent: <TInstance = unknown>(opts: IUseArtifactByTarget<TInstance> | IUseArtifactByKey) => TInstance;
25
+ /** Resolves the instance bound under the `configuration` namespace by `target` or `key`. A `target` bound under another namespace throws. */
18
26
  export declare const useConfiguration: <TInstance = unknown>(opts: IUseArtifactByTarget<TInstance> | IUseArtifactByKey) => TInstance;
27
+ /**
28
+ * Resolves the instance bound under the `repository` namespace by `target` or `key`. A `target` bound under another namespace throws.
29
+ *
30
+ * @example
31
+ * ```tsx
32
+ * import { repository, useRepository } from '@venizia/ardor';
33
+ *
34
+ * @repository()
35
+ * class ProductRepository {
36
+ * findStock(productId: string) { return productId ? 42 : 0; }
37
+ * }
38
+ *
39
+ * export function StockDisplay() {
40
+ * const productRepository = useRepository({ target: ProductRepository });
41
+ * return <span>Stock: {productRepository.findStock('p1')}</span>;
42
+ * }
43
+ * ```
44
+ */
19
45
  export declare const useRepository: <TInstance = unknown>(opts: IUseArtifactByTarget<TInstance> | IUseArtifactByKey) => TInstance;
20
46
  //# sourceMappingURL=use-artifact.d.ts.map
@@ -1,6 +1,6 @@
1
1
  import { BindingNamespaces } from '@venizia/ardor-kernel';
2
2
  import { getError } from '@venizia/ignis-inversion';
3
- import { useInjectable, useInjectableContainer, } from './use-injectable.js';
3
+ import { useInjectable, useApplicationContainer, } from './use-injectable.js';
4
4
  /**
5
5
  * `useService`, `useRepository`, `useProvider`, `useComponent`, `useConfiguration`. Unlike
6
6
  * `useInjectable`, a `{ target }` must be bound under the hook's namespace, or it throws.
@@ -8,7 +8,7 @@ import { useInjectable, useInjectableContainer, } from './use-injectable.js';
8
8
  const useArtifactInNamespace = (opts) => {
9
9
  const { options, namespace, hook } = opts;
10
10
  // Same inputs as `useInjectable`, so the check and the resolve read one container.
11
- const container = useInjectableContainer({ container: options.container });
11
+ const container = useApplicationContainer({ container: options.container });
12
12
  // A `key` carries its namespace in the string; only a `target` is checked.
13
13
  if (options.target) {
14
14
  const key = container.getMetadataRegistry().getBindingKey({ target: options.target });
@@ -22,26 +22,48 @@ const useArtifactInNamespace = (opts) => {
22
22
  }
23
23
  return useInjectable(options);
24
24
  };
25
+ /** Resolves the instance bound under the `service` namespace by `target` or `key`. A `target` bound under another namespace throws. */
25
26
  export const useService = (opts) => useArtifactInNamespace({
26
27
  options: opts,
27
28
  namespace: BindingNamespaces.SERVICE,
28
29
  hook: 'useService',
29
30
  });
31
+ /** Resolves the instance bound under the `provider` namespace by `target` or `key`. A `target` bound under another namespace throws. */
30
32
  export const useProvider = (opts) => useArtifactInNamespace({
31
33
  options: opts,
32
34
  namespace: BindingNamespaces.PROVIDER,
33
35
  hook: 'useProvider',
34
36
  });
37
+ /** Resolves the instance bound under the `component` namespace by `target` or `key`. A `target` bound under another namespace throws. */
35
38
  export const useComponent = (opts) => useArtifactInNamespace({
36
39
  options: opts,
37
40
  namespace: BindingNamespaces.COMPONENT,
38
41
  hook: 'useComponent',
39
42
  });
43
+ /** Resolves the instance bound under the `configuration` namespace by `target` or `key`. A `target` bound under another namespace throws. */
40
44
  export const useConfiguration = (opts) => useArtifactInNamespace({
41
45
  options: opts,
42
46
  namespace: BindingNamespaces.CONFIGURATION,
43
47
  hook: 'useConfiguration',
44
48
  });
49
+ /**
50
+ * Resolves the instance bound under the `repository` namespace by `target` or `key`. A `target` bound under another namespace throws.
51
+ *
52
+ * @example
53
+ * ```tsx
54
+ * import { repository, useRepository } from '@venizia/ardor';
55
+ *
56
+ * @repository()
57
+ * class ProductRepository {
58
+ * findStock(productId: string) { return productId ? 42 : 0; }
59
+ * }
60
+ *
61
+ * export function StockDisplay() {
62
+ * const productRepository = useRepository({ target: ProductRepository });
63
+ * return <span>Stock: {productRepository.findStock('p1')}</span>;
64
+ * }
65
+ * ```
66
+ */
45
67
  export const useRepository = (opts) => useArtifactInNamespace({
46
68
  options: opts,
47
69
  namespace: BindingNamespaces.REPOSITORY,
@@ -1,11 +1,18 @@
1
1
  export interface IUseAutosaveOptions<TData, TReturn> {
2
+ /** The value to save; a change starts the `interval` wait. */
2
3
  data: TData;
4
+ /** Called with the latest `data` once it has been still for `interval`. A rejection is logged, not thrown. */
3
5
  onSave: (data: TData) => Promise<TReturn> | TReturn | void;
4
6
  /** @default 2000 */
5
7
  interval?: number;
8
+ /** Save the latest `data` on unmount. */
6
9
  enableSaveOnUnmount?: boolean;
7
10
  /** @default true */
8
11
  enabled?: boolean;
9
12
  }
13
+ /**
14
+ * Calls `onSave` with `data` after it has stopped changing for `interval` milliseconds. The first
15
+ * render does not save. The latest `onSave` is always the one called.
16
+ */
10
17
  export declare const useAutosave: <TData, TReturn>(opts: IUseAutosaveOptions<TData, TReturn>) => void;
11
18
  //# sourceMappingURL=use-autosave.d.ts.map
@@ -2,6 +2,10 @@ import React from 'react';
2
2
  import { LoggerFactory } from '@venizia/ignis-helpers/core';
3
3
  import { useDebounce } from './use-debounce.js';
4
4
  const logger = LoggerFactory.getLogger(['useAutosave']);
5
+ /**
6
+ * Calls `onSave` with `data` after it has stopped changing for `interval` milliseconds. The first
7
+ * render does not save. The latest `onSave` is always the one called.
8
+ */
5
9
  export const useAutosave = (opts) => {
6
10
  const { data, onSave, interval = 2000, enableSaveOnUnmount, enabled = true } = opts;
7
11
  const valueOnCleanup = React.useRef(data);
@@ -1,7 +1,9 @@
1
1
  export interface IUseBeforeUnloadOptions {
2
2
  /** @default true */
3
3
  enabled?: boolean;
4
+ /** Set as the `returnValue` of the event; browsers may show their own text instead. */
4
5
  message?: string;
5
6
  }
7
+ /** While `enabled`, asks the browser to confirm before the page is left or reloaded. */
6
8
  export declare const useBeforeUnload: (opts: IUseBeforeUnloadOptions) => void;
7
9
  //# sourceMappingURL=use-before-unload.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ /** While `enabled`, asks the browser to confirm before the page is left or reloaded. */
2
3
  export const useBeforeUnload = (opts) => {
3
4
  const { enabled = true, message } = opts;
4
5
  const handleBeforeUnload = React.useCallback((event) => {
@@ -1,11 +1,17 @@
1
1
  export interface IUseConfirmReturn {
2
+ /** The pending prompt text; `undefined` when none is open. */
2
3
  message?: string;
4
+ /** Opens a prompt. Resolves `true` on `handleConfirm`, `false` on `handleAbort` or when another `confirm` replaces it. */
3
5
  confirm: (opts: {
4
6
  message: string;
5
7
  }) => Promise<boolean>;
8
+ /** Closes the prompt without settling it. */
6
9
  handleClose: () => void;
10
+ /** Resolves the pending `confirm` with `true` and closes. */
7
11
  handleConfirm: () => void;
12
+ /** Resolves the pending `confirm` with `false` and closes. */
8
13
  handleAbort: () => void;
9
14
  }
15
+ /** State for a confirmation prompt: `confirm` returns a promise that the prompt's buttons settle. */
10
16
  export declare const useConfirm: () => IUseConfirmReturn;
11
17
  //# sourceMappingURL=use-confirm.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ /** State for a confirmation prompt: `confirm` returns a promise that the prompt's buttons settle. */
2
3
  export const useConfirm = () => {
3
4
  const settle = React.useRef(undefined);
4
5
  const [message, setMessage] = React.useState();
@@ -1,7 +1,27 @@
1
+ export interface IUseCopyToClipboardOptions {
2
+ /** Set `isCopied` back to `false` this many milliseconds after a copy; 0 or less never resets. @default 0 */
3
+ resetMs?: number;
4
+ }
1
5
  export interface IUseCopyToClipboardReturn {
6
+ /** Resolves `true` when the text is on the clipboard and `false` otherwise, and never throws. */
2
7
  copy: (opts: {
3
8
  value: string;
4
9
  }) => Promise<boolean>;
10
+ /** The result of the last copy, until `resetMs` has passed. */
11
+ isCopied: boolean;
5
12
  }
6
- export declare const useCopyToClipboard: () => IUseCopyToClipboardReturn;
13
+ /**
14
+ * Copies text with `BrowserClipboard`; `isCopied` is the result of the last copy.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * import { useCopyToClipboard } from '@venizia/ardor';
19
+ *
20
+ * export function CopyButton() {
21
+ * const { copy, isCopied } = useCopyToClipboard({ resetMs: 2000 });
22
+ * return <button onClick={() => copy({ value: 'hello' })}>{isCopied ? 'Copied' : 'Copy'}</button>;
23
+ * }
24
+ * ```
25
+ */
26
+ export declare const useCopyToClipboard: (opts?: IUseCopyToClipboardOptions) => IUseCopyToClipboardReturn;
7
27
  //# sourceMappingURL=use-copy-to-clipboard.d.ts.map
@@ -1,21 +1,45 @@
1
+ import React from 'react';
1
2
  import { LoggerFactory } from '@venizia/ignis-helpers/core';
3
+ import { BrowserClipboard } from '@venizia/ardor-kernel';
2
4
  const logger = LoggerFactory.getLogger(['useCopyToClipboard']);
3
- // A hook is never handed an async function, so the copy lives outside it; it holds no state, which is
4
- // why the hook returns the same function on every render, as `useCallback(..., [])` did.
5
+ // A hook is never handed an async function, so the copy lives outside it.
5
6
  const copyToClipboard = async (opts) => {
6
- if (!navigator?.clipboard) {
7
- logger.warn('[copy] The clipboard is not supported');
8
- return false;
9
- }
10
- const { value } = opts;
11
- try {
12
- await navigator.clipboard.writeText(value);
13
- return true;
14
- }
15
- catch (error) {
16
- logger.warn('[copy] Copy failed | error: %s', error);
17
- return false;
7
+ const isCopied = await BrowserClipboard.writeText({ text: opts.value });
8
+ if (!isCopied) {
9
+ logger.warn('[copy] The clipboard did not take the text');
18
10
  }
11
+ return isCopied;
12
+ };
13
+ /**
14
+ * Copies text with `BrowserClipboard`; `isCopied` is the result of the last copy.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * import { useCopyToClipboard } from '@venizia/ardor';
19
+ *
20
+ * export function CopyButton() {
21
+ * const { copy, isCopied } = useCopyToClipboard({ resetMs: 2000 });
22
+ * return <button onClick={() => copy({ value: 'hello' })}>{isCopied ? 'Copied' : 'Copy'}</button>;
23
+ * }
24
+ * ```
25
+ */
26
+ export const useCopyToClipboard = (opts = {}) => {
27
+ const { resetMs = 0 } = opts;
28
+ const [isCopied, setCopied] = React.useState(false);
29
+ const timerRef = React.useRef(undefined);
30
+ React.useEffect(() => {
31
+ return () => clearTimeout(timerRef.current);
32
+ }, []);
33
+ const copy = React.useCallback((copyOpts) => {
34
+ return copyToClipboard(copyOpts).then((isDone) => {
35
+ clearTimeout(timerRef.current);
36
+ setCopied(isDone);
37
+ if (isDone && resetMs > 0) {
38
+ timerRef.current = setTimeout(() => setCopied(false), resetMs);
39
+ }
40
+ return isDone;
41
+ });
42
+ }, [resetMs]);
43
+ return { copy, isCopied };
19
44
  };
20
- export const useCopyToClipboard = () => ({ copy: copyToClipboard });
21
45
  //# sourceMappingURL=use-copy-to-clipboard.js.map
@@ -1,11 +1,15 @@
1
1
  export interface IUseDebounceOptions<TValue> {
2
+ /** The value to follow; the output changes once it has been still for `delay`. */
2
3
  value: TValue;
4
+ /** Milliseconds to wait after the last change. @default 500 */
3
5
  delay?: number;
4
6
  /** @default true */
5
7
  enabled?: boolean;
6
8
  }
7
9
  export interface IUseDebounceReturn<TValue> {
10
+ /** The last `value` that stayed unchanged for `delay`; the first `value` until then. */
8
11
  debouncedValue: TValue;
9
12
  }
13
+ /** Follows `value` after it has stopped changing for `delay`. Outside a browser, or when not `enabled`, it never updates. */
10
14
  export declare const useDebounce: <TValue>(opts: IUseDebounceOptions<TValue>) => IUseDebounceReturn<TValue>;
11
15
  //# sourceMappingURL=use-debounce.d.ts.map
@@ -1,5 +1,6 @@
1
1
  import React from 'react';
2
2
  import { App, BrowserUtility } from '@venizia/ardor-kernel';
3
+ /** Follows `value` after it has stopped changing for `delay`. Outside a browser, or when not `enabled`, it never updates. */
3
4
  export const useDebounce = (opts) => {
4
5
  const { value, delay, enabled = true } = opts;
5
6
  const [debouncedValue, setDebouncedValue] = React.useState(value);
@@ -0,0 +1,3 @@
1
+ /** `'visible'` or `'hidden'`, as the document says. `'visible'` in a server render. */
2
+ export declare const useDocumentVisibility: () => DocumentVisibilityState;
3
+ //# sourceMappingURL=use-document-visibility.d.ts.map
@@ -0,0 +1,10 @@
1
+ import React from 'react';
2
+ const subscribe = (listener) => {
3
+ document.addEventListener('visibilitychange', listener);
4
+ return () => document.removeEventListener('visibilitychange', listener);
5
+ };
6
+ /** `'visible'` or `'hidden'`, as the document says. `'visible'` in a server render. */
7
+ export const useDocumentVisibility = () => {
8
+ return React.useSyncExternalStore(subscribe, () => document.visibilityState, () => 'visible');
9
+ };
10
+ //# sourceMappingURL=use-document-visibility.js.map
@@ -1,9 +1,10 @@
1
1
  import React from 'react';
2
+ import { type TResizeBox } from './resize-observation.js';
2
3
  export interface IUseElementSizeOptions {
3
4
  /** The element to measure. Leave it out to measure the element the returned `ref` is given to. */
4
5
  ref?: React.RefObject<Element | null>;
5
6
  /** @default 'content-box' */
6
- box?: 'content-box' | 'border-box';
7
+ box?: TResizeBox;
7
8
  /** Round width and height to whole pixels, so a fractional size does not render again. @default false */
8
9
  round?: boolean;
9
10
  }
@@ -14,6 +15,7 @@ export interface IUseElementSizeReturn {
14
15
  measured: boolean;
15
16
  }
16
17
  export interface IUseElementSizeRefReturn extends IUseElementSizeReturn {
18
+ /** Give it to the element to measure. */
17
19
  ref: React.RefCallback<Element>;
18
20
  }
19
21
  /**
@@ -23,6 +25,16 @@ export interface IUseElementSizeRefReturn extends IUseElementSizeReturn {
23
25
  * is read when the effect runs, so it follows a new `ref`, `box` or `round`, not a `ref.current`
24
26
  * that is replaced later; pass a stable `ref`. A new element, `box` or `round` starts again from
25
27
  * 0 by 0 with `measured` false.
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * import { useElementSize } from '@venizia/ardor';
32
+ *
33
+ * export function Panel() {
34
+ * const { ref, width, height, measured } = useElementSize();
35
+ * return <div ref={ref}>{measured ? `${width} x ${height}` : '...'}</div>;
36
+ * }
37
+ * ```
26
38
  */
27
39
  export declare function useElementSize(opts: IUseElementSizeOptions & {
28
40
  ref: React.RefObject<Element | null>;