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

fix: JavaScript guard for maximum span of Grid Cells on smaller viewports #1666

Closed
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
11 changes: 8 additions & 3 deletions packages/react/src/Breakout/BreakoutCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { forwardRef } from 'react'
import type { HTMLAttributes, PropsWithChildren } from 'react'
import type { BreakoutRowNumber, BreakoutRowNumbers } from './Breakout'
import { breakoutCellClasses } from './breakoutCellClasses'
import type { GridColumnNumber, GridColumnNumbers } from '../Grid/Grid'
import type {
GridColumnNumbers,
GridMediumColumnNumber,
GridNarrowColumnNumber,
GridWideColumnNumber,
} from '../Grid/Grid'

type BreakoutCellSpanAllProp = {
/** Lets the cell span the full width of all grid variants. */
Expand All @@ -21,9 +26,9 @@ type BreakoutCellSpanAllProp = {

type BreakoutCellSpanAndStartProps = {
/** The amount of grid columns the cell spans. */
colSpan?: 'all' | GridColumnNumber | GridColumnNumbers
colSpan?: 'all' | GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers
/** The index of the grid column the cell starts at. */
colStart?: GridColumnNumber | GridColumnNumbers
colStart?: GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers
has?: 'figure'
}

Expand Down
11 changes: 9 additions & 2 deletions packages/react/src/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import type { ForwardedRef, HTMLAttributes, PropsWithChildren } from 'react'
import { GridCell } from './GridCell'
import { paddingClasses } from './paddingClasses'

export type GridColumnNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
export type GridColumnNumbers = { narrow: GridColumnNumber; medium: GridColumnNumber; wide: GridColumnNumber }
export type GridNarrowColumnNumber = 1 | 2 | 3 | 4
export type GridMediumColumnNumber = GridNarrowColumnNumber | 5 | 6 | 7 | 8
export type GridWideColumnNumber = GridMediumColumnNumber | 9 | 10 | 11 | 12

export type GridColumnNumbers = {
narrow: GridNarrowColumnNumber
medium: GridMediumColumnNumber
wide: GridWideColumnNumber
}
export type GridPaddingSize = 'small' | 'medium' | 'large'

type GridPaddingVerticalProp = {
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/Grid/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import clsx from 'clsx'
import { forwardRef } from 'react'
import type { HTMLAttributes, PropsWithChildren } from 'react'
import type { GridColumnNumber, GridColumnNumbers } from './Grid'
import type { GridColumnNumbers, GridMediumColumnNumber, GridNarrowColumnNumber, GridWideColumnNumber } from './Grid'
import { gridCellClasses } from './gridCellClasses'

type GridCellSpanAllProp = {
Expand All @@ -16,9 +16,9 @@ type GridCellSpanAllProp = {

type GridCellSpanAndStartProps = {
/** The amount of grid columns the cell spans. */
span?: GridColumnNumber | GridColumnNumbers
span?: GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers
/** The index of the grid column the cell starts at. */
start?: GridColumnNumber | GridColumnNumbers
start?: GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers
}

export type GridCellProps = {
Expand Down
64 changes: 59 additions & 5 deletions packages/react/src/Grid/gridCellClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* Copyright Gemeente Amsterdam
*/

import type { GridColumnNumbers, GridMediumColumnNumber, GridNarrowColumnNumber, GridWideColumnNumber } from './Grid'
import type { GridCellProps } from './GridCell'

export const addGridClass = (
prefix: string,
value?: number | { narrow: number; medium: number; wide: number } | 'all',
value?: GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers | 'all',
): string[] => {
if (value === 'all' || typeof value === 'number') {
return [`${prefix}${value}`]
Expand All @@ -18,7 +19,60 @@ export const addGridClass = (
return []
}

export const gridCellClasses = (colSpan?: GridCellProps['span'], colStart?: GridCellProps['start']): string[] => [
...addGridClass('ams-grid__cell--span-', colSpan),
...addGridClass('ams-grid__cell--start-', colStart),
]
const getErrorMessage = (colSpanPlusStart: number, max: number): string => {
return `The sum of the span and start values (${colSpanPlusStart}) must not exceed number of grid column lines (${max}).`
}

export const gridCellClasses = (colSpan?: GridCellProps['span'], colStart?: GridCellProps['start']): string[] => {
if (typeof colSpan !== 'string' && colStart) {
/* eslint-disable no-param-reassign */
if (typeof colSpan === 'number' && typeof colStart === 'number') {
if (window?.matchMedia(`(max-width: 576px)`) && colSpan + colStart > 5) {
console.error(getErrorMessage(colSpan + colStart, 5))
colSpan = 5 - colStart
} else if (window?.matchMedia(`(max-width: 1088px)`) && colSpan + colStart > 9) {
console.error(getErrorMessage(colSpan + colStart, 9))
colSpan = 9 - colStart
} else if (colSpan + colStart > 13) {
console.error(getErrorMessage(colSpan + colStart, 13))
colSpan = 13 - colStart
}
} else if (typeof colSpan === 'object' && typeof colStart === 'object') {
if (colSpan.narrow + colStart.narrow > 5) {
console.error(getErrorMessage(colSpan.narrow + colStart.narrow, 5))
colSpan.narrow = (5 - colStart.narrow) as GridNarrowColumnNumber
} else if (colSpan.medium + colStart.medium > 9) {
console.error(getErrorMessage(colSpan.medium + colStart.medium, 9))
colSpan.medium = (9 - colStart.medium) as GridMediumColumnNumber
} else if (colSpan.wide + colStart.wide > 13) {
console.error(getErrorMessage(colSpan.wide + colStart.wide, 13))
colSpan.wide = (13 - colStart.wide) as GridWideColumnNumber
}
} else if (typeof colSpan === 'number' && typeof colStart === 'object') {
if (window?.matchMedia(`(max-width: 576px)`) && colSpan + colStart.narrow > 5) {
console.error(getErrorMessage(colSpan + colStart.narrow, 5))
colSpan = 5 - colStart.narrow
} else if (window?.matchMedia(`(max-width: 1088px)`) && colSpan + colStart.medium > 9) {
console.error(getErrorMessage(colSpan + colStart.medium, 9))
colSpan = 9 - colStart.medium
} else if (colSpan + colStart.wide > 13) {
console.error(getErrorMessage(colSpan + colStart.wide, 13))
colSpan = 13 - colStart.wide
}
} else if (typeof colSpan === 'object' && typeof colStart === 'number') {
if (colSpan.narrow + colStart > 5) {
console.error(getErrorMessage(colSpan.narrow + colStart, 5))
colSpan.narrow = (5 - colStart) as GridNarrowColumnNumber
} else if (colSpan.medium + colStart > 9) {
console.error(getErrorMessage(colSpan.medium + colStart, 9))
colSpan.medium = (9 - colStart) as GridMediumColumnNumber
} else if (colSpan.wide + colStart > 13) {
console.error(getErrorMessage(colSpan.wide + colStart, 13))
colSpan.wide = (13 - colStart) as GridWideColumnNumber
}
}
/* eslint-enable no-param-reassign */
}

return [...addGridClass('ams-grid__cell--span-', colSpan), ...addGridClass('ams-grid__cell--start-', colStart)]
}
2 changes: 1 addition & 1 deletion packages/react/src/Grid/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Grid } from './Grid'
export type { GridProps, GridColumnNumber, GridColumnNumbers } from './Grid'
export type { GridProps, GridWideColumnNumber as GridColumnNumber, GridColumnNumbers } from './Grid'
export type { GridCellProps } from './GridCell'
1 change: 1 addition & 0 deletions storybook/src/components/Grid/Grid.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The starting position of a cell may also depend on the window width.
Use the `start` prop with 3 values, e.g. `start={{ narrow: 2, medium: 4, wide: 6 }}`.

<Canvas of={GridStories.StartPosition} />
<Canvas of={GridStories.StartPositionAndSpan} />

### Improve semantics

Expand Down
18 changes: 18 additions & 0 deletions storybook/src/components/Grid/Grid.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,21 @@ export const ImproveSemantics: CellStory = {
</Grid.Cell>
),
}

export const GridCellWithClassNames: CellStory = {
...CellStoryTemplate,
render: () => (
<Grid.Cell className="ams-grid__cell--span-2 ams-grid__cell--span-6-wide ams-grid__cell--start-4 ams-grid__cell--start-8-medium ams-grid__cell--start-5-wide">
<div className="ams-docs-item" />
</Grid.Cell>
),
}

export const StartPositionAndSpan: CellStory = {
...CellStoryTemplate,
args: {
children: <div className="ams-docs-item" />,
span: { narrow: 2, medium: 5, wide: 8 },
start: { narrow: 1, medium: 4, wide: 7 },
},
}
Loading