UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

140 lines 7.37 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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; }; 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 react_2 = __importStar(require("react")); const defaultImports_1 = require("../_common/defaultImports"); const Dialog_1 = require("../dialog/Dialog"); const ThemeProvider_1 = require("../theme/ThemeProvider"); const use_popover_1 = require("./use-popover"); 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 Popover = ({ children, anchorElement, placement, isOpen, offset, anchorPosition, hasAnchorPortal: hasAnchorPortalProp, shouldCaptureFocus = false, initialFocus, onOpenChange, role, closeOnClickOutside = true, strategy: strategyProp, }) => { const isInsideDialog = (0, Dialog_1.useIsInsideDialog)(); const strategy = isInsideDialog ? 'fixed' : 'absolute'; const hasAnchorPortal = 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, }); return ((0, jsx_runtime_1.jsx)(PopoverContext.Provider, { value: popover, children: children })); }; exports.Popover = Popover; const PopoverTrigger = (0, react_2.forwardRef)(function PopoverTrigger(_a, propsRef) { var { children, hasButtonWrapper = false } = _a, props = __rest(_a, ["children", "hasButtonWrapper"]); const context = (0, exports.usePopoverContext)(); // Example from floating UI // eslint-disable-next-line @typescript-eslint/no-explicit-any const childrenRef = children.ref; const ref = (0, react_1.useMergeRefs)([ context.refs.setReference, propsRef, childrenRef, ]); // `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({ ref }, props), children.props), { 'data-state': context.isOpen ? 'open' : 'closed' }))); } return ((0, jsx_runtime_1.jsx)("button", Object.assign({ ref: ref, type: "button", "data-state": context.isOpen ? 'open' : 'closed' }, context.getReferenceProps(props), { children: children }))); }); const PopoverContent = react_2.default.forwardRef(function PopoverContent({ as, className, style, children, htmlAttributes, }, propsRef) { const _a = (0, exports.usePopoverContext)(), { context: floatingContext } = _a, context = __rest(_a, ["context"]); const ref = (0, react_1.useMergeRefs)([context.refs.setFloating, propsRef]); const { themeClassName } = (0, ThemeProvider_1.useNeedleTheme)(); const classes = (0, defaultImports_1.classNames)('ndl-popover', themeClassName, className); const Component = as || 'div'; if (!floatingContext.open) return null; return ((0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { root: context.anchorElementAsPortalAnchor ? context.refs.reference.current : null, 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: ref }, context.getFloatingProps(Object.assign({}, htmlAttributes)), { children: children })) }) })); }); exports.Popover.Trigger = PopoverTrigger; exports.Popover.Content = PopoverContent; //# sourceMappingURL=Popover.js.map