@react-hive/honey-layout 18.0.0 → 18.1.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.
@@ -1,34 +1,60 @@
1
1
  import type { HoneyTheme } from '@react-hive/honey-style';
2
2
  import type { HoneyOverlayConfig, HoneyScreenState, HoneyOverlayId, HoneyActiveOverlay } from '../types';
3
3
  /**
4
- * Function to unregister a previously registered overlay.
4
+ * Returns the current active overlay stack snapshot.
5
+ *
6
+ * The returned array keeps the same identity until the overlay stack changes, allowing it to
7
+ * be consumed safely by `useSyncExternalStore`. Consumers must not mutate the snapshot.
8
+ *
9
+ * @returns The active overlays in registration order.
10
+ */
11
+ export type HoneyGetOverlaysSnapshot = () => readonly HoneyActiveOverlay[];
12
+ /**
13
+ * Subscribes to active overlay stack changes.
14
+ *
15
+ * @param subscriber - Callback invoked when a new overlay stack snapshot is available.
16
+ * @returns A cleanup function that removes the subscriber.
17
+ */
18
+ export type HoneySubscribeOverlays = (subscriber: () => void) => () => void;
19
+ /**
20
+ * Unregisters a previously registered overlay.
21
+ *
22
+ * @param targetOverlayId - The ID of the overlay to unregister.
5
23
  */
6
24
  export type HoneyUnregisterOverlay = (targetOverlayId: HoneyOverlayId) => void;
7
25
  /**
8
- * Function to register a new overlay and manage its lifecycle.
26
+ * Registers a new active overlay.
9
27
  *
10
- * @param overlayConfig - Configuration object for the overlay.
28
+ * @param overlayConfig - Configuration for the overlay.
29
+ * @returns The registered active overlay instance.
11
30
  */
12
31
  export type HoneyRegisterOverlay = (overlayConfig: HoneyOverlayConfig) => HoneyActiveOverlay;
32
+ /**
33
+ * Values and overlay-store operations exposed by `HoneyLayoutProvider`.
34
+ */
13
35
  export interface HoneyLayoutContextValue {
14
36
  /**
15
- * Represents the theme object.
37
+ * The active Honey theme.
16
38
  */
17
39
  theme: HoneyTheme;
18
40
  /**
19
- * Represents the current state of the screen.
41
+ * The current responsive screen state.
20
42
  */
21
43
  screenState: HoneyScreenState;
22
44
  /**
23
- * Active overlays.
45
+ * Returns the current overlay stack snapshot without subscribing the caller to changes.
46
+ */
47
+ getOverlaysSnapshot: HoneyGetOverlaysSnapshot;
48
+ /**
49
+ * Subscribes a consumer to overlay stack changes without re-rendering the layout provider.
24
50
  */
25
- overlays: HoneyActiveOverlay[];
51
+ subscribeOverlays: HoneySubscribeOverlays;
26
52
  /**
27
- * Function to register a new overlay.
53
+ * Registers an overlay and publishes a new overlay stack snapshot.
28
54
  */
29
55
  registerOverlay: HoneyRegisterOverlay;
30
56
  /**
31
- * Function to unregister an overlay.
57
+ * Unregisters an overlay and publishes a new snapshot when the overlay existed.
32
58
  */
33
59
  unregisterOverlay: HoneyUnregisterOverlay;
34
60
  }
@@ -1,49 +1,51 @@
1
- import type { HoneyOverlayId, HoneyOverlayEventListenerHandler, HoneyActiveOverlay, Nullable } from '../types';
1
+ import type { HoneyOverlayId, HoneyOverlayEventListenerHandler, Nullable, HoneyActiveOverlay } from '../types';
2
2
  interface UseHoneyOverlayOptions {
3
3
  /**
4
4
  * Whether the hook should resolve the overlay and attach event listeners.
5
5
  *
6
- * When `false`, the hook returns `null` and does not register any listeners.
6
+ * When `false`, the hook returns `null`, does not subscribe to overlay stack changes,
7
+ * and does not attach an overlay event listener.
7
8
  *
8
9
  * @default true
9
10
  */
10
11
  enabled?: boolean;
11
12
  /**
12
- * Callback fired when the active target overlay receives a keyup event.
13
+ * Callback fired when the target overlay receives a keyup event.
13
14
  *
14
- * The handler is registered only while the target overlay is active, the hook is enabled,
15
- * and the handler is provided. It is automatically removed when the overlay changes,
16
- * the handler changes, the hook is disabled, or the component unmounts.
15
+ * The handler is attached only while the target overlay is active and the hook is enabled.
16
+ * It is automatically removed when the overlay or handler changes, the hook is disabled,
17
+ * or the component unmounts.
17
18
  */
18
19
  onKeyUp?: HoneyOverlayEventListenerHandler;
19
20
  }
20
21
  /**
21
- * Returns an active overlay by ID and optionally attaches overlay event listeners.
22
+ * Returns an active overlay by ID and optionally attaches a keyup event listener.
22
23
  *
23
- * This hook looks up an overlay registered in `HoneyLayoutProvider` by its ID.
24
- * It can also subscribe to overlay-level events, such as `keyup`, for the matched overlay.
24
+ * The hook reads the overlay through the external overlay store exposed by
25
+ * `HoneyLayoutProvider`. It subscribes to stack changes with `useSyncExternalStore`, but
26
+ * selects only the requested overlay. Consequently, unrelated overlay stack changes do not
27
+ * re-render the consuming component when the selected overlay instance remains unchanged.
25
28
  *
26
29
  * @param targetOverlayId - The ID of the active overlay to find.
27
- * @param options - Optional configuration for resolving the overlay and attaching event handlers.
30
+ * @param options - Optional configuration for resolving the overlay and handling keyup events.
28
31
  *
29
- * @returns The matching active overlay instance, or `null` when the hook is disabled
30
- * or the overlay is not currently registered.
32
+ * @returns The matching active overlay, or `null` when the hook is disabled or the overlay is
33
+ * not currently registered.
31
34
  *
32
35
  * @remarks
33
- * - The hook only works with overlays that are currently active.
34
- * - If `enabled` is `false`, the hook returns `null` and does not attach listeners.
35
- * - If the overlay is inactive, unregistered, or already removed from the stack, `null` is returned.
36
- * - The `onKeyUp` listener is attached only to the matched overlay while the hook is enabled.
37
- * - Event listeners are automatically cleaned up when the overlay, handler, enabled state,
38
- * or component lifecycle changes.
36
+ * - The hook resolves only overlays that are currently registered.
37
+ * - If `enabled` is `false`, the snapshot is `null` and no keyup listener is attached.
38
+ * - If the target overlay is registered or unregistered, the consuming component is updated.
39
+ * - Changes to unrelated overlays do not cause a re-render when the selected snapshot is equal.
40
+ * - The `onKeyUp` listener is automatically cleaned up when its dependencies change.
39
41
  *
40
42
  * @example
41
43
  * ```tsx
42
44
  * const overlay = useHoneyOverlay('my-overlay-id', {
43
45
  * enabled: isOpen,
44
- * onKeyUp: (keyCode, overlay, e) => {
46
+ * onKeyUp: keyCode => {
45
47
  * if (keyCode === 'Escape') {
46
- * // Handle Escape key.
48
+ * closeOverlay();
47
49
  * }
48
50
  * },
49
51
  * });