framer-motion
Version:
A simple and powerful JavaScript animation library
1 lines • 4.14 kB
Source Map (JSON)
{"version":3,"file":"create-proxy.mjs","sources":["../../../../src/render/components/create-proxy.ts"],"sourcesContent":["import { warnOnce } from \"motion-utils\"\nimport { createMotionComponent, MotionComponentOptions } from \"../../motion\"\nimport { FeaturePackages } from \"../../motion/features/types\"\nimport { MotionProps } from \"../../motion/types\"\nimport { DOMMotionComponents } from \"../dom/types\"\nimport { CreateVisualElement } from \"../types\"\n\n/**\n * I'd rather the return type of `custom` to be implicit but this throws\n * incorrect relative paths in the exported types and API Extractor throws\n * a wobbly.\n */\ntype ComponentProps<Props> = React.PropsWithoutRef<Props & MotionProps> &\n React.RefAttributes<SVGElement | HTMLElement>\nexport type CustomDomComponent<Props> = React.ComponentType<\n ComponentProps<Props>\n>\n\ntype MotionProxy = typeof createMotionComponent &\n DOMMotionComponents & { create: typeof createMotionComponent }\n\nexport function createMotionProxy(\n preloadedFeatures?: FeaturePackages,\n createVisualElement?: CreateVisualElement\n): MotionProxy {\n if (typeof Proxy === \"undefined\") {\n return createMotionComponent as MotionProxy\n }\n\n /**\n * A cache of generated `motion` components, e.g `motion.div`, `motion.input` etc.\n * Rather than generating them anew every render.\n */\n const componentCache = new Map<string, any>()\n\n const factory = (Component: string, options?: MotionComponentOptions) => {\n return createMotionComponent(\n Component,\n options,\n preloadedFeatures,\n createVisualElement\n )\n }\n\n /**\n * Support for deprecated`motion(Component)` pattern\n */\n const deprecatedFactoryFunction = (\n Component: string,\n options?: MotionComponentOptions\n ) => {\n if (process.env.NODE_ENV !== \"production\") {\n warnOnce(\n false,\n \"motion() is deprecated. Use motion.create() instead.\"\n )\n }\n return factory(Component, options)\n }\n\n return new Proxy(deprecatedFactoryFunction, {\n /**\n * Called when `motion` is referenced with a prop: `motion.div`, `motion.input` etc.\n * The prop name is passed through as `key` and we can use that to generate a `motion`\n * DOM component with that name.\n */\n get: (_target, key: string) => {\n if (key === \"create\") return factory\n\n /**\n * If this element doesn't exist in the component cache, create it and cache.\n */\n if (!componentCache.has(key)) {\n componentCache.set(\n key,\n createMotionComponent(\n key,\n undefined,\n preloadedFeatures,\n createVisualElement\n )\n )\n }\n\n return componentCache.get(key)!\n },\n }) as MotionProxy\n}\n"],"names":[],"mappings":";;;AAqBgB,SAAA,iBAAiB,CAC7B,iBAAmC,EACnC,mBAAyC,EAAA;AAEzC,IAAA,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE;AAC9B,QAAA,OAAO,qBAAoC,CAAA;KAC9C;AAED;;;AAGG;AACH,IAAA,MAAM,cAAc,GAAG,IAAI,GAAG,EAAe,CAAA;AAE7C,IAAA,MAAM,OAAO,GAAG,CAAC,SAAiB,EAAE,OAAgC,KAAI;QACpE,OAAO,qBAAqB,CACxB,SAAS,EACT,OAAO,EACP,iBAAiB,EACjB,mBAAmB,CACtB,CAAA;AACL,KAAC,CAAA;AAED;;AAEG;AACH,IAAA,MAAM,yBAAyB,GAAG,CAC9B,SAAiB,EACjB,OAAgC,KAChC;QACA,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;AACvC,YAAA,QAAQ,CACJ,KAAK,EACL,sDAAsD,CACzD,CAAA;SACJ;AACD,QAAA,OAAO,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;AACtC,KAAC,CAAA;AAED,IAAA,OAAO,IAAI,KAAK,CAAC,yBAAyB,EAAE;AACxC;;;;AAIG;AACH,QAAA,GAAG,EAAE,CAAC,OAAO,EAAE,GAAW,KAAI;YAC1B,IAAI,GAAG,KAAK,QAAQ;AAAE,gBAAA,OAAO,OAAO,CAAA;AAEpC;;AAEG;YACH,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;AAC1B,gBAAA,cAAc,CAAC,GAAG,CACd,GAAG,EACH,qBAAqB,CACjB,GAAG,EACH,SAAS,EACT,iBAAiB,EACjB,mBAAmB,CACtB,CACJ,CAAA;aACJ;AAED,YAAA,OAAO,cAAc,CAAC,GAAG,CAAC,GAAG,CAAE,CAAA;SAClC;AACJ,KAAA,CAAgB,CAAA;AACrB;;;;"}