@instructure/ui-modal 11.7.4-snapshot-53 → 11.7.4-snapshot-80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,7 +26,7 @@ import { Component } from 'react'
26
26
 
27
27
  import { View } from '@instructure/ui-view/v11_6'
28
28
  import { omitProps } from '@instructure/ui-react-utils'
29
- import { getCSSStyleDeclaration } from '@instructure/ui-dom-utils'
29
+ import { getCSSStyleDeclaration, findTabbable } from '@instructure/ui-dom-utils'
30
30
 
31
31
  import { withStyle } from '@instructure/emotion'
32
32
  import generateStyle from './styles.js'
@@ -56,6 +56,8 @@ class ModalBody extends Component<ModalBodyProps> {
56
56
  }
57
57
 
58
58
  ref: UIElement | null = null
59
+ private resizeObserver?: ResizeObserver
60
+ private mutationObserver?: MutationObserver
59
61
 
60
62
  handleRef = (el: UIElement | null) => {
61
63
  const { elementRef } = this.props
@@ -87,12 +89,39 @@ class ModalBody extends Component<ModalBodyProps> {
87
89
  if (isFirefox) {
88
90
  this.setState({ isFirefox })
89
91
  }
92
+
93
+ const finalRef = this.getFinalRef(this.ref)
94
+ if (finalRef && typeof ResizeObserver !== 'undefined') {
95
+ this.resizeObserver = new ResizeObserver(() => this.forceUpdate())
96
+ this.resizeObserver.observe(finalRef)
97
+ this.mutationObserver = new MutationObserver(() => this.forceUpdate())
98
+ this.mutationObserver.observe(finalRef, {
99
+ childList: true,
100
+ subtree: true,
101
+ attributes: true,
102
+ attributeFilter: [
103
+ 'disabled',
104
+ 'tabindex',
105
+ 'hidden',
106
+ 'href',
107
+ 'contenteditable',
108
+ 'type',
109
+ 'class',
110
+ 'style'
111
+ ]
112
+ })
113
+ }
90
114
  }
91
115
 
92
116
  componentDidUpdate() {
93
117
  this.props.makeStyles?.()
94
118
  }
95
119
 
120
+ componentWillUnmount() {
121
+ this.resizeObserver?.disconnect()
122
+ this.mutationObserver?.disconnect()
123
+ }
124
+
96
125
  getFinalRef(el: UIElement): Element | undefined {
97
126
  if (!el) {
98
127
  return undefined
@@ -125,6 +154,8 @@ class ModalBody extends Component<ModalBodyProps> {
125
154
  (finalRef.scrollHeight ?? 0) -
126
155
  (finalRef.getBoundingClientRect()?.height ?? 0)
127
156
  ) > 1
157
+ const hasTabbableChildren = !!finalRef && findTabbable(finalRef).length > 0
158
+ const needsTabIndex = hasScrollbar && !hasTabbableChildren
128
159
  return (
129
160
  <ModalContext.Consumer>
130
161
  {(value) => (
@@ -138,8 +169,9 @@ class ModalBody extends Component<ModalBodyProps> {
138
169
  as={as}
139
170
  css={this.props.styles?.modalBody}
140
171
  padding={padding}
141
- // check if there is a scrollbar, if so, the element has to be tabbable
142
- {...(hasScrollbar
172
+ // check if there is a scrollbar and no focusable children, if so, the
173
+ // element has to be tabbable
174
+ {...(needsTabIndex
143
175
  ? {
144
176
  tabIndex: 0,
145
177
  'aria-label': value.bodyScrollAriaLabel
@@ -672,6 +672,7 @@ type: embed
672
672
  <Figure.Item>Modals should be able to be closed by clicking away, esc key and/or a close button</Figure.Item>
673
673
  <Figure.Item>We recommend that modals begin with a heading (typically H2)</Figure.Item>
674
674
  <Figure.Item>The Modal's header currently becomes non-sticky when the window height is too small, improving navigation of the Modal.Body, e.g., at higher zoom levels</Figure.Item>
675
+ <Figure.Item>`Modal.Body` automatically becomes a keyboard tab stop (its `tabIndex` is set to `0`) when it is vertically scrollable and contains no focusable elements, so it can be scrolled by keyboard-only users. This is re-evaluated dynamically as the body's content or size changes.</Figure.Item>
675
676
  </Figure>
676
677
  </Guidelines>
677
678
  ```
@@ -107,6 +107,14 @@ class Modal extends Component<ModalProps, ModalState> {
107
107
  componentDidUpdate(prevProps: ModalProps) {
108
108
  if (this.props.open !== prevProps.open) {
109
109
  this.setState({ transitioning: true, open: !!this.props.open })
110
+ // When closing, release focus from the Dialog.
111
+ // The Dialog stays open so its content can fade out, but the
112
+ // Transition marks the exiting subtree aria-hidden -- keeping focus inside
113
+ // it would hide a focused element from assistive tech resulting in a
114
+ // console warning
115
+ if (prevProps.open && !this.props.open) {
116
+ this._content?.blur()
117
+ }
110
118
  }
111
119
  this.props.makeStyles?.()
112
120
  }
@@ -284,31 +292,31 @@ class Modal extends Component<ModalProps, ModalState> {
284
292
  onOpen={this.handlePortalOpen}
285
293
  data-cid="Modal"
286
294
  >
287
- <Transition
288
- in={open}
289
- transitionOnMount
290
- type={transition}
291
- onEnter={onEnter}
292
- onEntering={onEntering}
293
- onEntered={createChainedFunction(
294
- this.handleTransitionComplete,
295
- onEntered,
296
- onOpen
297
- )}
298
- onExit={onExit}
299
- onExiting={onExiting}
300
- onExited={createChainedFunction(
301
- this.handleTransitionComplete,
302
- onExited,
303
- onClose
304
- )}
295
+ <ModalContext.Provider
296
+ value={{
297
+ bodyScrollAriaLabel: this.state.bodyScrollAriaLabel,
298
+ setBodyScrollAriaLabel: (txt: string) =>
299
+ this.setState({ bodyScrollAriaLabel: txt })
300
+ }}
305
301
  >
306
- <ModalContext.Provider
307
- value={{
308
- bodyScrollAriaLabel: this.state.bodyScrollAriaLabel,
309
- setBodyScrollAriaLabel: (txt: string) =>
310
- this.setState({ bodyScrollAriaLabel: txt })
311
- }}
302
+ <Transition
303
+ in={open}
304
+ transitionOnMount
305
+ type={transition}
306
+ onEnter={onEnter}
307
+ onEntering={onEntering}
308
+ onEntered={createChainedFunction(
309
+ this.handleTransitionComplete,
310
+ onEntered,
311
+ onOpen
312
+ )}
313
+ onExit={onExit}
314
+ onExiting={onExiting}
315
+ onExited={createChainedFunction(
316
+ this.handleTransitionComplete,
317
+ onExited,
318
+ onClose
319
+ )}
312
320
  >
313
321
  {constrain === 'parent' ? (
314
322
  <span css={this.props.styles?.constrainContext}>
@@ -317,8 +325,8 @@ class Modal extends Component<ModalProps, ModalState> {
317
325
  ) : (
318
326
  this.renderDialog(passthroughProps)
319
327
  )}
320
- </ModalContext.Provider>
321
- </Transition>
328
+ </Transition>
329
+ </ModalContext.Provider>
322
330
  </Portal>
323
331
  )
324
332
  }
@@ -26,7 +26,7 @@ import { Component } from 'react'
26
26
 
27
27
  import { View } from '@instructure/ui-view/latest'
28
28
  import { omitProps } from '@instructure/ui-react-utils'
29
- import { getCSSStyleDeclaration } from '@instructure/ui-dom-utils'
29
+ import { getCSSStyleDeclaration, findTabbable } from '@instructure/ui-dom-utils'
30
30
 
31
31
  import { withStyleNew } from '@instructure/emotion'
32
32
  import generateStyle from './styles.js'
@@ -56,6 +56,8 @@ class ModalBody extends Component<ModalBodyProps> {
56
56
  }
57
57
 
58
58
  ref: UIElement | null = null
59
+ private resizeObserver?: ResizeObserver
60
+ private mutationObserver?: MutationObserver
59
61
 
60
62
  handleRef = (el: UIElement | null) => {
61
63
  const { elementRef } = this.props
@@ -87,12 +89,39 @@ class ModalBody extends Component<ModalBodyProps> {
87
89
  if (isFirefox) {
88
90
  this.setState({ isFirefox })
89
91
  }
92
+
93
+ const finalRef = this.getFinalRef(this.ref)
94
+ if (finalRef && typeof ResizeObserver !== 'undefined') {
95
+ this.resizeObserver = new ResizeObserver(() => this.forceUpdate())
96
+ this.resizeObserver.observe(finalRef)
97
+ this.mutationObserver = new MutationObserver(() => this.forceUpdate())
98
+ this.mutationObserver.observe(finalRef, {
99
+ childList: true,
100
+ subtree: true,
101
+ attributes: true,
102
+ attributeFilter: [
103
+ 'disabled',
104
+ 'tabindex',
105
+ 'hidden',
106
+ 'href',
107
+ 'contenteditable',
108
+ 'type',
109
+ 'class',
110
+ 'style'
111
+ ]
112
+ })
113
+ }
90
114
  }
91
115
 
92
116
  componentDidUpdate() {
93
117
  this.props.makeStyles?.()
94
118
  }
95
119
 
120
+ componentWillUnmount() {
121
+ this.resizeObserver?.disconnect()
122
+ this.mutationObserver?.disconnect()
123
+ }
124
+
96
125
  getFinalRef(el: UIElement): Element | undefined {
97
126
  if (!el) {
98
127
  return undefined
@@ -133,6 +162,8 @@ class ModalBody extends Component<ModalBodyProps> {
133
162
  (finalRef.scrollHeight ?? 0) -
134
163
  (finalRef.getBoundingClientRect()?.height ?? 0)
135
164
  ) > 1
165
+ const hasTabbableChildren = !!finalRef && findTabbable(finalRef).length > 0
166
+ const needsTabIndex = hasScrollbar && !hasTabbableChildren
136
167
  return (
137
168
  <ModalContext.Consumer>
138
169
  {(value) => (
@@ -146,8 +177,9 @@ class ModalBody extends Component<ModalBodyProps> {
146
177
  as={as}
147
178
  css={this.props.styles?.modalBody}
148
179
  padding={spacing ? undefined : padding}
149
- // check if there is a scrollbar, if so, the element has to be tabbable
150
- {...(hasScrollbar
180
+ // check if there is a scrollbar and no focusable children, if so, the
181
+ // element has to be tabbable
182
+ {...(needsTabIndex
151
183
  ? {
152
184
  tabIndex: 0,
153
185
  'aria-label': value.bodyScrollAriaLabel
@@ -618,16 +618,14 @@ You can do this with the `insertAt` prop or a theme override:
618
618
  type: code
619
619
  ---
620
620
  <InstUISettingsProvider
621
- theme={{
622
- themeOverrides: {
623
- componentOverrides: {
624
- Mask: {
625
- zIndex: 555,
626
- }
627
- }
628
- }
629
- }}
630
- >
621
+ themeOverride={{
622
+ components: {
623
+ Mask: {
624
+ zIndex: 555
625
+ }
626
+ }
627
+ }}
628
+ >
631
629
  <Modal />
632
630
  </InstUISettingsProvider>
633
631
  ```
@@ -668,6 +666,7 @@ type: embed
668
666
  <Figure.Item>Modals should be able to be closed by clicking away, esc key and/or a close button</Figure.Item>
669
667
  <Figure.Item>We recommend that modals begin with a heading (typically H2)</Figure.Item>
670
668
  <Figure.Item>The Modal's header currently becomes non-sticky when the window height is too small, improving navigation of the Modal.Body, e.g., at higher zoom levels</Figure.Item>
669
+ <Figure.Item>`Modal.Body` automatically becomes a keyboard tab stop (its `tabIndex` is set to `0`) when it is vertically scrollable and contains no focusable elements, so it can be scrolled by keyboard-only users. This is re-evaluated dynamically as the body's content or size changes.</Figure.Item>
671
670
  </Figure>
672
671
  </Guidelines>
673
672
  ```
@@ -106,6 +106,14 @@ class Modal extends Component<ModalProps, ModalState> {
106
106
  componentDidUpdate(prevProps: ModalProps) {
107
107
  if (this.props.open !== prevProps.open) {
108
108
  this.setState({ transitioning: true, open: !!this.props.open })
109
+ // When closing, release focus from the Dialog.
110
+ // The Dialog stays open so its content can fade out, but the
111
+ // Transition marks the exiting subtree aria-hidden -- keeping focus inside
112
+ // it would hide a focused element from assistive tech resulting in a
113
+ // console warning
114
+ if (prevProps.open && !this.props.open) {
115
+ this._content?.blur()
116
+ }
109
117
  }
110
118
  this.props.makeStyles?.()
111
119
  }
@@ -205,8 +213,7 @@ class Modal extends Component<ModalProps, ModalState> {
205
213
  | keyof ModalPropsForTransition
206
214
  | 'constrain'
207
215
  | 'overflow'
208
- >,
209
- open?: boolean
216
+ >
210
217
  ) {
211
218
  const {
212
219
  onDismiss,
@@ -225,7 +232,7 @@ class Modal extends Component<ModalProps, ModalState> {
225
232
  <Dialog
226
233
  {...passthroughProps(props)}
227
234
  as={as}
228
- open={open && !this.state.transitioning}
235
+ open
229
236
  label={label}
230
237
  defaultFocusElement={this.defaultFocusElement}
231
238
  shouldCloseOnDocumentClick={shouldCloseOnDocumentClick}
@@ -284,41 +291,41 @@ class Modal extends Component<ModalProps, ModalState> {
284
291
  onOpen={this.handlePortalOpen}
285
292
  data-cid="Modal"
286
293
  >
287
- <Transition
288
- in={open}
289
- transitionOnMount
290
- type={transition}
291
- onEnter={onEnter}
292
- onEntering={onEntering}
293
- onEntered={createChainedFunction(
294
- this.handleTransitionComplete,
295
- onEntered,
296
- onOpen
297
- )}
298
- onExit={onExit}
299
- onExiting={onExiting}
300
- onExited={createChainedFunction(
301
- this.handleTransitionComplete,
302
- onExited,
303
- onClose
304
- )}
294
+ <ModalContext.Provider
295
+ value={{
296
+ bodyScrollAriaLabel: this.state.bodyScrollAriaLabel,
297
+ setBodyScrollAriaLabel: (txt: string) =>
298
+ this.setState({ bodyScrollAriaLabel: txt })
299
+ }}
305
300
  >
306
- <ModalContext.Provider
307
- value={{
308
- bodyScrollAriaLabel: this.state.bodyScrollAriaLabel,
309
- setBodyScrollAriaLabel: (txt: string) =>
310
- this.setState({ bodyScrollAriaLabel: txt })
311
- }}
301
+ <Transition
302
+ in={open}
303
+ transitionOnMount
304
+ type={transition}
305
+ onEnter={onEnter}
306
+ onEntering={onEntering}
307
+ onEntered={createChainedFunction(
308
+ this.handleTransitionComplete,
309
+ onEntered,
310
+ onOpen
311
+ )}
312
+ onExit={onExit}
313
+ onExiting={onExiting}
314
+ onExited={createChainedFunction(
315
+ this.handleTransitionComplete,
316
+ onExited,
317
+ onClose
318
+ )}
312
319
  >
313
320
  {constrain === 'parent' ? (
314
321
  <span css={this.props.styles?.constrainContext}>
315
- {this.renderDialog(passthroughProps, open)}
322
+ {this.renderDialog(passthroughProps)}
316
323
  </span>
317
324
  ) : (
318
- this.renderDialog(passthroughProps, open)
325
+ this.renderDialog(passthroughProps)
319
326
  )}
320
- </ModalContext.Provider>
321
- </Transition>
327
+ </Transition>
328
+ </ModalContext.Provider>
322
329
  </Portal>
323
330
  )
324
331
  }