UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

49 lines 2.1 kB
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; }; import React from "react"; import { useMergeRefs } from "../../hooks/index.js"; import { mergeProps } from "./merge-props.js"; /** * Workaround for RSC related issue. * https://github.com/radix-ui/primitives/issues/3776#issuecomment-3649236326 */ function unwrapLazy(element) { const arr = React.Children.toArray(element); return arr.length === 1 ? arr[0] : element; } function getChildRef(children) { if (!React.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.forwardRef((props, forwardedRef) => { var _a; const { children } = props, slotProps = __rest(props, ["children"]); const resolvedChildren = unwrapLazy(children); const childRef = getChildRef(resolvedChildren); const mergedRef = useMergeRefs(forwardedRef, childRef); if (React.isValidElement(resolvedChildren)) { return React.cloneElement(resolvedChildren, Object.assign(Object.assign({}, mergeProps(slotProps, resolvedChildren.props)), { ref: mergedRef })); } if (React.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; }); export { Slot }; //# sourceMappingURL=Slot.js.map