use-render.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. import { Fragment, useMemo, createElement } from 'react';
  2. import { useHTMLProps } from '../html/use-props.mjs';
  3. import { filterProps } from './utils/filter-props.mjs';
  4. import { isSVGComponent } from './utils/is-svg-component.mjs';
  5. import { useSVGProps } from '../svg/use-props.mjs';
  6. import { isMotionValue } from '../../value/utils/is-motion-value.mjs';
  7. function createUseRender(forwardMotionProps = false) {
  8. const useRender = (Component, props, ref, { latestValues }, isStatic) => {
  9. const useVisualProps = isSVGComponent(Component)
  10. ? useSVGProps
  11. : useHTMLProps;
  12. const visualProps = useVisualProps(props, latestValues, isStatic, Component);
  13. const filteredProps = filterProps(props, typeof Component === "string", forwardMotionProps);
  14. const elementProps = Component !== Fragment
  15. ? { ...filteredProps, ...visualProps, ref }
  16. : {};
  17. /**
  18. * If component has been handed a motion value as its child,
  19. * memoise its initial value and render that. Subsequent updates
  20. * will be handled by the onChange handler
  21. */
  22. const { children } = props;
  23. const renderedChildren = useMemo(() => (isMotionValue(children) ? children.get() : children), [children]);
  24. return createElement(Component, {
  25. ...elementProps,
  26. children: renderedChildren,
  27. });
  28. };
  29. return useRender;
  30. }
  31. export { createUseRender };