@atlaskit/icon
Version:
An icon is a symbol representing a command, device, directory, or common action.
125 lines (122 loc) • 4.62 kB
JavaScript
/* animated-icon-trigger.tsx generated by @compiled/babel-plugin v3.0.4 */
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "./animated-icon-trigger.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { AnimatedIconContext } from './animated-icon-context';
var wrapperStyles = null;
var isFocusVisible = function isFocusVisible(element) {
try {
return element.matches(':focus-visible');
} catch (_unused) {
// Environments without `:focus-visible` support: treat every focus as keyboard focus.
return true;
}
};
/**
* __Animated icon trigger__
*
* Tells animated icons inside it when to animate, without passing props or refs down.
* Becomes active on hover (and keyboard focus) or on click, depending on `trigger`.
*/
var AnimatedIconTrigger = function AnimatedIconTrigger(_ref) {
var children = _ref.children,
isActiveProp = _ref.isActive,
onReset = _ref.onReset,
testId = _ref.testId,
_ref$trigger = _ref.trigger,
trigger = _ref$trigger === void 0 ? 'hover' : _ref$trigger;
// Entering (hover) or clicking (click) activates it, and it clears once every icon that started has finished.
// In `hover` mode, leaving also clears it.
// One flag (rather than hover and focus separately) lets the motion end while still hovered.
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
isActiveState = _useState2[0],
setIsActiveState = _useState2[1];
var isControlled = isActiveProp !== undefined;
var isActive = isControlled ? isActiveProp : isActiveState;
// Keep the latest `onReset` in a ref so `start` (and the context value) stays stable when a consumer
// passes an inline function. Icons can then depend on `start` without restarting their motion.
var onResetRef = useRef(onReset);
useEffect(function () {
onResetRef.current = onReset;
}, [onReset]);
// One entry per icon motion that has started and not yet finished. Icons can have different durations,
// so the trigger only clears once the last of them is done, rather than when the first one finishes.
var pendingRef = useRef(new Set());
// Motions that were cancelled when the trigger went inactive never call `done`. Forget them so they
// can't keep the next activation from clearing, and so a late `done` from a motion that plays on
// after the trigger went inactive can't clear a newer activation.
useEffect(function () {
if (!isActive) {
pendingRef.current.clear();
}
}, [isActive]);
var start = useCallback(function () {
var token = Symbol('animated-icon');
pendingRef.current.add(token);
return {
done: function done() {
if (!pendingRef.current.delete(token) || pendingRef.current.size > 0) {
return;
}
if (isControlled) {
var _onResetRef$current;
(_onResetRef$current = onResetRef.current) === null || _onResetRef$current === void 0 || _onResetRef$current.call(onResetRef);
} else {
setIsActiveState(false);
}
},
cancel: function cancel() {
pendingRef.current.delete(token);
}
};
}, [isControlled]);
var value = useMemo(function () {
return {
isActive: isActive,
start: start
};
}, [isActive, start]);
if (isControlled) {
return /*#__PURE__*/React.createElement(AnimatedIconContext.Provider, {
value: value
}, children);
}
var handleFocus = function handleFocus(event) {
if (isFocusVisible(event.target)) {
setIsActiveState(true);
}
};
var handleBlur = function handleBlur(event) {
// Focus moving between children of the trigger should not deactivate it.
if (event.currentTarget.contains(event.relatedTarget)) {
return;
}
setIsActiveState(false);
};
return /*#__PURE__*/React.createElement(AnimatedIconContext.Provider, {
value: value
}, /*#__PURE__*/React.createElement("div", _extends({
role: "presentation",
"data-testid": testId
}, trigger === 'click' ? {
onClick: function onClick() {
return setIsActiveState(true);
}
} : {
onMouseEnter: function onMouseEnter() {
return setIsActiveState(true);
},
onMouseLeave: function onMouseLeave() {
return setIsActiveState(false);
},
onFocus: handleFocus,
onBlur: handleBlur
}, {
className: ax(["_1e0c1bgi"])
}), children));
};
export default AnimatedIconTrigger;