@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
92 lines • 4.42 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, { forwardRef } from "react";
import { Slot } from "../../slot/Slot.js";
import { useRenameCSS, useThemeInternal } from "../../theme/Theme.js";
import { omit } from "../../util/index.js";
import BasePrimitive, { PRIMITIVE_PROPS, } from "../base/BasePrimitive.js";
import { getResponsiveProps } from "../utilities/css.js";
import BoxNew from "./Box.darkside.js";
/**
* Foundational Layout-primitive for generic encapsulation & styling.
*
* @see [📝 Documentation](https://aksel.nav.no/komponenter/primitives/box)
* @see 🏷️ {@link BoxProps}
* @see [🤖 OverridableComponent](https://aksel.nav.no/grunnleggende/kode/overridablecomponent) support
*
* @example
* <Box padding="4">
* <BodyShort>Hei</BodyShort>
* </Box>
*
* @example
* <Box padding={{xs: '2', sm: '3', md: '4', lg: '5', xl: '6'}}>
* <BodyShort>Hei</BodyShort>
* </Box>
*
* @example
* <VStack gap="8">
* <Box padding="4">
* <BodyShort>Hei</BodyShort>
* </Box>
* <Box padding="4">
* <BodyShort>Hei</BodyShort>
* </Box>
* </VStack>
*/
export const BoxComponent = forwardRef((_a, ref) => {
var { children, className, as: Component = "div", background, borderColor, borderWidth, borderRadius, shadow, style: _style, asChild } = _a, rest = __rest(_a, ["children", "className", "as", "background", "borderColor", "borderWidth", "borderRadius", "shadow", "style", "asChild"]);
const themeContext = useThemeInternal(false);
const { cn } = useRenameCSS();
if (process.env.NODE_ENV !== "production" &&
(themeContext === null || themeContext === void 0 ? void 0 : themeContext.isDarkside) &&
(background || borderColor || shadow)) {
let errorText = ``;
if (background) {
errorText += `\n- background: "${background}"`;
}
if (borderColor) {
errorText += `\n- borderColor: "${borderColor}"`;
}
if (shadow) {
errorText += `\n- shadow: "${shadow}"`;
}
throw new Error(`<Box /> with properties 'background', 'borderColor' or 'shadow' cannot be used with Aksel <Theme /> (darkmode-support). \nTo continue using these properties, migrate to '<Box.New>' (BoxNew for RSC)\nUpdate these props:${errorText}`);
}
const prefix = (themeContext === null || themeContext === void 0 ? void 0 : themeContext.isDarkside) ? "ax" : "a";
const style = Object.assign(Object.assign(Object.assign({}, _style), { [`--__${prefix}c-box-background`]: background
? `var(--a-${background})`
: undefined, [`--__${prefix}c-box-shadow`]: shadow
? `var(--a-shadow-${shadow})`
: undefined, [`--__${prefix}c-box-border-color`]: borderColor
? `var(--a-${borderColor})`
: undefined, [`--__${prefix}c-box-border-width`]: borderWidth
? borderWidth
.split(" ")
.map((x) => `${x}px`)
.join(" ")
: undefined }), getResponsiveProps(prefix, "box", "radius", "radius", borderRadius, false, ["0"]));
const Comp = asChild ? Slot : Component;
return (React.createElement(BasePrimitive, Object.assign({}, rest),
React.createElement(Comp, Object.assign({}, omit(rest, PRIMITIVE_PROPS), { ref: ref, style: style, className: cn("navds-box", className, {
"navds-box-bg": background,
"navds-box-border-color": borderColor,
"navds-box-border-width": borderWidth,
"navds-box-border-radius": borderRadius && !(themeContext === null || themeContext === void 0 ? void 0 : themeContext.isDarkside),
"navds-box-radius": borderRadius && (themeContext === null || themeContext === void 0 ? void 0 : themeContext.isDarkside),
"navds-box-shadow": shadow,
}) }), children)));
});
export const Box = BoxComponent;
Box.New = BoxNew;
export default Box;
//# sourceMappingURL=Box.js.map