index.mjs 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import $9IrjX$babelruntimehelpersesmextends from "@babel/runtime/helpers/esm/extends";
  2. import {forwardRef as $9IrjX$forwardRef, Children as $9IrjX$Children, isValidElement as $9IrjX$isValidElement, createElement as $9IrjX$createElement, cloneElement as $9IrjX$cloneElement, Fragment as $9IrjX$Fragment} from "react";
  3. import {composeRefs as $9IrjX$composeRefs} from "@radix-ui/react-compose-refs";
  4. /* -------------------------------------------------------------------------------------------------
  5. * Slot
  6. * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$export$8c6ed5c666ac1360 = /*#__PURE__*/ $9IrjX$forwardRef((props, forwardedRef)=>{
  7. const { children: children , ...slotProps } = props;
  8. const childrenArray = $9IrjX$Children.toArray(children);
  9. const slottable = childrenArray.find($5e63c961fc1ce211$var$isSlottable);
  10. if (slottable) {
  11. // the new element to render is the one passed as a child of `Slottable`
  12. const newElement = slottable.props.children;
  13. const newChildren = childrenArray.map((child)=>{
  14. if (child === slottable) {
  15. // because the new element will be the one rendered, we are only interested
  16. // in grabbing its children (`newElement.props.children`)
  17. if ($9IrjX$Children.count(newElement) > 1) return $9IrjX$Children.only(null);
  18. return /*#__PURE__*/ $9IrjX$isValidElement(newElement) ? newElement.props.children : null;
  19. } else return child;
  20. });
  21. return /*#__PURE__*/ $9IrjX$createElement($5e63c961fc1ce211$var$SlotClone, $9IrjX$babelruntimehelpersesmextends({}, slotProps, {
  22. ref: forwardedRef
  23. }), /*#__PURE__*/ $9IrjX$isValidElement(newElement) ? /*#__PURE__*/ $9IrjX$cloneElement(newElement, undefined, newChildren) : null);
  24. }
  25. return /*#__PURE__*/ $9IrjX$createElement($5e63c961fc1ce211$var$SlotClone, $9IrjX$babelruntimehelpersesmextends({}, slotProps, {
  26. ref: forwardedRef
  27. }), children);
  28. });
  29. $5e63c961fc1ce211$export$8c6ed5c666ac1360.displayName = 'Slot';
  30. /* -------------------------------------------------------------------------------------------------
  31. * SlotClone
  32. * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$var$SlotClone = /*#__PURE__*/ $9IrjX$forwardRef((props, forwardedRef)=>{
  33. const { children: children , ...slotProps } = props;
  34. if (/*#__PURE__*/ $9IrjX$isValidElement(children)) return /*#__PURE__*/ $9IrjX$cloneElement(children, {
  35. ...$5e63c961fc1ce211$var$mergeProps(slotProps, children.props),
  36. ref: forwardedRef ? $9IrjX$composeRefs(forwardedRef, children.ref) : children.ref
  37. });
  38. return $9IrjX$Children.count(children) > 1 ? $9IrjX$Children.only(null) : null;
  39. });
  40. $5e63c961fc1ce211$var$SlotClone.displayName = 'SlotClone';
  41. /* -------------------------------------------------------------------------------------------------
  42. * Slottable
  43. * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$export$d9f1ccf0bdb05d45 = ({ children: children })=>{
  44. return /*#__PURE__*/ $9IrjX$createElement($9IrjX$Fragment, null, children);
  45. };
  46. /* ---------------------------------------------------------------------------------------------- */ function $5e63c961fc1ce211$var$isSlottable(child) {
  47. return /*#__PURE__*/ $9IrjX$isValidElement(child) && child.type === $5e63c961fc1ce211$export$d9f1ccf0bdb05d45;
  48. }
  49. function $5e63c961fc1ce211$var$mergeProps(slotProps, childProps) {
  50. // all child props should override
  51. const overrideProps = {
  52. ...childProps
  53. };
  54. for(const propName in childProps){
  55. const slotPropValue = slotProps[propName];
  56. const childPropValue = childProps[propName];
  57. const isHandler = /^on[A-Z]/.test(propName);
  58. if (isHandler) {
  59. // if the handler exists on both, we compose them
  60. if (slotPropValue && childPropValue) overrideProps[propName] = (...args)=>{
  61. childPropValue(...args);
  62. slotPropValue(...args);
  63. };
  64. else if (slotPropValue) overrideProps[propName] = slotPropValue;
  65. } else if (propName === 'style') overrideProps[propName] = {
  66. ...slotPropValue,
  67. ...childPropValue
  68. };
  69. else if (propName === 'className') overrideProps[propName] = [
  70. slotPropValue,
  71. childPropValue
  72. ].filter(Boolean).join(' ');
  73. }
  74. return {
  75. ...slotProps,
  76. ...overrideProps
  77. };
  78. }
  79. const $5e63c961fc1ce211$export$be92b6f5f03c0fe9 = $5e63c961fc1ce211$export$8c6ed5c666ac1360;
  80. export {$5e63c961fc1ce211$export$8c6ed5c666ac1360 as Slot, $5e63c961fc1ce211$export$d9f1ccf0bdb05d45 as Slottable, $5e63c961fc1ce211$export$be92b6f5f03c0fe9 as Root};
  81. //# sourceMappingURL=index.mjs.map