@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
74 lines • 4.28 kB
JavaScript
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 = React.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, } = useSpotlightContext();
const internalRef = React.useRef(null);
const mergedRefs = useMergeRefs([ref, internalRef]);
useEffect(() => {
if (internalRef.current)
registerTarget(id, internalRef);
return () => unregisterTarget(id);
}, [id, registerTarget, unregisterTarget]);
const isActive = isActiveSpotlight(id);
const indicator = useIndicator({
hasPulse,
indicatorPlacement,
indicatorVariant,
isOpen: isActive,
targetRef: internalRef,
});
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 && !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: _jsx(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) => (_jsx("div", {
/* @ts-expect-error inert not allowed */
inert: "", "aria-disabled": true, className: inertClasses, children: children })), children: children }) })), _jsx(FloatingPortal, { root: indicator.context.refs.reference.current, children: indicator.context.open && (_jsx(_Fragment, { children: _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