UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

103 lines 5.83 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 __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"); exports.SpotlightTarget = react_2.default.forwardRef(function SpotlightTarget({ id, hasPulse = true, indicatorVariant = 'border', indicatorPlacement = 'top-right', backgroundColor, borderRadius, children, className, htmlAttributes, as, shouldFitToChildren = false, }, ref) { 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) 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, }); 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 && !isOpen) { setIsOpen(true); e.preventDefault(); } }, style: { borderRadius }, 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 : -1, "aria-label": "Spotlight target" }, htmlAttributes, { children: (0, jsx_runtime_1.jsx)(conditional_wrap_1.ConditionalWrap, { shouldWrap: isActive, // TODO v4: inert should be fixed in react 19? https://github.com/facebook/react/pull/24730#event-12109747640. inert="" is a workaround for now wrap: (children) => ((0, jsx_runtime_1.jsx)("div", { /* @ts-expect-error inert not allowed */ inert: "", "aria-disabled": true, className: inertClasses, children: children })), children: children }) })), (0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { root: indicator.context.refs.reference.current, children: indicator.context.open && ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ className: indicator.indicatorClasses, ref: indicator.context.refs.setFloating, style: Object.assign(Object.assign({}, indicator.floatingStyles), indicator.style) }, indicator.getFloatingProps())) })) })] })); }); //# sourceMappingURL=SpotlightTarget.js.map