127 lines
4.6 KiB
JavaScript
127 lines
4.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
Object.defineProperty(exports, "useAnimateAtoms", {
|
|
enumerable: true,
|
|
get: function() {
|
|
return useAnimateAtoms;
|
|
}
|
|
});
|
|
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
|
|
function useAnimateAtomsInSupportedEnvironment() {
|
|
return _react.useCallback((element, value, options)=>{
|
|
const atoms = Array.isArray(value) ? value : [
|
|
value
|
|
];
|
|
const { isReducedMotion } = options;
|
|
const animations = atoms.map((motion)=>{
|
|
const { keyframes, ...params } = motion;
|
|
const animation = element.animate(keyframes, {
|
|
fill: 'forwards',
|
|
...params,
|
|
...isReducedMotion && {
|
|
duration: 1
|
|
}
|
|
});
|
|
animation.persist();
|
|
return animation;
|
|
});
|
|
return {
|
|
set playbackRate (rate){
|
|
animations.forEach((animation)=>{
|
|
animation.playbackRate = rate;
|
|
});
|
|
},
|
|
setMotionEndCallbacks (onfinish, oncancel) {
|
|
Promise.all(animations.map((animation)=>animation.finished)).then(()=>{
|
|
onfinish();
|
|
}).catch((err)=>{
|
|
var _element_ownerDocument_defaultView;
|
|
const DOMException = (_element_ownerDocument_defaultView = element.ownerDocument.defaultView) === null || _element_ownerDocument_defaultView === void 0 ? void 0 : _element_ownerDocument_defaultView.DOMException;
|
|
// Ignores "DOMException: The user aborted a request" that appears if animations are cancelled
|
|
if (DOMException && err instanceof DOMException && err.name === 'AbortError') {
|
|
oncancel();
|
|
return;
|
|
}
|
|
throw err;
|
|
});
|
|
},
|
|
cancel: ()=>{
|
|
animations.forEach((animation)=>{
|
|
animation.cancel();
|
|
});
|
|
},
|
|
pause: ()=>{
|
|
animations.forEach((animation)=>{
|
|
animation.pause();
|
|
});
|
|
},
|
|
play: ()=>{
|
|
animations.forEach((animation)=>{
|
|
animation.play();
|
|
});
|
|
},
|
|
finish: ()=>{
|
|
animations.forEach((animation)=>{
|
|
animation.finish();
|
|
});
|
|
}
|
|
};
|
|
}, []);
|
|
}
|
|
/**
|
|
* In test environments, this hook is used to delay the execution of a callback until the next render. This is necessary
|
|
* to ensure that the callback is not executed synchronously, which would cause the test to fail.
|
|
*
|
|
* @see https://github.com/microsoft/fluentui/issues/31701
|
|
*/ function useAnimateAtomsInTestEnvironment() {
|
|
const [count, setCount] = _react.useState(0);
|
|
const callbackRef = _react.useRef();
|
|
const realAnimateAtoms = useAnimateAtomsInSupportedEnvironment();
|
|
_react.useEffect(()=>{
|
|
if (count > 0) {
|
|
var _callbackRef_current;
|
|
(_callbackRef_current = callbackRef.current) === null || _callbackRef_current === void 0 ? void 0 : _callbackRef_current.call(callbackRef);
|
|
}
|
|
}, [
|
|
count
|
|
]);
|
|
return _react.useCallback((element, value, options)=>{
|
|
const ELEMENT_SUPPORTS_WEB_ANIMATIONS = typeof element.animate === 'function';
|
|
// Heads up!
|
|
// If the environment supports Web Animations API, we can use the native implementation.
|
|
if (ELEMENT_SUPPORTS_WEB_ANIMATIONS) {
|
|
return realAnimateAtoms(element, value, options);
|
|
}
|
|
return {
|
|
setMotionEndCallbacks (onfinish) {
|
|
callbackRef.current = onfinish;
|
|
setCount((v)=>v + 1);
|
|
},
|
|
set playbackRate (rate){
|
|
/* no-op */ },
|
|
cancel () {
|
|
/* no-op */ },
|
|
pause () {
|
|
/* no-op */ },
|
|
play () {
|
|
/* no-op */ },
|
|
finish () {
|
|
/* no-op */ }
|
|
};
|
|
}, [
|
|
realAnimateAtoms
|
|
]);
|
|
}
|
|
function useAnimateAtoms() {
|
|
'use no memo';
|
|
if (process.env.NODE_ENV === 'test') {
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
return useAnimateAtomsInTestEnvironment();
|
|
}
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
return useAnimateAtomsInSupportedEnvironment();
|
|
}
|