UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

166 lines 9.08 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpotlightTarget = 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 = __importStar(require("react")); const conditional_wrap_1 = require("../conditional-wrap"); const SpotlightProvider_1 = require("./SpotlightProvider"); const use_indicator_1 = require("./use-indicator"); 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, } = (0, SpotlightProvider_1.useSpotlightContext)(); const internalRef = react_2.default.useRef(null); const mergedRefs = (0, react_1.useMergeRefs)([ ref, internalRef, ]); (0, react_2.useEffect)(() => { if (internalRef.current !== null) { registerTarget(id, internalRef); } return () => unregisterTarget(id); }, [id, registerTarget, unregisterTarget]); const isActive = isActiveSpotlight(id); const indicator = (0, use_indicator_1.useIndicator)({ hasPulse, indicatorPlacement, indicatorVariant, isOpen: isActive, targetRef: internalRef, }); if (asChild === true && react_2.default.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 ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [react_2.default.cloneElement(child, mergedProps), (0, jsx_runtime_1.jsx)(conditional_wrap_1.ConditionalWrap, { shouldWrap: isActive && isOpen, wrap: (wrapChildren) => ((0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { children: wrapChildren })), children: indicator.context.open && ((0, jsx_runtime_1.jsx)("div", { className: indicator.indicatorClasses, ref: indicator.context.refs.setFloating, style: Object.assign(Object.assign({}, indicator.floatingStyles), indicator.style) })) })] })); } const classes = (0, classnames_1.default)('ndl-spotlight-target', { 'ndl-spotlight-target-fit-to-children': shouldFitToChildren, 'ndl-spotlight-target-open': isOpen && isActive, }, className); const inertClasses = (0, classnames_1.default)('ndl-spotlight-target-inert', { 'ndl-spotlight-target-inert-fit-to-children': shouldFitToChildren, }, className); return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.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: (0, jsx_runtime_1.jsx)(conditional_wrap_1.ConditionalWrap, { shouldWrap: isActive, wrap: (children) => ((0, jsx_runtime_1.jsx)("div", { inert: true, "aria-disabled": true, className: inertClasses, children: children })), children: children }) })), (0, jsx_runtime_1.jsx)(conditional_wrap_1.ConditionalWrap, { shouldWrap: isActive && isOpen, wrap: (wrapChildren) => (0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { children: wrapChildren }), children: indicator.context.open && ((0, jsx_runtime_1.jsx)("div", { className: indicator.indicatorClasses, ref: indicator.context.refs.setFloating, style: Object.assign(Object.assign({}, indicator.floatingStyles), indicator.style) })) })] })); }; exports.SpotlightTarget = SpotlightTarget; //# sourceMappingURL=SpotlightTarget.js.map