@glob-ui/slot 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +16 -2
- package/dist/index.d.mts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +112 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +86 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +64 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Martínez E
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @glob-ui/slot
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Renders its single child with the props, className, handlers and ref merged in. It is how `asChild` works.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @glob-ui/slot
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
Full docs, examples and the accessibility table: [Slot](https://github.com/kevin-martineze/GlobUI/tree/main/apps/docs/content/docs/utilities/slot.mdx).
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
MIT. See [LICENSE](./LICENSE).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type AnyProps = Record<string, unknown>;
|
|
4
|
+
/** Returns one callback ref that writes `node` into every ref passed. */
|
|
5
|
+
declare function composeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T>;
|
|
6
|
+
/**
|
|
7
|
+
* Merges slot props with child props. The child wins on plain props.
|
|
8
|
+
* className: cn(slot, child). style: shallow merge, child wins.
|
|
9
|
+
* on* handlers: child runs first; the slot handler runs unless the child called preventDefault().
|
|
10
|
+
*/
|
|
11
|
+
declare function mergeProps(slotProps: AnyProps, childProps: AnyProps): AnyProps;
|
|
12
|
+
interface SlotProps extends React.HTMLAttributes<HTMLElement> {
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
/** Renders its single child element with the Slot's props, className, handlers and ref merged in. */
|
|
16
|
+
declare const Slot: react.ForwardRefExoticComponent<SlotProps & react.RefAttributes<HTMLElement>>;
|
|
17
|
+
|
|
18
|
+
export { Slot, type SlotProps, composeRefs, mergeProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type AnyProps = Record<string, unknown>;
|
|
4
|
+
/** Returns one callback ref that writes `node` into every ref passed. */
|
|
5
|
+
declare function composeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T>;
|
|
6
|
+
/**
|
|
7
|
+
* Merges slot props with child props. The child wins on plain props.
|
|
8
|
+
* className: cn(slot, child). style: shallow merge, child wins.
|
|
9
|
+
* on* handlers: child runs first; the slot handler runs unless the child called preventDefault().
|
|
10
|
+
*/
|
|
11
|
+
declare function mergeProps(slotProps: AnyProps, childProps: AnyProps): AnyProps;
|
|
12
|
+
interface SlotProps extends React.HTMLAttributes<HTMLElement> {
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
/** Renders its single child element with the Slot's props, className, handlers and ref merged in. */
|
|
16
|
+
declare const Slot: react.ForwardRefExoticComponent<SlotProps & react.RefAttributes<HTMLElement>>;
|
|
17
|
+
|
|
18
|
+
export { Slot, type SlotProps, composeRefs, mergeProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __objRest = (source, exclude) => {
|
|
24
|
+
var target = {};
|
|
25
|
+
for (var prop in source)
|
|
26
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
27
|
+
target[prop] = source[prop];
|
|
28
|
+
if (source != null && __getOwnPropSymbols)
|
|
29
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
30
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
31
|
+
target[prop] = source[prop];
|
|
32
|
+
}
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
35
|
+
var __export = (target, all) => {
|
|
36
|
+
for (var name in all)
|
|
37
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
38
|
+
};
|
|
39
|
+
var __copyProps = (to, from, except, desc) => {
|
|
40
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
41
|
+
for (let key of __getOwnPropNames(from))
|
|
42
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
43
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
44
|
+
}
|
|
45
|
+
return to;
|
|
46
|
+
};
|
|
47
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
48
|
+
|
|
49
|
+
// src/index.ts
|
|
50
|
+
var index_exports = {};
|
|
51
|
+
__export(index_exports, {
|
|
52
|
+
Slot: () => Slot,
|
|
53
|
+
composeRefs: () => composeRefs,
|
|
54
|
+
mergeProps: () => mergeProps
|
|
55
|
+
});
|
|
56
|
+
module.exports = __toCommonJS(index_exports);
|
|
57
|
+
|
|
58
|
+
// src/slot.tsx
|
|
59
|
+
var import_react = require("react");
|
|
60
|
+
var import_utils = require("@glob-ui/utils");
|
|
61
|
+
function composeRefs(...refs) {
|
|
62
|
+
return (node) => {
|
|
63
|
+
for (const ref of refs) {
|
|
64
|
+
if (typeof ref === "function") ref(node);
|
|
65
|
+
else if (ref) ref.current = node;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function isPrevented(event) {
|
|
70
|
+
return typeof event === "object" && event !== null && event.defaultPrevented === true;
|
|
71
|
+
}
|
|
72
|
+
function mergeProps(slotProps, childProps) {
|
|
73
|
+
const merged = __spreadValues({}, slotProps);
|
|
74
|
+
for (const key of Object.keys(childProps)) {
|
|
75
|
+
const slotValue = slotProps[key];
|
|
76
|
+
const childValue = childProps[key];
|
|
77
|
+
if (/^on[A-Z]/.test(key) && typeof slotValue === "function" && typeof childValue === "function") {
|
|
78
|
+
merged[key] = (...args) => {
|
|
79
|
+
const result = childValue(...args);
|
|
80
|
+
if (!isPrevented(args[0])) slotValue(...args);
|
|
81
|
+
return result;
|
|
82
|
+
};
|
|
83
|
+
} else if (key === "className") {
|
|
84
|
+
merged[key] = (0, import_utils.cn)(slotValue, childValue);
|
|
85
|
+
} else if (key === "style") {
|
|
86
|
+
merged[key] = __spreadValues(__spreadValues({}, slotValue), childValue);
|
|
87
|
+
} else if (childValue !== void 0) {
|
|
88
|
+
merged[key] = childValue;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return merged;
|
|
92
|
+
}
|
|
93
|
+
var Slot = (0, import_react.forwardRef)((_a, forwardedRef) => {
|
|
94
|
+
var _b = _a, { children } = _b, slotProps = __objRest(_b, ["children"]);
|
|
95
|
+
const child = import_react.Children.only(children);
|
|
96
|
+
const childProps = (0, import_react.isValidElement)(child) ? child.props : {};
|
|
97
|
+
const childRef = childProps.ref;
|
|
98
|
+
const ref = (0, import_react.useMemo)(
|
|
99
|
+
() => forwardedRef && childRef ? composeRefs(forwardedRef, childRef) : forwardedRef != null ? forwardedRef : childRef,
|
|
100
|
+
[forwardedRef, childRef]
|
|
101
|
+
);
|
|
102
|
+
if (!(0, import_react.isValidElement)(child)) return null;
|
|
103
|
+
return (0, import_react.cloneElement)(child, __spreadProps(__spreadValues({}, mergeProps(slotProps, childProps)), { ref }));
|
|
104
|
+
});
|
|
105
|
+
Slot.displayName = "Slot";
|
|
106
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
107
|
+
0 && (module.exports = {
|
|
108
|
+
Slot,
|
|
109
|
+
composeRefs,
|
|
110
|
+
mergeProps
|
|
111
|
+
});
|
|
112
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/slot.tsx"],"sourcesContent":["export { Slot, composeRefs, mergeProps } from \"./slot\"\nexport type { SlotProps } from \"./slot\"\n","import { Children, cloneElement, forwardRef, isValidElement, useMemo } from \"react\"\nimport { cn } from \"@glob-ui/utils\"\n\ntype AnyProps = Record<string, unknown>\ntype Handler = (...args: unknown[]) => unknown\n\n/** Returns one callback ref that writes `node` into every ref passed. */\nfunction composeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {\n return (node) => {\n for (const ref of refs) {\n if (typeof ref === \"function\") ref(node)\n else if (ref) (ref as React.RefObject<T | null>).current = node\n }\n }\n}\n\nfunction isPrevented(event: unknown) {\n return typeof event === \"object\" && event !== null && (event as { defaultPrevented?: boolean }).defaultPrevented === true\n}\n\n/**\n * Merges slot props with child props. The child wins on plain props.\n * className: cn(slot, child). style: shallow merge, child wins.\n * on* handlers: child runs first; the slot handler runs unless the child called preventDefault().\n */\nfunction mergeProps(slotProps: AnyProps, childProps: AnyProps): AnyProps {\n const merged: AnyProps = { ...slotProps }\n for (const key of Object.keys(childProps)) {\n const slotValue = slotProps[key]\n const childValue = childProps[key]\n if (/^on[A-Z]/.test(key) && typeof slotValue === \"function\" && typeof childValue === \"function\") {\n merged[key] = (...args: unknown[]) => {\n const result = (childValue as Handler)(...args)\n if (!isPrevented(args[0])) (slotValue as Handler)(...args)\n return result\n }\n } else if (key === \"className\") {\n merged[key] = cn(slotValue as string, childValue as string)\n } else if (key === \"style\") {\n merged[key] = { ...(slotValue as object), ...(childValue as object) }\n } else if (childValue !== undefined) {\n merged[key] = childValue\n }\n }\n return merged\n}\n\nexport interface SlotProps extends React.HTMLAttributes<HTMLElement> {\n children?: React.ReactNode\n}\n\n/** Renders its single child element with the Slot's props, className, handlers and ref merged in. */\nconst Slot = forwardRef<HTMLElement, SlotProps>(({ children, ...slotProps }, forwardedRef) => {\n const child = Children.only(children)\n const childProps = (isValidElement(child) ? child.props : {}) as AnyProps\n // React 19: ref is a regular prop on the element.\n const childRef = childProps.ref as React.Ref<HTMLElement> | undefined\n\n // Only build a callback when both exist, so server-rendered markup never gets a function ref.\n const ref = useMemo(\n () => (forwardedRef && childRef ? composeRefs(forwardedRef, childRef) : (forwardedRef ?? childRef)),\n [forwardedRef, childRef]\n )\n\n if (!isValidElement(child)) return null\n return cloneElement(child, { ...mergeProps(slotProps, childProps), ref } as AnyProps)\n})\nSlot.displayName = \"Slot\"\n\nexport { Slot, composeRefs, mergeProps }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA4E;AAC5E,mBAAmB;AAMnB,SAAS,eAAkB,MAA0D;AACnF,SAAO,CAAC,SAAS;AACf,eAAW,OAAO,MAAM;AACtB,UAAI,OAAO,QAAQ,WAAY,KAAI,IAAI;AAAA,eAC9B,IAAK,CAAC,IAAkC,UAAU;AAAA,IAC7D;AAAA,EACF;AACF;AAEA,SAAS,YAAY,OAAgB;AACnC,SAAO,OAAO,UAAU,YAAY,UAAU,QAAS,MAAyC,qBAAqB;AACvH;AAOA,SAAS,WAAW,WAAqB,YAAgC;AACvE,QAAM,SAAmB,mBAAK;AAC9B,aAAW,OAAO,OAAO,KAAK,UAAU,GAAG;AACzC,UAAM,YAAY,UAAU,GAAG;AAC/B,UAAM,aAAa,WAAW,GAAG;AACjC,QAAI,WAAW,KAAK,GAAG,KAAK,OAAO,cAAc,cAAc,OAAO,eAAe,YAAY;AAC/F,aAAO,GAAG,IAAI,IAAI,SAAoB;AACpC,cAAM,SAAU,WAAuB,GAAG,IAAI;AAC9C,YAAI,CAAC,YAAY,KAAK,CAAC,CAAC,EAAG,CAAC,UAAsB,GAAG,IAAI;AACzD,eAAO;AAAA,MACT;AAAA,IACF,WAAW,QAAQ,aAAa;AAC9B,aAAO,GAAG,QAAI,iBAAG,WAAqB,UAAoB;AAAA,IAC5D,WAAW,QAAQ,SAAS;AAC1B,aAAO,GAAG,IAAI,kCAAM,YAA0B;AAAA,IAChD,WAAW,eAAe,QAAW;AACnC,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACA,SAAO;AACT;AAOA,IAAM,WAAO,yBAAmC,CAAC,IAA4B,iBAAiB;AAA7C,eAAE,WApDnD,IAoDiD,IAAe,sBAAf,IAAe,CAAb;AACjD,QAAM,QAAQ,sBAAS,KAAK,QAAQ;AACpC,QAAM,iBAAc,6BAAe,KAAK,IAAI,MAAM,QAAQ,CAAC;AAE3D,QAAM,WAAW,WAAW;AAG5B,QAAM,UAAM;AAAA,IACV,MAAO,gBAAgB,WAAW,YAAY,cAAc,QAAQ,IAAK,sCAAgB;AAAA,IACzF,CAAC,cAAc,QAAQ;AAAA,EACzB;AAEA,MAAI,KAAC,6BAAe,KAAK,EAAG,QAAO;AACnC,aAAO,2BAAa,OAAO,iCAAK,WAAW,WAAW,UAAU,IAArC,EAAwC,IAAI,EAAa;AACtF,CAAC;AACD,KAAK,cAAc;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/slot.tsx
|
|
34
|
+
import { Children, cloneElement, forwardRef, isValidElement, useMemo } from "react";
|
|
35
|
+
import { cn } from "@glob-ui/utils";
|
|
36
|
+
function composeRefs(...refs) {
|
|
37
|
+
return (node) => {
|
|
38
|
+
for (const ref of refs) {
|
|
39
|
+
if (typeof ref === "function") ref(node);
|
|
40
|
+
else if (ref) ref.current = node;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function isPrevented(event) {
|
|
45
|
+
return typeof event === "object" && event !== null && event.defaultPrevented === true;
|
|
46
|
+
}
|
|
47
|
+
function mergeProps(slotProps, childProps) {
|
|
48
|
+
const merged = __spreadValues({}, slotProps);
|
|
49
|
+
for (const key of Object.keys(childProps)) {
|
|
50
|
+
const slotValue = slotProps[key];
|
|
51
|
+
const childValue = childProps[key];
|
|
52
|
+
if (/^on[A-Z]/.test(key) && typeof slotValue === "function" && typeof childValue === "function") {
|
|
53
|
+
merged[key] = (...args) => {
|
|
54
|
+
const result = childValue(...args);
|
|
55
|
+
if (!isPrevented(args[0])) slotValue(...args);
|
|
56
|
+
return result;
|
|
57
|
+
};
|
|
58
|
+
} else if (key === "className") {
|
|
59
|
+
merged[key] = cn(slotValue, childValue);
|
|
60
|
+
} else if (key === "style") {
|
|
61
|
+
merged[key] = __spreadValues(__spreadValues({}, slotValue), childValue);
|
|
62
|
+
} else if (childValue !== void 0) {
|
|
63
|
+
merged[key] = childValue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return merged;
|
|
67
|
+
}
|
|
68
|
+
var Slot = forwardRef((_a, forwardedRef) => {
|
|
69
|
+
var _b = _a, { children } = _b, slotProps = __objRest(_b, ["children"]);
|
|
70
|
+
const child = Children.only(children);
|
|
71
|
+
const childProps = isValidElement(child) ? child.props : {};
|
|
72
|
+
const childRef = childProps.ref;
|
|
73
|
+
const ref = useMemo(
|
|
74
|
+
() => forwardedRef && childRef ? composeRefs(forwardedRef, childRef) : forwardedRef != null ? forwardedRef : childRef,
|
|
75
|
+
[forwardedRef, childRef]
|
|
76
|
+
);
|
|
77
|
+
if (!isValidElement(child)) return null;
|
|
78
|
+
return cloneElement(child, __spreadProps(__spreadValues({}, mergeProps(slotProps, childProps)), { ref }));
|
|
79
|
+
});
|
|
80
|
+
Slot.displayName = "Slot";
|
|
81
|
+
export {
|
|
82
|
+
Slot,
|
|
83
|
+
composeRefs,
|
|
84
|
+
mergeProps
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/slot.tsx"],"sourcesContent":["import { Children, cloneElement, forwardRef, isValidElement, useMemo } from \"react\"\nimport { cn } from \"@glob-ui/utils\"\n\ntype AnyProps = Record<string, unknown>\ntype Handler = (...args: unknown[]) => unknown\n\n/** Returns one callback ref that writes `node` into every ref passed. */\nfunction composeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {\n return (node) => {\n for (const ref of refs) {\n if (typeof ref === \"function\") ref(node)\n else if (ref) (ref as React.RefObject<T | null>).current = node\n }\n }\n}\n\nfunction isPrevented(event: unknown) {\n return typeof event === \"object\" && event !== null && (event as { defaultPrevented?: boolean }).defaultPrevented === true\n}\n\n/**\n * Merges slot props with child props. The child wins on plain props.\n * className: cn(slot, child). style: shallow merge, child wins.\n * on* handlers: child runs first; the slot handler runs unless the child called preventDefault().\n */\nfunction mergeProps(slotProps: AnyProps, childProps: AnyProps): AnyProps {\n const merged: AnyProps = { ...slotProps }\n for (const key of Object.keys(childProps)) {\n const slotValue = slotProps[key]\n const childValue = childProps[key]\n if (/^on[A-Z]/.test(key) && typeof slotValue === \"function\" && typeof childValue === \"function\") {\n merged[key] = (...args: unknown[]) => {\n const result = (childValue as Handler)(...args)\n if (!isPrevented(args[0])) (slotValue as Handler)(...args)\n return result\n }\n } else if (key === \"className\") {\n merged[key] = cn(slotValue as string, childValue as string)\n } else if (key === \"style\") {\n merged[key] = { ...(slotValue as object), ...(childValue as object) }\n } else if (childValue !== undefined) {\n merged[key] = childValue\n }\n }\n return merged\n}\n\nexport interface SlotProps extends React.HTMLAttributes<HTMLElement> {\n children?: React.ReactNode\n}\n\n/** Renders its single child element with the Slot's props, className, handlers and ref merged in. */\nconst Slot = forwardRef<HTMLElement, SlotProps>(({ children, ...slotProps }, forwardedRef) => {\n const child = Children.only(children)\n const childProps = (isValidElement(child) ? child.props : {}) as AnyProps\n // React 19: ref is a regular prop on the element.\n const childRef = childProps.ref as React.Ref<HTMLElement> | undefined\n\n // Only build a callback when both exist, so server-rendered markup never gets a function ref.\n const ref = useMemo(\n () => (forwardedRef && childRef ? composeRefs(forwardedRef, childRef) : (forwardedRef ?? childRef)),\n [forwardedRef, childRef]\n )\n\n if (!isValidElement(child)) return null\n return cloneElement(child, { ...mergeProps(slotProps, childProps), ref } as AnyProps)\n})\nSlot.displayName = \"Slot\"\n\nexport { Slot, composeRefs, mergeProps }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,UAAU,cAAc,YAAY,gBAAgB,eAAe;AAC5E,SAAS,UAAU;AAMnB,SAAS,eAAkB,MAA0D;AACnF,SAAO,CAAC,SAAS;AACf,eAAW,OAAO,MAAM;AACtB,UAAI,OAAO,QAAQ,WAAY,KAAI,IAAI;AAAA,eAC9B,IAAK,CAAC,IAAkC,UAAU;AAAA,IAC7D;AAAA,EACF;AACF;AAEA,SAAS,YAAY,OAAgB;AACnC,SAAO,OAAO,UAAU,YAAY,UAAU,QAAS,MAAyC,qBAAqB;AACvH;AAOA,SAAS,WAAW,WAAqB,YAAgC;AACvE,QAAM,SAAmB,mBAAK;AAC9B,aAAW,OAAO,OAAO,KAAK,UAAU,GAAG;AACzC,UAAM,YAAY,UAAU,GAAG;AAC/B,UAAM,aAAa,WAAW,GAAG;AACjC,QAAI,WAAW,KAAK,GAAG,KAAK,OAAO,cAAc,cAAc,OAAO,eAAe,YAAY;AAC/F,aAAO,GAAG,IAAI,IAAI,SAAoB;AACpC,cAAM,SAAU,WAAuB,GAAG,IAAI;AAC9C,YAAI,CAAC,YAAY,KAAK,CAAC,CAAC,EAAG,CAAC,UAAsB,GAAG,IAAI;AACzD,eAAO;AAAA,MACT;AAAA,IACF,WAAW,QAAQ,aAAa;AAC9B,aAAO,GAAG,IAAI,GAAG,WAAqB,UAAoB;AAAA,IAC5D,WAAW,QAAQ,SAAS;AAC1B,aAAO,GAAG,IAAI,kCAAM,YAA0B;AAAA,IAChD,WAAW,eAAe,QAAW;AACnC,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACA,SAAO;AACT;AAOA,IAAM,OAAO,WAAmC,CAAC,IAA4B,iBAAiB;AAA7C,eAAE,WApDnD,IAoDiD,IAAe,sBAAf,IAAe,CAAb;AACjD,QAAM,QAAQ,SAAS,KAAK,QAAQ;AACpC,QAAM,aAAc,eAAe,KAAK,IAAI,MAAM,QAAQ,CAAC;AAE3D,QAAM,WAAW,WAAW;AAG5B,QAAM,MAAM;AAAA,IACV,MAAO,gBAAgB,WAAW,YAAY,cAAc,QAAQ,IAAK,sCAAgB;AAAA,IACzF,CAAC,cAAc,QAAQ;AAAA,EACzB;AAEA,MAAI,CAAC,eAAe,KAAK,EAAG,QAAO;AACnC,SAAO,aAAa,OAAO,iCAAK,WAAW,WAAW,UAAU,IAArC,EAAwC,IAAI,EAAa;AACtF,CAAC;AACD,KAAK,cAAc;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glob-ui/slot",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": "^19"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@glob-ui/utils": "0.1.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsup": "^8.5.1"
|
|
33
|
+
},
|
|
34
|
+
"description": "Renders its single child with the props, className, handlers and ref merged in. It is how `asChild` works.",
|
|
35
|
+
"keywords": [
|
|
36
|
+
"react",
|
|
37
|
+
"glob-ui",
|
|
38
|
+
"components",
|
|
39
|
+
"accessible",
|
|
40
|
+
"tailwind",
|
|
41
|
+
"slot"
|
|
42
|
+
],
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"author": "Kevin Martínez E",
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/kevin-martineze/GlobUI.git",
|
|
48
|
+
"directory": "packages/slot"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://github.com/kevin-martineze/GlobUI#readme",
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/kevin-martineze/GlobUI/issues"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public",
|
|
56
|
+
"provenance": true
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": false,
|
|
59
|
+
"type": "commonjs",
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=18.18"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsup",
|
|
65
|
+
"dev": "tsup --watch --no-clean"
|
|
66
|
+
}
|
|
6
67
|
}
|