UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

55 lines 2.45 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Slot = void 0; const react_1 = __importDefault(require("react")); const hooks_1 = require("../../hooks"); const merge_props_1 = require("./merge-props"); /** * Workaround for RSC related issue. * https://github.com/radix-ui/primitives/issues/3776#issuecomment-3649236326 */ function unwrapLazy(element) { const arr = react_1.default.Children.toArray(element); return arr.length === 1 ? arr[0] : element; } function getChildRef(children) { if (!react_1.default.isValidElement(children)) { return null; } return Object.prototype.propertyIsEnumerable.call(children.props, "ref") ? children.props.ref // React 19 (children.ref still works, but gives a warning) : children.ref; // React <19 } const Slot = react_1.default.forwardRef((props, forwardedRef) => { var _a; const { children } = props, slotProps = __rest(props, ["children"]); const resolvedChildren = unwrapLazy(children); const childRef = getChildRef(resolvedChildren); const mergedRef = (0, hooks_1.useMergeRefs)(forwardedRef, childRef); if (react_1.default.isValidElement(resolvedChildren)) { return react_1.default.cloneElement(resolvedChildren, Object.assign(Object.assign({}, (0, merge_props_1.mergeProps)(slotProps, resolvedChildren.props)), { ref: mergedRef })); } if (react_1.default.Children.count(resolvedChildren) > 1) { const error = new Error("Aksel: Components using 'asChild' expects to recieve a single React element child."); error.name = "SlotError"; (_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, error, Slot); throw error; } return null; }); exports.Slot = Slot; //# sourceMappingURL=Slot.js.map