UNPKG

@momentum-ui/react-collaboration

Version:

Cisco Momentum UI Framework for React Collaboration Applications

69 lines 4.83 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 React, { forwardRef, useCallback } from 'react'; import Popover from '../Popover'; import { DEFAULTS, STYLE } from './Tooltip.constants'; import { useId } from '@react-aria/utils'; import './Tooltip.style.scss'; /** * Tooltip component * * Shows a non-interactable popover component with `tooltip` role, and update the aria label/description of the trigger component * * @see [WCAG - Tooltip pattern]{@link https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/} */ var Tooltip = forwardRef(function (_a, ref) { var type = _a.type, _b = _a.boundary, boundary = _b === void 0 ? DEFAULTS.BOUNDARY : _b, _c = _a.color, color = _c === void 0 ? DEFAULTS.COLOR : _c, _d = _a.offsetDistance, offsetDistance = _d === void 0 ? DEFAULTS.OFFSET_DISTANCE : _d, _e = _a.offsetSkidding, offsetSkidding = _e === void 0 ? DEFAULTS.OFFSET_SKIDDING : _e, _f = _a.placement, placement = _f === void 0 ? DEFAULTS.PLACEMENT : _f, _g = _a.strategy, strategy = _g === void 0 ? DEFAULTS.STRATEGY : _g, _h = _a.variant, variant = _h === void 0 ? DEFAULTS.VARIANT : _h, triggerComponent = _a.triggerComponent, children = _a.children, providedLabelId = _a.labelOrDescriptionId, ariaHaspopup = _a["aria-haspopup"], otherProps = __rest(_a, ["type", "boundary", "color", "offsetDistance", "offsetSkidding", "placement", "strategy", "variant", "triggerComponent", "children", "labelOrDescriptionId", 'aria-haspopup']); var generatedLabelId = useId(); var labelId = providedLabelId || generatedLabelId; var isLabelTooltip = type === 'label'; var isDescription = type === 'description'; // Update aria props manually, because // "The `aria` attribute is reserved for future use in React." var setInstance = useCallback(function (popoverInstance) { var _a; popoverInstance === null || popoverInstance === void 0 ? void 0 : popoverInstance.setProps({ // see https://atomiks.github.io/tippyjs/v6/all-props/#aria aria: { expanded: false, content: null, }, }); (_a = otherProps === null || otherProps === void 0 ? void 0 : otherProps.setInstance) === null || _a === void 0 ? void 0 : _a.call(otherProps, popoverInstance); }, [isLabelTooltip, otherProps === null || otherProps === void 0 ? void 0 : otherProps.setInstance]); var newTriggerComponent = React.cloneElement(triggerComponent, __assign(__assign(__assign({}, (isLabelTooltip && { 'aria-labelledby': labelId })), (isDescription && { 'aria-describedby': labelId })), (ariaHaspopup && { 'aria-haspopup': ariaHaspopup }))); // In label and description mode we must render tooltip content twice // First inside the popover, second in a hidden div for Screen Readers (SR) // because Tippy does not render the content until the user focus on the button, so the trigger // component does not have a label or description before tooltip appears // With SR the user can Read the page content without changing the focus so we need to provide a // always accessible label and description for the button. // We use aria-labelledby and aria-describedby because the `children` might contains HTML elements var triggerLabel = isLabelTooltip || isDescription ? (React.createElement("div", { className: STYLE.label, id: labelId }, children)) : null; return (React.createElement(React.Fragment, null, React.createElement(Popover, __assign({ ref: ref, interactive: false, trigger: "mouseenter focus", triggerComponent: newTriggerComponent, showArrow: true, addBackdrop: false, role: "tooltip", boundary: boundary, color: color, offsetDistance: offsetDistance, offsetSkidding: offsetSkidding, placement: placement, strategy: strategy, variant: variant, "aria-hidden": !isLabelTooltip }, otherProps, { setInstance: setInstance }), children), triggerLabel)); }); Tooltip.displayName = 'Tooltip'; export default Tooltip; //# sourceMappingURL=Tooltip.js.map