@radix-ui/react-slot 1.1.3-rc.5 → 1.2.0-rc.1
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 +9 -5
- package/dist/index.d.ts +9 -5
- package/dist/index.js +53 -37
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +53 -37
- package/dist/index.mjs.map +3 -3
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
1
|
import * as React from 'react';
|
|
3
2
|
|
|
4
3
|
interface SlotProps extends React.HTMLAttributes<HTMLElement> {
|
|
5
4
|
children?: React.ReactNode;
|
|
6
5
|
}
|
|
6
|
+
declare function createSlot(ownerName: string): React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
|
|
7
7
|
declare const Slot: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
|
|
8
|
-
|
|
8
|
+
interface SlottableProps {
|
|
9
9
|
children: React.ReactNode;
|
|
10
|
-
}
|
|
11
|
-
|
|
10
|
+
}
|
|
11
|
+
interface SlottableComponent extends React.FC<SlottableProps> {
|
|
12
|
+
__radixId: symbol;
|
|
13
|
+
}
|
|
14
|
+
declare function createSlottable(ownerName: string): SlottableComponent;
|
|
15
|
+
declare const Slottable: SlottableComponent;
|
|
12
16
|
|
|
13
|
-
export { Root, Slot, type SlotProps, Slottable };
|
|
17
|
+
export { Slot as Root, Slot, type SlotProps, Slottable, createSlot, createSlottable };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
1
|
import * as React from 'react';
|
|
3
2
|
|
|
4
3
|
interface SlotProps extends React.HTMLAttributes<HTMLElement> {
|
|
5
4
|
children?: React.ReactNode;
|
|
6
5
|
}
|
|
6
|
+
declare function createSlot(ownerName: string): React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
|
|
7
7
|
declare const Slot: React.ForwardRefExoticComponent<SlotProps & React.RefAttributes<HTMLElement>>;
|
|
8
|
-
|
|
8
|
+
interface SlottableProps {
|
|
9
9
|
children: React.ReactNode;
|
|
10
|
-
}
|
|
11
|
-
|
|
10
|
+
}
|
|
11
|
+
interface SlottableComponent extends React.FC<SlottableProps> {
|
|
12
|
+
__radixId: symbol;
|
|
13
|
+
}
|
|
14
|
+
declare function createSlottable(ownerName: string): SlottableComponent;
|
|
15
|
+
declare const Slottable: SlottableComponent;
|
|
12
16
|
|
|
13
|
-
export { Root, Slot, type SlotProps, Slottable };
|
|
17
|
+
export { Slot as Root, Slot, type SlotProps, Slottable, createSlot, createSlottable };
|
package/dist/index.js
CHANGED
|
@@ -30,9 +30,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// packages/react/slot/src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
Root: () =>
|
|
33
|
+
Root: () => Slot,
|
|
34
34
|
Slot: () => Slot,
|
|
35
|
-
Slottable: () => Slottable
|
|
35
|
+
Slottable: () => Slottable,
|
|
36
|
+
createSlot: () => createSlot,
|
|
37
|
+
createSlottable: () => createSlottable
|
|
36
38
|
});
|
|
37
39
|
module.exports = __toCommonJS(index_exports);
|
|
38
40
|
|
|
@@ -40,43 +42,58 @@ module.exports = __toCommonJS(index_exports);
|
|
|
40
42
|
var React = __toESM(require("react"));
|
|
41
43
|
var import_react_compose_refs = require("@radix-ui/react-compose-refs");
|
|
42
44
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
function createSlot(ownerName) {
|
|
46
|
+
const SlotClone = createSlotClone(ownerName);
|
|
47
|
+
const Slot2 = React.forwardRef((props, forwardedRef) => {
|
|
48
|
+
const { children, ...slotProps } = props;
|
|
49
|
+
const childrenArray = React.Children.toArray(children);
|
|
50
|
+
const slottable = childrenArray.find(isSlottable);
|
|
51
|
+
if (slottable) {
|
|
52
|
+
const newElement = slottable.props.children;
|
|
53
|
+
const newChildren = childrenArray.map((child) => {
|
|
54
|
+
if (child === slottable) {
|
|
55
|
+
if (React.Children.count(newElement) > 1) return React.Children.only(null);
|
|
56
|
+
return React.isValidElement(newElement) ? newElement.props.children : null;
|
|
57
|
+
} else {
|
|
58
|
+
return child;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotClone, { ...slotProps, ref: forwardedRef, children: React.isValidElement(newElement) ? React.cloneElement(newElement, void 0, newChildren) : null });
|
|
62
|
+
}
|
|
63
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotClone, { ...slotProps, ref: forwardedRef, children });
|
|
64
|
+
});
|
|
65
|
+
Slot2.displayName = `${ownerName}.Slot`;
|
|
66
|
+
return Slot2;
|
|
67
|
+
}
|
|
68
|
+
var Slot = createSlot("Slot");
|
|
69
|
+
function createSlotClone(ownerName) {
|
|
70
|
+
const SlotClone = React.forwardRef((props, forwardedRef) => {
|
|
71
|
+
const { children, ...slotProps } = props;
|
|
72
|
+
if (React.isValidElement(children)) {
|
|
73
|
+
const childrenRef = getElementRef(children);
|
|
74
|
+
const props2 = mergeProps(slotProps, children.props);
|
|
75
|
+
if (children.type !== React.Fragment) {
|
|
76
|
+
props2.ref = forwardedRef ? (0, import_react_compose_refs.composeRefs)(forwardedRef, childrenRef) : childrenRef;
|
|
55
77
|
}
|
|
56
|
-
|
|
57
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotClone, { ...slotProps, ref: forwardedRef, children: React.isValidElement(newElement) ? React.cloneElement(newElement, void 0, newChildren) : null });
|
|
58
|
-
}
|
|
59
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotClone, { ...slotProps, ref: forwardedRef, children });
|
|
60
|
-
});
|
|
61
|
-
Slot.displayName = "Slot";
|
|
62
|
-
var SlotClone = React.forwardRef((props, forwardedRef) => {
|
|
63
|
-
const { children, ...slotProps } = props;
|
|
64
|
-
if (React.isValidElement(children)) {
|
|
65
|
-
const childrenRef = getElementRef(children);
|
|
66
|
-
const props2 = mergeProps(slotProps, children.props);
|
|
67
|
-
if (children.type !== React.Fragment) {
|
|
68
|
-
props2.ref = forwardedRef ? (0, import_react_compose_refs.composeRefs)(forwardedRef, childrenRef) : childrenRef;
|
|
78
|
+
return React.cloneElement(children, props2);
|
|
69
79
|
}
|
|
70
|
-
return React.
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var
|
|
76
|
-
|
|
77
|
-
}
|
|
80
|
+
return React.Children.count(children) > 1 ? React.Children.only(null) : null;
|
|
81
|
+
});
|
|
82
|
+
SlotClone.displayName = `${ownerName}.SlotClone`;
|
|
83
|
+
return SlotClone;
|
|
84
|
+
}
|
|
85
|
+
var SLOTTABLE_IDENTIFIER = Symbol("radix.slottable");
|
|
86
|
+
function createSlottable(ownerName) {
|
|
87
|
+
const Slottable2 = ({ children }) => {
|
|
88
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
89
|
+
};
|
|
90
|
+
Slottable2.displayName = `${ownerName}.Slottable`;
|
|
91
|
+
Slottable2.__radixId = SLOTTABLE_IDENTIFIER;
|
|
92
|
+
return Slottable2;
|
|
93
|
+
}
|
|
94
|
+
var Slottable = createSlottable("Slottable");
|
|
78
95
|
function isSlottable(child) {
|
|
79
|
-
return React.isValidElement(child) && child.type ===
|
|
96
|
+
return React.isValidElement(child) && typeof child.type === "function" && "__radixId" in child.type && child.type.__radixId === SLOTTABLE_IDENTIFIER;
|
|
80
97
|
}
|
|
81
98
|
function mergeProps(slotProps, childProps) {
|
|
82
99
|
const overrideProps = { ...childProps };
|
|
@@ -114,5 +131,4 @@ function getElementRef(element) {
|
|
|
114
131
|
}
|
|
115
132
|
return element.props.ref || element.ref;
|
|
116
133
|
}
|
|
117
|
-
var Root = Slot;
|
|
118
134
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts", "../src/slot.tsx"],
|
|
4
|
-
"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\
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,gCAA4B;
|
|
6
|
-
"names": ["props"]
|
|
4
|
+
"sourcesContent": ["export {\n Slot,\n Slottable,\n //\n Root,\n createSlot,\n createSlottable,\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\nfunction createSlot(ownerName: string) {\n const SlotClone = createSlotClone(ownerName);\n const 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;\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 as { children: React.ReactNode }).children\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\n Slot.displayName = `${ownerName}.Slot`;\n return Slot;\n}\n\nconst Slot = createSlot('Slot');\n\n/* -------------------------------------------------------------------------------------------------\n * SlotClone\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotCloneProps {\n children: React.ReactNode;\n}\n\nfunction createSlotClone(ownerName: string) {\n const SlotClone = React.forwardRef<any, SlotCloneProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n\n if (React.isValidElement(children)) {\n const childrenRef = getElementRef(children);\n const props = mergeProps(slotProps, children.props as AnyProps);\n // do not pass ref to React.Fragment for React 19 compatibility\n if (children.type !== React.Fragment) {\n props.ref = forwardedRef ? composeRefs(forwardedRef, childrenRef) : childrenRef;\n }\n return React.cloneElement(children, props);\n }\n\n return React.Children.count(children) > 1 ? React.Children.only(null) : null;\n });\n\n SlotClone.displayName = `${ownerName}.SlotClone`;\n return SlotClone;\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst SLOTTABLE_IDENTIFIER = Symbol('radix.slottable');\n\ninterface SlottableProps {\n children: React.ReactNode;\n}\n\ninterface SlottableComponent extends React.FC<SlottableProps> {\n __radixId: symbol;\n}\n\nfunction createSlottable(ownerName: string) {\n const Slottable: SlottableComponent = ({ children }) => {\n return <>{children}</>;\n };\n Slottable.displayName = `${ownerName}.Slottable`;\n Slottable.__radixId = SLOTTABLE_IDENTIFIER;\n return Slottable;\n}\n\nconst Slottable = createSlottable('Slottable');\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype AnyProps = Record<string, any>;\n\nfunction isSlottable(\n child: React.ReactNode\n): child is React.ReactElement<SlottableProps, typeof Slottable> {\n return (\n React.isValidElement(child) &&\n typeof child.type === 'function' &&\n '__radixId' in child.type &&\n child.type.__radixId === SLOTTABLE_IDENTIFIER\n );\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\n// Before React 19 accessing `element.props.ref` will throw a warning and suggest using `element.ref`\n// After React 19 accessing `element.ref` does the opposite.\n// https://github.com/facebook/react/pull/28348\n//\n// Access the ref using the method that doesn't yield a warning.\nfunction getElementRef(element: React.ReactElement) {\n // React <=18 in DEV\n let getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get;\n let mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning;\n if (mayWarn) {\n return (element as any).ref;\n }\n\n // React 19 in DEV\n getter = Object.getOwnPropertyDescriptor(element, 'ref')?.get;\n mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning;\n if (mayWarn) {\n return (element.props as { ref?: React.Ref<unknown> }).ref;\n }\n\n // Not DEV\n return (element.props as { ref?: React.Ref<unknown> }).ref || (element as any).ref;\n}\n\nexport {\n createSlot,\n createSlottable,\n Slot,\n Slottable,\n //\n Slot as Root,\n};\nexport type { SlotProps };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,gCAA4B;AAmCpB;AAzBR,SAAS,WAAW,WAAmB;AACrC,QAAM,YAAY,gBAAgB,SAAS;AAC3C,QAAMA,QAAa,iBAAmC,CAAC,OAAO,iBAAiB;AAC7E,UAAM,EAAE,UAAU,GAAG,UAAU,IAAI;AACnC,UAAM,gBAAsB,eAAS,QAAQ,QAAQ;AACrD,UAAM,YAAY,cAAc,KAAK,WAAW;AAEhD,QAAI,WAAW;AAEb,YAAM,aAAa,UAAU,MAAM;AAEnC,YAAM,cAAc,cAAc,IAAI,CAAC,UAAU;AAC/C,YAAI,UAAU,WAAW;AAGvB,cAAU,eAAS,MAAM,UAAU,IAAI,EAAG,QAAa,eAAS,KAAK,IAAI;AACzE,iBAAa,qBAAe,UAAU,IACjC,WAAW,MAAwC,WACpD;AAAA,QACN,OAAO;AACL,iBAAO;AAAA,QACT;AAAA,MACF,CAAC;AAED,aACE,4CAAC,aAAW,GAAG,WAAW,KAAK,cAC5B,UAAM,qBAAe,UAAU,IACtB,mBAAa,YAAY,QAAW,WAAW,IACrD,MACN;AAAA,IAEJ;AAEA,WACE,4CAAC,aAAW,GAAG,WAAW,KAAK,cAC5B,UACH;AAAA,EAEJ,CAAC;AAED,EAAAA,MAAK,cAAc,GAAG,SAAS;AAC/B,SAAOA;AACT;AAEA,IAAM,OAAO,WAAW,MAAM;AAU9B,SAAS,gBAAgB,WAAmB;AAC1C,QAAM,YAAkB,iBAAgC,CAAC,OAAO,iBAAiB;AAC/E,UAAM,EAAE,UAAU,GAAG,UAAU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,oBAAc,QAAQ,IAAI,EAAE,GAAG,eAAe,GAAG,eAAe;AAAA,IAClE,WAAW,aAAa,aAAa;AACnC,oBAAc,QAAQ,IAAI,CAAC,eAAe,cAAc,EAAE,OAAO,OAAO,EAAE,KAAK,GAAG;AAAA,IACpF;AAAA,EACF;AAEA,SAAO,EAAE,GAAG,WAAW,GAAG,cAAc;AAC1C;AAOA,SAAS,cAAc,SAA6B;AAElD,MAAI,SAAS,OAAO,yBAAyB,QAAQ,OAAO,KAAK,GAAG;AACpE,MAAI,UAAU,UAAU,oBAAoB,UAAU,OAAO;AAC7D,MAAI,SAAS;AACX,WAAQ,QAAgB;AAAA,EAC1B;AAGA,WAAS,OAAO,yBAAyB,SAAS,KAAK,GAAG;AAC1D,YAAU,UAAU,oBAAoB,UAAU,OAAO;AACzD,MAAI,SAAS;AACX,WAAQ,QAAQ,MAAuC;AAAA,EACzD;AAGA,SAAQ,QAAQ,MAAuC,OAAQ,QAAgB;AACjF;",
|
|
6
|
+
"names": ["Slot", "props", "Slottable"]
|
|
7
7
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -2,43 +2,58 @@
|
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import { composeRefs } from "@radix-ui/react-compose-refs";
|
|
4
4
|
import { Fragment as Fragment2, jsx } from "react/jsx-runtime";
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
5
|
+
function createSlot(ownerName) {
|
|
6
|
+
const SlotClone = createSlotClone(ownerName);
|
|
7
|
+
const Slot2 = React.forwardRef((props, forwardedRef) => {
|
|
8
|
+
const { children, ...slotProps } = props;
|
|
9
|
+
const childrenArray = React.Children.toArray(children);
|
|
10
|
+
const slottable = childrenArray.find(isSlottable);
|
|
11
|
+
if (slottable) {
|
|
12
|
+
const newElement = slottable.props.children;
|
|
13
|
+
const newChildren = childrenArray.map((child) => {
|
|
14
|
+
if (child === slottable) {
|
|
15
|
+
if (React.Children.count(newElement) > 1) return React.Children.only(null);
|
|
16
|
+
return React.isValidElement(newElement) ? newElement.props.children : null;
|
|
17
|
+
} else {
|
|
18
|
+
return child;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return /* @__PURE__ */ jsx(SlotClone, { ...slotProps, ref: forwardedRef, children: React.isValidElement(newElement) ? React.cloneElement(newElement, void 0, newChildren) : null });
|
|
22
|
+
}
|
|
23
|
+
return /* @__PURE__ */ jsx(SlotClone, { ...slotProps, ref: forwardedRef, children });
|
|
24
|
+
});
|
|
25
|
+
Slot2.displayName = `${ownerName}.Slot`;
|
|
26
|
+
return Slot2;
|
|
27
|
+
}
|
|
28
|
+
var Slot = createSlot("Slot");
|
|
29
|
+
function createSlotClone(ownerName) {
|
|
30
|
+
const SlotClone = React.forwardRef((props, forwardedRef) => {
|
|
31
|
+
const { children, ...slotProps } = props;
|
|
32
|
+
if (React.isValidElement(children)) {
|
|
33
|
+
const childrenRef = getElementRef(children);
|
|
34
|
+
const props2 = mergeProps(slotProps, children.props);
|
|
35
|
+
if (children.type !== React.Fragment) {
|
|
36
|
+
props2.ref = forwardedRef ? composeRefs(forwardedRef, childrenRef) : childrenRef;
|
|
17
37
|
}
|
|
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 });
|
|
22
|
-
});
|
|
23
|
-
Slot.displayName = "Slot";
|
|
24
|
-
var SlotClone = React.forwardRef((props, forwardedRef) => {
|
|
25
|
-
const { children, ...slotProps } = props;
|
|
26
|
-
if (React.isValidElement(children)) {
|
|
27
|
-
const childrenRef = getElementRef(children);
|
|
28
|
-
const props2 = mergeProps(slotProps, children.props);
|
|
29
|
-
if (children.type !== React.Fragment) {
|
|
30
|
-
props2.ref = forwardedRef ? composeRefs(forwardedRef, childrenRef) : childrenRef;
|
|
38
|
+
return React.cloneElement(children, props2);
|
|
31
39
|
}
|
|
32
|
-
return React.
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
+
return React.Children.count(children) > 1 ? React.Children.only(null) : null;
|
|
41
|
+
});
|
|
42
|
+
SlotClone.displayName = `${ownerName}.SlotClone`;
|
|
43
|
+
return SlotClone;
|
|
44
|
+
}
|
|
45
|
+
var SLOTTABLE_IDENTIFIER = Symbol("radix.slottable");
|
|
46
|
+
function createSlottable(ownerName) {
|
|
47
|
+
const Slottable2 = ({ children }) => {
|
|
48
|
+
return /* @__PURE__ */ jsx(Fragment2, { children });
|
|
49
|
+
};
|
|
50
|
+
Slottable2.displayName = `${ownerName}.Slottable`;
|
|
51
|
+
Slottable2.__radixId = SLOTTABLE_IDENTIFIER;
|
|
52
|
+
return Slottable2;
|
|
53
|
+
}
|
|
54
|
+
var Slottable = createSlottable("Slottable");
|
|
40
55
|
function isSlottable(child) {
|
|
41
|
-
return React.isValidElement(child) && child.type ===
|
|
56
|
+
return React.isValidElement(child) && typeof child.type === "function" && "__radixId" in child.type && child.type.__radixId === SLOTTABLE_IDENTIFIER;
|
|
42
57
|
}
|
|
43
58
|
function mergeProps(slotProps, childProps) {
|
|
44
59
|
const overrideProps = { ...childProps };
|
|
@@ -76,10 +91,11 @@ function getElementRef(element) {
|
|
|
76
91
|
}
|
|
77
92
|
return element.props.ref || element.ref;
|
|
78
93
|
}
|
|
79
|
-
var Root = Slot;
|
|
80
94
|
export {
|
|
81
|
-
Root,
|
|
95
|
+
Slot as Root,
|
|
82
96
|
Slot,
|
|
83
|
-
Slottable
|
|
97
|
+
Slottable,
|
|
98
|
+
createSlot,
|
|
99
|
+
createSlottable
|
|
84
100
|
};
|
|
85
101
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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\
|
|
5
|
-
"mappings": ";AAAA,YAAY,WAAW;AACvB,SAAS,mBAAmB;
|
|
6
|
-
"names": ["Fragment", "props"]
|
|
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\nfunction createSlot(ownerName: string) {\n const SlotClone = createSlotClone(ownerName);\n const 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;\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 as { children: React.ReactNode }).children\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\n Slot.displayName = `${ownerName}.Slot`;\n return Slot;\n}\n\nconst Slot = createSlot('Slot');\n\n/* -------------------------------------------------------------------------------------------------\n * SlotClone\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotCloneProps {\n children: React.ReactNode;\n}\n\nfunction createSlotClone(ownerName: string) {\n const SlotClone = React.forwardRef<any, SlotCloneProps>((props, forwardedRef) => {\n const { children, ...slotProps } = props;\n\n if (React.isValidElement(children)) {\n const childrenRef = getElementRef(children);\n const props = mergeProps(slotProps, children.props as AnyProps);\n // do not pass ref to React.Fragment for React 19 compatibility\n if (children.type !== React.Fragment) {\n props.ref = forwardedRef ? composeRefs(forwardedRef, childrenRef) : childrenRef;\n }\n return React.cloneElement(children, props);\n }\n\n return React.Children.count(children) > 1 ? React.Children.only(null) : null;\n });\n\n SlotClone.displayName = `${ownerName}.SlotClone`;\n return SlotClone;\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst SLOTTABLE_IDENTIFIER = Symbol('radix.slottable');\n\ninterface SlottableProps {\n children: React.ReactNode;\n}\n\ninterface SlottableComponent extends React.FC<SlottableProps> {\n __radixId: symbol;\n}\n\nfunction createSlottable(ownerName: string) {\n const Slottable: SlottableComponent = ({ children }) => {\n return <>{children}</>;\n };\n Slottable.displayName = `${ownerName}.Slottable`;\n Slottable.__radixId = SLOTTABLE_IDENTIFIER;\n return Slottable;\n}\n\nconst Slottable = createSlottable('Slottable');\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype AnyProps = Record<string, any>;\n\nfunction isSlottable(\n child: React.ReactNode\n): child is React.ReactElement<SlottableProps, typeof Slottable> {\n return (\n React.isValidElement(child) &&\n typeof child.type === 'function' &&\n '__radixId' in child.type &&\n child.type.__radixId === SLOTTABLE_IDENTIFIER\n );\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\n// Before React 19 accessing `element.props.ref` will throw a warning and suggest using `element.ref`\n// After React 19 accessing `element.ref` does the opposite.\n// https://github.com/facebook/react/pull/28348\n//\n// Access the ref using the method that doesn't yield a warning.\nfunction getElementRef(element: React.ReactElement) {\n // React <=18 in DEV\n let getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get;\n let mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning;\n if (mayWarn) {\n return (element as any).ref;\n }\n\n // React 19 in DEV\n getter = Object.getOwnPropertyDescriptor(element, 'ref')?.get;\n mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning;\n if (mayWarn) {\n return (element.props as { ref?: React.Ref<unknown> }).ref;\n }\n\n // Not DEV\n return (element.props as { ref?: React.Ref<unknown> }).ref || (element as any).ref;\n}\n\nexport {\n createSlot,\n createSlottable,\n Slot,\n Slottable,\n //\n Slot as Root,\n};\nexport type { SlotProps };\n"],
|
|
5
|
+
"mappings": ";AAAA,YAAY,WAAW;AACvB,SAAS,mBAAmB;AAmCpB,SAkEG,YAAAA,WAlEH;AAzBR,SAAS,WAAW,WAAmB;AACrC,QAAM,YAAY,gBAAgB,SAAS;AAC3C,QAAMC,QAAa,iBAAmC,CAAC,OAAO,iBAAiB;AAC7E,UAAM,EAAE,UAAU,GAAG,UAAU,IAAI;AACnC,UAAM,gBA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,UAAU,oBAAoB,UAAU,OAAO;AACzD,MAAI,SAAS;AACX,WAAQ,QAAQ,MAAuC;AAAA,EACzD;AAGA,SAAQ,QAAQ,MAAuC,OAAQ,QAAgB;AACjF;",
|
|
6
|
+
"names": ["Fragment", "Slot", "props", "Slottable"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/react-slot",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-rc.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"version": "yarn version"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@radix-ui/react-compose-refs": "1.1.2-rc.
|
|
32
|
+
"@radix-ui/react-compose-refs": "1.1.2-rc.6"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@repo/eslint-config": "0.0.0",
|