{"version":3,"file":"use-moving-animation.mjs","names":[],"sources":["../../../src/components/media-edit/use-moving-animation.ts"],"sourcesContent":["import { Controller } from '@react-spring/web';\nimport { useLayoutEffect, useRef } from '@wordpress/element';\n\nfunction getAbsolutePosition( element: HTMLElement ) {\n\treturn {\n\t\ttop: element.offsetTop,\n\t\tleft: element.offsetLeft,\n\t};\n}\n\n/**\n * Hook used to compute the styles required to move a div into a new position.\n *\n * The way this animation works is the following:\n *  - It first renders the element as if there was no animation.\n *  - It takes a snapshot of the position of the element to use it\n *    as a destination point for the animation.\n *  - It restores the element to the previous position using a CSS transform\n *\n * Only animates on reorder of same items.\n * Cancels running animations when the set of items changes (e.g. an item is added or removed).\n *\n * @param index    Position of the element in the list; the animation runs when it changes.\n * @param itemsKey Identifies the set of items in the list, regardless of order.\n */\nexport default function useMovingAnimation( index: number, itemsKey: string ) {\n\tconst ref = useRef< HTMLDivElement >( null );\n\tconst previousRef = useRef< { top: number; left: number } >( undefined );\n\tconst previousItemsKeyRef = useRef( itemsKey );\n\t// Capture position before DOM updates (during render).\n\t// This runs synchronously during render, before layout effects.\n\tif ( ref.current ) {\n\t\tpreviousRef.current = getAbsolutePosition( ref.current );\n\t}\n\tuseLayoutEffect( () => {\n\t\tconst hasItemsChanged = previousItemsKeyRef.current !== itemsKey;\n\t\tpreviousItemsKeyRef.current = itemsKey;\n\t\tconst previous = previousRef.current;\n\t\tif ( ! previous || ! ref.current ) {\n\t\t\treturn;\n\t\t}\n\t\tif ( hasItemsChanged ) {\n\t\t\tref.current.style.transform = '';\n\t\t\treturn;\n\t\t}\n\t\tif ( window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches ) {\n\t\t\treturn;\n\t\t}\n\t\tconst controller = new Controller( {\n\t\t\tx: 0,\n\t\t\ty: 0,\n\t\t\tconfig: { mass: 5, tension: 2000, friction: 200 },\n\t\t\tonChange( { value } ) {\n\t\t\t\tif ( ! ref.current ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlet { x, y } = value;\n\t\t\t\tx = Math.round( x );\n\t\t\t\ty = Math.round( y );\n\t\t\t\tconst finishedMoving = x === 0 && y === 0;\n\t\t\t\tref.current.style.transform = finishedMoving\n\t\t\t\t\t? ''\n\t\t\t\t\t: `translate3d(${ x }px,${ y }px,0)`;\n\t\t\t},\n\t\t} );\n\t\tref.current.style.transform = '';\n\t\tconst destination = getAbsolutePosition( ref.current );\n\t\tconst x = Math.round( previous.left - destination.left );\n\t\tconst y = Math.round( previous.top - destination.top );\n\t\tcontroller.start( { x: 0, y: 0, from: { x, y } } );\n\t\treturn () => {\n\t\t\tcontroller.stop();\n\t\t\tcontroller.set( { x: 0, y: 0 } );\n\t\t};\n\t}, [ index, itemsKey ] );\n\treturn ref;\n}\n"],"mappings":";;;AAGA,SAAS,oBAAqB,SAAuB;CACpD,OAAO;EACN,KAAK,QAAQ;EACb,MAAM,QAAQ;CACf;AACD;;;;;;;;;;;;;;;;AAiBA,SAAwB,mBAAoB,OAAe,UAAmB;CAC7E,MAAM,MAAM,OAA0B,IAAK;CAC3C,MAAM,cAAc,OAAyC,KAAA,CAAU;CACvE,MAAM,sBAAsB,OAAQ,QAAS;CAG7C,IAAK,IAAI,SACR,YAAY,UAAU,oBAAqB,IAAI,OAAQ;CAExD,sBAAuB;EACtB,MAAM,kBAAkB,oBAAoB,YAAY;EACxD,oBAAoB,UAAU;EAC9B,MAAM,WAAW,YAAY;EAC7B,IAAK,CAAE,YAAY,CAAE,IAAI,SACxB;EAED,IAAK,iBAAkB;GACtB,IAAI,QAAQ,MAAM,YAAY;GAC9B;EACD;EACA,IAAK,OAAO,WAAY,kCAAmC,CAAC,CAAC,SAC5D;EAED,MAAM,aAAa,IAAI,WAAY;GAClC,GAAG;GACH,GAAG;GACH,QAAQ;IAAE,MAAM;IAAG,SAAS;IAAM,UAAU;GAAI;GAChD,SAAU,EAAE,SAAU;IACrB,IAAK,CAAE,IAAI,SACV;IAED,IAAI,EAAE,GAAG,MAAM;IACf,IAA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