@loke/ui
Version:
To install dependencies:
182 lines (179 loc) • 8.31 kB
JavaScript
var __create = Object.create;
var { getPrototypeOf: __getProtoOf, defineProperty: __defProp, getOwnPropertyNames: __getOwnPropNames, getOwnPropertyDescriptor: __getOwnPropDesc } = Object, __hasOwnProp = Object.prototype.hasOwnProperty;
function __accessProp(key) {
return this[key];
}
var __toESMCache_node, __toESMCache_esm, __toESM = (mod, isNodeMode, target) => {
var canCache = mod != null && typeof mod === "object";
if (canCache) {
var cache = isNodeMode ? __toESMCache_node ??= /* @__PURE__ */ new WeakMap : __toESMCache_esm ??= /* @__PURE__ */ new WeakMap, cached = cache.get(mod);
if (cached)
return cached;
}
target = mod != null ? __create(__getProtoOf(mod)) : {};
let to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target;
if (mod && typeof mod === "object" || typeof mod === "function") {
for (let key of __getOwnPropNames(mod))
if (!__hasOwnProp.call(to, key))
__defProp(to, key, {
get: __accessProp.bind(mod, key),
enumerable: !0
});
}
if (canCache)
cache.set(mod, to);
return to;
}, __toCommonJS = (from) => {
var entry = (__moduleCache ??= /* @__PURE__ */ new WeakMap).get(from), desc;
if (entry)
return entry;
if (entry = __defProp({}, "__esModule", { value: !0 }), from && typeof from === "object" || typeof from === "function") {
for (var key of __getOwnPropNames(from))
if (!__hasOwnProp.call(entry, key))
__defProp(entry, key, {
get: __accessProp.bind(from, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return __moduleCache.set(from, entry), entry;
}, __moduleCache;
var __returnValue = (v) => v;
function __exportSetter(name, newValue) {
this[name] = __returnValue.bind(null, newValue);
}
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: !0,
configurable: !0,
set: __exportSetter.bind(all, name)
});
};
// src/components/slot/index.ts
var exports_slot = {};
__export(exports_slot, {
Root: () => Slot,
Slot: () => Slot,
Slottable: () => Slottable,
createSlot: () => createSlot,
createSlottable: () => createSlottable
});
module.exports = __toCommonJS(exports_slot);
// src/components/slot/slot.tsx
var import_compose_refs = require("@loke/ui/compose-refs"), import_react = __toESM(require("react"), 1);
function createSlot(ownerName) {
let Slot = import_react.default.forwardRef((props, forwardedRef) => {
let { children, ...slotProps } = props, slottableElement = null, hasSlottable = !1, newChildren = [];
if (isLazyComponent(children) && typeof use === "function")
children = use(children._payload);
if (import_react.default.Children.forEach(children, (maybeSlottable) => {
if (isSlottable(maybeSlottable)) {
hasSlottable = !0;
let slottable = maybeSlottable, child = "child" in slottable.props ? slottable.props.child : slottable.props.children;
if (isLazyComponent(child) && typeof use === "function")
child = use(child._payload);
slottableElement = getSlottableElementFromSlottable(slottable, child), newChildren.push(slottableElement?.props.children);
} else
newChildren.push(maybeSlottable);
}), slottableElement)
slottableElement = import_react.default.cloneElement(slottableElement, void 0, newChildren);
else if (!hasSlottable && import_react.default.Children.count(children) === 1 && import_react.default.isValidElement(children))
slottableElement = children;
let slottableElementRef = slottableElement ? getElementRef(slottableElement) : void 0, composedRef = import_compose_refs.useComposedRefs(forwardedRef, slottableElementRef);
if (!slottableElement) {
if (children || children === 0)
throw Error(hasSlottable ? createSlottableError(ownerName) : createSlotError(ownerName));
return children;
}
let mergedProps = mergeProps(slotProps, slottableElement.props);
if (slottableElement.type !== import_react.default.Fragment)
mergedProps.ref = forwardedRef ? composedRef : slottableElementRef;
return import_react.default.cloneElement(slottableElement, mergedProps);
});
return Slot.displayName = `${ownerName}.Slot`, Slot;
}
var Slot = createSlot("Slot"), SLOTTABLE_IDENTIFIER = Symbol.for("loke.slottable");
function createSlottable(ownerName) {
let Slottable = (props) => ("child" in props) ? props.children(props.child) : props.children;
return Slottable.displayName = `${ownerName}.Slottable`, Slottable.__lokeId = SLOTTABLE_IDENTIFIER, Slottable;
}
var Slottable = createSlottable("Slottable");
function getSlottableElementFromSlottable(slottable, child) {
if ("child" in slottable.props) {
let renderChild = slottable.props.child;
if (!import_react.default.isValidElement(renderChild))
return null;
return import_react.default.cloneElement(renderChild, void 0, slottable.props.children(renderChild.props.children));
}
return import_react.default.isValidElement(child) ? child : null;
}
var HANDLER_REGEX = /^on[A-Z]/;
function isEventHandler(value) {
return typeof value === "function";
}
function mergeProps(slotProps, childProps) {
let overrideProps = { ...childProps };
for (let propName in childProps) {
if (!Object.hasOwn(childProps, propName))
continue;
let slotPropValue = slotProps[propName], childPropValue = childProps[propName];
if (HANDLER_REGEX.test(propName)) {
if (isEventHandler(slotPropValue) && isEventHandler(childPropValue))
overrideProps[propName] = (...args) => {
let result = childPropValue(...args);
return slotPropValue(...args), result;
};
else if (isEventHandler(slotPropValue))
overrideProps[propName] = slotPropValue;
} else if (propName === "style")
overrideProps[propName] = {
...isRecord(slotPropValue) ? slotPropValue : {},
...isRecord(childPropValue) ? childPropValue : {}
};
else if (propName === "className")
overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
else if (propName === "aria-describedby" && slotPropValue !== void 0)
overrideProps[propName] = concatAriaDescribedby(childPropValue, slotPropValue);
}
return { ...slotProps, ...overrideProps };
}
function isRecord(value) {
return typeof value === "object" && value !== null;
}
function getElementRef(element) {
let getter = Object.getOwnPropertyDescriptor(element.props, "ref")?.get, mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning;
if (mayWarn)
return element.ref;
if (getter = Object.getOwnPropertyDescriptor(element, "ref")?.get, mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning, mayWarn)
return element.props.ref;
return element.props.ref || element.ref;
}
function isSlottable(child) {
return import_react.default.isValidElement(child) && typeof child.type === "function" && "__lokeId" in child.type && child.type.__lokeId === SLOTTABLE_IDENTIFIER;
}
var REACT_LAZY_TYPE = Symbol.for("react.lazy");
function isLazyComponent(element) {
return element != null && typeof element === "object" && "$$typeof" in element && element.$$typeof === REACT_LAZY_TYPE && "_payload" in element && isPromiseLike(element._payload);
}
function isPromiseLike(value) {
return typeof value === "object" && value !== null && "then" in value;
}
var ARIA_ID_SEPARATOR = /\s+/, concatAriaDescribedby = (...values) => {
let ids = /* @__PURE__ */ new Set;
for (let value of values) {
if (typeof value !== "string")
continue;
for (let id of value.trim().split(ARIA_ID_SEPARATOR))
if (id)
ids.add(id);
}
return ids.size > 0 ? Array.from(ids).join(" ") : void 0;
};
function createSlotError(ownerName) {
return `${ownerName} failed to slot onto its children. Expected a single React element child or \`Slottable\`.`;
}
function createSlottableError(ownerName) {
return `${ownerName} failed to slot onto its \`Slottable\`. Expected \`Slottable\` to receive a single React element child.`;
}
var use = import_react.default[" use ".trim().toString()];