Skip to content

Page resize observer & Toolbar/PageHeaderTools responsiveness based on page width#6827

Merged
jschuler merged 32 commits into
patternfly:mainfrom
jschuler:resize-observers
Jan 20, 2022
Merged

Page resize observer & Toolbar/PageHeaderTools responsiveness based on page width#6827
jschuler merged 32 commits into
patternfly:mainfrom
jschuler:resize-observers

Conversation

@jschuler
Copy link
Copy Markdown
Collaborator

@jschuler jschuler commented Jan 18, 2022

What: Towards #6030

  • Added a page resize observer to the Page component
  • Adds width, and getBreakpoint to the PageContext. The width is the width of the Page (pf-c-page) container. Children components can use the context to base logic around that (like the Toolbar components in this PR). The getBreakpoint function defaults to the one in util.ts, but can be overwritten if desired.
  • Adds a pf-m-breakpoint-[default|sm|md|lg|xl|2xl] class as a sibling to the Page (pf-c-page) container. This allows for CSS to target page based breakpoints as described in this issue

A new util function getBreakpoint was added to return the breakpoint for a given width.

The util.ts formatBreakpointMods function was updated to accept a breakpoint argument.
Before, you would have a string of class names that are media query based, i.e. pf-m-hidden pf-m-visible-on-md pf-m-hidden-on-lg, or pf-m-spacer-none pf-m-spacer-md-on-md, etc. If the breakpoint argument is supplied, we instead return now a single class based on the known width.

To test this, I added a lightbulb icon button to the masthead which toggles a drawer in this demo:
https://patternfly-react-pr-6827.surge.sh/components/page/react-demos/sticky-section-group/

Additional issues/PRs: patternfly/patternfly#4622, patternfly/patternfly#4625
TBD: More PRs to follow to update components that use the formatBreakpointMods function, or set breakpoint based modifier classes

Demo (note, this also contains the change from core here patternfly/patternfly#4625 which is not yet merged):

Screen.Recording.2022-01-19.at.3.36.42.PM.mov

@patternfly-build
Copy link
Copy Markdown
Collaborator

patternfly-build commented Jan 18, 2022

@nicolethoen nicolethoen linked an issue Jan 19, 2022 that may be closed by this pull request
Comment on lines +147 to +150
if (useResizeObserver) {
this.observer = getResizeObserver(this.pageRef.current, this.handleResize);
} else {
window.addEventListener('resize', this.handleResize);
Copy link
Copy Markdown
Member

@dlabrecq dlabrecq Jan 19, 2022

Choose a reason for hiding this comment

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

Curious why we offer useResizeObserver as a choice?

Note that getResizeObserver will use a window event listener if the this.pageRef.current ref is not available.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

hmm, I suppose we can just enable the observer by default without providing a prop. And will update the code to remove the extra listener, thanks!

@nicolethoen nicolethoen self-requested a review January 19, 2022 16:37
Copy link
Copy Markdown
Contributor

@mattnolting mattnolting left a comment

Choose a reason for hiding this comment

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

Fantastic work @jschuler! L🤖 TM!

Comment thread packages/react-core/src/helpers/util.ts Outdated
) =>
Object.entries(mods || {})
stylePrefix: string = '',
breakpoint?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My only feedback, I'd like to see xs -> default until we have an actual value for xs.

Copy link
Copy Markdown
Contributor

@mattnolting mattnolting left a comment

Choose a reason for hiding this comment

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

👏👏👏 Love it

Copy link
Copy Markdown
Contributor

@tlabaj tlabaj left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@nicolethoen nicolethoen left a comment

Choose a reason for hiding this comment

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

@jschuler I think that at least the stacked toolbar example is behaving differently after this update. Can you just verify that the toolbar examples are all behaving as you'd expect with the updates?

The stacked example is now collapsing the ToolbarToggleGroup at full page width - it didn't do that before.

export const getBreakpoint = (width: number): 'default' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' => {
if (width === null) {
width = canUseDOM ? window.innerWidth : 1200;
return null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This does cause a classname to get applied - pf-m-breakpoint-null so I feel like that's not quite ideal?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

thanks, I added a check in the page component to make sure width is defined first

@jschuler jschuler merged commit 1510908 into patternfly:main Jan 20, 2022
@patternfly-build
Copy link
Copy Markdown
Collaborator

Your changes have been released in:

  • eslint-plugin-patternfly-react@4.31.7
  • @patternfly/react-catalog-view-extension@4.43.7
  • @patternfly/react-charts@6.45.7
  • @patternfly/react-code-editor@4.33.7
  • @patternfly/react-console@4.43.7
  • @patternfly/react-core@4.192.7
  • @patternfly/react-docs@5.53.7
  • @patternfly/react-icons@4.43.7
  • @patternfly/react-inline-edit-extension@4.37.7
  • demo-app-ts@4.152.7
  • @patternfly/react-integration@4.154.7
  • @patternfly/react-log-viewer@4.37.7
  • @patternfly/react-styles@4.42.7
  • @patternfly/react-table@4.61.7
  • @patternfly/react-tokens@4.44.7
  • @patternfly/react-topology@4.39.7
  • @patternfly/react-virtualized-extension@4.39.7
  • transformer-cjs-imports@4.30.7

Thanks for your contribution! 🎉

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.

Masthead / Toolbar responsiveness issues

6 participants