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: Disallow invalid number of columns for narrow and medium Grids #1628

Merged

Conversation

RubenSibon
Copy link
Contributor

@RubenSibon RubenSibon commented Oct 2, 2024

Describe the pull request

There is a undesirable feature in Grid that allows setting a higher number of columns than is possible for that specific breakpoint.

What

Do not allow developers to set an invalid number of columns per media breakpoint on a Grid.Cell in a Grid via TypeScript.

Why

It restricts the freedom of developers to write invalid code.

How

Added separate GridNarrowColumnNumber and GridMediumColumnNumber types.

Checklist

Before submitting your pull request, please ensure you have done the following. Check each checkmark if you have done so or if it wasn't necessary:

  • Add or update unit tests
  • Add or update documentation
  • Add or update stories
  • Add or update exports in index.* files
  • Start the PR title with a Conventional Commit prefix, as explained here.

Additional notes

  • Documentation or stories do not need to be updated because8 they already state the valid number of columns per breakpoint.
  • I couldn't think of a way to use unit tests to test this.

@RubenSibon RubenSibon self-assigned this Oct 2, 2024
@VincentSmedinga VincentSmedinga changed the title fix: disallow invalid number of columns per breakpoint fix: Disallow invalid number of Grid columns per breakpoint Oct 2, 2024
@VincentSmedinga
Copy link
Contributor

The CSS classes we render from these props can receive the same treatment. Although, the last time I tried, I ran into a gotcha that I can’t remember now. Please perform sufficient visual tests on different window widths with high and low values, single and triplets, etc.

@VincentSmedinga
Copy link
Contributor

VincentSmedinga commented Oct 3, 2024

Oh, and please do the same for SpotlightGrid after #1629 has been merged Breakout.

@github-actions github-actions bot temporarily deployed to demo-DES-962-grid-max-span-for-smaller-viewports October 4, 2024 14:41 Destroyed
…hub.com:Amsterdam/design-system into fix/DES-962-grid-max-span-for-smaller-viewports
@github-actions github-actions bot temporarily deployed to demo-DES-962-grid-max-span-for-smaller-viewports October 4, 2024 14:46 Destroyed
…/DES-962-grid-max-span-for-smaller-viewports
@github-actions github-actions bot temporarily deployed to demo-DES-962-grid-max-span-for-smaller-viewports October 16, 2024 07:23 Destroyed
@RubenSibon
Copy link
Contributor Author

@@ -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
Copy link
Contributor

@VincentSmedinga VincentSmedinga Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has overlap: 1 | 2 | 3 | 4 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | …

We discussed Pick<>, but that’s for object types, not literals.

This may be an option:

type Subset<T, U> = T extends U ? T : never

export type GridColumnNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12

export type GridColumnNumbers = {
  narrow: Subset<GridColumnNumber, 1 | 2 | 3 | 4>
  medium: Subset<GridColumnNumber, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8>
  wide: GridColumnNumber
}

Types that accept all three variations can keep using GridColumnNumber.

Copy link
Contributor

@VincentSmedinga VincentSmedinga Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an alternative. The definition is more complex, but usage is simpler.

type Range<N extends number, Result extends Array<unknown> = []> = Result['length'] extends N
  ? Result[number]
  : Range<N, [...Result, Result['length']]>

export type GridColumnNumber = Range<12>

export type GridColumnNumbers = {
  narrow: Range<4>
  medium: Range<8>
  wide: Range<12>
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subset seems best?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I like how simple it is to use Range even if the definition is more complex.

I did something similar in the experimental branch:

type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N

Copy link
Contributor Author

@RubenSibon RubenSibon Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end we chose the KISS solution. :-)

@VincentSmedinga VincentSmedinga changed the title fix: Disallow invalid number of Grid columns per breakpoint fix: Disallow invalid number of columns for narrow and medium Grids Oct 16, 2024
@github-actions github-actions bot temporarily deployed to demo-DES-962-grid-max-span-for-smaller-viewports October 16, 2024 13:47 Destroyed
@github-actions github-actions bot temporarily deployed to demo-DES-962-grid-max-span-for-smaller-viewports October 16, 2024 15:19 Destroyed
@github-actions github-actions bot temporarily deployed to demo-DES-962-grid-max-span-for-smaller-viewports October 16, 2024 15:23 Destroyed
@github-actions github-actions bot temporarily deployed to demo-DES-962-grid-max-span-for-smaller-viewports October 17, 2024 07:49 Destroyed
…hub.com:Amsterdam/design-system into fix/DES-962-grid-max-span-for-smaller-viewports
@github-actions github-actions bot temporarily deployed to demo-DES-962-grid-max-span-for-smaller-viewports October 17, 2024 07:54 Destroyed
@VincentSmedinga VincentSmedinga merged commit 7dc0ee8 into develop Oct 17, 2024
6 checks passed
@VincentSmedinga VincentSmedinga deleted the fix/DES-962-grid-max-span-for-smaller-viewports branch October 17, 2024 13:49
@github-actions github-actions bot mentioned this pull request Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants