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

Deprecate static breakpoint services #6119

Merged
merged 9 commits into from
Aug 10, 2022

Conversation

cee-chen
Copy link
Member

@cee-chen cee-chen commented Aug 9, 2022

Summary

I strongly recommend following along by commit!

  • Removes the exported BREAKPOINTS, BREAKPOINT_KEYS, getBreakpoint, isWithinBreakpoints, isWithinMaxBreakpoint, and isWithinMinBreakpoint breakpoint services
  • Adds new useIsWithinMaxBreakpoint and useIsWithinMinBreakpoint hooks. These hooks correctly respect EUI theme breakpoint overrides instead of using static Amsterdam defaults.
    • Note that these new hooks now only take a breakpoint size instead of also accepting a numerical width (a change I decided on for simplicity - open to thoughts!)
  • DRYs out the remaining components that were using isWithinMinBreakpoint / repeating their own window resize listeners (EuiFlyout and EuiCollapsibleNav) to use useIsWithinMinBreakpoint
    • Note that the change to remove numerical widths affects these components - from what I checked in Kibana however there are no usages

Screenshots

Checklist

  • Checked in mobile
  • Added documentation
  • Added or updated jest and cypress tests
  • Checked for breaking changes and labeled appropriately
  • A changelog entry exists and is marked appropriately

- [ ] Checked in both light and dark modes
- [ ] Checked in Chrome, Safari, Edge, and Firefox
- [ ] Props have proper autodocs and playground toggles
- [ ] Checked Code Sandbox works for any docs examples
- [ ] Checked for accessibility including keyboard-only and screenreader modes
- [ ] Updated the Figma library counterpart

- in favor of _EuiThemeBreakpoints type from global_styling
- Preserve the type export, since the global_styling one has a _ prefix, but move the export out to the main index.ts
+ rename hook file to more generic name in prepration for max/min width hooks
+ use snake_case to match rest of repo
+ add tests that previously did not exist
+ per new hook typing, only accept a named size instead of a number
- in favor of memoizing its behavior in `CurrentEuiBreakpoint` logic

+ update testenv mock slightly - it's not super DRY, but it's fine
@cee-chen cee-chen marked this pull request as ready for review August 9, 2022 23:59
Comment on lines +150 to +152
* Named breakpoint (`xs` through `xl`) for customizing the minimum window width to enable the `push` type
*/
pushMinBreakpoint?: EuiBreakpointSize | number;
pushMinBreakpoint?: EuiBreakpointSize;
Copy link
Member Author

Choose a reason for hiding this comment

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

Definitely open to thoughts on this change, but Kibana doesn't appear to be using either pushMinBreakpoint or dockedBreakpoint and I thought it would be simpler (especially now that we allow overriding theme breakpoints/adding new breakpoints) to only accept named sizes instead of arbitrary numerical widths.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems fine to me

Comment on lines +45 to +56
// Ensure the breakpoints map is sorted from largest value to smallest
const sortedBreakpoints: _EuiThemeBreakpoints = useMemo(
() => sortMapByLargeToSmallValues(breakpoints),
[breakpoints]
);

// Find the breakpoint (key) whose value is <= windowWidth starting with largest first
const getBreakpoint = useCallback(
(width: number) =>
keysOf(sortedBreakpoints).find((key) => sortedBreakpoints[key] <= width),
[sortedBreakpoints]
);
Copy link
Member Author

@cee-chen cee-chen Aug 10, 2022

Choose a reason for hiding this comment

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

(The above code follows up on the memoization comment I made in #6111 (comment))

I went back and forth on creating a new useGetBreakpoint() hook vs simply baking getBreakpoint into the CurrentEuiBreakpoint logic.

After looking more closely at Kibana's usage of getBreakpoint, I decided on deprecating in favor of useCurrentEuiBreakpoint. Out of 5 usages of getBreakpoint, only 1 appears to be using a ResizeObserver instead of window width, and based on its code comments the EuiResizeObserver appears to be used as syntactical sugar and they primarily want to respond to window width. TL;DR, I'm 99% sure all usages of getBreakpoint in Kibana can be replaced with useCurrentEuiBreakpoint.

I also slightly preferred baking getBreakpoint's logic into the the provider because you cannot call a hook within a callback, which would have made useGetBreakpoint usage in the onWindowResize callback fairly tricky.

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6119/

Copy link
Contributor

@thompsongl thompsongl left a comment

Choose a reason for hiding this comment

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

I strongly recommend following along by commit!

🙇‍♂️ So helpful!

@cee-chen cee-chen merged commit 3efa3ee into elastic:main Aug 10, 2022
@cee-chen cee-chen deleted the breakpoints-deprecations branch August 10, 2022 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants