Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 HGrid align-prop #2242

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/dirty-toes-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@navikt/ds-react": patch
"@navikt/ds-css": patch
---

HGrid: Har nå `align`-prop for bedre kontroll over child-elementer
2 changes: 2 additions & 0 deletions @navikt/core/css/hgrid.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
--__ac-hgrid-gap-lg: initial;
--__ac-hgrid-gap-xl: initial;
--__ac-hgrid-gap: var(--__ac-hgrid-gap-xs);
--__ac-hgrid-align: initial;

display: grid;
grid-template-columns: var(--__ac-hgrid-columns);
gap: var(--__ac-hgrid-gap);
align-items: var(--__ac-hgrid-align);
}

@media (min-width: 480px) {
Expand Down
7 changes: 6 additions & 1 deletion @navikt/core/react/src/layout/grid/HGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface HGridProps extends HTMLAttributes<HTMLDivElement> {
* gap={{ sm: "2", md: "2", lg: "6", xl: "6"}}
*/
gap?: ResponsiveProp<SpacingScale>;
/**
* Vertical alignment of children. Elements will by default stretch to the height of parent-element.
*/
align?: "start" | "center" | "end";
}
/**
* Horizontal Grid Primitive with dynamic columns and gap based on breakpoints.
Expand Down Expand Up @@ -53,9 +57,10 @@ export interface HGridProps extends HTMLAttributes<HTMLDivElement> {
* </HGrid>
*/
export const HGrid = forwardRef<HTMLDivElement, HGridProps>(
({ className, columns, gap, style, ...rest }, ref) => {
({ className, columns, gap, style, align, ...rest }, ref) => {
const styles: React.CSSProperties = {
...style,
"--__ac-hgrid-align": align,
...getResponsiveProps(`hgrid`, "gap", "spacing", gap),
...getResponsiveValue(`hgrid`, "columns", formatGrid(columns)),
};
Expand Down
24 changes: 22 additions & 2 deletions @navikt/core/react/src/layout/grid/h-grid.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { HGrid } from ".";
import { VStack } from "../stack";

const columnsVariants = {
Number: "columnNumber",
Expand Down Expand Up @@ -86,12 +87,31 @@ export const DynamicColumns = {
),
};

function Placeholder({ text }) {
export const AlignItems = {
render: () => (
<VStack gap="8">
<HGrid gap="4" columns={2} align="start">
<Placeholder text="start" height="8rem" />
<Placeholder text="auto" height="auto" />
</HGrid>
<HGrid gap="4" columns={2} align="center">
<Placeholder text="center" height="8rem" />
<Placeholder text="auto" height="auto" />
</HGrid>
<HGrid gap="4" columns={2} align="end">
<Placeholder text="end" height="8rem" />
<Placeholder text="auto" height="auto" />
</HGrid>
</VStack>
),
};

function Placeholder({ text, height }: { text: string; height?: string }) {
return (
<div
style={{
background: "var(--a-deepblue-900)",
height: "5rem",
height: height ?? "5rem",
width: "auto",
color: "white",
}}
Expand Down
73 changes: 73 additions & 0 deletions aksel.nav.no/website/pages/eksempler/h-grid/align.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { HGrid, VStack } from "@navikt/ds-react";
import { withDsExample } from "components/website-modules/examples/withDsExample";

const Example = () => {
return (
<Background>
<VStack gap="12">
<HGrid gap="4" columns={2} align="start">
<Placeholder height="10rem">Start</Placeholder>
<Placeholder />
</HGrid>
<HGrid gap="4" columns={2} align="center">
<Placeholder height="10rem">Center</Placeholder>
<Placeholder />
</HGrid>
<HGrid gap="4" columns={2} align="end">
<Placeholder height="10rem">End</Placeholder>
<Placeholder />
</HGrid>
</VStack>
</Background>
);
};

const Placeholder = ({ height = "auto", width = "auto", ...props }) => {
return (
<div
{...props}
style={{
display: "grid",
placeContent: "center",
background: "var(--a-deepblue-500)",
height: height ?? undefined,
minHeight: "2rem",
width: width ?? undefined,
color: "var(--a-text-on-action)",
}}
/>
);
};

const Background = ({
children,
width = "100%",
}: {
children: React.ReactNode;
width?: string;
}) => {
return (
<div
style={{
background: "var(--a-deepblue-100)",
width,
height: "auto",
}}
>
{children}
</div>
);
};

export default withDsExample(Example, "static");

/* Storybook story */
export const Demo = {
render: Example,
desc: "Align gir HGrid mer kontroll over hvordan child-elementer skal vises",
};

export const args = {
index: 3,
desc: "Med responsiv gap kan man dynamiskt tilpasse spacing basert på brekkpunktene våre.",
};