UNPKG

@elastic/eui

Version:

Elastic UI Component Library

55 lines (49 loc) 2.52 kB
/* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import { useContext, useRef } from 'react'; import { warnOnce, useGeneratedHtmlId } from '../../../services'; import { EuiFlyoutManagerContext } from './provider'; import { useIsFlyoutRegistered } from './selectors'; // Ensure uniqueness across multiple hook instances, including in test envs var autoGeneratedFlyoutIdCounter = 0; export { useIsFlyoutActive, useHasActiveSession, useCurrentSession, useCurrentMainFlyout, useCurrentChildFlyout, useFlyoutWidth, useFlyoutPagination, useParentFlyoutSize, useHasChildFlyout, usePushPaddingOffsets, useHasPushPadding } from './selectors'; export { useFlyoutLayoutMode } from './layout_mode'; export { useIsInManagedFlyout } from './context'; // Convenience selector for a flyout's activity stage /** Access the flyout manager context (state and actions). */ export var useFlyoutManager = function useFlyoutManager() { return useContext(EuiFlyoutManagerContext); }; /** * Stable flyout ID utility. Uses the passed `id` if provided and not already registered, * otherwise generates a deterministic ID for the component's lifetime. * The ID remains stable across re-renders to maintain consistency in effects and other hooks. */ export var useFlyoutId = function useFlyoutId(flyoutId) { var defaultId = useGeneratedHtmlId({ prefix: 'flyout-' }); var isRegistered = useIsFlyoutRegistered(flyoutId); // Use ref to maintain ID stability across re-renders var componentIdRef = useRef(undefined); if (!componentIdRef.current) { // Determine the ID to use if (!flyoutId) { // No ID provided, generate a new one componentIdRef.current = "".concat(defaultId, "-").concat(++autoGeneratedFlyoutIdCounter); } else if (isRegistered) { // ID is provided but already registered, generate a new one warnOnce("flyout-id-".concat(flyoutId), "Flyout with ID ".concat(flyoutId, " already registered; using new ID ").concat(defaultId)); componentIdRef.current = "".concat(defaultId, "-").concat(++autoGeneratedFlyoutIdCounter); } else { // ID is provided and not registered, use it componentIdRef.current = flyoutId; } } return componentIdRef.current; };