Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions packages/ui-modal/src/Modal/ModalBody/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import generateComponentTheme from './theme'
import { propTypes, allowedProps } from './props'
import type { ModalBodyProps } from './props'
import { UIElement } from '@instructure/shared-types'
import ModalContext from '../ModalContext'

/**
---
Expand Down Expand Up @@ -120,29 +121,36 @@ class ModalBody extends Component<ModalBodyProps> {
// TODO rethink, the 'as' prop, likely its not a good idea to allow React
// components. See INSTUI-4674
const finalRef = this.getFinalRef(this.ref)

const hasScrollbar =
finalRef &&
Math.abs(
(finalRef.scrollHeight ?? 0) -
(finalRef.getBoundingClientRect()?.height ?? 0)
) > 1
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was increased from 0.05 to prevent scrollbar when e.g. one value is 23.5 and the other 24.

return (
<View
{...passthroughProps}
display="block"
width={isFit ? '100%' : undefined}
height={isFit ? '100%' : undefined}
elementRef={this.handleRef}
as={as}
css={this.props.styles?.modalBody}
padding={padding}
// check if there is a scrollbar, if so, the element has to be tabbable to be able to scroll with keyboard only
// epsilon tolerance is used to avoid false positives, this is generally safer than Math rounding techniques
{...(finalRef &&
Math.abs(
(finalRef.scrollHeight ?? 0) -
(finalRef.getBoundingClientRect()?.height ?? 0)
) > 0.05
? { tabIndex: 0 }
: {})}
>
{children}
</View>
<ModalContext.Consumer>
{(value) => (
<View
{...passthroughProps}
display="block"
width={isFit ? '100%' : undefined}
height={isFit ? '100%' : undefined}
elementRef={this.handleRef}
as={as}
css={this.props.styles?.modalBody}
padding={padding}
// check if there is a scrollbar, if so, the element has to be tabbable
{...(hasScrollbar
? {
tabIndex: 0,
'aria-label': value.bodyScrollAriaLabel
}
: {})}
>
{children}
</View>
)}
</ModalContext.Consumer>
)
}
}
Expand Down
46 changes: 46 additions & 0 deletions packages/ui-modal/src/Modal/ModalContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import { createContext } from 'react'

type ModalContextType = {
/**
* Needed for setting the correct aria-label in the modal body when its scrollable.
* This is a value read from the Modal's Header.
*/
bodyScrollAriaLabel?: string
setBodyScrollAriaLabel?: (txt: string) => void
}

/**
* React context created by the `Modal` component
* @private
*/
const ModalContext = createContext<ModalContextType>({
bodyScrollAriaLabel: undefined
})

export default ModalContext
export { ModalContext }
export type { ModalContextType }
26 changes: 26 additions & 0 deletions packages/ui-modal/src/Modal/ModalHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import generateComponentTheme from './theme'

import { propTypes, allowedProps } from './props'
import type { ModalHeaderProps, ModalHeaderStyleProps } from './props'
import ModalContext from '../ModalContext'

type CloseButtonChild = ComponentElement<CloseButtonProps, CloseButton>

Expand All @@ -62,9 +63,34 @@ class ModalHeader extends Component<ModalHeaderProps> {
}

ref: HTMLDivElement | null = null
declare context: React.ContextType<typeof ModalContext>
static contextType = ModalContext

/**
* Gets all text in a DOM subtree, text under <button> nodes is excluded
*/
getTextExcludingButtons = (root: Node) => {
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
acceptNode(node) {
return node.parentElement?.closest('button')
? NodeFilter.FILTER_REJECT
: NodeFilter.FILTER_ACCEPT
}
})
let text = ''
let current
while ((current = walker.nextNode())) {
text += current.nodeValue
}
return text
}

handleRef = (el: HTMLDivElement | null) => {
this.ref = el
if (el) {
const txt = this.getTextExcludingButtons(el)
this.context.setBodyScrollAriaLabel?.(txt)
}
}

componentDidMount() {
Expand Down
26 changes: 18 additions & 8 deletions packages/ui-modal/src/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import type {
ModalPropsForPortal,
ModalPropsForTransition
} from './props'
import ModalContext from './ModalContext'

/**
---
Expand Down Expand Up @@ -86,7 +87,8 @@ class Modal extends Component<ModalProps, ModalState> {
this.state = {
transitioning: false,
open: props.open ?? false,
windowHeight: 99999
windowHeight: 99999,
bodyScrollAriaLabel: undefined
}
}

Expand Down Expand Up @@ -302,13 +304,21 @@ class Modal extends Component<ModalProps, ModalState> {
onClose
)}
>
{constrain === 'parent' ? (
<span css={this.props.styles?.constrainContext}>
{this.renderDialog(passthroughProps)}
</span>
) : (
this.renderDialog(passthroughProps)
)}
<ModalContext.Provider
value={{
bodyScrollAriaLabel: this.state.bodyScrollAriaLabel,
setBodyScrollAriaLabel: (txt: string) =>
this.setState({ bodyScrollAriaLabel: txt })
}}
>
{constrain === 'parent' ? (
<span css={this.props.styles?.constrainContext}>
{this.renderDialog(passthroughProps)}
</span>
) : (
this.renderDialog(passthroughProps)
)}
</ModalContext.Provider>
</Transition>
</Portal>
)
Expand Down
4 changes: 4 additions & 0 deletions packages/ui-modal/src/Modal/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ type ModalState = {
transitioning: boolean
open: boolean
windowHeight: number
/**
* The `aria-label` on the Modal's body if it's scrollable.
*/
bodyScrollAriaLabel?: string
}

const propTypes: PropValidators<PropKeys> = {
Expand Down