UNPKG

motion-dom

Version:
1 lines • 5.27 kB
{"version":3,"file":"projection.mjs","sources":["../../../../src/gestures/drag/projection.ts"],"sourcesContent":["import { Box, noop, SubscriptionManager } from \"motion-utils\"\nimport { resolveVisualElement } from \"../../layout/visual-element\"\nimport { roundBox } from \"../../projection/geometry/round\"\nimport type { IProjectionNode } from \"../../projection/node/types\"\nimport {\n measureDocumentScroll,\n measurePageBox,\n} from \"../../projection/utils/measure\"\nimport { visualElementStore } from \"../../render/store\"\n\ntype CreateNode = (element: HTMLElement) => [IProjectionNode, VoidFunction]\n\n/**\n * animateLayout() registers a function here that creates nodes in the\n * layout projection tree. When it's in the bundle, drag() uses those, so\n * the two work together (e.g. a drag follows a layout change mid-gesture).\n * Otherwise drag() makes do with a node that only measures. Bundles that\n * don't tree-shake (dev pre-bundling, CDN builds) always register it, and\n * a drag() started before animateLayout() is lazy loaded keeps its node.\n */\nexport const layoutProjection: { create?: CreateNode } = {}\n\n/**\n * The document root of measurement-only nodes. Like the projection tree's\n * root, it keeps its scroll until drag controls clear it to re-measure.\n */\nconst root: any = {\n updateScroll: () => {\n root.scroll ||= { offset: measureDocumentScroll() }\n },\n}\n\n/**\n * The part of a projection node that drag controls use: measuring the\n * element's page box, for its layout and constraints. It never projects\n * or joins a layout update.\n */\nconst createMeasureNode: CreateNode = (element) => {\n const visualElement = resolveVisualElement(element)\n const measureListeners = new SubscriptionManager()\n\n const node: any = {\n instance: element,\n root,\n path: [root],\n setOptions: noop,\n /**\n * Truthy like the full node's method, which HTMLVisualElement reads\n * (rather than calls) to default transform values.\n */\n isProjecting: noop,\n applyProjectionStyles: noop,\n removeElementScroll: (box: Box) => box,\n updateLayout: () => {\n const layoutBox = measurePageBox(\n element,\n root,\n visualElement.getTransformPagePoint()\n )\n roundBox(layoutBox)\n node.layout = { layoutBox }\n measureListeners.notify()\n },\n addEventListener: (name: string, handler: VoidFunction) =>\n name === \"measure\" ? measureListeners.add(handler) : noop,\n /**\n * Detach from the VisualElement too, so it doesn't later remount\n * (or a `motion` component's layout feature adopt) a node that\n * can't do either.\n */\n unmount: () => {\n node.instance = undefined\n if (visualElement.projection === node) {\n visualElement.projection = undefined\n }\n },\n }\n\n visualElement.projection = node\n visualElement.current || visualElement.mount(element)\n\n return [node, node.unmount]\n}\n\n/**\n * Get a projection node for drag() to measure an element with, and a\n * function that releases it. Reuses a node a `motion` component or\n * animateLayout() owns.\n */\nexport const acquireDragNode: CreateNode = (element) => {\n const owned = visualElementStore.get(element)?.projection\n\n /**\n * A VisualElement can outlive its node (e.g. an unmounted `motion`\n * component's), so only reuse a node that's still mounted.\n */\n return owned?.instance\n ? [owned, noop as VoidFunction]\n : (layoutProjection.create || createMeasureNode)(element)\n}\n"],"names":[],"mappings":";;;;;;AAYA;;;;;;;AAOG;AACI,MAAM,gBAAgB,GAA4B;AAEzD;;;AAGG;AACH,MAAM,IAAI,GAAQ;IACd,YAAY,EAAE,MAAK;AACf,QAAA,IAAI,CAAC,MAAM,KAAX,IAAI,CAAC,MAAM,GAAK,EAAE,MAAM,EAAE,qBAAqB,EAAE,EAAE,CAAA;IACvD,CAAC;CACJ;AAED;;;;AAIG;AACH,MAAM,iBAAiB,GAAe,CAAC,OAAO,KAAI;AAC9C,IAAA,MAAM,aAAa,GAAG,oBAAoB,CAAC,OAAO,CAAC;AACnD,IAAA,MAAM,gBAAgB,GAAG,IAAI,mBAAmB,EAAE;AAElD,IAAA,MAAM,IAAI,GAAQ;AACd,QAAA,QAAQ,EAAE,OAAO;QACjB,IAAI;QACJ,IAAI,EAAE,CAAC,IAAI,CAAC;AACZ,QAAA,UAAU,EAAE,IAAI;AAChB;;;AAGG;AACH,QAAA,YAAY,EAAE,IAAI;AAClB,QAAA,qBAAqB,EAAE,IAAI;AAC3B,QAAA,mBAAmB,EAAE,CAAC,GAAQ,KAAK,GAAG;QACtC,YAAY,EAAE,MAAK;AACf,YAAA,MAAM,SAAS,GAAG,cAAc,CAC5B,OAAO,EACP,IAAI,EACJ,aAAa,CAAC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