Skip to content

Commit 00faa96

Browse files
authored
fix(page): hide PageSideBar onClick on mobile when isManagedSidebar (#4781)
* fix(page): hide PageSideBar onClick on mobile when isManagedSidebar * fix lint * update snapshot * address feedback * fix lint * revert to 250ms * use mainRef instead to fix trigger on button click
1 parent bcf68bd commit 00faa96

File tree

1 file changed

+30
-8
lines changed
  • packages/react-core/src/components/Page

1 file changed

+30
-8
lines changed

packages/react-core/src/components/Page/Page.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export class Page extends React.Component<PageProps, PageState> {
9191
isNotificationDrawerExpanded: false,
9292
onNotificationDrawerExpand: () => null
9393
};
94+
mainRef = React.createRef<HTMLDivElement>();
9495

9596
constructor(props: PageProps) {
9697
super(props);
@@ -107,30 +108,50 @@ export class Page extends React.Component<PageProps, PageState> {
107108
componentDidMount() {
108109
const { isManagedSidebar, onPageResize } = this.props;
109110
if (isManagedSidebar || onPageResize) {
110-
window.addEventListener('resize', debounce(this.handleResize, 250));
111+
window.addEventListener('resize', this.handleResize);
112+
const currentRef = this.mainRef.current;
113+
if (currentRef) {
114+
currentRef.addEventListener('mousedown', this.handleMainClick);
115+
currentRef.addEventListener('touchstart', this.handleMainClick);
116+
}
111117
// Initial check if should be shown
112-
this.handleResize();
118+
this.resize();
113119
}
114120
}
115121

116122
componentWillUnmount() {
117123
const { isManagedSidebar, onPageResize } = this.props;
118124
if (isManagedSidebar || onPageResize) {
119-
window.removeEventListener('resize', debounce(this.handleResize, 250));
125+
window.removeEventListener('resize', this.handleResize);
126+
const currentRef = this.mainRef.current;
127+
if (currentRef) {
128+
currentRef.removeEventListener('mousedown', this.handleMainClick);
129+
currentRef.removeEventListener('touchstart', this.handleMainClick);
130+
}
120131
}
121132
}
122133

123-
handleResize = () => {
124-
const { onPageResize } = this.props;
125-
const windowSize = window.innerWidth;
134+
isMobile = () =>
126135
// eslint-disable-next-line radix
127-
const mobileView = windowSize < Number.parseInt(globalBreakpointXl.value, 10);
136+
window.innerWidth < Number.parseInt(globalBreakpointXl.value, 10);
137+
138+
resize = () => {
139+
const { onPageResize } = this.props;
140+
const mobileView = this.isMobile();
128141
if (onPageResize) {
129-
onPageResize({ mobileView, windowSize });
142+
onPageResize({ mobileView, windowSize: window.innerWidth });
130143
}
131144
this.setState({ mobileView });
132145
};
133146

147+
handleResize = debounce(this.resize, 250);
148+
149+
handleMainClick = (ev: any) => {
150+
if (this.isMobile() && this.state.mobileIsNavOpen && this.mainRef.current) {
151+
this.setState({ mobileIsNavOpen: false });
152+
}
153+
};
154+
134155
onNavToggleMobile = () => {
135156
this.setState(prevState => ({
136157
mobileIsNavOpen: !prevState.mobileIsNavOpen
@@ -178,6 +199,7 @@ export class Page extends React.Component<PageProps, PageState> {
178199

179200
const main = (
180201
<main
202+
ref={this.mainRef}
181203
role={role}
182204
id={mainContainerId}
183205
className={css(styles.pageMain)}

0 commit comments

Comments
 (0)