@react-hive/honey-layout 17.5.0 → 17.7.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.
@@ -5973,34 +5973,41 @@ __webpack_require__.r(__webpack_exports__);
5973
5973
 
5974
5974
 
5975
5975
  /**
5976
- * Hook for interacting with an active overlay managed by `HoneyLayoutProvider`.
5976
+ * Returns an active overlay by ID and optionally attaches overlay event listeners.
5977
5977
  *
5978
- * @param targetOverlayId - The unique ID of the overlay you want to interact with.
5979
- * @param options - Optional configuration such as event handlers (e.g., `onKeyUp`).
5978
+ * This hook looks up an overlay registered in `HoneyLayoutProvider` by its ID.
5979
+ * It can also subscribe to overlay-level events, such as `keyup`, for the matched overlay.
5980
5980
  *
5981
- * @returns The overlay instance matching the provided ID, or `undefined` if not found.
5981
+ * @param targetOverlayId - The ID of the active overlay to find.
5982
+ * @param options - Optional configuration for resolving the overlay and attaching event handlers.
5983
+ *
5984
+ * @returns The matching active overlay instance, or `null` when the hook is disabled
5985
+ * or the overlay is not currently registered.
5982
5986
  *
5983
5987
  * @remarks
5984
- * - This hook only works with overlays that are currently active.
5985
- * - If the overlay is not active or not registered, `undefined` will be returned.
5986
- * - Event handlers like `onKeyUp` are automatically registered and cleaned up for the overlay.
5988
+ * - The hook only works with overlays that are currently active.
5989
+ * - If `enabled` is `false`, the hook returns `null` and does not attach listeners.
5990
+ * - If the overlay is inactive, unregistered, or already removed from the stack, `null` is returned.
5991
+ * - The `onKeyUp` listener is attached only to the matched overlay while the hook is enabled.
5992
+ * - Event listeners are automatically cleaned up when the overlay, handler, enabled state,
5993
+ * or component lifecycle changes.
5987
5994
  *
5988
5995
  * @example
5989
5996
  * ```tsx
5990
5997
  * const overlay = useHoneyOverlay('my-overlay-id', {
5991
- * onKeyUp: (keyCode, e) => {
5998
+ * enabled: isOpen,
5999
+ * onKeyUp: (keyCode, overlay, e) => {
5992
6000
  * if (keyCode === 'Escape') {
5993
- * console.log('Escape key pressed!');
6001
+ * // Handle Escape key.
5994
6002
  * }
5995
6003
  * },
5996
6004
  * });
5997
6005
  * ```
5998
6006
  */
