UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

135 lines 7.59 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Popover = exports.usePopoverContext = exports.placementTranslation = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); /** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ const react_1 = require("@floating-ui/react"); const classnames_1 = __importDefault(require("classnames")); const react_2 = __importDefault(require("react")); const conditional_wrap_1 = require("../conditional-wrap"); const dialog_context_1 = require("../dialog/dialog-context"); const ThemeProvider_1 = require("../theme/ThemeProvider"); const use_popover_1 = require("./use-popover"); const usePatchFloatingFocusGuards_1 = require("./usePatchFloatingFocusGuards"); exports.placementTranslation = { 'bottom-end-top-end': 'bottom-end', 'bottom-start-top-start': 'bottom-start', 'bottom-middle-top-middle': 'bottom', 'top-end-bottom-end': 'top-end', 'top-start-bottom-start': 'top-start', 'top-middle-bottom-middle': 'top', 'bottom-start-bottom-end': 'left-end', 'top-start-top-end': 'left-start', 'middle-start-middle-end': 'left', 'bottom-end-bottom-start': 'right-end', 'top-end-top-start': 'right-start', 'middle-end-middle-start': 'right', }; const PopoverContext = react_2.default.createContext(null); const usePopoverContext = () => { const context = react_2.default.useContext(PopoverContext); if (context === null) { throw new Error('Popover components must be wrapped in <Popover />'); } return context; }; exports.usePopoverContext = usePopoverContext; const PopoverComponent = ({ children, anchorElement, placement, isOpen, offset, anchorPosition, hasAnchorPortal: hasAnchorPortalProp, shouldCaptureFocus = false, initialFocus, onOpenChange, role, closeOnClickOutside = true, isPortaled: isPortaledProp, strategy: strategyProp, }) => { const isInsideDialog = (0, dialog_context_1.useIsInsideDialog)(); const strategy = isInsideDialog ? 'fixed' : 'absolute'; const hasAnchorPortal = isInsideDialog; const isPortaled = !isInsideDialog; const popover = (0, use_popover_1.usePopover)({ placement: placement ? exports.placementTranslation[placement] : undefined, isOpen: isOpen, offsetOption: offset, anchorElement: anchorElement, anchorElementAsPortalAnchor: hasAnchorPortalProp !== null && hasAnchorPortalProp !== void 0 ? hasAnchorPortalProp : hasAnchorPortal, anchorPosition, onOpenChange: onOpenChange, shouldCaptureFocus: shouldCaptureFocus, initialFocus, role, closeOnClickOutside, strategy: strategyProp !== null && strategyProp !== void 0 ? strategyProp : strategy, isPortaled: isPortaledProp !== null && isPortaledProp !== void 0 ? isPortaledProp : isPortaled, }); return ((0, jsx_runtime_1.jsx)(PopoverContext.Provider, { value: popover, children: children })); }; const PopoverTrigger = (_a) => { var { children, hasButtonWrapper = false, ref } = _a, props = __rest(_a, ["children", "hasButtonWrapper", "ref"]); const context = (0, exports.usePopoverContext)(); // Example from floating UI // eslint-disable-next-line @typescript-eslint/no-explicit-any const childrenProps = children.props; const mergedRefs = (0, react_1.useMergeRefs)([ context.refs.setReference, ref, childrenProps === null || childrenProps === void 0 ? void 0 : childrenProps.ref, ]); // `hasButtonWrapper` allows the user to pass any element as the anchor if (hasButtonWrapper && react_2.default.isValidElement(children)) { return react_2.default.cloneElement(children, context.getReferenceProps(Object.assign(Object.assign(Object.assign({}, props), childrenProps), { 'data-state': context.isOpen ? 'open' : 'closed', ref: mergedRefs }))); } return ((0, jsx_runtime_1.jsx)("button", Object.assign({ ref: context.refs.setReference, type: "button", "data-state": context.isOpen ? 'open' : 'closed' }, context.getReferenceProps(props), { children: children }))); }; const PopoverContent = (_a) => { var { as, className, style, children, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "className", "style", "children", "htmlAttributes", "ref"]); const _b = (0, exports.usePopoverContext)(), { context: floatingContext } = _b, context = __rest(_b, ["context"]); const refs = (0, react_1.useMergeRefs)([context.refs.setFloating, ref]); const { themeClassName } = (0, ThemeProvider_1.useNeedleTheme)(); const classes = (0, classnames_1.default)('ndl-popover', themeClassName, className); const Component = as !== null && as !== void 0 ? as : 'div'; // Patch to not get axe errors for the focus guards // To be noted is that axe gives us a false positive error here since the focus guards directly pass focus to other elements // https://github.com/floating-ui/floating-ui/issues/2462 (0, usePatchFloatingFocusGuards_1.usePatchFloatingFocusGuards)(); if (!floatingContext.open) { return null; } return ((0, jsx_runtime_1.jsx)(conditional_wrap_1.ConditionalWrap, { shouldWrap: context.isPortaled, wrap: (wrapChildren) => { var _a; return ((0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { root: ((_a = context.anchorElementAsPortalAnchor) !== null && _a !== void 0 ? _a : false) ? context.refs.reference.current : undefined, children: wrapChildren })); }, children: (0, jsx_runtime_1.jsx)(react_1.FloatingFocusManager, { context: floatingContext, modal: context.shouldCaptureFocus, initialFocus: context.initialFocus, children: (0, jsx_runtime_1.jsx)(Component, Object.assign({ className: classes, "aria-labelledby": context.labelId, "aria-describedby": context.descriptionId, style: Object.assign(Object.assign(Object.assign({}, context.floatingStyles), context.transitionStyles), style), ref: refs }, context.getFloatingProps(Object.assign({}, htmlAttributes)), restProps, { children: children })) }) })); }; const Popover = Object.assign(PopoverComponent, { Content: PopoverContent, Trigger: PopoverTrigger, }); exports.Popover = Popover; //# sourceMappingURL=Popover.js.map