@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
33 lines • 1.63 kB
JavaScript
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 { mergeRefs } from "../util/hooks/useMergeRefs.js";
import { mergeProps } from "./merge-props.js";
const Slot = React.forwardRef((props, forwardedRef) => {
var _a;
const { children } = props, slotProps = __rest(props, ["children"]);
if (React.isValidElement(children)) {
const childRef = Object.prototype.propertyIsEnumerable.call(children.props, "ref")
? children.props.ref // React 19 (children.ref still works, but gives a warning)
: children.ref; // React <19
return React.cloneElement(children, Object.assign(Object.assign({}, mergeProps(slotProps, children.props)), { ref: forwardedRef ? mergeRefs([forwardedRef, childRef]) : childRef }));
}
if (React.Children.count(children) > 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