@radix-ui/react-slot 1.0.2 → 1.1.0-rc.2

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.
package/dist/index.d.mts CHANGED
@@ -1,11 +1,12 @@
1
- import * as React from "react";
2
- export interface SlotProps extends React.HTMLAttributes<HTMLElement> {
1
+ import * as React from 'react';
2
+
3
+ interface SlotProps extends React.HTMLAttributes<HTMLElement> {
3
4
  children?: React.ReactNode;
4
5
  }
5
- export const Slot: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
6
- export const Slottable: ({ children }: {
6
+ declare const Slot: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
7
+ declare const Slottable: ({ children }: {
7
8
  children: React.ReactNode;
8
9
  }) => JSX.Element;
9
- export const Root: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
10
+ declare const Root: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
10
11
 
11
- //# sourceMappingURL=index.d.ts.map
12
+ export { Root, Slot, type SlotProps, Slottable };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import * as React from "react";
2
- export interface SlotProps extends React.HTMLAttributes<HTMLElement> {
1
+ import * as React from 'react';
2
+
3
+ interface SlotProps extends React.HTMLAttributes<HTMLElement> {
3
4
  children?: React.ReactNode;
4
5
  }
5
- export const Slot: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
6
- export const Slottable: ({ children }: {
6
+ declare const Slot: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
7
+ declare const Slottable: ({ children }: {
7
8
  children: React.ReactNode;
8
9
  }) => JSX.Element;
9
- export const Root: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
10
+ declare const Root: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
10
11
 
11
- //# sourceMappingURL=index.d.ts.map
12
+ export { Root, Slot, type SlotProps, Slottable };
package/dist/index.js CHANGED
@@ -1,98 +1,97 @@
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$export(e, n, v, s) {
6
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
- }
8
- function $parcel$interopDefault(a) {
9
- return a && a.__esModule ? a.default : a;
10
- }
11
-
12
- $parcel$export(module.exports, "Slot", () => $82dc8d030dec7549$export$8c6ed5c666ac1360);
13
- $parcel$export(module.exports, "Slottable", () => $82dc8d030dec7549$export$d9f1ccf0bdb05d45);
14
- $parcel$export(module.exports, "Root", () => $82dc8d030dec7549$export$be92b6f5f03c0fe9);
15
-
16
-
1
+ "use strict";
2
+ (() => {
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
10
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
11
+ }) : x)(function(x) {
12
+ if (typeof require !== "undefined") return require.apply(this, arguments);
13
+ throw Error('Dynamic require of "' + x + '" is not supported');
14
+ });
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from))
18
+ if (!__hasOwnProp.call(to, key) && key !== except)
19
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
+ // If the importer is in node compatibility mode or this is not an ESM
25
+ // file that has been converted to a CommonJS file using a Babel-
26
+ // compatible transform (i.e. "__esModule" has not been set), then set
27
+ // "default" to the CommonJS "module.exports" for node compatibility.
28
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
+ mod
30
+ ));
17
31
 
18
- /* -------------------------------------------------------------------------------------------------
19
- * Slot
20
- * -----------------------------------------------------------------------------------------------*/ const $82dc8d030dec7549$export$8c6ed5c666ac1360 = /*#__PURE__*/ $dAvBt$react.forwardRef((props, forwardedRef)=>{
21
- const { children: children , ...slotProps } = props;
22
- const childrenArray = $dAvBt$react.Children.toArray(children);
23
- const slottable = childrenArray.find($82dc8d030dec7549$var$isSlottable);
32
+ // packages/react/slot/src/Slot.tsx
33
+ var React = __toESM(__require("react"));
34
+ var import_react_compose_refs = __require("@radix-ui/react-compose-refs");
35
+ var import_jsx_runtime = __require("react/jsx-runtime");
36
+ var Slot = React.forwardRef((props, forwardedRef) => {
37
+ const { children, ...slotProps } = props;
38
+ const childrenArray = React.Children.toArray(children);
39
+ const slottable = childrenArray.find(isSlottable);
24
40
  if (slottable) {
25
- // the new element to render is the one passed as a child of `Slottable`
26
- const newElement = slottable.props.children;
27
- const newChildren = childrenArray.map((child)=>{
28
- if (child === slottable) {
29
- // because the new element will be the one rendered, we are only interested
30
- // in grabbing its children (`newElement.props.children`)
31
- if ($dAvBt$react.Children.count(newElement) > 1) return $dAvBt$react.Children.only(null);
32
- return /*#__PURE__*/ $dAvBt$react.isValidElement(newElement) ? newElement.props.children : null;
33
- } else return child;
34
- });
35
- return /*#__PURE__*/ $dAvBt$react.createElement($82dc8d030dec7549$var$SlotClone, ($parcel$interopDefault($dAvBt$babelruntimehelpersextends))({}, slotProps, {
36
- ref: forwardedRef
37
- }), /*#__PURE__*/ $dAvBt$react.isValidElement(newElement) ? /*#__PURE__*/ $dAvBt$react.cloneElement(newElement, undefined, newChildren) : null);
41
+ const newElement = slottable.props.children;
42
+ const newChildren = childrenArray.map((child) => {
43
+ if (child === slottable) {
44
+ if (React.Children.count(newElement) > 1) return React.Children.only(null);
45
+ return React.isValidElement(newElement) ? newElement.props.children : null;
46
+ } else {
47
+ return child;
48
+ }
49
+ });
50
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotClone, { ...slotProps, ref: forwardedRef, children: React.isValidElement(newElement) ? React.cloneElement(newElement, void 0, newChildren) : null });
38
51
  }
39
- return /*#__PURE__*/ $dAvBt$react.createElement($82dc8d030dec7549$var$SlotClone, ($parcel$interopDefault($dAvBt$babelruntimehelpersextends))({}, slotProps, {
40
- ref: forwardedRef
41
- }), children);
42
- });
43
- $82dc8d030dec7549$export$8c6ed5c666ac1360.displayName = 'Slot';
44
- /* -------------------------------------------------------------------------------------------------
45
- * SlotClone
46
- * -----------------------------------------------------------------------------------------------*/ const $82dc8d030dec7549$var$SlotClone = /*#__PURE__*/ $dAvBt$react.forwardRef((props, forwardedRef)=>{
47
- const { children: children , ...slotProps } = props;
48
- if (/*#__PURE__*/ $dAvBt$react.isValidElement(children)) return /*#__PURE__*/ $dAvBt$react.cloneElement(children, {
49
- ...$82dc8d030dec7549$var$mergeProps(slotProps, children.props),
50
- ref: forwardedRef ? $dAvBt$radixuireactcomposerefs.composeRefs(forwardedRef, children.ref) : children.ref
51
- });
52
- return $dAvBt$react.Children.count(children) > 1 ? $dAvBt$react.Children.only(null) : null;
53
- });
54
- $82dc8d030dec7549$var$SlotClone.displayName = 'SlotClone';
55
- /* -------------------------------------------------------------------------------------------------
56
- * Slottable
57
- * -----------------------------------------------------------------------------------------------*/ const $82dc8d030dec7549$export$d9f1ccf0bdb05d45 = ({ children: children })=>{
58
- return /*#__PURE__*/ $dAvBt$react.createElement($dAvBt$react.Fragment, null, children);
59
- };
60
- /* ---------------------------------------------------------------------------------------------- */ function $82dc8d030dec7549$var$isSlottable(child) {
61
- return /*#__PURE__*/ $dAvBt$react.isValidElement(child) && child.type === $82dc8d030dec7549$export$d9f1ccf0bdb05d45;
62
- }
63
- function $82dc8d030dec7549$var$mergeProps(slotProps, childProps) {
64
- // all child props should override
65
- const overrideProps = {
66
- ...childProps
67
- };
68
- for(const propName in childProps){
69
- const slotPropValue = slotProps[propName];
70
- const childPropValue = childProps[propName];
71
- const isHandler = /^on[A-Z]/.test(propName);
72
- if (isHandler) {
73
- // if the handler exists on both, we compose them
74
- if (slotPropValue && childPropValue) overrideProps[propName] = (...args)=>{
75
- childPropValue(...args);
76
- slotPropValue(...args);
77
- };
78
- else if (slotPropValue) overrideProps[propName] = slotPropValue;
79
- } else if (propName === 'style') overrideProps[propName] = {
80
- ...slotPropValue,
81
- ...childPropValue
82
- };
83
- else if (propName === 'className') overrideProps[propName] = [
84
- slotPropValue,
85
- childPropValue
86
- ].filter(Boolean).join(' ');
52
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotClone, { ...slotProps, ref: forwardedRef, children });
53
+ });
54
+ Slot.displayName = "Slot";
55
+ var SlotClone = React.forwardRef((props, forwardedRef) => {
56
+ const { children, ...slotProps } = props;
57
+ if (React.isValidElement(children)) {
58
+ return React.cloneElement(children, {
59
+ ...mergeProps(slotProps, children.props),
60
+ ref: forwardedRef ? (0, import_react_compose_refs.composeRefs)(forwardedRef, children.ref) : children.ref
61
+ });
87
62
  }
88
- return {
89
- ...slotProps,
90
- ...overrideProps
91
- };
92
- }
93
- const $82dc8d030dec7549$export$be92b6f5f03c0fe9 = $82dc8d030dec7549$export$8c6ed5c666ac1360;
94
-
95
-
96
-
97
-
63
+ return React.Children.count(children) > 1 ? React.Children.only(null) : null;
64
+ });
65
+ SlotClone.displayName = "SlotClone";
66
+ var Slottable = ({ children }) => {
67
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
68
+ };
69
+ function isSlottable(child) {
70
+ return React.isValidElement(child) && child.type === Slottable;
71
+ }
72
+ function mergeProps(slotProps, childProps) {
73
+ const overrideProps = { ...childProps };
74
+ for (const propName in childProps) {
75
+ const slotPropValue = slotProps[propName];
76
+ const childPropValue = childProps[propName];
77
+ const isHandler = /^on[A-Z]/.test(propName);
78
+ if (isHandler) {
79
+ if (slotPropValue && childPropValue) {
80
+ overrideProps[propName] = (...args) => {
81
+ childPropValue(...args);
82
+ slotPropValue(...args);
83
+ };
84
+ } else if (slotPropValue) {
85
+ overrideProps[propName] = slotPropValue;
86
+ }
87
+ } else if (propName === "style") {
88
+ overrideProps[propName] = { ...slotPropValue, ...childPropValue };
89
+ } else if (propName === "className") {
90
+ overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
91
+ }
92
+ }
93
+ return { ...slotProps, ...overrideProps };
94
+ }
95
+ var Root = Slot;
96
+ })();
98
97
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1,7 @@
1
- {"mappings":";;;;;;;;;;;;;;A;;;ACGA;;oGAEA,CAMA,MAAMA,yCAAI,GAAA,aAAGG,CAAAA,uBAAA,CAAyC,CAACG,KAAD,EAAQC,YAAR,GAAyB;IAC7E,MAAM,E,UAAEC,QAAF,CAAA,EAAY,GAAGC,SAAH,EAAZ,GAA6BH,KAAnC,AAAM;IACN,MAAMI,aAAa,GAAGP,qBAAA,CAAeS,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,IAAIV,qBAAA,CAAeiB,KAAf,CAAqBJ,UAArB,CAAA,GAAmC,CAAvC,EAA0C,OAAOb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,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,GAAGtB,SAAL;QAAgB,GAAGsB,aAAH;KAAvB,CAAO;CACR;AAED,MAAM7B,yCAAI,GAAGF,yCAAb,AAAA;;AD3HA","sources":["packages/react/slot/src/index.ts","packages/react/slot/src/Slot.tsx"],"sourcesContent":["export {\n Slot,\n Slottable,\n //\n Root,\n} from './Slot';\nexport type { SlotProps } 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: forwardedRef ? composeRefs(forwardedRef, (children as any).ref) : (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 (isHandler) {\n // if the handler exists on both, we compose them\n if (slotPropValue && childPropValue) {\n overrideProps[propName] = (...args: unknown[]) => {\n childPropValue(...args);\n slotPropValue(...args);\n };\n }\n // but if it exists only on the slot, we use only this one\n else if (slotPropValue) {\n overrideProps[propName] = slotPropValue;\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","Slottable","Root","React","composeRefs","forwardRef","props","forwardedRef","children","slotProps","childrenArray","Children","toArray","slottable","find","isSlottable","newElement","newChildren","map","child","count","only","isValidElement","cloneElement","undefined","displayName","SlotClone","mergeProps","ref","type","childProps","overrideProps","propName","slotPropValue","childPropValue","isHandler","test","args","filter","Boolean","join"],"version":3,"file":"index.js.map"}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/Slot.tsx"],
4
+ "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 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: forwardedRef ? composeRefs(forwardedRef, (children as any).ref) : (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 (isHandler) {\n // if the handler exists on both, we compose them\n if (slotPropValue && childPropValue) {\n overrideProps[propName] = (...args: unknown[]) => {\n childPropValue(...args);\n slotPropValue(...args);\n };\n }\n // but if it exists only on the slot, we use only this one\n else if (slotPropValue) {\n overrideProps[propName] = slotPropValue;\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"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,cAAuB;AACvB,kCAA4B;AAiCtB;AAvBN,MAAM,OAAa,iBAAmC,CAAC,OAAO,iBAAiB;AAC7E,UAAM,EAAE,UAAU,GAAG,UAAU,IAAI;AACnC,UAAM,gBAAsB,eAAS,QAAQ,QAAQ;AACrD,UAAM,YAAY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AAW,QAAQ;AAE1C,YAAM,YAAY,WAAW,KAAK,QAAQ;AAC1C,UAAI,WAAW;AAEb,YAAI,iBAAiB,gBAAgB;AACnC,wBAAc,QAAQ,IAAI,IAAI,SAAoB;AAChD,2BAAe,GAAG,IAAI;AACtB,0BAAc,GAAG,IAAI;AAAA,UACvB;AAAA,QACF,WAES,eAAe;AACtB,wBAAc,QAAQ,IAAI;AAAA,QAC5B;AAAA,MACF,WAES,aAAa,SAAS;AAC7B,sBAAc,QAAQ,IAAI,EAAE,GAAG,eAAe,GAAG,eAAe;AAAA,MAClE,WAAW,aAAa,aAAa;AACnC,sBAAc,QAAQ,IAAI,CAAC,eAAe,cAAc,EAAE,OAAO,OAAO,EAAE,KAAK,GAAG;AAAA,MACpF;AAAA,IACF;AAEA,WAAO,EAAE,GAAG,WAAW,GAAG,cAAc;AAAA,EAC1C;AAEA,MAAM,OAAO;",
6
+ "names": []
7
+ }
package/dist/index.mjs CHANGED
@@ -1,89 +1,70 @@
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
-
6
-
7
-
8
- /* -------------------------------------------------------------------------------------------------
9
- * Slot
10
- * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$export$8c6ed5c666ac1360 = /*#__PURE__*/ $9IrjX$forwardRef((props, forwardedRef)=>{
11
- const { children: children , ...slotProps } = props;
12
- const childrenArray = $9IrjX$Children.toArray(children);
13
- const slottable = childrenArray.find($5e63c961fc1ce211$var$isSlottable);
14
- if (slottable) {
15
- // the new element to render is the one passed as a child of `Slottable`
16
- const newElement = slottable.props.children;
17
- const newChildren = childrenArray.map((child)=>{
18
- if (child === slottable) {
19
- // because the new element will be the one rendered, we are only interested
20
- // in grabbing its children (`newElement.props.children`)
21
- if ($9IrjX$Children.count(newElement) > 1) return $9IrjX$Children.only(null);
22
- return /*#__PURE__*/ $9IrjX$isValidElement(newElement) ? newElement.props.children : null;
23
- } else return child;
24
- });
25
- return /*#__PURE__*/ $9IrjX$createElement($5e63c961fc1ce211$var$SlotClone, $9IrjX$babelruntimehelpersesmextends({}, slotProps, {
26
- ref: forwardedRef
27
- }), /*#__PURE__*/ $9IrjX$isValidElement(newElement) ? /*#__PURE__*/ $9IrjX$cloneElement(newElement, undefined, newChildren) : null);
28
- }
29
- return /*#__PURE__*/ $9IrjX$createElement($5e63c961fc1ce211$var$SlotClone, $9IrjX$babelruntimehelpersesmextends({}, slotProps, {
30
- ref: forwardedRef
31
- }), children);
1
+ // packages/react/slot/src/Slot.tsx
2
+ import * as React from "react";
3
+ import { composeRefs } from "@radix-ui/react-compose-refs";
4
+ import { Fragment, jsx } from "react/jsx-runtime";
5
+ var Slot = React.forwardRef((props, forwardedRef) => {
6
+ const { children, ...slotProps } = props;
7
+ const childrenArray = React.Children.toArray(children);
8
+ const slottable = childrenArray.find(isSlottable);
9
+ if (slottable) {
10
+ const newElement = slottable.props.children;
11
+ const newChildren = childrenArray.map((child) => {
12
+ if (child === slottable) {
13
+ if (React.Children.count(newElement) > 1) return React.Children.only(null);
14
+ return React.isValidElement(newElement) ? newElement.props.children : null;
15
+ } else {
16
+ return child;
17
+ }
18
+ });
19
+ return /* @__PURE__ */ jsx(SlotClone, { ...slotProps, ref: forwardedRef, children: React.isValidElement(newElement) ? React.cloneElement(newElement, void 0, newChildren) : null });
20
+ }
21
+ return /* @__PURE__ */ jsx(SlotClone, { ...slotProps, ref: forwardedRef, children });
32
22
  });
33
- $5e63c961fc1ce211$export$8c6ed5c666ac1360.displayName = 'Slot';
34
- /* -------------------------------------------------------------------------------------------------
35
- * SlotClone
36
- * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$var$SlotClone = /*#__PURE__*/ $9IrjX$forwardRef((props, forwardedRef)=>{
37
- const { children: children , ...slotProps } = props;
38
- if (/*#__PURE__*/ $9IrjX$isValidElement(children)) return /*#__PURE__*/ $9IrjX$cloneElement(children, {
39
- ...$5e63c961fc1ce211$var$mergeProps(slotProps, children.props),
40
- ref: forwardedRef ? $9IrjX$composeRefs(forwardedRef, children.ref) : children.ref
23
+ Slot.displayName = "Slot";
24
+ var SlotClone = React.forwardRef((props, forwardedRef) => {
25
+ const { children, ...slotProps } = props;
26
+ if (React.isValidElement(children)) {
27
+ return React.cloneElement(children, {
28
+ ...mergeProps(slotProps, children.props),
29
+ ref: forwardedRef ? composeRefs(forwardedRef, children.ref) : children.ref
41
30
  });
42
- return $9IrjX$Children.count(children) > 1 ? $9IrjX$Children.only(null) : null;
31
+ }
32
+ return React.Children.count(children) > 1 ? React.Children.only(null) : null;
43
33
  });
44
- $5e63c961fc1ce211$var$SlotClone.displayName = 'SlotClone';
45
- /* -------------------------------------------------------------------------------------------------
46
- * Slottable
47
- * -----------------------------------------------------------------------------------------------*/ const $5e63c961fc1ce211$export$d9f1ccf0bdb05d45 = ({ children: children })=>{
48
- return /*#__PURE__*/ $9IrjX$createElement($9IrjX$Fragment, null, children);
34
+ SlotClone.displayName = "SlotClone";
35
+ var Slottable = ({ children }) => {
36
+ return /* @__PURE__ */ jsx(Fragment, { children });
49
37
  };
50
- /* ---------------------------------------------------------------------------------------------- */ function $5e63c961fc1ce211$var$isSlottable(child) {
51
- return /*#__PURE__*/ $9IrjX$isValidElement(child) && child.type === $5e63c961fc1ce211$export$d9f1ccf0bdb05d45;
38
+ function isSlottable(child) {
39
+ return React.isValidElement(child) && child.type === Slottable;
52
40
  }
53
- function $5e63c961fc1ce211$var$mergeProps(slotProps, childProps) {
54
- // all child props should override
55
- const overrideProps = {
56
- ...childProps
57
- };
58
- for(const propName in childProps){
59
- const slotPropValue = slotProps[propName];
60
- const childPropValue = childProps[propName];
61
- const isHandler = /^on[A-Z]/.test(propName);
62
- if (isHandler) {
63
- // if the handler exists on both, we compose them
64
- if (slotPropValue && childPropValue) overrideProps[propName] = (...args)=>{
65
- childPropValue(...args);
66
- slotPropValue(...args);
67
- };
68
- else if (slotPropValue) overrideProps[propName] = slotPropValue;
69
- } else if (propName === 'style') overrideProps[propName] = {
70
- ...slotPropValue,
71
- ...childPropValue
41
+ function mergeProps(slotProps, childProps) {
42
+ const overrideProps = { ...childProps };
43
+ for (const propName in childProps) {
44
+ const slotPropValue = slotProps[propName];
45
+ const childPropValue = childProps[propName];
46
+ const isHandler = /^on[A-Z]/.test(propName);
47
+ if (isHandler) {
48
+ if (slotPropValue && childPropValue) {
49
+ overrideProps[propName] = (...args) => {
50
+ childPropValue(...args);
51
+ slotPropValue(...args);
72
52
  };
73
- else if (propName === 'className') overrideProps[propName] = [
74
- slotPropValue,
75
- childPropValue
76
- ].filter(Boolean).join(' ');
53
+ } else if (slotPropValue) {
54
+ overrideProps[propName] = slotPropValue;
55
+ }
56
+ } else if (propName === "style") {
57
+ overrideProps[propName] = { ...slotPropValue, ...childPropValue };
58
+ } else if (propName === "className") {
59
+ overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
77
60
  }
78
- return {
79
- ...slotProps,
80
- ...overrideProps
81
- };
61
+ }
62
+ return { ...slotProps, ...overrideProps };
82
63
  }
83
- const $5e63c961fc1ce211$export$be92b6f5f03c0fe9 = $5e63c961fc1ce211$export$8c6ed5c666ac1360;
84
-
85
-
86
-
87
-
88
- export {$5e63c961fc1ce211$export$8c6ed5c666ac1360 as Slot, $5e63c961fc1ce211$export$d9f1ccf0bdb05d45 as Slottable, $5e63c961fc1ce211$export$be92b6f5f03c0fe9 as Root};
64
+ var Root = Slot;
65
+ export {
66
+ Root,
67
+ Slot,
68
+ Slottable
69
+ };
89
70
  //# sourceMappingURL=index.mjs.map
@@ -1 +1,7 @@
1
- {"mappings":";;;;A;;;ACGA;;oGAEA,CAMA,MAAMA,yCAAI,GAAA,aAAGG,CAAA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GAAG;QAAE,GAAGD,UAA3B;KAAA,AAAsB;IAEtB,IAAK,MAAME,QAAX,IAAuBF,UAAvB,CAAmC;QACjC,MAAMG,aAAa,GAAGxB,SAAS,CAACuB,QAAD,CAA/B,AAAA;QACA,MAAME,cAAc,GAAGJ,UAAU,CAACE,QAAD,CAAjC,AAAA;QAEA,MAAMG,SAAS,GAAG,WAAWC,IAAX,CAAgBJ,QAAhB,CAAlB,AAAA;QACA,IAAIG,SAAJ,EAAe;YACb,iDAAA;YACA,IAAIF,aAAa,IAAIC,cAArB,EACEH,aAAa,CAACC,QAAD,CAAb,GAA0B,CAAIK,GAAAA,IAAJ,GAAwB;gBAChDH,cAAc,IAAIG,IAAJ,CAAd,CAAAH;gBACAD,aAAa,IAAII,IAAJ,CAAb,CAAAJ;aAFF,CAGC;iBAGE,IAAIA,aAAJ,EACHF,aAAa,CAACC,QAAD,CAAb,GAA0BC,aAA1B,CAAAF;SAVJ,MAcK,IAAIC,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,GAAGtB,SAAL;QAAgB,GAAGsB,aAAH;KAAvB,CAAO;CACR;AAED,MAAM7B,yCAAI,GAAGF,yCAAb,AAAA;;AD3HA","sources":["packages/react/slot/src/index.ts","packages/react/slot/src/Slot.tsx"],"sourcesContent":["export {\n Slot,\n Slottable,\n //\n Root,\n} from './Slot';\nexport type { SlotProps } 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: forwardedRef ? composeRefs(forwardedRef, (children as any).ref) : (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 (isHandler) {\n // if the handler exists on both, we compose them\n if (slotPropValue && childPropValue) {\n overrideProps[propName] = (...args: unknown[]) => {\n childPropValue(...args);\n slotPropValue(...args);\n };\n }\n // but if it exists only on the slot, we use only this one\n else if (slotPropValue) {\n overrideProps[propName] = slotPropValue;\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","Slottable","Root","React","composeRefs","forwardRef","props","forwardedRef","children","slotProps","childrenArray","Children","toArray","slottable","find","isSlottable","newElement","newChildren","map","child","count","only","isValidElement","cloneElement","undefined","displayName","SlotClone","mergeProps","ref","type","childProps","overrideProps","propName","slotPropValue","childPropValue","isHandler","test","args","filter","Boolean","join"],"version":3,"file":"index.mjs.map"}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/Slot.tsx"],
4
+ "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 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: forwardedRef ? composeRefs(forwardedRef, (children as any).ref) : (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 (isHandler) {\n // if the handler exists on both, we compose them\n if (slotPropValue && childPropValue) {\n overrideProps[propName] = (...args: unknown[]) => {\n childPropValue(...args);\n slotPropValue(...args);\n };\n }\n // but if it exists only on the slot, we use only this one\n else if (slotPropValue) {\n overrideProps[propName] = slotPropValue;\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"],
5
+ "mappings": ";AAAA,YAAY,WAAW;AACvB,SAAS,mBAAmB;AAiCtB,SA6CG,UA7CH;AAvBN,IAAM,OAAa,iBAAmC,CAAC,OAAO,iBAAiB;AAC7E,QAAM,EAAE,UAAU,GAAG,UAAU,IAAI;AACnC,QAAM,gBAAsB,eAAS,QAAQ,QAAQ;AACrD,QAAM,YAAY,cAAc,KAAK,WAAW;AAEhD,MAAI,WAAW;AAEb,UAAM,aAAa,UAAU,MAAM;AAEnC,UAAM,cAAc,cAAc,IAAI,CAAC,UAAU;AAC/C,UAAI,UAAU,WAAW;AAGvB,YAAU,eAAS,MAAM,UAAU,IAAI,EAAG,QAAa,eAAS,KAAK,IAAI;AACzE,eAAa,qBAAe,UAAU,IACjC,WAAW,MAAM,WAClB;AAAA,MACN,OAAO;AACL,eAAO;AAAA,MACT;AAAA,IACF,CAAC;AAED,WACE,oBAAC,aAAW,GAAG,WAAW,KAAK,cAC5B,UAAM,qBAAe,UAAU,IACtB,mBAAa,YAAY,QAAW,WAAW,IACrD,MACN;AAAA,EAEJ;AAEA,SACE,oBAAC,aAAW,GAAG,WAAW,KAAK,cAC5B,UACH;AAEJ,CAAC;AAED,KAAK,cAAc;AAUnB,IAAM,YAAkB,iBAAgC,CAAC,OAAO,iBAAiB;AAC/E,QAAM,EAAE,UAAU,GAAG,UAAU,IAAI;AAEnC,MAAU,qBAAe,QAAQ,G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.0-rc.2",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": {
@@ -28,8 +28,7 @@
28
28
  "version": "yarn version"
29
29
  },
30
30
  "dependencies": {
31
- "@babel/runtime": "^7.13.10",
32
- "@radix-ui/react-compose-refs": "1.0.1"
31
+ "@radix-ui/react-compose-refs": "1.1.0-rc.2"
33
32
  },
34
33
  "peerDependencies": {
35
34
  "@types/react": "*",
@@ -47,5 +46,6 @@
47
46
  },
48
47
  "bugs": {
49
48
  "url": "https://github.com/radix-ui/primitives/issues"
50
- }
49
+ },
50
+ "stableVersion": "1.0.2"
51
51
  }
@@ -1 +0,0 @@
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;AA2CF,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 {\n Slot,\n Slottable,\n //\n Root,\n} from './Slot';\nexport type { SlotProps } from './Slot';\n"],"names":[],"version":3,"file":"index.d.ts.map"}