UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

39 lines 1.98 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 useMergeRefs_1 = require("../util/hooks/useMergeRefs"); const merge_props_1 = require("./merge-props"); const Slot = react_1.default.forwardRef((props, forwardedRef) => { var _a; const { children } = props, slotProps = __rest(props, ["children"]); if (react_1.default.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_1.default.cloneElement(children, Object.assign(Object.assign({}, (0, merge_props_1.mergeProps)(slotProps, children.props)), { ref: forwardedRef ? (0, useMergeRefs_1.mergeRefs)([forwardedRef, childRef]) : childRef })); } if (react_1.default.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; }); exports.Slot = Slot; //# sourceMappingURL=Slot.js.map