UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

130 lines 7.15 kB
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, Fragment as _Fragment, jsxs as _jsxs } 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 { FloatingPortal, useMergeRefs } from '@floating-ui/react'; import classNames from 'classnames'; import React, { useEffect } from 'react'; import { ConditionalWrap } from '../conditional-wrap'; import { useSpotlightContext } from './SpotlightProvider'; import { useIndicator, } from './use-indicator'; export const SpotlightTarget = (_a) => { var { id, hasPulse = true, indicatorVariant = 'border', indicatorPlacement = 'top-right', borderRadius, shouldFitToChildren = false, children, className, htmlAttributes, style, as, ref, asChild } = _a, restProps = __rest(_a, ["id", "hasPulse", "indicatorVariant", "indicatorPlacement", "borderRadius", "shouldFitToChildren", "children", "className", "htmlAttributes", "style", "as", "ref", "asChild"]); const Component = as !== null && as !== void 0 ? as : 'div'; const { isActiveSpotlight, isOpen, setIsOpen, registerTarget, unregisterTarget, setActiveSpotlight, } = useSpotlightContext(); const internalRef = React.useRef(null); const childRef = asChild && React.isValidElement(children) ? children.ref : undefined; const mergedRefs = useMergeRefs([ ref, internalRef, childRef, ]); useEffect(() => { if (internalRef.current !== null) { registerTarget(id, internalRef); } return () => unregisterTarget(id); }, [id, registerTarget, unregisterTarget]); const isActive = isActiveSpotlight(id); const indicator = useIndicator({ hasPulse, indicatorPlacement, indicatorVariant, isOpen: isActive, targetRef: internalRef, }); if (asChild === true && React.isValidElement(children)) { const child = children; const onClick = (e) => { var _a, _b; if (isActive) { // When spotlight is active, always prevent the child's click handler e.preventDefault(); if (!isOpen) { setIsOpen(true); } // Don't call child.props?.onClick when spotlight is active return; } // Only call the child's click handler when spotlight is not active (_b = (_a = child.props) === null || _a === void 0 ? void 0 : _a.onClick) === null || _b === void 0 ? void 0 : _b.call(_a, e); }; const onKeyDown = (e) => { var _a, _b; if (isActive && !isOpen && (e.code === 'Enter' || e.code === 'Space')) { e.preventDefault(); setIsOpen(true); return; } if (isActive && e.code === 'Escape') { e.preventDefault(); setActiveSpotlight(null); return; } (_b = (_a = child.props) === null || _a === void 0 ? void 0 : _a.onKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, e); }; // IMPORTANT: do NOT force role/tabIndex here; preserves Composite semantics for the SideNavigation const mergedProps = Object.assign({ id, onClick, onKeyDown, ref: mergedRefs, style: Object.assign(Object.assign({}, style), child.props.style) }, (htmlAttributes ? Object.assign(Object.assign({}, htmlAttributes), { role: undefined, tabIndex: undefined }) : {})); return (_jsxs(_Fragment, { children: [React.cloneElement(child, mergedProps), _jsx(ConditionalWrap, { shouldWrap: isActive && isOpen, wrap: (wrapChildren) => (_jsx(FloatingPortal, { children: wrapChildren })), children: indicator.context.open && (_jsx("div", { className: indicator.indicatorClasses, ref: indicator.context.refs.setFloating, style: Object.assign(Object.assign({}, indicator.floatingStyles), indicator.style) })) })] })); } const classes = classNames('ndl-spotlight-target', { 'ndl-spotlight-target-fit-to-children': shouldFitToChildren, 'ndl-spotlight-target-open': isOpen && isActive, }, className); const inertClasses = classNames('ndl-spotlight-target-inert', { 'ndl-spotlight-target-inert-fit-to-children': shouldFitToChildren, }, className); return (_jsxs(_Fragment, { children: [_jsx(Component, Object.assign({ role: isActive ? 'button' : undefined, id: id, ref: mergedRefs, className: classes, onClick: (e) => { if (isActive) { e.preventDefault(); e.stopPropagation(); if (!isOpen) { setIsOpen(true); } } }, style: Object.assign({ borderRadius }, style), onKeyDown: (e) => { if (isActive && !isOpen && (e.code === 'Enter' || e.code === 'Space')) { setIsOpen(true); e.preventDefault(); } else if (e.code === 'Escape' && isActive) { setActiveSpotlight(null); } }, tabIndex: isActive ? 0 : undefined }, (isActive && { 'aria-label': 'Open spotlight' }), restProps, htmlAttributes, { children: _jsx(ConditionalWrap, { shouldWrap: isActive, wrap: (children) => (_jsx("div", { inert: true, "aria-disabled": true, className: inertClasses, children: children })), children: children }) })), _jsx(ConditionalWrap, { shouldWrap: isActive && isOpen, wrap: (wrapChildren) => _jsx(FloatingPortal, { children: wrapChildren }), children: indicator.context.open && (_jsx("div", { className: indicator.indicatorClasses, ref: indicator.context.refs.setFloating, style: Object.assign(Object.assign({}, indicator.floatingStyles), indicator.style) })) })] })); }; //# sourceMappingURL=SpotlightTarget.js.map