@radix-ui/react-slot 0.1.3-rc.2 → 0.1.3-rc.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"mappings":"A;AAOA,0BAAoB,SAAQ,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC;IAC3D,QAAQ,CAAC,EAAE,MAAM,SAAS,CAAC;CAC5B;AAED,OAAA,MAAM,mFAwBJ,CAAC;AA+BH,OAAA,MAAM;cAAuC,MAAM,SAAS;iBAE3D,CAAC;AAqCF,OAAA,MAAM,mFAAW,CAAC","sources":["./packages/react/slot/src/packages/react/slot/src/Slot.tsx","./packages/react/slot/src/packages/react/slot/src/index.ts"],"sourcesContent":[null,null],"names":[],"version":3,"file":"index.d.ts.map"}
1
+ {"mappings":";AAOA,0BAAoB,SAAQ,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC;IAC3D,QAAQ,CAAC,EAAE,MAAM,SAAS,CAAC;CAC5B;AAED,OAAA,MAAM,mFAoCJ,CAAC;AA+BH,OAAA,MAAM;cAAuC,MAAM,SAAS;iBAE3D,CAAC;AAqCF,OAAA,MAAM,mFAAW,CAAC","sources":["packages/react/slot/src/packages/react/slot/src/Slot.tsx","packages/react/slot/src/packages/react/slot/src/index.ts","packages/react/slot/src/index.ts"],"sourcesContent":[null,null,"export * from './Slot';\n"],"names":[],"version":3,"file":"index.d.ts.map"}
package/dist/index.js CHANGED
@@ -1,2 +1,113 @@
1
- var e,r,n,t=require("@radix-ui/react-compose-refs").composeRefs,l=(e={},r=require("react"),Object.keys(r).forEach((function(n){"default"!==n&&"__esModule"!==n&&Object.defineProperty(e,n,{enumerable:!0,get:function(){return r[n]}})})),e),o=(n=require("@babel/runtime/helpers/extends"))&&n.__esModule?n.default:n;const s=/*#__PURE__*/l.forwardRef(((e,r)=>{const{children:n,...t}=e;return l.Children.toArray(n).some(i)?/*#__PURE__*/l.createElement(l.Fragment,null,l.Children.map(n,(e=>i(e)?/*#__PURE__*/l.createElement(c,o({},t,{ref:r}),e.props.children):e))):/*#__PURE__*/l.createElement(c,o({},t,{ref:r}),n)}));exports.Slot=s,s.displayName="Slot";const c=/*#__PURE__*/l.forwardRef(((e,r)=>{const{children:n,...o}=e;/*#__PURE__*/return l.isValidElement(n)?/*#__PURE__*/l.cloneElement(n,{...u(o,n.props),ref:t(r,n.ref)}):l.Children.count(n)>1?l.Children.only(null):null}));c.displayName="SlotClone";const a=({children:e})=>/*#__PURE__*/l.createElement(l.Fragment,null,e);function i(e){/*#__PURE__*/return l.isValidElement(e)&&e.type===a}function u(e,r){const n={...r};for(const t in r){const l=e[t],o=r[t];/^on[A-Z]/.test(t)?n[t]=(...e)=>{null==o||o(...e),null==l||l(...e)}:"style"===t?n[t]={...l,...o}:"className"===t&&(n[t]=[l,o].filter(Boolean).join(" "))}return{...e,...n}}exports.Slottable=a;const d=s;exports.Root=d;
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
+
5
+ function $parcel$exportWildcard(dest, source) {
6
+ Object.keys(source).forEach(function(key) {
7
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
8
+ return;
9
+ }
10
+
11
+ Object.defineProperty(dest, key, {
12
+ enumerable: true,
13
+ get: function get() {
14
+ return source[key];
15
+ }
16
+ });
17
+ });
18
+
19
+ return dest;
20
+ }
21
+ function $parcel$interopDefault(a) {
22
+ return a && a.__esModule ? a.default : a;
23
+ }
24
+ function $parcel$export(e, n, v, s) {
25
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
26
+ }
27
+ var $82dc8d030dec7549$exports = {};
28
+
29
+ $parcel$export($82dc8d030dec7549$exports, "Slot", () => $82dc8d030dec7549$export$8c6ed5c666ac1360);
30
+ $parcel$export($82dc8d030dec7549$exports, "Slottable", () => $82dc8d030dec7549$export$d9f1ccf0bdb05d45);
31
+ $parcel$export($82dc8d030dec7549$exports, "Root", () => $82dc8d030dec7549$export$be92b6f5f03c0fe9);
32
+
33
+
34
+
35
+ /* -------------------------------------------------------------------------------------------------
36
+ * Slot
37
+ * -----------------------------------------------------------------------------------------------*/ const $82dc8d030dec7549$export$8c6ed5c666ac1360 = /*#__PURE__*/ $dAvBt$react.forwardRef((props, forwardedRef)=>{
38
+ const { children: children , ...slotProps } = props;
39
+ const childrenArray = $dAvBt$react.Children.toArray(children);
40
+ const slottable = childrenArray.find($82dc8d030dec7549$var$isSlottable);
41
+ if (slottable) {
42
+ // the new element to render is the one passed as a child of `Slottable`
43
+ const newElement = slottable.props.children;
44
+ const newChildren = childrenArray.map((child)=>{
45
+ if (child === slottable) {
46
+ // because the new element will be the one rendered, we are only interested
47
+ // in grabbing its children (`newElement.props.children`)
48
+ if ($dAvBt$react.Children.count(newElement) > 1) return $dAvBt$react.Children.only(null);
49
+ return /*#__PURE__*/ $dAvBt$react.isValidElement(newElement) ? newElement.props.children : null;
50
+ } else return child;
51
+ });
52
+ return /*#__PURE__*/ $dAvBt$react.createElement($82dc8d030dec7549$var$SlotClone, ($parcel$interopDefault($dAvBt$babelruntimehelpersextends))({}, slotProps, {
53
+ ref: forwardedRef
54
+ }), /*#__PURE__*/ $dAvBt$react.isValidElement(newElement) ? /*#__PURE__*/ $dAvBt$react.cloneElement(newElement, undefined, newChildren) : null);
55
+ }
56
+ return /*#__PURE__*/ $dAvBt$react.createElement($82dc8d030dec7549$var$SlotClone, ($parcel$interopDefault($dAvBt$babelruntimehelpersextends))({}, slotProps, {
57
+ ref: forwardedRef
58
+ }), children);
59
+ });
60
+ $82dc8d030dec7549$export$8c6ed5c666ac1360.displayName = 'Slot';
61
+ /* -------------------------------------------------------------------------------------------------
62
+ * SlotClone
63
+ * -----------------------------------------------------------------------------------------------*/ const $82dc8d030dec7549$var$SlotClone = /*#__PURE__*/ $dAvBt$react.forwardRef((props, forwardedRef)=>{
64
+ const { children: children , ...slotProps } = props;
65
+ if (/*#__PURE__*/ $dAvBt$react.isValidElement(children)) return /*#__PURE__*/ $dAvBt$react.cloneElement(children, {
66
+ ...$82dc8d030dec7549$var$mergeProps(slotProps, children.props),
67
+ ref: $dAvBt$radixuireactcomposerefs.composeRefs(forwardedRef, children.ref)
68
+ });
69
+ return $dAvBt$react.Children.count(children) > 1 ? $dAvBt$react.Children.only(null) : null;
70
+ });
71
+ $82dc8d030dec7549$var$SlotClone.displayName = 'SlotClone';
72
+ /* -------------------------------------------------------------------------------------------------
73
+ * Slottable
74
+ * -----------------------------------------------------------------------------------------------*/ const $82dc8d030dec7549$export$d9f1ccf0bdb05d45 = ({ children: children })=>{
75
+ return /*#__PURE__*/ $dAvBt$react.createElement($dAvBt$react.Fragment, null, children);
76
+ };
77
+ /* ---------------------------------------------------------------------------------------------- */ function $82dc8d030dec7549$var$isSlottable(child) {
78
+ return /*#__PURE__*/ $dAvBt$react.isValidElement(child) && child.type === $82dc8d030dec7549$export$d9f1ccf0bdb05d45;
79
+ }
80
+ function $82dc8d030dec7549$var$mergeProps(slotProps, childProps) {
81
+ // all child props should override
82
+ const overrideProps = {
83
+ ...childProps
84
+ };
85
+ for(const propName in childProps){
86
+ const slotPropValue = slotProps[propName];
87
+ const childPropValue = childProps[propName];
88
+ const isHandler = /^on[A-Z]/.test(propName); // if it's a handler, modify the override by composing the base handler
89
+ if (isHandler) overrideProps[propName] = (...args)=>{
90
+ childPropValue === null || childPropValue === void 0 || childPropValue(...args);
91
+ slotPropValue === null || slotPropValue === void 0 || slotPropValue(...args);
92
+ };
93
+ else if (propName === 'style') overrideProps[propName] = {
94
+ ...slotPropValue,
95
+ ...childPropValue
96
+ };
97
+ else if (propName === 'className') overrideProps[propName] = [
98
+ slotPropValue,
99
+ childPropValue
100
+ ].filter(Boolean).join(' ');
101
+ }
102
+ return {
103
+ ...slotProps,
104
+ ...overrideProps
105
+ };
106
+ }
107
+ const $82dc8d030dec7549$export$be92b6f5f03c0fe9 = $82dc8d030dec7549$export$8c6ed5c666ac1360;
108
+
109
+
110
+ $parcel$exportWildcard(module.exports, $82dc8d030dec7549$exports);
111
+
112
+
2
113
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"IAYgCA,EAAMC,EAZNC,2DAYAF,KAAMC,mBACpCE,OAAOC,KAAKH,GAAQI,SAAQ,SAASC,GACvB,YAARA,GAA6B,eAARA,GAIzBH,OAAOI,eAAeP,EAAMM,EAAK,CAC/BE,YAAY,EACZC,IAAK,WACH,OAAOR,EAAOK,SAKbN,MA1BuBE,8CAClBA,EAAEQ,WAAaR,EAAES,QAAUT,ECUzC,MAAMU,eAAOC,EAAMC,YAAmC,CAACC,EAAOC,KAC5D,MAAMC,SAAEA,KAAaC,GAAcH,EAEnC,OAAIF,EAAMM,SAASC,QAAQH,GAAUI,KAAKC,gBAEtCC,EAAAC,cAAAD,EAAAE,SAAA,KACGZ,EAAMM,SAASO,IAAIT,GAAWU,GACtBL,EAAYK,gBACjBJ,EAAAC,cAACI,EAADC,EAAA,GAAeX,EAAf,CAA0BY,IAAKd,IAC5BW,EAAMZ,MAAME,UAGfU,kBAQRJ,EAAAC,cAACI,EAADC,EAAA,GAAeX,EAAf,CAA0BY,IAAKd,IAC5BC,qBAKPL,EAAKmB,YAAc,OAUnB,MAAMC,eAAYnB,EAAMC,YAAgC,CAACC,EAAOC,KAC9D,MAAMC,SAAEA,KAAaC,GAAcH,eAEnC,OAAIF,EAAMoB,eAAehB,gBAChBJ,EAAMqB,aAAajB,EAAU,IAC/BkB,EAAWjB,EAAWD,EAASF,OAClCe,IAAKM,EAAYpB,EAAeC,EAAiBa,OAI9CjB,EAAMM,SAASkB,MAAMpB,GAAY,EAAIJ,EAAMM,SAASmB,KAAK,MAAQ,QAG1EN,EAAUD,YAAc,YAMxB,MAAMQ,EAAY,EAAGtB,SAAAA,kBACZM,EAAAC,cAAAD,EAAAE,SAAA,KAAGR,GAOZ,SAASK,EAAYK,gBACnB,OAAOd,EAAMoB,eAAeN,IAAUA,EAAMa,OAASD,EAGvD,SAASJ,EAAWjB,EAAqBuB,GAEvC,MAAMC,EAAgB,IAAKD,GAE3B,IAAK,MAAME,KAAYF,EAAY,CACjC,MAAMG,EAAgB1B,EAAUyB,GAC1BE,EAAiBJ,EAAWE,GAEhB,WAAWG,KAAKH,GAGhCD,EAAcC,GAAY,IAAII,KAC5BF,MAAAA,GAAAA,KAAoBE,GACpBH,MAAAA,GAAAA,KAAmBG,IAID,UAAbJ,EACPD,EAAcC,GAAY,IAAKC,KAAkBC,GAC3B,cAAbF,IACTD,EAAcC,GAAY,CAACC,EAAeC,GAAgBG,OAAOC,SAASC,KAAK,MAInF,MAAO,IAAKhC,KAAcwB,uBAG5B,MAAMS,EAAOvC","sources":["./node_modules/@parcel/scope-hoisting/lib/helpers.js","./packages/react/slot/src/Slot.tsx"],"sourcesContent":["function $parcel$interopDefault(a) {\n return a && a.__esModule ? a.default : a;\n}\n\nfunction $parcel$defineInteropFlag(a) {\n Object.defineProperty(a, '__esModule', {value: true});\n}\n\nfunction $parcel$reexport(e, n, v) {\n Object.defineProperty(e, n, {get: v, enumerable: true});\n}\n\nfunction $parcel$exportWildcard(dest, source) {\n Object.keys(source).forEach(function(key) {\n if (key === 'default' || key === '__esModule') {\n return;\n }\n\n Object.defineProperty(dest, key, {\n enumerable: true,\n get: function get() {\n return source[key];\n },\n });\n });\n\n return dest;\n}\n\nfunction $parcel$missingModule(name) {\n var err = new Error(\"Cannot find module '\" + name + \"'\");\n err.code = 'MODULE_NOT_FOUND';\n throw err;\n}\n\nvar $parcel$global =\n typeof globalThis !== 'undefined'\n ? globalThis\n : typeof self !== 'undefined'\n ? self\n : typeof window !== 'undefined'\n ? window\n : typeof global !== 'undefined'\n ? global\n : {};\n","import * as React from 'react';\nimport { composeRefs } from '@radix-ui/react-compose-refs';\n\n/* -------------------------------------------------------------------------------------------------\n * Slot\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotProps extends React.HTMLAttributes<HTMLElement> {\n children?: React.ReactNode;\n}\n\nconst Slot = React.forwardRef<HTMLElement, SlotProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n\n if (React.Children.toArray(children).some(isSlottable)) {\n return (\n <>\n {React.Children.map(children, (child) => {\n return isSlottable(child) ? (\n <SlotClone {...slotProps} ref={forwardedRef}>\n {child.props.children}\n </SlotClone>\n ) : (\n child\n );\n })}\n </>\n );\n }\n\n return (\n <SlotClone {...slotProps} ref={forwardedRef}>\n {children}\n </SlotClone>\n );\n});\n\nSlot.displayName = 'Slot';\n\n/* -------------------------------------------------------------------------------------------------\n * SlotClone\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotCloneProps {\n children: React.ReactNode;\n}\n\nconst SlotClone = React.forwardRef<any, SlotCloneProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n\n if (React.isValidElement(children)) {\n return React.cloneElement(children, {\n ...mergeProps(slotProps, children.props),\n ref: composeRefs(forwardedRef, (children as any).ref),\n });\n }\n\n return React.Children.count(children) > 1 ? React.Children.only(null) : null;\n});\n\nSlotClone.displayName = 'SlotClone';\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst Slottable = ({ children }: { children: React.ReactNode }) => {\n return <>{children}</>;\n};\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype AnyProps = Record<string, any>;\n\nfunction isSlottable(child: React.ReactNode): child is React.ReactElement {\n return React.isValidElement(child) && child.type === Slottable;\n}\n\nfunction mergeProps(slotProps: AnyProps, childProps: AnyProps) {\n // all child props should override\n const overrideProps = { ...childProps };\n\n for (const propName in childProps) {\n const slotPropValue = slotProps[propName];\n const childPropValue = childProps[propName];\n\n const isHandler = /^on[A-Z]/.test(propName);\n // if it's a handler, modify the override by composing the base handler\n if (isHandler) {\n overrideProps[propName] = (...args: unknown[]) => {\n childPropValue?.(...args);\n slotPropValue?.(...args);\n };\n }\n // if it's `style`, we merge them\n else if (propName === 'style') {\n overrideProps[propName] = { ...slotPropValue, ...childPropValue };\n } else if (propName === 'className') {\n overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(' ');\n }\n }\n\n return { ...slotProps, ...overrideProps };\n}\n\nconst Root = Slot;\n\nexport {\n Slot,\n Slottable,\n //\n Root,\n};\nexport type { SlotProps };\n"],"names":["dest","source","a","Object","keys","forEach","key","defineProperty","enumerable","get","__esModule","default","Slot","React","forwardRef","props","forwardedRef","children","slotProps","Children","toArray","some","isSlottable","_react","createElement","Fragment","map","child","$bd3c7a2329cce5affa12c89d44fd473$var$SlotClone","_babelRuntimeHelpersExtends","ref","displayName","SlotClone","isValidElement","cloneElement","mergeProps","composeRefs","count","only","Slottable","type","childProps","overrideProps","propName","slotPropValue","childPropValue","test","args","filter","Boolean","join","Root"],"version":3,"file":"index.js.map"}
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;A;;;ACGA;;oGAEA,CAMA,MAAME,yCAAI,GAAA,aAAGF,CAAAA,uBAAA,CAAyC,CAACI,KAAD,EAAQC,YAAR,GAAyB;IAC7E,MAAM,E,UAAEC,QAAF,CAAA,EAAY,GAAGC,SAAH,EAAZ,GAA6BH,KAAnC,AAAM;IACN,MAAMI,aAAa,GAAGR,qBAAA,CAAeU,OAAf,CAAuBJ,QAAvB,CAAtB,AAAA;IACA,MAAMK,SAAS,GAAGH,aAAa,CAACI,IAAd,CAAmBC,iCAAnB,CAAlB,AAAA;IAEA,IAAIF,SAAJ,EAAe;QACb,wEAAA;QACA,MAAMG,UAAU,GAAGH,SAAS,CAACP,KAAV,CAAgBE,QAAnC,AAAA;QAEA,MAAMS,WAAW,GAAGP,aAAa,CAACQ,GAAd,CAAmBC,CAAAA,KAAD,GAAW;YAC/C,IAAIA,KAAK,KAAKN,SAAd,EAAyB;gBACvB,2EAAA;gBACA,yDAAA;gBACA,IAAIX,qBAAA,CAAekB,KAAf,CAAqBJ,UAArB,CA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,CAACC,QAAD,CAAb,GAA0B;YAAE,GAAGC,aAAL;YAAoB,GAAGC,cAAH;SAA9C,CAA0B;aACrB,IAAIF,QAAQ,KAAK,WAAjB,EACLD,aAAa,CAACC,QAAD,CAAb,GAA0B;YAACC,aAAD;YAAgBC,cAAhB;SAAA,CAAgCI,MAAhC,CAAuCC,OAAvC,CAAA,CAAgDC,IAAhD,CAAqD,GAArD,CAA1B,CAAAT;KAEH;IAED,OAAO;QAAE,GAAGvB,SAAL;QAAgB,GAAGuB,aAAH;KAAvB,CAAO;CACR;AAED,MAAMU,yCAAI,GAAGtC,yCAAb,AAAA;;ADrHA","sources":["packages/react/slot/src/index.ts","packages/react/slot/src/Slot.tsx"],"sourcesContent":["export * from './Slot';\n","import * as React from 'react';\nimport { composeRefs } from '@radix-ui/react-compose-refs';\n\n/* -------------------------------------------------------------------------------------------------\n * Slot\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotProps extends React.HTMLAttributes<HTMLElement> {\n children?: React.ReactNode;\n}\n\nconst Slot = React.forwardRef<HTMLElement, SlotProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n const childrenArray = React.Children.toArray(children);\n const slottable = childrenArray.find(isSlottable);\n\n if (slottable) {\n // the new element to render is the one passed as a child of `Slottable`\n const newElement = slottable.props.children as React.ReactNode;\n\n const newChildren = childrenArray.map((child) => {\n if (child === slottable) {\n // because the new element will be the one rendered, we are only interested\n // in grabbing its children (`newElement.props.children`)\n if (React.Children.count(newElement) > 1) return React.Children.only(null);\n return React.isValidElement(newElement)\n ? (newElement.props.children as React.ReactNode)\n : null;\n } else {\n return child;\n }\n });\n\n return (\n <SlotClone {...slotProps} ref={forwardedRef}>\n {React.isValidElement(newElement)\n ? React.cloneElement(newElement, undefined, newChildren)\n : null}\n </SlotClone>\n );\n }\n\n return (\n <SlotClone {...slotProps} ref={forwardedRef}>\n {children}\n </SlotClone>\n );\n});\n\nSlot.displayName = 'Slot';\n\n/* -------------------------------------------------------------------------------------------------\n * SlotClone\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotCloneProps {\n children: React.ReactNode;\n}\n\nconst SlotClone = React.forwardRef<any, SlotCloneProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n\n if (React.isValidElement(children)) {\n return React.cloneElement(children, {\n ...mergeProps(slotProps, children.props),\n ref: composeRefs(forwardedRef, (children as any).ref),\n });\n }\n\n return React.Children.count(children) > 1 ? React.Children.only(null) : null;\n});\n\nSlotClone.displayName = 'SlotClone';\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst Slottable = ({ children }: { children: React.ReactNode }) => {\n return <>{children}</>;\n};\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype AnyProps = Record<string, any>;\n\nfunction isSlottable(child: React.ReactNode): child is React.ReactElement {\n return React.isValidElement(child) && child.type === Slottable;\n}\n\nfunction mergeProps(slotProps: AnyProps, childProps: AnyProps) {\n // all child props should override\n const overrideProps = { ...childProps };\n\n for (const propName in childProps) {\n const slotPropValue = slotProps[propName];\n const childPropValue = childProps[propName];\n\n const isHandler = /^on[A-Z]/.test(propName);\n // if it's a handler, modify the override by composing the base handler\n if (isHandler) {\n overrideProps[propName] = (...args: unknown[]) => {\n childPropValue?.(...args);\n slotPropValue?.(...args);\n };\n }\n // if it's `style`, we merge them\n else if (propName === 'style') {\n overrideProps[propName] = { ...slotPropValue, ...childPropValue };\n } else if (propName === 'className') {\n overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(' ');\n }\n }\n\n return { ...slotProps, ...overrideProps };\n}\n\nconst Root = Slot;\n\nexport {\n Slot,\n Slottable,\n //\n Root,\n};\nexport type { SlotProps };\n"],"names":["React","composeRefs","Slot","forwardRef","props","forwardedRef","children","slotProps","childrenArray","Children","toArray","slottable","find","isSlottable","newElement","newChildren","map","child","count","only","isValidElement","cloneElement","undefined","displayName","SlotClone","mergeProps","ref","Slottable","type","childProps","overrideProps","propName","slotPropValue","childPropValue","isHandler","test","args","filter","Boolean","join","Root"],"version":3,"file":"index.js.map"}
@@ -1,2 +1,94 @@
1
- import{composeRefs as e}from"@radix-ui/react-compose-refs";import*as t from"react";import r from"@babel/runtime/helpers/esm/extends";export const Slot=/*#__PURE__*/t.forwardRef(((e,o)=>{const{children:a,...s}=e;return t.Children.toArray(a).some(l)?/*#__PURE__*/t.createElement(t.Fragment,null,t.Children.map(a,(e=>l(e)?/*#__PURE__*/t.createElement(n,r({},s,{ref:o}),e.props.children):e))):/*#__PURE__*/t.createElement(n,r({},s,{ref:o}),a)}));Slot.displayName="Slot";const n=/*#__PURE__*/t.forwardRef(((r,n)=>{const{children:l,...a}=r;/*#__PURE__*/return t.isValidElement(l)?/*#__PURE__*/t.cloneElement(l,{...o(a,l.props),ref:e(n,l.ref)}):t.Children.count(l)>1?t.Children.only(null):null}));n.displayName="SlotClone";export const Slottable=({children:e})=>/*#__PURE__*/t.createElement(t.Fragment,null,e);function l(e){/*#__PURE__*/return t.isValidElement(e)&&e.type===Slottable}function o(e,t){const r={...t};for(const n in t){const l=e[n],o=t[n];/^on[A-Z]/.test(n)?r[n]=(...e)=>{null==o||o(...e),null==l||l(...e)}:"style"===n?r[n]={...l,...o}:"className"===n&&(r[n]=[l,o].filter(Boolean).join(" "))}return{...e,...r}}export const Root=Slot;
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
+ function $parcel$export(e, n, v, s) {
6
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
+ }
8
+ var $5e63c961fc1ce211$exports = {};
9
+
10
+ $parcel$export($5e63c961fc1ce211$exports, "Slot", () => $5e63c961fc1ce211$export$8c6ed5c666ac1360);
11
+ $parcel$export($5e63c961fc1ce211$exports, "Slottable", () => $5e63c961fc1ce211$export$d9f1ccf0bdb05d45);
12
+ $parcel$export($5e63c961fc1ce211$exports, "Root", () => $5e63c961fc1ce211$export$be92b6f5f03c0fe9);
13
+
14
+
15
+
16
+ /* -------------------------------------------------------------------------------------------------
17
+ * Slot
18
+ * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$export$8c6ed5c666ac1360 = /*#__PURE__*/ $9IrjX$forwardRef((props, forwardedRef)=>{
19
+ const { children: children , ...slotProps } = props;
20
+ const childrenArray = $9IrjX$Children.toArray(children);
21
+ const slottable = childrenArray.find($5e63c961fc1ce211$var$isSlottable);
22
+ if (slottable) {
23
+ // the new element to render is the one passed as a child of `Slottable`
24
+ const newElement = slottable.props.children;
25
+ const newChildren = childrenArray.map((child)=>{
26
+ if (child === slottable) {
27
+ // because the new element will be the one rendered, we are only interested
28
+ // in grabbing its children (`newElement.props.children`)
29
+ if ($9IrjX$Children.count(newElement) > 1) return $9IrjX$Children.only(null);
30
+ return /*#__PURE__*/ $9IrjX$isValidElement(newElement) ? newElement.props.children : null;
31
+ } else return child;
32
+ });
33
+ return /*#__PURE__*/ $9IrjX$createElement($5e63c961fc1ce211$var$SlotClone, $9IrjX$babelruntimehelpersesmextends({}, slotProps, {
34
+ ref: forwardedRef
35
+ }), /*#__PURE__*/ $9IrjX$isValidElement(newElement) ? /*#__PURE__*/ $9IrjX$cloneElement(newElement, undefined, newChildren) : null);
36
+ }
37
+ return /*#__PURE__*/ $9IrjX$createElement($5e63c961fc1ce211$var$SlotClone, $9IrjX$babelruntimehelpersesmextends({}, slotProps, {
38
+ ref: forwardedRef
39
+ }), children);
40
+ });
41
+ $5e63c961fc1ce211$export$8c6ed5c666ac1360.displayName = 'Slot';
42
+ /* -------------------------------------------------------------------------------------------------
43
+ * SlotClone
44
+ * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$var$SlotClone = /*#__PURE__*/ $9IrjX$forwardRef((props, forwardedRef)=>{
45
+ const { children: children , ...slotProps } = props;
46
+ if (/*#__PURE__*/ $9IrjX$isValidElement(children)) return /*#__PURE__*/ $9IrjX$cloneElement(children, {
47
+ ...$5e63c961fc1ce211$var$mergeProps(slotProps, children.props),
48
+ ref: $9IrjX$composeRefs(forwardedRef, children.ref)
49
+ });
50
+ return $9IrjX$Children.count(children) > 1 ? $9IrjX$Children.only(null) : null;
51
+ });
52
+ $5e63c961fc1ce211$var$SlotClone.displayName = 'SlotClone';
53
+ /* -------------------------------------------------------------------------------------------------
54
+ * Slottable
55
+ * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$export$d9f1ccf0bdb05d45 = ({ children: children })=>{
56
+ return /*#__PURE__*/ $9IrjX$createElement($9IrjX$Fragment, null, children);
57
+ };
58
+ /* ---------------------------------------------------------------------------------------------- */ function $5e63c961fc1ce211$var$isSlottable(child) {
59
+ return /*#__PURE__*/ $9IrjX$isValidElement(child) && child.type === $5e63c961fc1ce211$export$d9f1ccf0bdb05d45;
60
+ }
61
+ function $5e63c961fc1ce211$var$mergeProps(slotProps, childProps) {
62
+ // all child props should override
63
+ const overrideProps = {
64
+ ...childProps
65
+ };
66
+ for(const propName in childProps){
67
+ const slotPropValue = slotProps[propName];
68
+ const childPropValue = childProps[propName];
69
+ const isHandler = /^on[A-Z]/.test(propName); // if it's a handler, modify the override by composing the base handler
70
+ if (isHandler) overrideProps[propName] = (...args)=>{
71
+ childPropValue === null || childPropValue === void 0 || childPropValue(...args);
72
+ slotPropValue === null || slotPropValue === void 0 || slotPropValue(...args);
73
+ };
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 $5e63c961fc1ce211$export$be92b6f5f03c0fe9 = $5e63c961fc1ce211$export$8c6ed5c666ac1360;
89
+
90
+
91
+
92
+
93
+ export {$5e63c961fc1ce211$export$8c6ed5c666ac1360 as Slot, $5e63c961fc1ce211$export$d9f1ccf0bdb05d45 as Slottable, $5e63c961fc1ce211$export$be92b6f5f03c0fe9 as Root};
2
94
  //# sourceMappingURL=index.module.js.map
@@ -1 +1 @@
1
- {"mappings":"4IAWA,MAAMA,kBAAOC,EAAMC,YAAmC,CAACC,EAAOC,KAC5D,MAAMC,SAAEA,KAAaC,GAAcH,EAEnC,OAAIF,EAAMM,SAASC,QAAQH,GAAUI,KAAKC,gBAEtCC,EAAAC,cAAAD,EAAAE,SAAA,KACGZ,EAAMM,SAASO,IAAIT,GAAWU,GACtBL,EAAYK,gBACjBJ,EAAAC,cAACI,EAADC,EAAA,GAAeX,EAAf,CAA0BY,IAAKd,IAC5BW,EAAMZ,MAAME,UAGfU,kBAQRJ,EAAAC,cAACI,EAADC,EAAA,GAAeX,EAAf,CAA0BY,IAAKd,IAC5BC,MAKPL,KAAKmB,YAAc,OAUnB,MAAMC,eAAYnB,EAAMC,YAAgC,CAACC,EAAOC,KAC9D,MAAMC,SAAEA,KAAaC,GAAcH,eAEnC,OAAIF,EAAMoB,eAAehB,gBAChBJ,EAAMqB,aAAajB,EAAU,IAC/BkB,EAAWjB,EAAWD,EAASF,OAClCe,IAAKM,EAAYpB,EAAeC,EAAiBa,OAI9CjB,EAAMM,SAASkB,MAAMpB,GAAY,EAAIJ,EAAMM,SAASmB,KAAK,MAAQ,QAG1EN,EAAUD,YAAc,mBAMxB,MAAMQ,UAAY,EAAGtB,SAAAA,kBACZM,EAAAC,cAAAD,EAAAE,SAAA,KAAGR,GAOZ,SAASK,EAAYK,gBACnB,OAAOd,EAAMoB,eAAeN,IAAUA,EAAMa,OAASD,UAGvD,SAASJ,EAAWjB,EAAqBuB,GAEvC,MAAMC,EAAgB,IAAKD,GAE3B,IAAK,MAAME,KAAYF,EAAY,CACjC,MAAMG,EAAgB1B,EAAUyB,GAC1BE,EAAiBJ,EAAWE,GAEhB,WAAWG,KAAKH,GAGhCD,EAAcC,GAAY,IAAII,KAC5BF,MAAAA,GAAAA,KAAoBE,GACpBH,MAAAA,GAAAA,KAAmBG,IAID,UAAbJ,EACPD,EAAcC,GAAY,IAAKC,KAAkBC,GAC3B,cAAbF,IACTD,EAAcC,GAAY,CAACC,EAAeC,GAAgBG,OAAOC,SAASC,KAAK,MAInF,MAAO,IAAKhC,KAAcwB,UAG5B,MAAMS,KAAOvC","sources":["./packages/react/slot/src/Slot.tsx"],"sourcesContent":["import * as React from 'react';\nimport { composeRefs } from '@radix-ui/react-compose-refs';\n\n/* -------------------------------------------------------------------------------------------------\n * Slot\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotProps extends React.HTMLAttributes<HTMLElement> {\n children?: React.ReactNode;\n}\n\nconst Slot = React.forwardRef<HTMLElement, SlotProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n\n if (React.Children.toArray(children).some(isSlottable)) {\n return (\n <>\n {React.Children.map(children, (child) => {\n return isSlottable(child) ? (\n <SlotClone {...slotProps} ref={forwardedRef}>\n {child.props.children}\n </SlotClone>\n ) : (\n child\n );\n })}\n </>\n );\n }\n\n return (\n <SlotClone {...slotProps} ref={forwardedRef}>\n {children}\n </SlotClone>\n );\n});\n\nSlot.displayName = 'Slot';\n\n/* -------------------------------------------------------------------------------------------------\n * SlotClone\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotCloneProps {\n children: React.ReactNode;\n}\n\nconst SlotClone = React.forwardRef<any, SlotCloneProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n\n if (React.isValidElement(children)) {\n return React.cloneElement(children, {\n ...mergeProps(slotProps, children.props),\n ref: composeRefs(forwardedRef, (children as any).ref),\n });\n }\n\n return React.Children.count(children) > 1 ? React.Children.only(null) : null;\n});\n\nSlotClone.displayName = 'SlotClone';\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst Slottable = ({ children }: { children: React.ReactNode }) => {\n return <>{children}</>;\n};\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype AnyProps = Record<string, any>;\n\nfunction isSlottable(child: React.ReactNode): child is React.ReactElement {\n return React.isValidElement(child) && child.type === Slottable;\n}\n\nfunction mergeProps(slotProps: AnyProps, childProps: AnyProps) {\n // all child props should override\n const overrideProps = { ...childProps };\n\n for (const propName in childProps) {\n const slotPropValue = slotProps[propName];\n const childPropValue = childProps[propName];\n\n const isHandler = /^on[A-Z]/.test(propName);\n // if it's a handler, modify the override by composing the base handler\n if (isHandler) {\n overrideProps[propName] = (...args: unknown[]) => {\n childPropValue?.(...args);\n slotPropValue?.(...args);\n };\n }\n // if it's `style`, we merge them\n else if (propName === 'style') {\n overrideProps[propName] = { ...slotPropValue, ...childPropValue };\n } else if (propName === 'className') {\n overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(' ');\n }\n }\n\n return { ...slotProps, ...overrideProps };\n}\n\nconst Root = Slot;\n\nexport {\n Slot,\n Slottable,\n //\n Root,\n};\nexport type { SlotProps };\n"],"names":["Slot","React","forwardRef","props","forwardedRef","children","slotProps","Children","toArray","some","isSlottable","_react","createElement","Fragment","map","child","$d34e8c328cb3dd19ab7a2f6eafa6ad$var$SlotClone","_babelRuntimeHelpersEsmExtends","ref","displayName","SlotClone","isValidElement","cloneElement","mergeProps","composeRefs","count","only","Slottable","type","childProps","overrideProps","propName","slotPropValue","childPropValue","test","args","filter","Boolean","join","Root"],"version":3,"file":"index.module.js.map"}
1
+ {"mappings":";;;;;;;;;;;;A;;;ACGA;;oGAEA,CAMA,MAAME,yCAAI,GAAA,aAAGF,CAAAA,iBAAA,CAAyC,CAACI,KAAD,EAAQC,YAAR,GAAyB;IAC7E,MAAM,E,UAAEC,QAAF,CAAA,EAAY,GAAGC,SAAH,EAAZ,GAA6BH,KAAnC,AAAM;IACN,MAAM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AjC,AAAA;QAEA,MAAMG,SAAS,GAAG,WAAWC,IAAX,CAAgBJ,QAAhB,CAAlB,AAJiC,EAKjC,uEADA;QAEA,IAAIG,SAAJ,EACEJ,aAAa,CAACC,QAAD,CAAb,GAA0B,CAAIK,GAAAA,IAAJ,GAAwB;YAChDH,cAAc,KAAA,IAAd,IAAAA,cAAc,KAAA,KAAA,CAAd,IAAAA,cAAc,IAAMG,IAAN,CAAd,CAAAH;YACAD,aAAa,KAAA,IAAb,IAAAA,aAAa,KAAA,KAAA,CAAb,IAAAA,aAAa,IAAMI,IAAN,CAAb,CAAAJ;SAFF,CAGC;aAGE,IAAID,QAAQ,KAAK,OAAjB,EACHD,aAAa,CAACC,QAAD,CAAb,GAA0B;YAAE,GAAGC,aAAL;YAAoB,GAAGC,cAAH;SAA9C,CAA0B;aACrB,IAAIF,QAAQ,KAAK,WAAjB,EACLD,aAAa,CAACC,QAAD,CAAb,GAA0B;YAACC,aAAD;YAAgBC,cAAhB;SAAA,CAAgCI,MAAhC,CAAuCC,OAAvC,CAAA,CAAgDC,IAAhD,CAAqD,GAArD,CAA1B,CAAAT;KAEH;IAED,OAAO;QAAE,GAAGvB,SAAL;QAAgB,GAAGuB,aAAH;KAAvB,CAAO;CACR;AAED,MAAMU,yCAAI,GAAGtC,yCAAb,AAAA;;ADrHA","sources":["packages/react/slot/src/index.ts","packages/react/slot/src/Slot.tsx"],"sourcesContent":["export * from './Slot';\n","import * as React from 'react';\nimport { composeRefs } from '@radix-ui/react-compose-refs';\n\n/* -------------------------------------------------------------------------------------------------\n * Slot\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotProps extends React.HTMLAttributes<HTMLElement> {\n children?: React.ReactNode;\n}\n\nconst Slot = React.forwardRef<HTMLElement, SlotProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n const childrenArray = React.Children.toArray(children);\n const slottable = childrenArray.find(isSlottable);\n\n if (slottable) {\n // the new element to render is the one passed as a child of `Slottable`\n const newElement = slottable.props.children as React.ReactNode;\n\n const newChildren = childrenArray.map((child) => {\n if (child === slottable) {\n // because the new element will be the one rendered, we are only interested\n // in grabbing its children (`newElement.props.children`)\n if (React.Children.count(newElement) > 1) return React.Children.only(null);\n return React.isValidElement(newElement)\n ? (newElement.props.children as React.ReactNode)\n : null;\n } else {\n return child;\n }\n });\n\n return (\n <SlotClone {...slotProps} ref={forwardedRef}>\n {React.isValidElement(newElement)\n ? React.cloneElement(newElement, undefined, newChildren)\n : null}\n </SlotClone>\n );\n }\n\n return (\n <SlotClone {...slotProps} ref={forwardedRef}>\n {children}\n </SlotClone>\n );\n});\n\nSlot.displayName = 'Slot';\n\n/* -------------------------------------------------------------------------------------------------\n * SlotClone\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotCloneProps {\n children: React.ReactNode;\n}\n\nconst SlotClone = React.forwardRef<any, SlotCloneProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n\n if (React.isValidElement(children)) {\n return React.cloneElement(children, {\n ...mergeProps(slotProps, children.props),\n ref: composeRefs(forwardedRef, (children as any).ref),\n });\n }\n\n return React.Children.count(children) > 1 ? React.Children.only(null) : null;\n});\n\nSlotClone.displayName = 'SlotClone';\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst Slottable = ({ children }: { children: React.ReactNode }) => {\n return <>{children}</>;\n};\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype AnyProps = Record<string, any>;\n\nfunction isSlottable(child: React.ReactNode): child is React.ReactElement {\n return React.isValidElement(child) && child.type === Slottable;\n}\n\nfunction mergeProps(slotProps: AnyProps, childProps: AnyProps) {\n // all child props should override\n const overrideProps = { ...childProps };\n\n for (const propName in childProps) {\n const slotPropValue = slotProps[propName];\n const childPropValue = childProps[propName];\n\n const isHandler = /^on[A-Z]/.test(propName);\n // if it's a handler, modify the override by composing the base handler\n if (isHandler) {\n overrideProps[propName] = (...args: unknown[]) => {\n childPropValue?.(...args);\n slotPropValue?.(...args);\n };\n }\n // if it's `style`, we merge them\n else if (propName === 'style') {\n overrideProps[propName] = { ...slotPropValue, ...childPropValue };\n } else if (propName === 'className') {\n overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(' ');\n }\n }\n\n return { ...slotProps, ...overrideProps };\n}\n\nconst Root = Slot;\n\nexport {\n Slot,\n Slottable,\n //\n Root,\n};\nexport type { SlotProps };\n"],"names":["React","composeRefs","Slot","forwardRef","props","forwardedRef","children","slotProps","childrenArray","Children","toArray","slottable","find","isSlottable","newElement","newChildren","map","child","count","only","isValidElement","cloneElement","undefined","displayName","SlotClone","mergeProps","ref","Slottable","type","childProps","overrideProps","propName","slotPropValue","childPropValue","isHandler","test","args","filter","Boolean","join","Root"],"version":3,"file":"index.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radix-ui/react-slot",
3
- "version": "0.1.3-rc.2",
3
+ "version": "0.1.3-rc.22",
4
4
  "license": "MIT",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@babel/runtime": "^7.13.10",
20
- "@radix-ui/react-compose-refs": "0.1.1-rc.2"
20
+ "@radix-ui/react-compose-refs": "0.1.1-rc.22"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "react": "^16.8 || ^17.0 || ^18.0"