-
Notifications
You must be signed in to change notification settings - Fork 101
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
fix(ui-modal): make Modal's header non-sticky with small window height #1854
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import { Children, Component, isValidElement, ReactElement } from 'react' | |
import { passthroughProps, safeCloneElement } from '@instructure/ui-react-utils' | ||
import { createChainedFunction } from '@instructure/ui-utils' | ||
import { testable } from '@instructure/ui-testable' | ||
import { canUseDOM } from '@instructure/ui-dom-utils' | ||
|
||
import { Transition } from '@instructure/ui-motion' | ||
import { Portal } from '@instructure/ui-portal' | ||
|
@@ -86,7 +87,8 @@ class Modal extends Component<ModalProps, ModalState> { | |
|
||
this.state = { | ||
transitioning: false, | ||
open: props.open ?? false | ||
open: props.open ?? false, | ||
windowHeight: 99999 | ||
} | ||
} | ||
|
||
|
@@ -101,6 +103,7 @@ class Modal extends Component<ModalProps, ModalState> { | |
|
||
componentDidMount() { | ||
this.props.makeStyles?.() | ||
window.addEventListener('resize', this.updateHeight) | ||
} | ||
|
||
componentDidUpdate(prevProps: ModalProps) { | ||
|
@@ -110,6 +113,14 @@ class Modal extends Component<ModalProps, ModalState> { | |
this.props.makeStyles?.() | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('resize', this.updateHeight) | ||
} | ||
|
||
updateHeight = () => { | ||
this.setState({ windowHeight: window.innerHeight }) | ||
} | ||
|
||
get defaultFocusElement() { | ||
return this.props.defaultFocusElement | ||
} | ||
|
@@ -146,21 +157,84 @@ class Modal extends Component<ModalProps, ModalState> { | |
} | ||
} | ||
|
||
getWindowHeightInRem = (): number => { | ||
if (!canUseDOM) { | ||
return Infinity | ||
} | ||
const rootFontSize = parseFloat( | ||
getComputedStyle(document.documentElement)?.fontSize || '16' | ||
) | ||
if (isNaN(rootFontSize) || rootFontSize <= 0) { | ||
return Infinity | ||
} | ||
return window.innerHeight / rootFontSize | ||
} | ||
|
||
renderChildren() { | ||
const { children, variant, overflow } = this.props | ||
|
||
// header should be non-sticky for small viewport height (ca. 320px) | ||
if (this.getWindowHeightInRem() <= 20) { | ||
return this.renderForSmallViewportHeight() | ||
} | ||
|
||
return Children.map(children as ReactElement, (child) => { | ||
if (!child) return // ignore null, falsy children | ||
return this.cloneChildWithProps(child, variant, overflow) | ||
}) | ||
} | ||
|
||
renderForSmallViewportHeight() { | ||
const { children, variant, overflow, styles } = this.props | ||
|
||
const headerAndBody: React.ReactNode[] = [] | ||
|
||
const childrenArray = Children.toArray(children) | ||
|
||
// Separate header and body elements | ||
const filteredChildren = childrenArray.filter((child) => { | ||
if (isValidElement(child)) { | ||
return safeCloneElement(child, { | ||
variant: variant, | ||
overflow: (child?.props as { overflow: string })?.overflow || overflow | ||
}) | ||
} else { | ||
return child | ||
Comment on lines
-156
to
-161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this to the cloneChildWithProps function |
||
if (child.type === Modal.Header || child.type === Modal.Body) { | ||
if (child.type === Modal.Header) { | ||
const headerWithProp = safeCloneElement(child, { | ||
smallPortView: true | ||
}) | ||
headerAndBody.push(headerWithProp) | ||
} else { | ||
headerAndBody.push(child) | ||
} | ||
return false | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
// Adds the <div> to the beginning of the filteredChildren array | ||
if (headerAndBody.length > 0) { | ||
Comment on lines
+212
to
+213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed to put the header and the body into the same element in order to be able scroll them together, so it results in some minor DOM change. |
||
filteredChildren.unshift( | ||
<div css={styles?.joinedHeaderAndBody}>{headerAndBody}</div> | ||
) | ||
} | ||
|
||
return Children.map(filteredChildren as ReactElement[], (child) => { | ||
if (!child) return // ignore null, falsy children | ||
return this.cloneChildWithProps(child, variant, overflow) | ||
}) | ||
} | ||
|
||
cloneChildWithProps( | ||
child: React.ReactNode, | ||
variant: string | undefined, | ||
overflow: string | undefined | ||
) { | ||
if (isValidElement(child)) { | ||
return safeCloneElement(child, { | ||
variant: variant, | ||
overflow: (child?.props as { overflow: string })?.overflow || overflow | ||
}) | ||
} else { | ||
return child | ||
} | ||
} | ||
|
||
renderDialog( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to add this because otherwise the close button starts scrolling with the text instead of staying in the Modal.Header