UNPKG

@atlaskit/icon

Version:

An icon is a symbol representing a command, device, directory, or common action.

113 lines (110 loc) • 4.05 kB
/* animated-icon-trigger.tsx generated by @compiled/babel-plugin v3.0.4 */ import _extends from "@babel/runtime/helpers/extends"; 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'; const wrapperStyles = null; const isFocusVisible = element => { try { return element.matches(':focus-visible'); } catch { // 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`. */ const AnimatedIconTrigger = ({ children, isActive: isActiveProp, onReset, testId, trigger = 'hover' }) => { // 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. const [isActiveState, setIsActiveState] = useState(false); const isControlled = isActiveProp !== undefined; const 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. const onResetRef = useRef(onReset); useEffect(() => { 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. const 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(() => { if (!isActive) { pendingRef.current.clear(); } }, [isActive]); const start = useCallback(() => { const token = Symbol('animated-icon'); pendingRef.current.add(token); return { 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 ? void 0 : _onResetRef$current.call(onResetRef); } else { setIsActiveState(false); } }, cancel: () => { pendingRef.current.delete(token); } }; }, [isControlled]); const value = useMemo(() => ({ isActive, start }), [isActive, start]); if (isControlled) { return /*#__PURE__*/React.createElement(AnimatedIconContext.Provider, { value: value }, children); } const handleFocus = event => { if (isFocusVisible(event.target)) { setIsActiveState(true); } }; const 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: () => setIsActiveState(true) } : { onMouseEnter: () => setIsActiveState(true), onMouseLeave: () => setIsActiveState(false), onFocus: handleFocus, onBlur: handleBlur }, { className: ax(["_1e0c1bgi"]) }), children)); }; export default AnimatedIconTrigger;