@elastic/eui
Version:
Elastic UI Component Library
70 lines (64 loc) • 3.88 kB
JavaScript
var _excluded = ["titleId"];
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
/*
* 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 { useGeneratedHtmlId } from '../../services';
import { MENU_DISPLAY_ALWAYS, MENU_DISPLAY_AUTO } from './const';
import { useMemo } from 'react';
import classnames from 'classnames';
/**
* @internal
*/
/**
* Hook to manage flyout menu state and rendering logic.
* Determines whether the menu should be rendered based on display mode
* and menu content, and computes the appropriate aria-labelledby value.
*
* @internal
*/
export var useEuiFlyoutMenu = function useEuiFlyoutMenu(_ref) {
var _flyoutMenuProps$hist, _flyoutMenuProps$hist2, _flyoutMenuProps$cust, _flyoutMenuProps$cust2;
var _flyoutMenuProps = _ref.flyoutMenuProps,
flyoutMenuDisplayMode = _ref.flyoutMenuDisplayMode,
_ariaLabelledBy = _ref.ariaLabelledBy;
var generatedMenuId = useGeneratedHtmlId();
var _ref2 = _flyoutMenuProps || {},
_titleId = _ref2.titleId,
flyoutMenuProps = _objectWithoutProperties(_ref2, _excluded);
var hasMenu = !!_flyoutMenuProps;
var flyoutMenuId = useMemo(function () {
if (!hasMenu) return undefined;
return _titleId || generatedMenuId;
}, [hasMenu, _titleId, generatedMenuId]);
// Determine if the menu has any content
// hasBackButton or hasHistory or hasCustomActions or hasVisibleTitle or hasPagination
var menuHasContent = hasMenu && (!!flyoutMenuProps.showBackButton || ((_flyoutMenuProps$hist = (_flyoutMenuProps$hist2 = flyoutMenuProps.historyItems) === null || _flyoutMenuProps$hist2 === void 0 ? void 0 : _flyoutMenuProps$hist2.length) !== null && _flyoutMenuProps$hist !== void 0 ? _flyoutMenuProps$hist : 0) > 0 || ((_flyoutMenuProps$cust = (_flyoutMenuProps$cust2 = flyoutMenuProps.customActions) === null || _flyoutMenuProps$cust2 === void 0 ? void 0 : _flyoutMenuProps$cust2.length) !== null && _flyoutMenuProps$cust !== void 0 ? _flyoutMenuProps$cust : 0) > 0 ||
// Component defaults to hiding the title, so only explicit false means the title will be visible
!!(flyoutMenuProps.title && flyoutMenuProps.hideTitle === false) || !!(flyoutMenuProps.pagination && flyoutMenuProps.pagination.total > 1));
// Determine if the menu should be rendered based on the display mode and menu content
var shouldRenderMenu = useMemo(function () {
if (!hasMenu) return false;
if (flyoutMenuDisplayMode === MENU_DISPLAY_ALWAYS) return true;
if (flyoutMenuDisplayMode === MENU_DISPLAY_AUTO) return menuHasContent;
return false;
}, [hasMenu, flyoutMenuDisplayMode, menuHasContent]);
// If the flyout menu is to be rendered, ensure the flyout has aria-labelledby referencing the menu's titleId
var ariaLabelledBy = useMemo(function () {
if (flyoutMenuId && shouldRenderMenu) {
return classnames(flyoutMenuId, _ariaLabelledBy);
}
return _ariaLabelledBy;
}, [flyoutMenuId, _ariaLabelledBy, shouldRenderMenu]);
return {
flyoutMenuId: flyoutMenuId,
flyoutMenuProps: flyoutMenuProps,
shouldRenderMenu: shouldRenderMenu,
ariaLabelledBy: ariaLabelledBy
};
};