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

Add support for setting screens in JS config #14415

Merged
merged 18 commits into from
Sep 18, 2024

Conversation

philipp-spiess
Copy link
Member

@philipp-spiess philipp-spiess commented Sep 13, 2024

This PR adds support for the simple case of the screens option inside JS config paths. This allows JS configs to extend the responsive theme by adding custom breakpoints. Here's an example from our v3 docs:

{
  theme: {
    screens: {
      'sm': '640px',
      // => @media (min-width: 640px) { ... }

      'md': '768px',
      // => @media (min-width: 768px) { ... }

      'lg': '1024px',
      // => @media (min-width: 1024px) { ... }

      'xl': '1280px',
      // => @media (min-width: 1280px) { ... }

      '2xl': '1536px',
      // => @media (min-width: 1536px) { ... }
    }
  }
}

For simple breakpoints, this will extend the core breakpoints and will work with the min-* and max-* utilities. However, we also support complex ways of setting up custom screens like this:

{
  theme: {
    extend: {
      screens: {
        sm: { max: '639px' },
        md: [
          { min: '668px', max: '767px' },
          { min: '868px' },
        ],
        lg: { min: '868px' },
        xl: { min: '1024px', max: '1279px' },
        tall: { raw: '(min-height: 800px)' },
      },
    },
  },
}

For these complex setups, we only generate the shorthand variant (e.g. tall) but those won't integrate within min-* and max-*. In v3, adding any of these complex configurations would omit any min-* and max-* variants.

@philipp-spiess philipp-spiess changed the title Support JS config to overwrite breakpoints Upgrade complex breakpoint configuration Sep 13, 2024
@philipp-spiess philipp-spiess force-pushed the feat/complex-screen-config-compat branch from d4de6cf to c8889d5 Compare September 13, 2024 11:14
@philipp-spiess philipp-spiess changed the title Upgrade complex breakpoint configuration Add support for setting screens in JS config Sep 17, 2024
@philipp-spiess philipp-spiess force-pushed the feat/complex-screen-config-compat branch 2 times, most recently from 84a2a16 to affd493 Compare September 17, 2024 09:50
@philipp-spiess philipp-spiess marked this pull request as ready for review September 17, 2024 10:44
@philipp-spiess philipp-spiess marked this pull request as draft September 17, 2024 11:26
@philipp-spiess philipp-spiess force-pushed the feat/complex-screen-config-compat branch from 552af20 to 8bdbee9 Compare September 17, 2024 13:00
@philipp-spiess philipp-spiess marked this pull request as ready for review September 17, 2024 15:19
@philipp-spiess philipp-spiess force-pushed the feat/complex-screen-config-compat branch from 3ec0449 to fd36a58 Compare September 17, 2024 15:33
}

if (coreOrder && insertOrder === undefined) {
insertOrder = allocateOrderAfter(designSystem, coreOrder)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible for us to do this all at one time at the end rather than after every inserted variant?

We're definitely doing more traversals and object replacements than we need to but if it proves to be too complex then this is fine for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have to do it once for every utility that is inserted this way. Each one will handed their own order. I do need a test for this since I think it might now be adding it in inverse insertion order.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah it would make sense that this is the reason some things are going being inserted in reverse order. I was thinking of something like this:

  • Get a list of all the variants after min (and their compare fns)
  • Insert our appropriate screens into the min group
  • Insert other screens after the min group
  • Move the previously recorded variants past the last variant we just inserted (only if there is one)

I think this would work but I'm not sure if the separate compareFn map complicates that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm I’m not sure I follow, I think we can keep doing what we do right now but only loop over the variants in reverse order, this way we:

  • Still insert appropriate screens into the min group. For that, the order does not matter since it'll use the compare function
  • Insert complex variants after the min group. But since we loop in reverse order, we will will always move the previously inserted complex variant down.

I pushed a commit that changes this and it fixes the order.

packages/tailwindcss/src/compat/screens-config.test.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/compat/screens-config.test.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/compat/screens-config.test.ts Outdated Show resolved Hide resolved
@philipp-spiess philipp-spiess merged commit 2ddb715 into next Sep 18, 2024
3 checks passed
@philipp-spiess philipp-spiess deleted the feat/complex-screen-config-compat branch September 18, 2024 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants