@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
128 lines • 6.88 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 classNames from 'classnames';
import React from 'react';
import { ConditionalWrap } from '../conditional-wrap';
import { useIsInsideDialog } from '../dialog/dialog-context';
import { useNeedleTheme } from '../theme/ThemeProvider';
import { usePopover } from './use-popover';
import { usePatchFloatingFocusGuards } from './usePatchFloatingFocusGuards';
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;
};
const PopoverComponent = ({ children, anchorElement, placement, isOpen, offset, anchorPosition, hasAnchorPortal: hasAnchorPortalProp, shouldCaptureFocus = false, initialFocus, onOpenChange, role, closeOnClickOutside = true, isPortaled: isPortaledProp, strategy: strategyProp, }) => {
const isInsideDialog = useIsInsideDialog();
const strategy = isInsideDialog ? 'fixed' : 'absolute';
const hasAnchorPortal = isInsideDialog;
const isPortaled = !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,
isPortaled: isPortaledProp !== null && isPortaledProp !== void 0 ? isPortaledProp : isPortaled,
});
return (_jsx(PopoverContext.Provider, { value: popover, children: children }));
};
const PopoverTrigger = (_a) => {
var { children, hasButtonWrapper = false, ref } = _a, props = __rest(_a, ["children", "hasButtonWrapper", "ref"]);
const context = usePopoverContext();
// Example from floating UI
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const childrenProps = children.props;
const mergedRefs = 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.isValidElement(children)) {
return React.cloneElement(children, context.getReferenceProps(Object.assign(Object.assign(Object.assign({}, props), childrenProps), { 'data-state': context.isOpen ? 'open' : 'closed', ref: mergedRefs })));
}
return (_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 = usePopoverContext(), { context: floatingContext } = _b, context = __rest(_b, ["context"]);
const refs = useMergeRefs([context.refs.setFloating, ref]);
const { themeClassName } = useNeedleTheme();
const classes = classNames('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
usePatchFloatingFocusGuards();
if (!floatingContext.open) {
return null;
}
return (_jsx(ConditionalWrap, { shouldWrap: context.isPortaled, wrap: (wrapChildren) => {
var _a;
return (_jsx(FloatingPortal, { root: ((_a = context.anchorElementAsPortalAnchor) !== null && _a !== void 0 ? _a : false)
? context.refs.reference.current
: undefined, children: wrapChildren }));
}, 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: refs }, context.getFloatingProps(Object.assign({}, htmlAttributes)), restProps, { children: children })) }) }));
};
const Popover = Object.assign(PopoverComponent, {
Content: PopoverContent,
Trigger: PopoverTrigger,
});
export { Popover };
//# sourceMappingURL=Popover.js.map