-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into add-semibold-fonts
- Loading branch information
Showing
17 changed files
with
587 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@navikt/ds-css": minor | ||
"@navikt/ds-react": minor | ||
--- | ||
|
||
[Layout] Columns komponent :tada: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
.navds-hgrid { | ||
--__ac-hgrid-columns-xs: initial; | ||
--__ac-hgrid-columns-sm: initial; | ||
--__ac-hgrid-columns-md: initial; | ||
--__ac-hgrid-columns-lg: initial; | ||
--__ac-hgrid-columns-xl: initial; | ||
--__ac-hgrid-columns: var(--__ac-hgrid-columns-xs); | ||
--__ac-hgrid-gap-xs: initial; | ||
--__ac-hgrid-gap-sm: initial; | ||
--__ac-hgrid-gap-md: initial; | ||
--__ac-hgrid-gap-lg: initial; | ||
--__ac-hgrid-gap-xl: initial; | ||
--__ac-hgrid-gap: var(--__ac-hgrid-gap-xs); | ||
|
||
display: grid; | ||
grid-template-columns: var(--__ac-hgrid-columns); | ||
gap: var(--__ac-hgrid-gap); | ||
} | ||
|
||
@media (min-width: 480px) { | ||
.navds-hgrid { | ||
--__ac-hgrid-columns: var(--__ac-hgrid-columns-sm, var(--__ac-hgrid-columns-xs)); | ||
--__ac-hgrid-gap: var(--__ac-hgrid-gap-sm, var(--__ac-hgrid-gap-xs)); | ||
} | ||
} | ||
|
||
@media (min-width: 768px) { | ||
.navds-hgrid { | ||
--__ac-hgrid-columns: var(--__ac-hgrid-columns-md, var(--__ac-hgrid-columns-sm, var(--__ac-hgrid-columns-xs))); | ||
--__ac-hgrid-gap: var(--__ac-hgrid-gap-md, var(--__ac-hgrid-gap-sm, var(--__ac-hgrid-gap-xs))); | ||
} | ||
} | ||
|
||
@media (min-width: 1024px) { | ||
.navds-hgrid { | ||
--__ac-hgrid-columns: var( | ||
--__ac-hgrid-columns-lg, | ||
var(--__ac-hgrid-columns-md, var(--__ac-hgrid-columns-sm, var(--__ac-hgrid-columns-xs))) | ||
); | ||
--__ac-hgrid-gap: var(--__ac-hgrid-gap-lg, var(--__ac-hgrid-gap-md, var(--__ac-hgrid-gap-sm, var(--__ac-hgrid-gap-xs)))); | ||
} | ||
} | ||
|
||
@media (min-width: 1280px) { | ||
.navds-hgrid { | ||
--__ac-hgrid-columns: var( | ||
--__ac-hgrid-columns-xl, | ||
var(--__ac-hgrid-columns-lg, var(--__ac-hgrid-columns-md, var(--__ac-hgrid-columns-sm, var(--__ac-hgrid-columns-xs)))) | ||
); | ||
--__ac-hgrid-gap: var( | ||
--__ac-hgrid-gap-xl, | ||
var(--__ac-hgrid-gap-lg, var(--__ac-hgrid-gap-md, var(--__ac-hgrid-gap-sm, var(--__ac-hgrid-gap-xs)))) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React, { forwardRef, HTMLAttributes } from "react"; | ||
import cl from "clsx"; | ||
import { | ||
getResponsiveProps, | ||
getResponsiveValue, | ||
ResponsiveProp, | ||
SpacingScale, | ||
} from "../utilities/css"; | ||
|
||
export interface HGridProps extends HTMLAttributes<HTMLDivElement> { | ||
children: React.ReactNode; | ||
/** | ||
* Number of columns to display. Can be a number, a string with a unit or tokens for spesific breakpoints. | ||
* Sets `grid-template-columns`, so `fr`, `minmax` etc. works. | ||
* @example | ||
* columns={{ sm: 1, md: 1, lg: "1fr auto", xl: "1fr auto"}} | ||
* @example | ||
* columns={3} | ||
* @example | ||
* columns="repeat(3, minmax(0, 1fr))" | ||
*/ | ||
columns?: ResponsiveProp<number | string>; | ||
/** Spacing between columns. Based on spacing-tokens. | ||
* @example | ||
* gap="6" | ||
* gap={{ sm: "2", md: "2", lg: "6", xl: "6"}} | ||
*/ | ||
gap?: ResponsiveProp<SpacingScale>; | ||
} | ||
/** | ||
* Horizontal Grid Primitive with dynamic columns and gap based on breakpoints. | ||
* | ||
* @see [📝 Documentation](https://aksel.nav.no/komponenter/core/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<HTMLDivElement, HGridProps>( | ||
({ className, columns, gap, style, ...rest }, ref) => { | ||
const styles: React.CSSProperties = { | ||
...style, | ||
...getResponsiveProps(`hgrid`, "gap", "spacing", gap), | ||
...getResponsiveValue(`hgrid`, "columns", formatGrid(columns)), | ||
}; | ||
|
||
return ( | ||
<div | ||
{...rest} | ||
ref={ref} | ||
className={cl("navds-hgrid", className)} | ||
style={styles} | ||
/> | ||
); | ||
} | ||
); | ||
|
||
function formatGrid( | ||
props?: ResponsiveProp<number | string> | ||
): ResponsiveProp<number | string> { | ||
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: number | string) => { | ||
if (typeof prop === "number") { | ||
return `repeat(${prop}, minmax(0, 1fr))`; | ||
} | ||
|
||
return prop; | ||
}; | ||
|
||
export default HGrid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React from "react"; | ||
import { HGrid } from "."; | ||
|
||
const columnsVariants = { | ||
Number: "columnNumber", | ||
String: "columnString", | ||
Object: "columnObject", | ||
}; | ||
|
||
export default { | ||
title: "ds-react/HGrid", | ||
component: HGrid, | ||
parameters: { | ||
layout: "fullscreen", | ||
}, | ||
argTypes: { | ||
columnsType: { | ||
defaultValue: Object.keys(columnsVariants)[0], | ||
options: Object.keys(columnsVariants), | ||
control: { type: "radio" }, | ||
}, | ||
}, | ||
}; | ||
|
||
/* const getColumnsProp = () */ | ||
|
||
export const Default = { | ||
render: (props) => ( | ||
<HGrid | ||
gap={props?.gap ?? 4} | ||
columns={props[columnsVariants[props.columnsType]]} | ||
> | ||
<Placeholder text="1" /> | ||
<Placeholder text="2" /> | ||
<Placeholder text="3" /> | ||
<Placeholder text="4" /> | ||
</HGrid> | ||
), | ||
args: { | ||
columnNumber: 4, | ||
columnObject: { xs: 1, md: 4 }, | ||
columnString: "repeat(3, minmax(0, 1fr))", | ||
gap: "4", | ||
}, | ||
}; | ||
|
||
export const Gap = { | ||
render: () => ( | ||
<HGrid gap="6"> | ||
<Placeholder text="1" /> | ||
<Placeholder text="2" /> | ||
<Placeholder text="3" /> | ||
<Placeholder text="4" /> | ||
</HGrid> | ||
), | ||
}; | ||
|
||
export const DynamicGap = { | ||
render: () => ( | ||
<HGrid gap={{ xs: "2", md: "8" }}> | ||
<Placeholder text="1" /> | ||
<Placeholder text="2" /> | ||
<Placeholder text="3" /> | ||
<Placeholder text="4" /> | ||
</HGrid> | ||
), | ||
}; | ||
|
||
export const Columns = { | ||
render: () => ( | ||
<HGrid gap="4" columns={2}> | ||
<Placeholder text="1" /> | ||
<Placeholder text="2" /> | ||
<Placeholder text="3" /> | ||
<Placeholder text="4" /> | ||
</HGrid> | ||
), | ||
}; | ||
|
||
export const DynamicColumns = { | ||
render: () => ( | ||
<HGrid gap="4" columns={{ sm: "1fr 5fr", md: "2fr 2fr" }}> | ||
<Placeholder text="1" /> | ||
<Placeholder text="2" /> | ||
</HGrid> | ||
), | ||
}; | ||
|
||
function Placeholder({ text }) { | ||
return ( | ||
<div | ||
style={{ | ||
background: "var(--a-deepblue-900)", | ||
height: "5rem", | ||
width: "auto", | ||
color: "white", | ||
}} | ||
> | ||
{text} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as HGrid, type HGridProps } from "./HGrid"; |
Oops, something went wrong.