5999
- const useHoneyOverlay = (targetOverlayId, { onKeyUp } = {}) => {
6007
+ const useHoneyOverlay = (targetOverlayId, { enabled = true, onKeyUp } = {}) => {
6000
6008
  const { overlays } = (0,_hooks__WEBPACK_IMPORTED_MODULE_1__.useHoneyLayout)();
6001
- const overlay = overlays.find(overlay => overlay.id === targetOverlayId);
6009
+ const overlay = (0,react__WEBPACK_IMPORTED_MODULE_0__.useMemo)(() => (enabled ? (overlays.find(overlay => overlay.id === targetOverlayId) ?? null) : null), [enabled, overlays, targetOverlayId]);
6002
6010
  (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
6003
- // If no overlay is found or no `onKeyUp` handler is provided, skip setting up the listener
6004
6011
  if (!overlay || !onKeyUp) {
6005
6012
  return;
6006
6013
  }
@@ -6175,16 +6182,29 @@ __webpack_require__.r(__webpack_exports__);
6175
6182
 
6176
6183
 
6177
6184
  /**
6178
- * Hook to manage a stack of overlays, allowing registration and unregistration of overlays,
6179
- * as well as handling keyboard events for the topmost overlay.
6185
+ * Manages the active overlay stack and global keyboard event dispatching.
6186
+ *
6187
+ * The hook keeps registered overlays in stack order, where the latest registered overlay
6188
+ * is treated as the top-level overlay. Keyboard events are forwarded only to the top-level
6189
+ * overlay, allowing nested or overlapping overlays to handle key interactions predictably.
6190
+ *
6191
+ * @returns An object containing the active overlays stack and helper methods for registering
6192
+ * and unregistering overlays.
6180
6193
  */
6181
6194
  const useHoneyOverlays = () => {
6182
- const overlaysRef = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)([]);
6195
+ const [overlays, setOverlays] = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)([]);
6183
6196
  (0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(() => {
6197
+ /**
6198
+ * Handles global keyup events and forwards them to the top-level overlay.
6199
+ *
6200
+ * Only the latest registered overlay receives keyboard events. This prevents inactive
6201
+ * or visually hidden overlays lower in the stack from reacting to the same key press.
6202
+ *
6203
+ * @param e - The native keyboard event emitted by the document.
6204
+ */
6184
6205
  const handleKeyUp = (e) => {
6185
- const overlays = overlaysRef.current;
6186
6206
  if (!overlays.length) {
6187
- // No overlays to handle key events
6207
+ // No overlays to handle key events.
6188
6208
  return;
6189
6209
  }
6190
6210
  const topLevelOverlay = overlays[overlays.length - 1];
@@ -6194,13 +6214,19 @@ const useHoneyOverlays = () => {
6194
6214
  return () => {
6195
6215
  document.removeEventListener('keyup', handleKeyUp);
6196
6216
  };
6197
- }, []);
6217
+ }, [overlays]);
6198
6218
  /**
6199
- * Registers a new overlay and adds it to the stack.
6219
+ * Registers a new overlay and adds it to the top of the overlay stack.
6220
+ *
6221
+ * If no custom ID is provided, an ephemeral ID is generated automatically.
6222
+ * The returned overlay object exposes methods for storing its container element,
6223
+ * subscribing to overlay events, removing event listeners, and notifying registered
6224
+ * listeners when matching events occur.
6200
6225
  *
6201
- * @param overlayConfig - The configuration for the overlay, including optional ID and event handlers.
6226
+ * @param overlayConfig - The overlay configuration, including an optional ID, optional keyup
6227
+ * handler, and optional list of keyboard codes the overlay should listen to.
6202
6228
  *
6203
- * @returns The registered overlay object.
6229
+ * @returns The registered active overlay instance.
6204
6230
  */
6205
6231
  const registerOverlay = (0,react__WEBPACK_IMPORTED_MODULE_0__.useCallback)(overlayConfig => {
6206
6232
  const overlayId = overlayConfig.id ?? (0,_react_hive_honey_utils__WEBPACK_IMPORTED_MODULE_1__.generateEphemeralId)();
@@ -6211,18 +6237,54 @@ const useHoneyOverlays = () => {
6211
6237
  const overlay = {
6212
6238
  containerRef,
6213
6239
  id: overlayId,
6240
+ /**
6241
+ * Stores the overlay container element reference.
6242
+ *
6243
+ * This allows consumers and overlay helpers to access the DOM element associated
6244
+ * with the active overlay after it has been mounted.
6245
+ *
6246
+ * @param element - The overlay container element, or `null` when unavailable.
6247
+ */
6214
6248
  setContainerRef: element => {
6215
6249
  containerRef.current = element;
6216
6250
  },
6251
+ /**
6252
+ * Adds a listener for a supported overlay event.
6253
+ *
6254
+ * @param type - The overlay event type to listen for.
6255
+ * @param handler - The event handler to call when the event is notified.
6256
+ */
6217
6257
  addListener: (type, handler) => {
6218
6258
  listeners.push([type, handler]);
6219
6259
  },
6260
+ /**
6261
+ * Removes a previously registered overlay event listener.
6262
+ *
6263
+ * The listener is removed only when both the event type and handler reference match.
6264
+ *
6265
+ * @param targetType - The event type of the listener to remove.
6266
+ * @param targetHandler - The exact handler reference to remove.
6267
+ */
6220
6268
  removeListener: (targetType, targetHandler) => {
6221
6269
  const targetListenerIndex = listeners.findIndex(([type, listenerHandler]) => type === targetType && listenerHandler === targetHandler);
6222
6270
  if (targetListenerIndex !== -1) {
6223
6271
  listeners.splice(targetListenerIndex, 1);
6224
6272
  }
6225
6273
  },
6274
+ /**
6275
+ * Notifies matching listeners for a specific overlay event.
6276
+ *
6277
+ * If `listenKeys` is provided in the overlay config, listeners are called only when
6278
+ * the received key code is included in that list. When no `listenKeys` are provided,
6279
+ * all key codes are accepted.
6280
+ *
6281
+ * The native event is prevented before listeners are called, ensuring handled overlay
6282
+ * keyboard interactions do not trigger default browser behaviour.
6283
+ *
6284
+ * @param targetEventType - The event type being dispatched.
6285
+ * @param keyCode - The keyboard code associated with the event.
6286
+ * @param e - The native keyboard event.
6287
+ */
6226
6288
  notifyListeners: (targetEventType, keyCode, e) => {
6227
6289
  const listenKeys = overlayConfig.listenKeys ?? [];
6228
6290
  if (!listenKeys.length || listenKeys.includes(keyCode)) {
@@ -6235,19 +6297,21 @@ const useHoneyOverlays = () => {
6235
6297
  }
6236
6298
  },
6237
6299
  };
6238
- overlaysRef.current.push(overlay);
6300
+ setOverlays(prevOverlays => [...prevOverlays, overlay]);
6239
6301
  return overlay;
6240
6302
  }, []);
6241
6303
  /**
6242
- * Unregisters an overlay by its ID and removes it from the stack.
6304
+ * Unregisters an overlay by ID and removes it from the overlay stack.
6243
6305
  *
6244
- * @param targetOverlayId - The ID of the overlay to be removed.
6306
+ * This should usually be called when an overlay is deactivated or unmounted.
6307
+ *
6308
+ * @param targetOverlayId - The ID of the overlay to remove.
6245
6309
  */
6246
6310
  const unregisterOverlay = (0,react__WEBPACK_IMPORTED_MODULE_0__.useCallback)(targetOverlayId => {
6247
- overlaysRef.current = overlaysRef.current.filter(overlay => overlay.id !== targetOverlayId);
6311
+ setOverlays(prevOverlays => prevOverlays.filter(overlay => overlay.id !== targetOverlayId));
6248
6312
  }, []);
6249
6313
  return {
6250
- overlays: overlaysRef.current,
6314
+ overlays,
6251
6315
  registerOverlay,
6252
6316
  unregisterOverlay,
6253
6317
  };