UNPKG

6.99 kBSource Map (JSON)View Raw
1{"version":3,"names":["_tools","require","_react","_react2","_interopRequireWildcard","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","exitableByKey","children","map","Map","Children","forEach","child","_child$props","props","exit","isString","AnimatePresence","_ref","fr","useForceRender","childArr","toArray","childrenPrevious","usePrevious","childrenByKey","childrenByKeyPrevious","exiting","useRef","prevChild","current","childrenToRender","delete","index","indexOf","splice","createElement","Fragment","animKeys","keys","cloneElement","animate","onAnimationComplete","animKey","arrayRemove","length"],"sources":["AnimatePresence.tsx"],"sourcesContent":["import { arrayRemove, isString } from '@legendapp/tools';\nimport { useForceRender, usePrevious } from '@legendapp/tools/react';\nimport React, { Children, cloneElement, Key, ReactElement, ReactNode, useRef } from 'react';\n\ninterface Props {\n children: ReactNode;\n}\n\nfunction exitableByKey(children: ReactNode[]) {\n const map = new Map<Key, ReactElement>();\n Children.forEach(children, (child: ReactElement) => {\n if (child.key && child.props?.exit && isString(child.key)) {\n map.set(child.key, child);\n }\n });\n return map;\n}\n\nexport function AnimatePresence({ children }: Props) {\n const fr = useForceRender();\n const childArr = Children.toArray(children);\n const childrenPrevious = usePrevious(childArr);\n\n // Map children and previous children to { key: child }\n const childrenByKey = exitableByKey(childArr);\n const childrenByKeyPrevious = usePrevious(childrenByKey);\n\n // Add newly exited elements to the exiting map\n const exiting = useRef(new Map<Key, ReactElement>());\n if (childrenByKeyPrevious) {\n childrenByKeyPrevious.forEach((prevChild, key) => {\n if (!childrenByKey.get(key)) {\n exiting.current.set(key, prevChild);\n }\n });\n }\n\n // Render exiting elements into the position they were previously\n let childrenToRender = [...childArr];\n exiting.current.forEach((child, key) => {\n if (childrenByKey.get(key)) {\n exiting.current.delete(key);\n } else {\n const index = childrenPrevious.indexOf(child);\n childrenToRender.splice(index, 0, child);\n }\n });\n\n return (\n <>\n {childrenToRender.map((child: ReactElement) => {\n if (child && child.props.exit) {\n const key = child.key;\n const animKeys = Object.keys(child.props.exit);\n // Remove the child when all exit animations end\n return key && exiting.current.get(key) && animKeys\n ? cloneElement(child, {\n animate: child.props.exit,\n onAnimationComplete: (animKey) => {\n if (exiting.current.has(key)) {\n arrayRemove(animKeys, animKey);\n if (animKeys.length === 0) {\n exiting.current.delete(key);\n fr();\n }\n }\n },\n })\n : child;\n }\n return child;\n })}\n </>\n );\n}\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAC,uBAAA,CAAAH,OAAA;AAA4F,SAAAI,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAF,wBAAAM,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAG,KAAA,GAAAR,wBAAA,CAAAC,WAAA,OAAAO,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAJ,GAAA,YAAAG,KAAA,CAAAE,GAAA,CAAAL,GAAA,SAAAM,MAAA,WAAAC,qBAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,GAAA,IAAAX,GAAA,QAAAW,GAAA,kBAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAd,GAAA,EAAAW,GAAA,SAAAI,IAAA,GAAAR,qBAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAV,GAAA,EAAAW,GAAA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