UNPKG

@kcirtaptrick/framer-motion

Version:

A simple and powerful React animation library

64 lines (61 loc) 2.63 kB
import { __values } from 'tslib'; import * as React from 'react'; import { useMemo } from 'react'; import { PresenceContext } from '../../context/PresenceContext.mjs'; import { useConstant } from '../../utils/use-constant.mjs'; import { useId } from '../../utils/use-id.mjs'; var PresenceChild = function (_a) { var children = _a.children, initial = _a.initial, isPresent = _a.isPresent, onExitComplete = _a.onExitComplete, custom = _a.custom, presenceAffectsLayout = _a.presenceAffectsLayout; var presenceChildren = useConstant(newChildrenMap); var id = useId(); var context = useMemo(function () { return ({ id: id, initial: initial, isPresent: isPresent, custom: custom, onExitComplete: function (childId) { var e_1, _a; presenceChildren.set(childId, true); try { for (var _b = __values(presenceChildren.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var isComplete = _c.value; if (!isComplete) return; // can stop searching when any is incomplete } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } onExitComplete === null || onExitComplete === void 0 ? void 0 : onExitComplete(); }, register: function (childId) { presenceChildren.set(childId, false); return function () { return presenceChildren.delete(childId); }; }, }); }, /** * If the presence of a child affects the layout of the components around it, * we want to make a new context value to ensure they get re-rendered * so they can detect that layout change. */ presenceAffectsLayout ? undefined : [isPresent]); useMemo(function () { presenceChildren.forEach(function (_, key) { return presenceChildren.set(key, false); }); }, [isPresent]); /** * If there's no `motion` components to fire exit animations, we want to remove this * component immediately. */ React.useEffect(function () { !isPresent && !presenceChildren.size && (onExitComplete === null || onExitComplete === void 0 ? void 0 : onExitComplete()); }, [isPresent]); return (React.createElement(PresenceContext.Provider, { value: context }, children)); }; function newChildrenMap() { return new Map(); } export { PresenceChild };