@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
112 lines • 5.83 kB
JavaScript
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;
};
import { jsx as _jsx } from "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/>.
*/
import { FloatingFocusManager, FloatingPortal, useMergeRefs, } from '@floating-ui/react';
import React, { forwardRef } from 'react';
import { classNames } from '../_common/defaultImports';
import { useIsInsideDialog } from '../dialog/Dialog';
import { useNeedleTheme } from '../theme/ThemeProvider';
import { usePopover } from './use-popover';
export const 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.createContext(null);
export const usePopoverContext = () => {
const context = React.useContext(PopoverContext);
if (context === null) {
throw new Error('Popover components must be wrapped in <Popover />');
}
return context;
};
export const Popover = ({ children, anchorElement, placement, isOpen, offset, anchorPosition, hasAnchorPortal: hasAnchorPortalProp, shouldCaptureFocus = false, initialFocus, onOpenChange, role, closeOnClickOutside = true, strategy: strategyProp, }) => {
const isInsideDialog = useIsInsideDialog();
const strategy = isInsideDialog ? 'fixed' : 'absolute';
const hasAnchorPortal = isInsideDialog;
const popover = usePopover({
placement: placement ? 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 (_jsx(PopoverContext.Provider, { value: popover, children: children }));
};
const PopoverTrigger = forwardRef(function PopoverTrigger(_a, propsRef) {
var { children, hasButtonWrapper = false } = _a, props = __rest(_a, ["children", "hasButtonWrapper"]);
const context = usePopoverContext();
// Example from floating UI
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const childrenRef = children.ref;
const ref = useMergeRefs([
context.refs.setReference,
propsRef,
childrenRef,
]);
// `hasButtonWrapper` allows the user to pass any element as the anchor
if (hasButtonWrapper && React.isValidElement(children)) {
return React.cloneElement(children, context.getReferenceProps(Object.assign(Object.assign(Object.assign({ ref }, props), children.props), { 'data-state': context.isOpen ? 'open' : 'closed' })));
}
return (_jsx("button", Object.assign({ ref: ref, type: "button", "data-state": context.isOpen ? 'open' : 'closed' }, context.getReferenceProps(props), { children: children })));
});
const PopoverContent = React.forwardRef(function PopoverContent({ as, className, style, children, htmlAttributes, }, propsRef) {
const _a = usePopoverContext(), { context: floatingContext } = _a, context = __rest(_a, ["context"]);
const ref = useMergeRefs([context.refs.setFloating, propsRef]);
const { themeClassName } = useNeedleTheme();
const classes = classNames('ndl-popover', themeClassName, className);
const Component = as || 'div';
if (!floatingContext.open)
return null;
return (_jsx(FloatingPortal, { root: context.anchorElementAsPortalAnchor
? context.refs.reference.current
: null, children: _jsx(FloatingFocusManager, { context: floatingContext, modal: context.shouldCaptureFocus, initialFocus: context.initialFocus, children: _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 })) }) }));
});
Popover.Trigger = PopoverTrigger;
Popover.Content = PopoverContent;
//# sourceMappingURL=Popover.js.map