@radix-ui/react-slot 1.3.0 → 1.3.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 +14 -5
- package/dist/index.d.ts +14 -5
- package/dist/index.js +87 -34
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +89 -34
- package/dist/index.mjs.map +4 -4
- package/package.json +9 -8
package/dist/index.d.mts
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
|
-
declare
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
declare const mergeProps: <SlotProps extends AnyProps = AnyProps, ChildProps extends AnyProps = SlotProps, ReturnProps extends AnyProps = SlotProps & ChildProps>(slotProps: SlotProps, childProps: ChildProps) => ReturnProps;
|
|
4
|
+
interface MergePropsFunction<SlotProps extends AnyProps = UnknownProps, ChildProps extends AnyProps = SlotProps, ReturnProps extends AnyProps = SlotProps & ChildProps> {
|
|
5
|
+
(slotProps: SlotProps, childProps: ChildProps): ReturnProps;
|
|
7
6
|
}
|
|
7
|
+
type AnyProps = Record<string, any>;
|
|
8
|
+
type UnknownProps = Record<string, unknown>;
|
|
9
|
+
|
|
10
|
+
interface SlotProviderProps {
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
mergeProps: MergePropsFunction<AnyProps, AnyProps, AnyProps>;
|
|
13
|
+
}
|
|
14
|
+
declare const SlotProvider: React.FC<SlotProviderProps>;
|
|
8
15
|
type SlotProps<Elem extends Element = HTMLElement, Props = React.HTMLAttributes<Elem>> = Props & {
|
|
9
16
|
children?: React.ReactNode;
|
|
17
|
+
mergeProps?: MergePropsFunction;
|
|
10
18
|
};
|
|
11
19
|
declare function createSlot<Elem extends Element = HTMLElement, Props = React.HTMLAttributes<Elem>>(ownerName: string): React.ForwardRefExoticComponent<React.PropsWithoutRef<SlotProps<Elem, Props>> & React.RefAttributes<Elem>>;
|
|
12
20
|
declare const Slot: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLElement> & {
|
|
13
21
|
children?: React.ReactNode;
|
|
22
|
+
mergeProps?: MergePropsFunction;
|
|
14
23
|
} & React.RefAttributes<HTMLElement>>;
|
|
15
24
|
type SlottableChildrenProps = {
|
|
16
25
|
children: React.ReactNode;
|
|
@@ -26,4 +35,4 @@ interface SlottableComponent extends React.FC<SlottableProps> {
|
|
|
26
35
|
declare function createSlottable(ownerName: string): SlottableComponent;
|
|
27
36
|
declare const Slottable: SlottableComponent;
|
|
28
37
|
|
|
29
|
-
export { Slot as Root, Slot, type SlotProps, Slottable, createSlot, createSlottable };
|
|
38
|
+
export { type MergePropsFunction, SlotProvider as Provider, Slot as Root, Slot, type SlotProps, SlotProvider, Slottable, createSlot, createSlottable, mergeProps };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
|
-
declare
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
declare const mergeProps: <SlotProps extends AnyProps = AnyProps, ChildProps extends AnyProps = SlotProps, ReturnProps extends AnyProps = SlotProps & ChildProps>(slotProps: SlotProps, childProps: ChildProps) => ReturnProps;
|
|
4
|
+
interface MergePropsFunction<SlotProps extends AnyProps = UnknownProps, ChildProps extends AnyProps = SlotProps, ReturnProps extends AnyProps = SlotProps & ChildProps> {
|
|
5
|
+
(slotProps: SlotProps, childProps: ChildProps): ReturnProps;
|
|
7
6
|
}
|
|
7
|
+
type AnyProps = Record<string, any>;
|
|
8
|
+
type UnknownProps = Record<string, unknown>;
|
|
9
|
+
|
|
10
|
+
interface SlotProviderProps {
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
mergeProps: MergePropsFunction<AnyProps, AnyProps, AnyProps>;
|
|
13
|
+
}
|
|
14
|
+
declare const SlotProvider: React.FC<SlotProviderProps>;
|
|
8
15
|
type SlotProps<Elem extends Element = HTMLElement, Props = React.HTMLAttributes<Elem>> = Props & {
|
|
9
16
|
children?: React.ReactNode;
|
|
17
|
+
mergeProps?: MergePropsFunction;
|
|
10
18
|
};
|
|
11
19
|
declare function createSlot<Elem extends Element = HTMLElement, Props = React.HTMLAttributes<Elem>>(ownerName: string): React.ForwardRefExoticComponent<React.PropsWithoutRef<SlotProps<Elem, Props>> & React.RefAttributes<Elem>>;
|
|
12
20
|
declare const Slot: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLElement> & {
|
|
13
21
|
children?: React.ReactNode;
|
|
22
|
+
mergeProps?: MergePropsFunction;
|
|
14
23
|
} & React.RefAttributes<HTMLElement>>;
|
|
15
24
|
type SlottableChildrenProps = {
|
|
16
25
|
children: React.ReactNode;
|
|
@@ -26,4 +35,4 @@ interface SlottableComponent extends React.FC<SlottableProps> {
|
|
|
26
35
|
declare function createSlottable(ownerName: string): SlottableComponent;
|
|
27
36
|
declare const Slottable: SlottableComponent;
|
|
28
37
|
|
|
29
|
-
export { Slot as Root, Slot, type SlotProps, Slottable, createSlot, createSlottable };
|
|
38
|
+
export { type MergePropsFunction, SlotProvider as Provider, Slot as Root, Slot, type SlotProps, SlotProvider, Slottable, createSlot, createSlottable, mergeProps };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
"use client";
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
10
|
var __export = (target, all) => {
|
|
9
11
|
for (var name in all)
|
|
10
12
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -30,21 +32,87 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
32
|
// src/index.ts
|
|
31
33
|
var index_exports = {};
|
|
32
34
|
__export(index_exports, {
|
|
35
|
+
Provider: () => SlotProvider,
|
|
33
36
|
Root: () => Slot,
|
|
34
37
|
Slot: () => Slot,
|
|
38
|
+
SlotProvider: () => SlotProvider,
|
|
35
39
|
Slottable: () => Slottable,
|
|
36
40
|
createSlot: () => createSlot,
|
|
37
|
-
createSlottable: () => createSlottable
|
|
41
|
+
createSlottable: () => createSlottable,
|
|
42
|
+
mergeProps: () => mergeProps
|
|
38
43
|
});
|
|
39
44
|
module.exports = __toCommonJS(index_exports);
|
|
40
45
|
|
|
41
46
|
// src/slot.tsx
|
|
42
47
|
var React = __toESM(require("react"));
|
|
43
48
|
var import_react_compose_refs = require("@radix-ui/react-compose-refs");
|
|
49
|
+
|
|
50
|
+
// src/merge-props.ts
|
|
51
|
+
var import_is_development = require("@radix-ui/primitive/is-development");
|
|
52
|
+
var mergeProps = /* @__PURE__ */ __name(((slotProps, childProps) => {
|
|
53
|
+
const overrideProps = { ...childProps };
|
|
54
|
+
for (const propName in childProps) {
|
|
55
|
+
const slotPropValue = slotProps[propName];
|
|
56
|
+
const childPropValue = childProps[propName];
|
|
57
|
+
const isHandler = /^on[A-Z]/.test(propName);
|
|
58
|
+
if (isHandler) {
|
|
59
|
+
if (slotPropValue && childPropValue) {
|
|
60
|
+
const slotIsFunction = typeof slotPropValue === "function";
|
|
61
|
+
const childIsFunction = typeof childPropValue === "function";
|
|
62
|
+
if (import_is_development.IS_DEVELOPMENT) {
|
|
63
|
+
if (!slotIsFunction || !childIsFunction) {
|
|
64
|
+
console.warn(
|
|
65
|
+
`Slot: Expected a function for ${propName}, but received ${[typeof slotPropValue, typeof childPropValue].filter((v) => v !== "function").join(" and ")}.`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
overrideProps[propName] = (...args) => {
|
|
70
|
+
const result = childIsFunction ? childPropValue(...args) : void 0;
|
|
71
|
+
if (slotIsFunction) {
|
|
72
|
+
slotPropValue(...args);
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
};
|
|
76
|
+
} else if (slotPropValue) {
|
|
77
|
+
overrideProps[propName] = slotPropValue;
|
|
78
|
+
}
|
|
79
|
+
} else if (propName === "style") {
|
|
80
|
+
overrideProps[propName] = {
|
|
81
|
+
...typeof slotPropValue === "object" ? slotPropValue : null,
|
|
82
|
+
...typeof childPropValue === "object" ? childPropValue : null
|
|
83
|
+
};
|
|
84
|
+
} else if (propName === "className") {
|
|
85
|
+
overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
|
|
86
|
+
} else if (propName === "aria-describedby") {
|
|
87
|
+
overrideProps[propName] = concatAriaDescribedby(childPropValue, slotPropValue);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { ...slotProps, ...overrideProps };
|
|
91
|
+
}), "mergeProps");
|
|
92
|
+
function concatAriaDescribedby(...values) {
|
|
93
|
+
const ids = /* @__PURE__ */ new Set();
|
|
94
|
+
for (const value of values) {
|
|
95
|
+
if (typeof value !== "string") continue;
|
|
96
|
+
for (const id of String(value).trim().split(/\s+/)) {
|
|
97
|
+
if (id) ids.add(id);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return ids.size > 0 ? Array.from(ids).join(" ") : void 0;
|
|
101
|
+
}
|
|
102
|
+
__name(concatAriaDescribedby, "concatAriaDescribedby");
|
|
103
|
+
|
|
104
|
+
// src/slot.tsx
|
|
105
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
106
|
+
var SlotContext = React.createContext(mergeProps);
|
|
107
|
+
SlotContext.displayName = "SlotContext";
|
|
108
|
+
var SlotProvider = /* @__PURE__ */ __name(({ children, mergeProps: mergeProps2 }) => {
|
|
109
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotContext.Provider, { value: mergeProps2, children });
|
|
110
|
+
}, "SlotProvider");
|
|
44
111
|
// @__NO_SIDE_EFFECTS__
|
|
45
112
|
function createSlot(ownerName) {
|
|
46
113
|
const Slot2 = React.forwardRef((props, forwardedRef) => {
|
|
47
|
-
|
|
114
|
+
const context = React.useContext(SlotContext);
|
|
115
|
+
let { children, mergeProps: mergePropsProp = context, ...slotProps } = props;
|
|
48
116
|
let slottableElement = null;
|
|
49
117
|
let hasSlottable = false;
|
|
50
118
|
const newChildren = [];
|
|
@@ -86,7 +154,10 @@ function createSlot(ownerName) {
|
|
|
86
154
|
}
|
|
87
155
|
return children;
|
|
88
156
|
}
|
|
89
|
-
const mergedProps =
|
|
157
|
+
const mergedProps = mergePropsProp(
|
|
158
|
+
slotProps,
|
|
159
|
+
slottableElement.props ?? {}
|
|
160
|
+
);
|
|
90
161
|
if (slottableElement.type !== React.Fragment) {
|
|
91
162
|
mergedProps.ref = forwardedRef ? composedRef : slottableElementRef;
|
|
92
163
|
}
|
|
@@ -95,48 +166,26 @@ function createSlot(ownerName) {
|
|
|
95
166
|
Slot2.displayName = `${ownerName}.Slot`;
|
|
96
167
|
return Slot2;
|
|
97
168
|
}
|
|
169
|
+
__name(createSlot, "createSlot");
|
|
98
170
|
var Slot = /* @__PURE__ */ createSlot("Slot");
|
|
99
171
|
var SLOTTABLE_IDENTIFIER = Symbol.for("radix.slottable");
|
|
100
172
|
// @__NO_SIDE_EFFECTS__
|
|
101
173
|
function createSlottable(ownerName) {
|
|
102
|
-
const Slottable2 = (props) => "child" in props ? props.children(props.child) : props.children;
|
|
174
|
+
const Slottable2 = /* @__PURE__ */ __name((props) => "child" in props ? props.children(props.child) : props.children, "Slottable");
|
|
103
175
|
Slottable2.displayName = `${ownerName}.Slottable`;
|
|
104
176
|
Slottable2.__radixId = SLOTTABLE_IDENTIFIER;
|
|
105
177
|
return Slottable2;
|
|
106
178
|
}
|
|
179
|
+
__name(createSlottable, "createSlottable");
|
|
107
180
|
var Slottable = /* @__PURE__ */ createSlottable("Slottable");
|
|
108
|
-
var getSlottableElementFromSlottable = (slottable, child) => {
|
|
181
|
+
var getSlottableElementFromSlottable = /* @__PURE__ */ __name((slottable, child) => {
|
|
109
182
|
if ("child" in slottable.props) {
|
|
110
183
|
const child2 = slottable.props.child;
|
|
111
184
|
if (!React.isValidElement(child2)) return null;
|
|
112
185
|
return React.cloneElement(child2, void 0, slottable.props.children(child2.props.children));
|
|
113
186
|
}
|
|
114
187
|
return React.isValidElement(child) ? child : null;
|
|
115
|
-
};
|
|
116
|
-
function mergeProps(slotProps, childProps) {
|
|
117
|
-
const overrideProps = { ...childProps };
|
|
118
|
-
for (const propName in childProps) {
|
|
119
|
-
const slotPropValue = slotProps[propName];
|
|
120
|
-
const childPropValue = childProps[propName];
|
|
121
|
-
const isHandler = /^on[A-Z]/.test(propName);
|
|
122
|
-
if (isHandler) {
|
|
123
|
-
if (slotPropValue && childPropValue) {
|
|
124
|
-
overrideProps[propName] = (...args) => {
|
|
125
|
-
const result = childPropValue(...args);
|
|
126
|
-
slotPropValue(...args);
|
|
127
|
-
return result;
|
|
128
|
-
};
|
|
129
|
-
} else if (slotPropValue) {
|
|
130
|
-
overrideProps[propName] = slotPropValue;
|
|
131
|
-
}
|
|
132
|
-
} else if (propName === "style") {
|
|
133
|
-
overrideProps[propName] = { ...slotPropValue, ...childPropValue };
|
|
134
|
-
} else if (propName === "className") {
|
|
135
|
-
overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return { ...slotProps, ...overrideProps };
|
|
139
|
-
}
|
|
188
|
+
}, "getSlottableElementFromSlottable");
|
|
140
189
|
function getElementRef(element) {
|
|
141
190
|
let getter = Object.getOwnPropertyDescriptor(element.props, "ref")?.get;
|
|
142
191
|
let mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning;
|
|
@@ -150,21 +199,25 @@ function getElementRef(element) {
|
|
|
150
199
|
}
|
|
151
200
|
return element.props.ref || element.ref;
|
|
152
201
|
}
|
|
202
|
+
__name(getElementRef, "getElementRef");
|
|
153
203
|
function isSlottable(child) {
|
|
154
204
|
return React.isValidElement(child) && typeof child.type === "function" && "__radixId" in child.type && child.type.__radixId === SLOTTABLE_IDENTIFIER;
|
|
155
205
|
}
|
|
206
|
+
__name(isSlottable, "isSlottable");
|
|
156
207
|
var REACT_LAZY_TYPE = Symbol.for("react.lazy");
|
|
157
208
|
function isLazyComponent(element) {
|
|
158
209
|
return element != null && typeof element === "object" && "$$typeof" in element && element.$$typeof === REACT_LAZY_TYPE && "_payload" in element && isPromiseLike(element._payload);
|
|
159
210
|
}
|
|
211
|
+
__name(isLazyComponent, "isLazyComponent");
|
|
160
212
|
function isPromiseLike(value) {
|
|
161
213
|
return typeof value === "object" && value !== null && "then" in value;
|
|
162
214
|
}
|
|
163
|
-
|
|
215
|
+
__name(isPromiseLike, "isPromiseLike");
|
|
216
|
+
var createSlotError = /* @__PURE__ */ __name((ownerName) => {
|
|
164
217
|
return `${ownerName} failed to slot onto its children. Expected a single React element child or \`Slottable\`.`;
|
|
165
|
-
};
|
|
166
|
-
var createSlottableError = (ownerName) => {
|
|
218
|
+
}, "createSlotError");
|
|
219
|
+
var createSlottableError = /* @__PURE__ */ __name((ownerName) => {
|
|
167
220
|
return `${ownerName} failed to slot onto its \`Slottable\`. Expected \`Slottable\` to receive a single React element child.`;
|
|
168
|
-
};
|
|
221
|
+
}, "createSlottableError");
|
|
169
222
|
var use = React[" use ".trim().toString()];
|
|
170
223
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/index.ts", "../src/slot.tsx"],
|
|
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 { useComposedRefs } from '@radix-ui/react-compose-refs';\n\ndeclare module 'react' {\n interface ReactElement {\n $$typeof?: symbol | string;\n }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Slot\n * -----------------------------------------------------------------------------------------------*/\n\nexport type Usable<T> = PromiseLike<T> | React.Context<T>;\n\ntype SlotProps<Elem extends Element = HTMLElement, Props = React.HTMLAttributes<Elem>> = Props & {\n children?: React.ReactNode;\n};\n\n/* @__NO_SIDE_EFFECTS__ */ export function createSlot<\n Elem extends Element = HTMLElement,\n Props = React.HTMLAttributes<Elem>,\n>(ownerName: string) {\n const Slot = React.forwardRef<Elem, SlotProps<Elem, Props>>((props, forwardedRef) => {\n let { children, ...slotProps } = props;\n let slottableElement: React.ReactElement | null = null;\n let hasSlottable = false;\n const newChildren: React.ReactNode[] = [];\n\n if (isLazyComponent(children) && typeof use === 'function') {\n children = use(children._payload);\n }\n\n React.Children.forEach(children, (maybeSlottable) => {\n if (isSlottable(maybeSlottable)) {\n hasSlottable = true;\n const slottable = maybeSlottable;\n let child = 'child' in slottable.props ? slottable.props.child : slottable.props.children;\n\n if (isLazyComponent(child) && typeof use === 'function') {\n child = use(child._payload);\n }\n\n slottableElement = getSlottableElementFromSlottable(slottable, child);\n newChildren.push((slottableElement?.props as any)?.children);\n } else {\n newChildren.push(maybeSlottable);\n }\n });\n\n if (slottableElement) {\n slottableElement = React.cloneElement(slottableElement, undefined, newChildren);\n } else if (\n // A `Slottable` was found but it didn't resolve to a single element (e.g.\n // it wrapped multiple elements, text, or a render-prop `child` that\n // wasn't an element). Don't fall back to treating the `Slottable` wrapper\n // itself as the slot target \u2014 throw a descriptive error below instead.\n !hasSlottable &&\n React.Children.count(children) === 1 &&\n React.isValidElement(children)\n ) {\n slottableElement = children;\n }\n\n const slottableElementRef = slottableElement ? getElementRef(slottableElement) : undefined;\n const composedRef = useComposedRefs(forwardedRef, slottableElementRef);\n\n if (!slottableElement) {\n // Empty/falsy children (`null`, `undefined`, `false`, no children, etc.) are valid and\n // render nothing. Anything else is content we couldn't slot onto a single element, which\n // is a usage error, so we fail loudly with a clear message.\n if (children || children === 0) {\n throw new Error(\n hasSlottable ? createSlottableError(ownerName) : createSlotError(ownerName),\n );\n }\n return children;\n }\n\n const mergedProps = mergeProps(slotProps, slottableElement.props ?? {});\n\n // do not pass ref to React.Fragment for React 19 compatibility\n if (slottableElement.type !== React.Fragment) {\n mergedProps.ref = forwardedRef ? composedRef : slottableElementRef;\n }\n\n return React.cloneElement(slottableElement, mergedProps);\n });\n\n Slot.displayName = `${ownerName}.Slot`;\n return Slot;\n}\n\nconst Slot = createSlot('Slot');\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst SLOTTABLE_IDENTIFIER = Symbol.for('radix.slottable');\n\ntype SlottableChildrenProps = { children: React.ReactNode };\ntype SlottableRenderFnProps = {\n child: React.ReactNode;\n children: (slottable: React.ReactNode) => React.ReactNode;\n};\n\ntype SlottableProps = SlottableRenderFnProps | SlottableChildrenProps;\ninterface SlottableComponent extends React.FC<SlottableProps> {\n __radixId: symbol;\n}\n\n/* @__NO_SIDE_EFFECTS__ */ export function createSlottable(ownerName: string) {\n const Slottable: SlottableComponent = (props) =>\n 'child' in props ? props.children(props.child) : props.children;\n\n Slottable.displayName = `${ownerName}.Slottable`;\n Slottable.__radixId = SLOTTABLE_IDENTIFIER;\n return Slottable;\n}\n\nconst Slottable = createSlottable('Slottable');\n\n/* -------------------------------------------------------------------------------------------------\n * getSlottableElementFromSlottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst getSlottableElementFromSlottable = (slottable: SlottableElement, child: React.ReactNode) => {\n if ('child' in slottable.props) {\n const child = slottable.props.child;\n if (!React.isValidElement<React.PropsWithChildren>(child)) return null;\n return React.cloneElement(child, undefined, slottable.props.children(child.props.children));\n }\n\n return React.isValidElement(child) ? child : null;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * mergeProps\n * -----------------------------------------------------------------------------------------------*/\n\ntype AnyProps = Record<string, any>;\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 const result = childPropValue(...args);\n slotPropValue(...args);\n return result;\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/* -------------------------------------------------------------------------------------------------\n * getElementRef\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\n/* ---------------------------------------------------------------------------------------------- */\n\ntype SlottableElement = React.ReactElement<SlottableProps, SlottableComponent>;\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\nconst REACT_LAZY_TYPE = Symbol.for('react.lazy');\n\ninterface LazyReactElement extends React.ReactElement {\n $$typeof: typeof REACT_LAZY_TYPE;\n _payload: PromiseLike<Exclude<React.ReactNode, PromiseLike<any>>>;\n}\n\nfunction isLazyComponent(element: React.ReactNode): element is LazyReactElement {\n return (\n element != null &&\n typeof element === 'object' &&\n '$$typeof' in element &&\n element.$$typeof === REACT_LAZY_TYPE &&\n '_payload' in element &&\n isPromiseLike(element._payload)\n );\n}\n\nfunction isPromiseLike(value: unknown): value is PromiseLike<unknown> {\n return typeof value === 'object' && value !== null && 'then' in value;\n}\n\nconst createSlotError = (ownerName: string) => {\n return `${ownerName} failed to slot onto its children. Expected a single React element child or \\`Slottable\\`.`;\n};\n\nconst createSlottableError = (ownerName: string) => {\n return `${ownerName} failed to slot onto its \\`Slottable\\`. Expected \\`Slottable\\` to receive a single React element child.`;\n};\n\nconst use: typeof React.use | undefined = (React as any)[' use '.trim().toString()];\n\nexport {\n Slot,\n Slottable,\n //\n Slot as Root,\n};\nexport type { SlotProps };\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["Slot", "Slottable", "child"]
|
|
3
|
+
"sources": ["../src/index.ts", "../src/slot.tsx", "../src/merge-props.ts"],
|
|
4
|
+
"sourcesContent": ["'use client';\nexport {\n Slot,\n Slottable,\n SlotProvider,\n //\n Root,\n Provider,\n //\n createSlot,\n createSlottable,\n //\n mergeProps,\n} from './slot';\nexport type { SlotProps, MergePropsFunction } from './slot';\n", "import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport type { AnyProps, MergePropsFunction } from './merge-props';\nimport { mergeProps } from './merge-props';\n\ndeclare module 'react' {\n interface ReactElement {\n $$typeof?: symbol | string;\n }\n}\n\ntype SlotContextValue = MergePropsFunction;\n\nconst SlotContext = React.createContext<SlotContextValue>(mergeProps);\nSlotContext.displayName = 'SlotContext';\n\n/* -------------------------------------------------------------------------------------------------\n * SlotProvider\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotProviderProps {\n children: React.ReactNode;\n mergeProps: MergePropsFunction<AnyProps, AnyProps, AnyProps>;\n}\n\nconst SlotProvider: React.FC<SlotProviderProps> = ({ children, mergeProps }) => {\n return <SlotContext.Provider value={mergeProps}>{children}</SlotContext.Provider>;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * Slot\n * -----------------------------------------------------------------------------------------------*/\n\nexport type Usable<T> = PromiseLike<T> | React.Context<T>;\n\ntype SlotProps<Elem extends Element = HTMLElement, Props = React.HTMLAttributes<Elem>> = Props & {\n children?: React.ReactNode;\n mergeProps?: MergePropsFunction;\n};\n\n/* @__NO_SIDE_EFFECTS__ */ export function createSlot<\n Elem extends Element = HTMLElement,\n Props = React.HTMLAttributes<Elem>,\n>(ownerName: string) {\n const Slot = React.forwardRef<Elem, SlotProps<Elem, Props>>((props, forwardedRef) => {\n const context = React.useContext(SlotContext);\n let { children, mergeProps: mergePropsProp = context, ...slotProps } = props;\n let slottableElement: React.ReactElement | null = null;\n let hasSlottable = false;\n const newChildren: React.ReactNode[] = [];\n\n if (isLazyComponent(children) && typeof use === 'function') {\n children = use(children._payload);\n }\n\n React.Children.forEach(children, (maybeSlottable) => {\n if (isSlottable(maybeSlottable)) {\n hasSlottable = true;\n const slottable = maybeSlottable;\n let child = 'child' in slottable.props ? slottable.props.child : slottable.props.children;\n\n if (isLazyComponent(child) && typeof use === 'function') {\n child = use(child._payload);\n }\n\n slottableElement = getSlottableElementFromSlottable(slottable, child);\n newChildren.push((slottableElement?.props as any)?.children);\n } else {\n newChildren.push(maybeSlottable);\n }\n });\n\n if (slottableElement) {\n slottableElement = React.cloneElement(slottableElement, undefined, newChildren);\n } else if (\n // A `Slottable` was found but it didn't resolve to a single element (e.g.\n // it wrapped multiple elements, text, or a render-prop `child` that\n // wasn't an element). Don't fall back to treating the `Slottable` wrapper\n // itself as the slot target \u2014 throw a descriptive error below instead.\n !hasSlottable &&\n React.Children.count(children) === 1 &&\n React.isValidElement(children)\n ) {\n slottableElement = children;\n }\n\n const slottableElementRef = slottableElement ? getElementRef(slottableElement) : undefined;\n const composedRef = useComposedRefs(forwardedRef, slottableElementRef);\n\n if (!slottableElement) {\n // Empty/falsy children (`null`, `undefined`, `false`, no children, etc.)\n // are valid and render nothing. Anything else is content we couldn't slot\n // onto a single element, which is a usage error, so we fail loudly with a\n // clear message.\n if (children || children === 0) {\n throw new Error(\n hasSlottable ? createSlottableError(ownerName) : createSlotError(ownerName),\n );\n }\n return children;\n }\n\n const mergedProps = mergePropsProp(\n slotProps,\n (slottableElement.props ?? {}) as Record<string, unknown>,\n );\n\n // do not pass ref to React.Fragment for React 19 compatibility\n if (slottableElement.type !== React.Fragment) {\n mergedProps.ref = forwardedRef ? composedRef : slottableElementRef;\n }\n\n return React.cloneElement(slottableElement, mergedProps);\n });\n\n Slot.displayName = `${ownerName}.Slot`;\n return Slot;\n}\n\nconst Slot = createSlot('Slot');\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst SLOTTABLE_IDENTIFIER = Symbol.for('radix.slottable');\n\ntype SlottableChildrenProps = { children: React.ReactNode };\ntype SlottableRenderFnProps = {\n child: React.ReactNode;\n children: (slottable: React.ReactNode) => React.ReactNode;\n};\n\ntype SlottableProps = SlottableRenderFnProps | SlottableChildrenProps;\ninterface SlottableComponent extends React.FC<SlottableProps> {\n __radixId: symbol;\n}\n\n/* @__NO_SIDE_EFFECTS__ */ export function createSlottable(ownerName: string) {\n const Slottable: SlottableComponent = (props) =>\n 'child' in props ? props.children(props.child) : props.children;\n\n Slottable.displayName = `${ownerName}.Slottable`;\n Slottable.__radixId = SLOTTABLE_IDENTIFIER;\n return Slottable;\n}\n\nconst Slottable = createSlottable('Slottable');\n\n/* -------------------------------------------------------------------------------------------------\n * getSlottableElementFromSlottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst getSlottableElementFromSlottable = (slottable: SlottableElement, child: React.ReactNode) => {\n if ('child' in slottable.props) {\n const child = slottable.props.child;\n if (!React.isValidElement<React.PropsWithChildren>(child)) return null;\n return React.cloneElement(child, undefined, slottable.props.children(child.props.children));\n }\n\n return React.isValidElement(child) ? child : null;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * getElementRef\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\n/* ---------------------------------------------------------------------------------------------- */\n\ntype SlottableElement = React.ReactElement<SlottableProps, SlottableComponent>;\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\nconst REACT_LAZY_TYPE = Symbol.for('react.lazy');\n\ninterface LazyReactElement extends React.ReactElement {\n $$typeof: typeof REACT_LAZY_TYPE;\n _payload: PromiseLike<Exclude<React.ReactNode, PromiseLike<any>>>;\n}\n\nfunction isLazyComponent(element: React.ReactNode): element is LazyReactElement {\n return (\n element != null &&\n typeof element === 'object' &&\n '$$typeof' in element &&\n element.$$typeof === REACT_LAZY_TYPE &&\n '_payload' in element &&\n isPromiseLike(element._payload)\n );\n}\n\nfunction isPromiseLike(value: unknown): value is PromiseLike<unknown> {\n return typeof value === 'object' && value !== null && 'then' in value;\n}\n\nconst createSlotError = (ownerName: string) => {\n return `${ownerName} failed to slot onto its children. Expected a single React element child or \\`Slottable\\`.`;\n};\n\nconst createSlottableError = (ownerName: string) => {\n return `${ownerName} failed to slot onto its \\`Slottable\\`. Expected \\`Slottable\\` to receive a single React element child.`;\n};\n\nconst use: typeof React.use | undefined = (React as any)[' use '.trim().toString()];\n\nexport {\n Slot,\n Slottable,\n SlotProvider,\n //\n Slot as Root,\n SlotProvider as Provider,\n //\n mergeProps,\n};\nexport type { SlotProps };\nexport type { MergePropsFunction } from './merge-props';\n", "import { IS_DEVELOPMENT } from '@radix-ui/primitive/is-development';\n\nconst mergeProps = (<\n SlotProps extends AnyProps = AnyProps,\n ChildProps extends AnyProps = SlotProps,\n ReturnProps extends AnyProps = SlotProps & ChildProps,\n>(\n slotProps: SlotProps,\n childProps: ChildProps,\n): ReturnProps => {\n const overrideProps = { ...childProps };\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 const slotIsFunction = typeof slotPropValue === 'function';\n const childIsFunction = typeof childPropValue === 'function';\n if (IS_DEVELOPMENT) {\n if (!slotIsFunction || !childIsFunction) {\n console.warn(\n `Slot: Expected a function for ${propName}, but received ${[typeof slotPropValue, typeof childPropValue].filter((v) => v !== 'function').join(' and ')}.`,\n );\n }\n }\n\n // @ts-expect-error - we don't know the type of the function. This\n // technically makes the return signature a lie, not sure if it's worth\n // handling.\n overrideProps[propName] = (...args: unknown[]) => {\n const result = childIsFunction ? childPropValue(...args) : undefined;\n if (slotIsFunction) {\n slotPropValue(...args);\n }\n return result;\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\n // if it's `style`, we merge them\n else if (propName === 'style') {\n (overrideProps as any)[propName] = {\n ...(typeof slotPropValue === 'object' ? slotPropValue : null),\n ...(typeof childPropValue === 'object' ? childPropValue : null),\n };\n } else if (propName === 'className') {\n (overrideProps as any)[propName] = [slotPropValue, childPropValue].filter(Boolean).join(' ');\n } else if (propName === 'aria-describedby') {\n (overrideProps as any)[propName] = concatAriaDescribedby(childPropValue, slotPropValue);\n }\n }\n\n return { ...slotProps, ...overrideProps } as ReturnProps;\n}) satisfies MergePropsFunction;\n\n// TODO: Move to primitive once that package exposed individual sub-modules\nfunction concatAriaDescribedby(...values: unknown[]): string | undefined {\n const ids = new Set<string>();\n for (const value of values) {\n if (typeof value !== 'string') continue;\n for (const id of String(value).trim().split(/\\s+/)) {\n if (id) ids.add(id);\n }\n }\n\n return ids.size > 0 ? Array.from(ids).join(' ') : undefined;\n}\n\ninterface MergePropsFunction<\n SlotProps extends AnyProps = UnknownProps,\n ChildProps extends AnyProps = SlotProps,\n ReturnProps extends AnyProps = SlotProps & ChildProps,\n> {\n (slotProps: SlotProps, childProps: ChildProps): ReturnProps;\n}\n\ntype AnyProps = Record<string, any>;\ntype UnknownProps = Record<string, unknown>;\n\nexport { mergeProps };\nexport type { MergePropsFunction, AnyProps, UnknownProps };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,gCAAgC;;;ACDhC,4BAA+B;AAE/B,IAAM,aAAc,yBAKlB,WACA,eACgB;AAChB,QAAM,gBAAgB,EAAE,GAAG,WAAW;AACtC,aAAW,YAAY,YAAY;AACjC,UAAM,gBAAgB,UAAU,QAAQ;AACxC,UAAM,iBAAiB,WAAW,QAAQ;AAE1C,UAAM,YAAY,WAAW,KAAK,QAAQ;AAC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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;AAjBS;AAuBT,SAAS,YACP,OAC+D;AAC/D,SACQ,qBAAe,KAAK,KAC1B,OAAO,MAAM,SAAS,cACtB,eAAe,MAAM,QACrB,MAAM,KAAK,cAAc;AAE7B;AATS;AAWT,IAAM,kBAAkB,OAAO,IAAI,YAAY;AAO/C,SAAS,gBAAgB,SAAuD;AAC9E,SACE,WAAW,QACX,OAAO,YAAY,YACnB,cAAc,WACd,QAAQ,aAAa,mBACrB,cAAc,WACd,cAAc,QAAQ,QAAQ;AAElC;AATS;AAWT,SAAS,cAAc,OAA+C;AACpE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,UAAU;AAClE;AAFS;AAIT,IAAM,kBAAkB,wBAAC,cAAsB;AAC7C,SAAO,GAAG,SAAS;AACrB,GAFwB;AAIxB,IAAM,uBAAuB,wBAAC,cAAsB;AAClD,SAAO,GAAG,SAAS;AACrB,GAF6B;AAI7B,IAAM,MAAqC,MAAc,QAAQ,KAAK,EAAE,SAAS,CAAC;",
|
|
6
|
+
"names": ["mergeProps", "Slot", "Slottable", "child"]
|
|
7
7
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,77 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
|
|
1
5
|
// src/slot.tsx
|
|
2
6
|
import * as React from "react";
|
|
3
7
|
import { useComposedRefs } from "@radix-ui/react-compose-refs";
|
|
8
|
+
|
|
9
|
+
// src/merge-props.ts
|
|
10
|
+
import { IS_DEVELOPMENT } from "@radix-ui/primitive/is-development";
|
|
11
|
+
var mergeProps = /* @__PURE__ */ __name(((slotProps, childProps) => {
|
|
12
|
+
const overrideProps = { ...childProps };
|
|
13
|
+
for (const propName in childProps) {
|
|
14
|
+
const slotPropValue = slotProps[propName];
|
|
15
|
+
const childPropValue = childProps[propName];
|
|
16
|
+
const isHandler = /^on[A-Z]/.test(propName);
|
|
17
|
+
if (isHandler) {
|
|
18
|
+
if (slotPropValue && childPropValue) {
|
|
19
|
+
const slotIsFunction = typeof slotPropValue === "function";
|
|
20
|
+
const childIsFunction = typeof childPropValue === "function";
|
|
21
|
+
if (IS_DEVELOPMENT) {
|
|
22
|
+
if (!slotIsFunction || !childIsFunction) {
|
|
23
|
+
console.warn(
|
|
24
|
+
`Slot: Expected a function for ${propName}, but received ${[typeof slotPropValue, typeof childPropValue].filter((v) => v !== "function").join(" and ")}.`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
overrideProps[propName] = (...args) => {
|
|
29
|
+
const result = childIsFunction ? childPropValue(...args) : void 0;
|
|
30
|
+
if (slotIsFunction) {
|
|
31
|
+
slotPropValue(...args);
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
} else if (slotPropValue) {
|
|
36
|
+
overrideProps[propName] = slotPropValue;
|
|
37
|
+
}
|
|
38
|
+
} else if (propName === "style") {
|
|
39
|
+
overrideProps[propName] = {
|
|
40
|
+
...typeof slotPropValue === "object" ? slotPropValue : null,
|
|
41
|
+
...typeof childPropValue === "object" ? childPropValue : null
|
|
42
|
+
};
|
|
43
|
+
} else if (propName === "className") {
|
|
44
|
+
overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
|
|
45
|
+
} else if (propName === "aria-describedby") {
|
|
46
|
+
overrideProps[propName] = concatAriaDescribedby(childPropValue, slotPropValue);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { ...slotProps, ...overrideProps };
|
|
50
|
+
}), "mergeProps");
|
|
51
|
+
function concatAriaDescribedby(...values) {
|
|
52
|
+
const ids = /* @__PURE__ */ new Set();
|
|
53
|
+
for (const value of values) {
|
|
54
|
+
if (typeof value !== "string") continue;
|
|
55
|
+
for (const id of String(value).trim().split(/\s+/)) {
|
|
56
|
+
if (id) ids.add(id);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return ids.size > 0 ? Array.from(ids).join(" ") : void 0;
|
|
60
|
+
}
|
|
61
|
+
__name(concatAriaDescribedby, "concatAriaDescribedby");
|
|
62
|
+
|
|
63
|
+
// src/slot.tsx
|
|
64
|
+
import { jsx } from "react/jsx-runtime";
|
|
65
|
+
var SlotContext = React.createContext(mergeProps);
|
|
66
|
+
SlotContext.displayName = "SlotContext";
|
|
67
|
+
var SlotProvider = /* @__PURE__ */ __name(({ children, mergeProps: mergeProps2 }) => {
|
|
68
|
+
return /* @__PURE__ */ jsx(SlotContext.Provider, { value: mergeProps2, children });
|
|
69
|
+
}, "SlotProvider");
|
|
4
70
|
// @__NO_SIDE_EFFECTS__
|
|
5
71
|
function createSlot(ownerName) {
|
|
6
72
|
const Slot2 = React.forwardRef((props, forwardedRef) => {
|
|
7
|
-
|
|
73
|
+
const context = React.useContext(SlotContext);
|
|
74
|
+
let { children, mergeProps: mergePropsProp = context, ...slotProps } = props;
|
|
8
75
|
let slottableElement = null;
|
|
9
76
|
let hasSlottable = false;
|
|
10
77
|
const newChildren = [];
|
|
@@ -46,7 +113,10 @@ function createSlot(ownerName) {
|
|
|
46
113
|
}
|
|
47
114
|
return children;
|
|
48
115
|
}
|
|
49
|
-
const mergedProps =
|
|
116
|
+
const mergedProps = mergePropsProp(
|
|
117
|
+
slotProps,
|
|
118
|
+
slottableElement.props ?? {}
|
|
119
|
+
);
|
|
50
120
|
if (slottableElement.type !== React.Fragment) {
|
|
51
121
|
mergedProps.ref = forwardedRef ? composedRef : slottableElementRef;
|
|
52
122
|
}
|
|
@@ -55,48 +125,26 @@ function createSlot(ownerName) {
|
|
|
55
125
|
Slot2.displayName = `${ownerName}.Slot`;
|
|
56
126
|
return Slot2;
|
|
57
127
|
}
|
|
128
|
+
__name(createSlot, "createSlot");
|
|
58
129
|
var Slot = /* @__PURE__ */ createSlot("Slot");
|
|
59
130
|
var SLOTTABLE_IDENTIFIER = Symbol.for("radix.slottable");
|
|
60
131
|
// @__NO_SIDE_EFFECTS__
|
|
61
132
|
function createSlottable(ownerName) {
|
|
62
|
-
const Slottable2 = (props) => "child" in props ? props.children(props.child) : props.children;
|
|
133
|
+
const Slottable2 = /* @__PURE__ */ __name((props) => "child" in props ? props.children(props.child) : props.children, "Slottable");
|
|
63
134
|
Slottable2.displayName = `${ownerName}.Slottable`;
|
|
64
135
|
Slottable2.__radixId = SLOTTABLE_IDENTIFIER;
|
|
65
136
|
return Slottable2;
|
|
66
137
|
}
|
|
138
|
+
__name(createSlottable, "createSlottable");
|
|
67
139
|
var Slottable = /* @__PURE__ */ createSlottable("Slottable");
|
|
68
|
-
var getSlottableElementFromSlottable = (slottable, child) => {
|
|
140
|
+
var getSlottableElementFromSlottable = /* @__PURE__ */ __name((slottable, child) => {
|
|
69
141
|
if ("child" in slottable.props) {
|
|
70
142
|
const child2 = slottable.props.child;
|
|
71
143
|
if (!React.isValidElement(child2)) return null;
|
|
72
144
|
return React.cloneElement(child2, void 0, slottable.props.children(child2.props.children));
|
|
73
145
|
}
|
|
74
146
|
return React.isValidElement(child) ? child : null;
|
|
75
|
-
};
|
|
76
|
-
function mergeProps(slotProps, childProps) {
|
|
77
|
-
const overrideProps = { ...childProps };
|
|
78
|
-
for (const propName in childProps) {
|
|
79
|
-
const slotPropValue = slotProps[propName];
|
|
80
|
-
const childPropValue = childProps[propName];
|
|
81
|
-
const isHandler = /^on[A-Z]/.test(propName);
|
|
82
|
-
if (isHandler) {
|
|
83
|
-
if (slotPropValue && childPropValue) {
|
|
84
|
-
overrideProps[propName] = (...args) => {
|
|
85
|
-
const result = childPropValue(...args);
|
|
86
|
-
slotPropValue(...args);
|
|
87
|
-
return result;
|
|
88
|
-
};
|
|
89
|
-
} else if (slotPropValue) {
|
|
90
|
-
overrideProps[propName] = slotPropValue;
|
|
91
|
-
}
|
|
92
|
-
} else if (propName === "style") {
|
|
93
|
-
overrideProps[propName] = { ...slotPropValue, ...childPropValue };
|
|
94
|
-
} else if (propName === "className") {
|
|
95
|
-
overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return { ...slotProps, ...overrideProps };
|
|
99
|
-
}
|
|
147
|
+
}, "getSlottableElementFromSlottable");
|
|
100
148
|
function getElementRef(element) {
|
|
101
149
|
let getter = Object.getOwnPropertyDescriptor(element.props, "ref")?.get;
|
|
102
150
|
let mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning;
|
|
@@ -110,28 +158,35 @@ function getElementRef(element) {
|
|
|
110
158
|
}
|
|
111
159
|
return element.props.ref || element.ref;
|
|
112
160
|
}
|
|
161
|
+
__name(getElementRef, "getElementRef");
|
|
113
162
|
function isSlottable(child) {
|
|
114
163
|
return React.isValidElement(child) && typeof child.type === "function" && "__radixId" in child.type && child.type.__radixId === SLOTTABLE_IDENTIFIER;
|
|
115
164
|
}
|
|
165
|
+
__name(isSlottable, "isSlottable");
|
|
116
166
|
var REACT_LAZY_TYPE = Symbol.for("react.lazy");
|
|
117
167
|
function isLazyComponent(element) {
|
|
118
168
|
return element != null && typeof element === "object" && "$$typeof" in element && element.$$typeof === REACT_LAZY_TYPE && "_payload" in element && isPromiseLike(element._payload);
|
|
119
169
|
}
|
|
170
|
+
__name(isLazyComponent, "isLazyComponent");
|
|
120
171
|
function isPromiseLike(value) {
|
|
121
172
|
return typeof value === "object" && value !== null && "then" in value;
|
|
122
173
|
}
|
|
123
|
-
|
|
174
|
+
__name(isPromiseLike, "isPromiseLike");
|
|
175
|
+
var createSlotError = /* @__PURE__ */ __name((ownerName) => {
|
|
124
176
|
return `${ownerName} failed to slot onto its children. Expected a single React element child or \`Slottable\`.`;
|
|
125
|
-
};
|
|
126
|
-
var createSlottableError = (ownerName) => {
|
|
177
|
+
}, "createSlotError");
|
|
178
|
+
var createSlottableError = /* @__PURE__ */ __name((ownerName) => {
|
|
127
179
|
return `${ownerName} failed to slot onto its \`Slottable\`. Expected \`Slottable\` to receive a single React element child.`;
|
|
128
|
-
};
|
|
180
|
+
}, "createSlottableError");
|
|
129
181
|
var use = React[" use ".trim().toString()];
|
|
130
182
|
export {
|
|
183
|
+
SlotProvider as Provider,
|
|
131
184
|
Slot as Root,
|
|
132
185
|
Slot,
|
|
186
|
+
SlotProvider,
|
|
133
187
|
Slottable,
|
|
134
188
|
createSlot,
|
|
135
|
-
createSlottable
|
|
189
|
+
createSlottable,
|
|
190
|
+
mergeProps
|
|
136
191
|
};
|
|
137
192
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/slot.tsx"],
|
|
4
|
-
"sourcesContent": ["import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\n\ndeclare module 'react' {\n interface ReactElement {\n $$typeof?: symbol | string;\n }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Slot\n * -----------------------------------------------------------------------------------------------*/\n\nexport type Usable<T> = PromiseLike<T> | React.Context<T>;\n\ntype SlotProps<Elem extends Element = HTMLElement, Props = React.HTMLAttributes<Elem>> = Props & {\n children?: React.ReactNode;\n};\n\n/* @__NO_SIDE_EFFECTS__ */ export function createSlot<\n Elem extends Element = HTMLElement,\n Props = React.HTMLAttributes<Elem>,\n>(ownerName: string) {\n const Slot = React.forwardRef<Elem, SlotProps<Elem, Props>>((props, forwardedRef) => {\n let { children, ...slotProps } = props;\n let slottableElement: React.ReactElement | null = null;\n let hasSlottable = false;\n const newChildren: React.ReactNode[] = [];\n\n if (isLazyComponent(children) && typeof use === 'function') {\n children = use(children._payload);\n }\n\n React.Children.forEach(children, (maybeSlottable) => {\n if (isSlottable(maybeSlottable)) {\n hasSlottable = true;\n const slottable = maybeSlottable;\n let child = 'child' in slottable.props ? slottable.props.child : slottable.props.children;\n\n if (isLazyComponent(child) && typeof use === 'function') {\n child = use(child._payload);\n }\n\n slottableElement = getSlottableElementFromSlottable(slottable, child);\n newChildren.push((slottableElement?.props as any)?.children);\n } else {\n newChildren.push(maybeSlottable);\n }\n });\n\n if (slottableElement) {\n slottableElement = React.cloneElement(slottableElement, undefined, newChildren);\n } else if (\n // A `Slottable` was found but it didn't resolve to a single element (e.g.\n // it wrapped multiple elements, text, or a render-prop `child` that\n // wasn't an element). Don't fall back to treating the `Slottable` wrapper\n // itself as the slot target \u2014 throw a descriptive error below instead.\n !hasSlottable &&\n React.Children.count(children) === 1 &&\n React.isValidElement(children)\n ) {\n slottableElement = children;\n }\n\n const slottableElementRef = slottableElement ? getElementRef(slottableElement) : undefined;\n const composedRef = useComposedRefs(forwardedRef, slottableElementRef);\n\n if (!slottableElement) {\n // Empty/falsy children (`null`, `undefined`, `false`, no children, etc.) are valid and\n // render nothing. Anything else is content we couldn't slot onto a single element, which\n // is a usage error, so we fail loudly with a clear message.\n if (children || children === 0) {\n throw new Error(\n hasSlottable ? createSlottableError(ownerName) : createSlotError(ownerName),\n );\n }\n return children;\n }\n\n const mergedProps = mergeProps(slotProps, slottableElement.props ?? {});\n\n // do not pass ref to React.Fragment for React 19 compatibility\n if (slottableElement.type !== React.Fragment) {\n mergedProps.ref = forwardedRef ? composedRef : slottableElementRef;\n }\n\n return React.cloneElement(slottableElement, mergedProps);\n });\n\n Slot.displayName = `${ownerName}.Slot`;\n return Slot;\n}\n\nconst Slot = createSlot('Slot');\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst SLOTTABLE_IDENTIFIER = Symbol.for('radix.slottable');\n\ntype SlottableChildrenProps = { children: React.ReactNode };\ntype SlottableRenderFnProps = {\n child: React.ReactNode;\n children: (slottable: React.ReactNode) => React.ReactNode;\n};\n\ntype SlottableProps = SlottableRenderFnProps | SlottableChildrenProps;\ninterface SlottableComponent extends React.FC<SlottableProps> {\n __radixId: symbol;\n}\n\n/* @__NO_SIDE_EFFECTS__ */ export function createSlottable(ownerName: string) {\n const Slottable: SlottableComponent = (props) =>\n 'child' in props ? props.children(props.child) : props.children;\n\n Slottable.displayName = `${ownerName}.Slottable`;\n Slottable.__radixId = SLOTTABLE_IDENTIFIER;\n return Slottable;\n}\n\nconst Slottable = createSlottable('Slottable');\n\n/* -------------------------------------------------------------------------------------------------\n * getSlottableElementFromSlottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst getSlottableElementFromSlottable = (slottable: SlottableElement, child: React.ReactNode) => {\n if ('child' in slottable.props) {\n const child = slottable.props.child;\n if (!React.isValidElement<React.PropsWithChildren>(child)) return null;\n return React.cloneElement(child, undefined, slottable.props.children(child.props.children));\n }\n\n return React.isValidElement(child) ? child : null;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * mergeProps\n * -----------------------------------------------------------------------------------------------*/\n\ntype AnyProps = Record<string, any>;\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 const result = childPropValue(...args);\n slotPropValue(...args);\n return result;\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/* -------------------------------------------------------------------------------------------------\n * getElementRef\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\n/* ---------------------------------------------------------------------------------------------- */\n\ntype SlottableElement = React.ReactElement<SlottableProps, SlottableComponent>;\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\nconst REACT_LAZY_TYPE = Symbol.for('react.lazy');\n\ninterface LazyReactElement extends React.ReactElement {\n $$typeof: typeof REACT_LAZY_TYPE;\n _payload: PromiseLike<Exclude<React.ReactNode, PromiseLike<any>>>;\n}\n\nfunction isLazyComponent(element: React.ReactNode): element is LazyReactElement {\n return (\n element != null &&\n typeof element === 'object' &&\n '$$typeof' in element &&\n element.$$typeof === REACT_LAZY_TYPE &&\n '_payload' in element &&\n isPromiseLike(element._payload)\n );\n}\n\nfunction isPromiseLike(value: unknown): value is PromiseLike<unknown> {\n return typeof value === 'object' && value !== null && 'then' in value;\n}\n\nconst createSlotError = (ownerName: string) => {\n return `${ownerName} failed to slot onto its children. Expected a single React element child or \\`Slottable\\`.`;\n};\n\nconst createSlottableError = (ownerName: string) => {\n return `${ownerName} failed to slot onto its \\`Slottable\\`. Expected \\`Slottable\\` to receive a single React element child.`;\n};\n\nconst use: typeof React.use | undefined = (React as any)[' use '.trim().toString()];\n\nexport {\n Slot,\n Slottable,\n //\n Slot as Root,\n};\nexport type { SlotProps };\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["Slot", "Slottable", "child"]
|
|
3
|
+
"sources": ["../src/slot.tsx", "../src/merge-props.ts"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport type { AnyProps, MergePropsFunction } from './merge-props';\nimport { mergeProps } from './merge-props';\n\ndeclare module 'react' {\n interface ReactElement {\n $$typeof?: symbol | string;\n }\n}\n\ntype SlotContextValue = MergePropsFunction;\n\nconst SlotContext = React.createContext<SlotContextValue>(mergeProps);\nSlotContext.displayName = 'SlotContext';\n\n/* -------------------------------------------------------------------------------------------------\n * SlotProvider\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SlotProviderProps {\n children: React.ReactNode;\n mergeProps: MergePropsFunction<AnyProps, AnyProps, AnyProps>;\n}\n\nconst SlotProvider: React.FC<SlotProviderProps> = ({ children, mergeProps }) => {\n return <SlotContext.Provider value={mergeProps}>{children}</SlotContext.Provider>;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * Slot\n * -----------------------------------------------------------------------------------------------*/\n\nexport type Usable<T> = PromiseLike<T> | React.Context<T>;\n\ntype SlotProps<Elem extends Element = HTMLElement, Props = React.HTMLAttributes<Elem>> = Props & {\n children?: React.ReactNode;\n mergeProps?: MergePropsFunction;\n};\n\n/* @__NO_SIDE_EFFECTS__ */ export function createSlot<\n Elem extends Element = HTMLElement,\n Props = React.HTMLAttributes<Elem>,\n>(ownerName: string) {\n const Slot = React.forwardRef<Elem, SlotProps<Elem, Props>>((props, forwardedRef) => {\n const context = React.useContext(SlotContext);\n let { children, mergeProps: mergePropsProp = context, ...slotProps } = props;\n let slottableElement: React.ReactElement | null = null;\n let hasSlottable = false;\n const newChildren: React.ReactNode[] = [];\n\n if (isLazyComponent(children) && typeof use === 'function') {\n children = use(children._payload);\n }\n\n React.Children.forEach(children, (maybeSlottable) => {\n if (isSlottable(maybeSlottable)) {\n hasSlottable = true;\n const slottable = maybeSlottable;\n let child = 'child' in slottable.props ? slottable.props.child : slottable.props.children;\n\n if (isLazyComponent(child) && typeof use === 'function') {\n child = use(child._payload);\n }\n\n slottableElement = getSlottableElementFromSlottable(slottable, child);\n newChildren.push((slottableElement?.props as any)?.children);\n } else {\n newChildren.push(maybeSlottable);\n }\n });\n\n if (slottableElement) {\n slottableElement = React.cloneElement(slottableElement, undefined, newChildren);\n } else if (\n // A `Slottable` was found but it didn't resolve to a single element (e.g.\n // it wrapped multiple elements, text, or a render-prop `child` that\n // wasn't an element). Don't fall back to treating the `Slottable` wrapper\n // itself as the slot target \u2014 throw a descriptive error below instead.\n !hasSlottable &&\n React.Children.count(children) === 1 &&\n React.isValidElement(children)\n ) {\n slottableElement = children;\n }\n\n const slottableElementRef = slottableElement ? getElementRef(slottableElement) : undefined;\n const composedRef = useComposedRefs(forwardedRef, slottableElementRef);\n\n if (!slottableElement) {\n // Empty/falsy children (`null`, `undefined`, `false`, no children, etc.)\n // are valid and render nothing. Anything else is content we couldn't slot\n // onto a single element, which is a usage error, so we fail loudly with a\n // clear message.\n if (children || children === 0) {\n throw new Error(\n hasSlottable ? createSlottableError(ownerName) : createSlotError(ownerName),\n );\n }\n return children;\n }\n\n const mergedProps = mergePropsProp(\n slotProps,\n (slottableElement.props ?? {}) as Record<string, unknown>,\n );\n\n // do not pass ref to React.Fragment for React 19 compatibility\n if (slottableElement.type !== React.Fragment) {\n mergedProps.ref = forwardedRef ? composedRef : slottableElementRef;\n }\n\n return React.cloneElement(slottableElement, mergedProps);\n });\n\n Slot.displayName = `${ownerName}.Slot`;\n return Slot;\n}\n\nconst Slot = createSlot('Slot');\n\n/* -------------------------------------------------------------------------------------------------\n * Slottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst SLOTTABLE_IDENTIFIER = Symbol.for('radix.slottable');\n\ntype SlottableChildrenProps = { children: React.ReactNode };\ntype SlottableRenderFnProps = {\n child: React.ReactNode;\n children: (slottable: React.ReactNode) => React.ReactNode;\n};\n\ntype SlottableProps = SlottableRenderFnProps | SlottableChildrenProps;\ninterface SlottableComponent extends React.FC<SlottableProps> {\n __radixId: symbol;\n}\n\n/* @__NO_SIDE_EFFECTS__ */ export function createSlottable(ownerName: string) {\n const Slottable: SlottableComponent = (props) =>\n 'child' in props ? props.children(props.child) : props.children;\n\n Slottable.displayName = `${ownerName}.Slottable`;\n Slottable.__radixId = SLOTTABLE_IDENTIFIER;\n return Slottable;\n}\n\nconst Slottable = createSlottable('Slottable');\n\n/* -------------------------------------------------------------------------------------------------\n * getSlottableElementFromSlottable\n * -----------------------------------------------------------------------------------------------*/\n\nconst getSlottableElementFromSlottable = (slottable: SlottableElement, child: React.ReactNode) => {\n if ('child' in slottable.props) {\n const child = slottable.props.child;\n if (!React.isValidElement<React.PropsWithChildren>(child)) return null;\n return React.cloneElement(child, undefined, slottable.props.children(child.props.children));\n }\n\n return React.isValidElement(child) ? child : null;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * getElementRef\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\n/* ---------------------------------------------------------------------------------------------- */\n\ntype SlottableElement = React.ReactElement<SlottableProps, SlottableComponent>;\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\nconst REACT_LAZY_TYPE = Symbol.for('react.lazy');\n\ninterface LazyReactElement extends React.ReactElement {\n $$typeof: typeof REACT_LAZY_TYPE;\n _payload: PromiseLike<Exclude<React.ReactNode, PromiseLike<any>>>;\n}\n\nfunction isLazyComponent(element: React.ReactNode): element is LazyReactElement {\n return (\n element != null &&\n typeof element === 'object' &&\n '$$typeof' in element &&\n element.$$typeof === REACT_LAZY_TYPE &&\n '_payload' in element &&\n isPromiseLike(element._payload)\n );\n}\n\nfunction isPromiseLike(value: unknown): value is PromiseLike<unknown> {\n return typeof value === 'object' && value !== null && 'then' in value;\n}\n\nconst createSlotError = (ownerName: string) => {\n return `${ownerName} failed to slot onto its children. Expected a single React element child or \\`Slottable\\`.`;\n};\n\nconst createSlottableError = (ownerName: string) => {\n return `${ownerName} failed to slot onto its \\`Slottable\\`. Expected \\`Slottable\\` to receive a single React element child.`;\n};\n\nconst use: typeof React.use | undefined = (React as any)[' use '.trim().toString()];\n\nexport {\n Slot,\n Slottable,\n SlotProvider,\n //\n Slot as Root,\n SlotProvider as Provider,\n //\n mergeProps,\n};\nexport type { SlotProps };\nexport type { MergePropsFunction } from './merge-props';\n", "import { IS_DEVELOPMENT } from '@radix-ui/primitive/is-development';\n\nconst mergeProps = (<\n SlotProps extends AnyProps = AnyProps,\n ChildProps extends AnyProps = SlotProps,\n ReturnProps extends AnyProps = SlotProps & ChildProps,\n>(\n slotProps: SlotProps,\n childProps: ChildProps,\n): ReturnProps => {\n const overrideProps = { ...childProps };\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 const slotIsFunction = typeof slotPropValue === 'function';\n const childIsFunction = typeof childPropValue === 'function';\n if (IS_DEVELOPMENT) {\n if (!slotIsFunction || !childIsFunction) {\n console.warn(\n `Slot: Expected a function for ${propName}, but received ${[typeof slotPropValue, typeof childPropValue].filter((v) => v !== 'function').join(' and ')}.`,\n );\n }\n }\n\n // @ts-expect-error - we don't know the type of the function. This\n // technically makes the return signature a lie, not sure if it's worth\n // handling.\n overrideProps[propName] = (...args: unknown[]) => {\n const result = childIsFunction ? childPropValue(...args) : undefined;\n if (slotIsFunction) {\n slotPropValue(...args);\n }\n return result;\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\n // if it's `style`, we merge them\n else if (propName === 'style') {\n (overrideProps as any)[propName] = {\n ...(typeof slotPropValue === 'object' ? slotPropValue : null),\n ...(typeof childPropValue === 'object' ? childPropValue : null),\n };\n } else if (propName === 'className') {\n (overrideProps as any)[propName] = [slotPropValue, childPropValue].filter(Boolean).join(' ');\n } else if (propName === 'aria-describedby') {\n (overrideProps as any)[propName] = concatAriaDescribedby(childPropValue, slotPropValue);\n }\n }\n\n return { ...slotProps, ...overrideProps } as ReturnProps;\n}) satisfies MergePropsFunction;\n\n// TODO: Move to primitive once that package exposed individual sub-modules\nfunction concatAriaDescribedby(...values: unknown[]): string | undefined {\n const ids = new Set<string>();\n for (const value of values) {\n if (typeof value !== 'string') continue;\n for (const id of String(value).trim().split(/\\s+/)) {\n if (id) ids.add(id);\n }\n }\n\n return ids.size > 0 ? Array.from(ids).join(' ') : undefined;\n}\n\ninterface MergePropsFunction<\n SlotProps extends AnyProps = UnknownProps,\n ChildProps extends AnyProps = SlotProps,\n ReturnProps extends AnyProps = SlotProps & ChildProps,\n> {\n (slotProps: SlotProps, childProps: ChildProps): ReturnProps;\n}\n\ntype AnyProps = Record<string, any>;\ntype UnknownProps = Record<string, unknown>;\n\nexport { mergeProps };\nexport type { MergePropsFunction, AnyProps, UnknownProps };\n"],
|
|
5
|
+
"mappings": ";;;;;AAAA,YAAY,WAAW;AACvB,SAAS,uBAAuB;;;ACDhC,SAAS,sBAAsB;AAE/B,IAAM,aAAc,yBAKlB,WACA,eACgB;AAChB,QAAM,gBAAgB,EAAE,GAAG,WAAW;AACtC,aAAW,YAAY,YAAY;AACjC,UAAM,gBAAgB,UAAU,QAAQ;AACxC,UAAM,iBAAiB,WAAW,QAAQ;AAE1C,UAAM,YAAY,WAAW,KAAK,QAAQ;AAC1C,QAAI,WAAW;AAEb,UAAI,iBAAiB,gBAAgB;AACnC,cAAM,iBAAiB,OAAO,kBAAkB;AAChD,cAAM,kBAAkB,OAAO,mBAAmB;AAClD,YAAI,gBAAgB;AAClB,cAAI,CAAC,kBAAkB,CAAC,iBAAiB;AACvC,oBAAQ;AAAA,cACN,iCAAiC,QAAQ,kBAAkB,CAAC,OAAO,eAAe,OAAO,cAAc,EAAE,OAAO,CAAC,MAAM,MAAM,UAAU,EAAE,KAAK,OAAO,CAAC;AAAA,YACxJ;AAAA,UACF;AAAA,QACF;AAKA,sBAAc,QAAQ,IAAI,IAAI,SAAoB;AAChD,gBAAM,SAAS,kBAAkB,eAAe,GAAG,IAAI,IAAI;AAC3D,cAAI,gBAAgB;AAClB,0BAAc,GAAG,IAAI;AAAA,UACvB;AACA,iBAAO;AAAA,QACT;AAAA,MACF,WAES,eAAe;AACtB,sBAAc,QAAQ,IAAI;AAAA,MAC5B;AAAA,IACF,WAGS,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YACnB,cAAc,WACd,QAAQ,aAAa,mBACrB,cAAc,WACd,cAAc,QAAQ,QAAQ;AAElC;AATS;AAWT,SAAS,cAAc,OAA+C;AACpE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,UAAU;AAClE;AAFS;AAIT,IAAM,kBAAkB,wBAAC,cAAsB;AAC7C,SAAO,GAAG,SAAS;AACrB,GAFwB;AAIxB,IAAM,uBAAuB,wBAAC,cAAsB;AAClD,SAAO,GAAG,SAAS;AACrB,GAF6B;AAI7B,IAAM,MAAqC,MAAc,QAAQ,KAAK,EAAE,SAAS,CAAC;",
|
|
6
|
+
"names": ["mergeProps", "Slot", "Slottable", "child"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/react-slot",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,16 +11,17 @@
|
|
|
11
11
|
],
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@radix-ui/
|
|
14
|
+
"@radix-ui/primitive": "1.1.7",
|
|
15
|
+
"@radix-ui/react-compose-refs": "1.1.4"
|
|
15
16
|
},
|
|
16
17
|
"devDependencies": {
|
|
17
|
-
"@types/react": "^19.2.
|
|
18
|
-
"@types/react-dom": "^19.2.
|
|
19
|
-
"react": "^19.2.
|
|
20
|
-
"react-dom": "^19.2.
|
|
18
|
+
"@types/react": "^19.2.17",
|
|
19
|
+
"@types/react-dom": "^19.2.3",
|
|
20
|
+
"react": "^19.2.7",
|
|
21
|
+
"react-dom": "^19.2.7",
|
|
21
22
|
"typescript": "^5.9.3",
|
|
22
|
-
"@repo/
|
|
23
|
-
"@repo/
|
|
23
|
+
"@repo/builder": "0.0.0",
|
|
24
|
+
"@repo/typescript-config": "0.0.0"
|
|
24
25
|
},
|
|
25
26
|
"peerDependencies": {
|
|
26
27
|
"@types/react": "*",
|