@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
101 lines • 4.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useIndicator = useIndicator;
/**
*
* 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 = require("react");
const crossAxisMult = {
left: -1,
middle: 0,
right: 1,
};
const calcOffset = (variant, placement, rects) => {
if (variant === 'border') {
// Border variant is always centered around the middle point
return {
crossAxis: 0,
mainAxis: -rects.reference.height / 2 - rects.floating.height / 2,
};
}
const [mainAxisPlacement, crossAxisPlacement] = placement.split('-');
/** Note for future reference:
* Main axis (vertical) is zero at the bottom border of the target.
* Cross axis (horizontal) is zero at the middle of the target
* */
let mainAxis = 0;
if (mainAxisPlacement === 'bottom') {
// put the indicator one height of the point above the bottom border of the target
mainAxis = -2 * rects.floating.height;
}
else if (mainAxisPlacement === 'middle') {
// put the indicator in the middle of the target
mainAxis = -rects.reference.height / 2 - rects.floating.height / 2;
}
else if (mainAxisPlacement === 'top') {
// put the indicator one height of the point below the top border of the target
mainAxis = -rects.reference.height + rects.floating.height;
}
return {
crossAxis: crossAxisMult[crossAxisPlacement] *
(rects.reference.width / 2 - rects.floating.width * 1.5),
mainAxis,
};
};
function useIndicator({ isOpen, indicatorVariant = 'border', indicatorPlacement = 'top-right', targetRef, hasPulse = true, }) {
const data = (0, react_1.useFloating)({
elements: {
reference: targetRef === null || targetRef === void 0 ? void 0 : targetRef.current,
},
middleware: [
(0, react_1.offset)(({ rects }) => calcOffset(indicatorVariant, indicatorPlacement, rects)),
],
open: isOpen,
placement: 'bottom',
whileElementsMounted: react_1.autoUpdate,
});
const context = data.context;
const role = (0, react_1.useRole)(context);
const interactions = (0, react_1.useInteractions)([role]);
return (0, react_2.useMemo)(() => {
var _a, _b, _c, _d, _e, _f;
return (Object.assign(Object.assign(Object.assign({}, interactions), data), { indicatorClasses: (0, classnames_1.default)('ndl-spotlight-target-indicator', {
'ndl-spotlight-target-indicator-border': indicatorVariant === 'border',
'ndl-spotlight-target-indicator-point': indicatorVariant === 'point',
'ndl-spotlight-target-pulse': hasPulse,
}), style: {
borderRadius: indicatorVariant === 'border'
? `${(_b = (_a = targetRef === null || targetRef === void 0 ? void 0 : targetRef.current) === null || _a === void 0 ? void 0 : _a.style.borderRadius) !== null && _b !== void 0 ? _b : 0}`
: '50%',
height: indicatorVariant === 'border'
? `${(_d = (_c = targetRef === null || targetRef === void 0 ? void 0 : targetRef.current) === null || _c === void 0 ? void 0 : _c.clientHeight) !== null && _d !== void 0 ? _d : 0}px`
: '8px',
width: indicatorVariant === 'border'
? `${(_f = (_e = targetRef === null || targetRef === void 0 ? void 0 : targetRef.current) === null || _e === void 0 ? void 0 : _e.clientWidth) !== null && _f !== void 0 ? _f : 0}px`
: '8px',
} }));
}, [interactions, data, indicatorVariant, targetRef, hasPulse]);
}
//# sourceMappingURL=use-indicator.js.map