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

[@mantine/core] Separate handle breakpoints override in mergeTheme #4051

Merged
merged 6 commits into from
Apr 18, 2023

Conversation

7iomka
Copy link
Contributor

@7iomka 7iomka commented Apr 11, 2023

Related issues:
#4050

Instead of merge breakpoints, use breakpoints from override if provided (with an additional check for required keys).

@cyantree
Copy link
Contributor

Another approach would be:

  1. Merge the breakpoints
  2. Sort the resulting object by breakpoint pixel value as IMHO this is the expected order

Then you wouldn't need to specify all default breakpoints when overriding them.

With your approach it's arguable that this is a breaking change as partial breakpoint overrides wouldn't work anymore and have to be adjusted to work again.
Or am I missing something?

@7iomka
Copy link
Contributor Author

7iomka commented Apr 12, 2023

@cyantree Yes, you are right, thanks!
I used your suggestion, please see the latest commit.

Copy link
Contributor

@cyantree cyantree left a comment

Choose a reason for hiding this comment

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

Maybe also add a test for this in merge-theme.test.ts.

@7iomka
Copy link
Contributor Author

7iomka commented Apr 13, 2023

@cyantree I'm not good in tests.
I tried a simple test but I get errors when I try to run
Tell me what I did wrong.
or how to write a test to pass?)
because according to the code it should pass.

@cyantree
Copy link
Contributor

It failed because there's an error in your change implementation. So your test caught this. 👍

Here's the fixed code (you need to merge with acc):

    if (key === 'breakpoints' && themeOverride.breakpoints) {
      const mergedBreakpoints = { ...currentTheme.breakpoints, ...themeOverride.breakpoints };

      return {
        ...acc,
        breakpoints: Object.fromEntries(
          Object.entries(mergedBreakpoints).sort(
            (a, b) => getBreakpointValue(a[1]) - getBreakpointValue(b[1])
          )
        ),
      };
    }

If you want to cover more cases test merging with multiple units and also define your custom breakpoints in descending order. Maybe also test what happens with your overriding an existing breakpoint.

Here's an updated test for this:

    expect(
      Object.entries(mergeTheme(themeBase, { breakpoints: { xxl: '999em', min: '1', xs: '10rem' } }).breakpoints)
    ).toStrictEqual(Object.entries({ min: '1', ...{ ...themeBase.breakpoints, xs: '10rem' }, xxl: '999em' }));

I've also introduced Object.entries() as direct object checking doesn't regard the key order as normally this isn't relevant and therefore might make writing tests more harder than necessary:

// This passes but shouldn't in our case
expect({ a: 1, b: 1 }).toStrictEqual({ b: 1, a: 1 });

// This correctly fails
expect(Object.entries({ a: 1, b: 1 })).toStrictEqual(Object.entries({ b: 1, a: 1 }));

@7iomka
Copy link
Contributor Author

7iomka commented Apr 13, 2023

@cyantree Yes, thank you very much for correcting the error and the tests!

@rtivital rtivital merged commit 6bd8fe4 into mantinedev:master Apr 18, 2023
1 check passed
@rtivital
Copy link
Member

Thanks!

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.

None yet

3 participants