UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

75 lines 3.05 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, { 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, getResponsiveValue } from "../utilities/css.js"; /** * Horizontal Grid Primitive with dynamic columns and gap based on breakpoints. * * @see [📝 Documentation](https://aksel.nav.no/komponenter/primitives/hgrid) * @see 🏷️ {@link HGridProps} * * @example * <HGrid gap="6" columns={3}> * <div /> * <div /> * <div /> * </HGrid> * @example * <HGrid gap={{xs: "2", md: "6"}} columns={3}> * <div /> * <div /> * <div /> * </HGrid> * @example * <HGrid gap="6" columns={{ sm: 1, md: 1, lg: "1fr auto", xl: "1fr auto"}}> * <div /> * <div /> * <div /> * </HGrid> */ export const HGrid = forwardRef((_a, ref) => { var { children, className, as: Component = "div", columns, gap, style, align, asChild } = _a, rest = __rest(_a, ["children", "className", "as", "columns", "gap", "style", "align", "asChild"]); const themeContext = useThemeInternal(false); const prefix = (themeContext === null || themeContext === void 0 ? void 0 : themeContext.isDarkside) ? "ax" : "a"; const { cn } = useRenameCSS(); const styles = Object.assign(Object.assign(Object.assign(Object.assign({}, style), { [`--__${prefix}c-hgrid-align`]: align }), getResponsiveProps(prefix, `hgrid`, "gap", "spacing", gap)), getResponsiveValue(prefix, `hgrid`, "columns", formatGrid(columns))); const Comp = asChild ? Slot : Component; return (React.createElement(BasePrimitive, Object.assign({}, rest), React.createElement(Comp, Object.assign({}, omit(rest, PRIMITIVE_PROPS), { ref: ref, className: cn("navds-hgrid", className, { "navds-hgrid-gap": gap, "navds-hgrid-align": align, }), style: styles }), children))); }); function formatGrid(props) { if (!props) { return {}; } if (typeof props === "string" || typeof props === "number") { return getColumnValue(props); } return Object.fromEntries(Object.entries(props).map(([breakpointToken, columnValue]) => [ breakpointToken, getColumnValue(columnValue), ])); } const getColumnValue = (prop) => { if (typeof prop === "number") { return `repeat(${prop}, minmax(0, 1fr))`; } return prop; }; export default HGrid; //# sourceMappingURL=HGrid.js.map