create-proxy.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { warnOnce } from 'motion-utils';
  2. function createDOMMotionComponentProxy(componentFactory) {
  3. if (typeof Proxy === "undefined") {
  4. return componentFactory;
  5. }
  6. /**
  7. * A cache of generated `motion` components, e.g `motion.div`, `motion.input` etc.
  8. * Rather than generating them anew every render.
  9. */
  10. const componentCache = new Map();
  11. const deprecatedFactoryFunction = (...args) => {
  12. if (process.env.NODE_ENV !== "production") {
  13. warnOnce(false, "motion() is deprecated. Use motion.create() instead.");
  14. }
  15. return componentFactory(...args);
  16. };
  17. return new Proxy(deprecatedFactoryFunction, {
  18. /**
  19. * Called when `motion` is referenced with a prop: `motion.div`, `motion.input` etc.
  20. * The prop name is passed through as `key` and we can use that to generate a `motion`
  21. * DOM component with that name.
  22. */
  23. get: (_target, key) => {
  24. if (key === "create")
  25. return componentFactory;
  26. /**
  27. * If this element doesn't exist in the component cache, create it and cache.
  28. */
  29. if (!componentCache.has(key)) {
  30. componentCache.set(key, componentFactory(key));
  31. }
  32. return componentCache.get(key);
  33. },
  34. });
  35. }
  36. export { createDOMMotionComponentProxy };