@momentum-ui/react-collaboration
Version:
Cisco Momentum UI Framework for React Collaboration Applications
85 lines • 5.06 kB
JavaScript
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, useEffect, useRef, useState } from 'react';
import Popover from '../Popover';
import { DEFAULTS, STYLE } from './Toggletip.constants';
import classNames from 'classnames';
import { v4 as uuidV4 } from 'uuid';
import ScreenReaderAnnouncer from '../ScreenReaderAnnouncer';
/**
* Toggletip component
*
* @see [WCAG - Tooltip pattern]{@link https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/}
*
* Toggletip content rendered in a interactive Popover.
*
* Consumer of the component need to take care of the label of the triggerComponent
*/
var Toggletip = forwardRef(function (_a, ref) {
var _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, className = _a.className, otherProps = __rest(_a, ["boundary", "color", "offsetDistance", "offsetSkidding", "placement", "strategy", "variant", "triggerComponent", "children", "className"]);
var tippyRef = useRef(null);
var triggerComponentRef = useRef(null);
var announcerId = useState(uuidV4)[0];
var _j = useState(false), isTooltipOpen = _j[0], setIsTooltipOpen = _j[1];
// SR should announce the content of the toggletip everytime it displays tooltip content
var handleShow = function () {
setIsTooltipOpen(true);
};
var handleHide = function () {
setIsTooltipOpen(false);
};
useEffect(function () {
if (isTooltipOpen) {
ScreenReaderAnnouncer.announce({ body: children }, announcerId);
}
}, [isTooltipOpen, children]);
// Update aria props manually, because "The `aria` attribute is reserved for future use in React."
// see https://atomiks.github.io/tippyjs/v6/all-props/#aria
var setInstance = useCallback(function (popoverInstance) {
var _a, _b;
(_a = popoverInstance === null || popoverInstance === void 0 ? void 0 : popoverInstance.setProps) === null || _a === void 0 ? void 0 : _a.call(popoverInstance, { aria: { expanded: 'auto', content: null } });
tippyRef.current = popoverInstance;
(_b = otherProps === null || otherProps === void 0 ? void 0 : otherProps.setInstance) === null || _b === void 0 ? void 0 : _b.call(otherProps, popoverInstance);
}, []);
// Hide popover on when the trigger component loose focus
var hidePopoverOnBlur = useCallback(function () {
var _a;
(_a = tippyRef.current) === null || _a === void 0 ? void 0 : _a.hide();
}, [tippyRef.current]);
useEffect(function () {
var triggerRef = triggerComponentRef.current;
if (triggerRef) {
triggerRef.addEventListener('blur', hidePopoverOnBlur);
return function () { return triggerRef.removeEventListener('blur', hidePopoverOnBlur); };
}
}, [triggerComponentRef.current]);
/**
* Toggletip's popover is interactive to make the content selectable and VoiceOver reads the whole content.
*/
return (React.createElement(React.Fragment, null,
React.createElement(Popover, __assign({ ref: ref, className: classNames(STYLE.wrapper, className), trigger: "click", triggerComponent: React.cloneElement(triggerComponent, { ref: triggerComponentRef }), showArrow: true, interactive: true, addBackdrop: true, role: "dialog", boundary: boundary, color: color, offsetDistance: offsetDistance, offsetSkidding: offsetSkidding, placement: placement, strategy: strategy, variant: variant }, otherProps, { onShow: handleShow, onHide: handleHide, setInstance: setInstance }), children),
React.createElement(ScreenReaderAnnouncer, { identity: announcerId })));
});
Toggletip.displayName = 'Toggletip';
export default Toggletip;
//# sourceMappingURL=Toggletip.js.map