index.js 5.1 KB

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