Skip to content

Commit 73868fb

Browse files
committed
chore(docs): Update ModalWithDropdown example
Example was using out-of-date PatternFly syntax from a prior version. It should now show how to properly add a dropdown to a modal that allows for keyboard accessibility.
1 parent fe63802 commit 73868fb

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

packages/react-core/src/components/Modal/examples/Modal.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import formStyles from '@patternfly/react-styles/css/components/Form/form';
1717

1818
### Basic modals
1919

20-
Basic modals give users the option to either confirm or cancel an action.
20+
Basic modals give users the option to either confirm or cancel an action.
2121

2222
To flag an open modal, use the `isOpen` property. To execute a callback when a modal is closed, use the `onClose` property.
2323

@@ -71,7 +71,7 @@ To choose a specific width for a modal, use the `width` property. The following
7171

7272
### Custom header
7373

74-
To add a custom header to a modal, your custom content must be passed as a child of the `<ModalHeader>` component. Do not pass the `title` property to `<ModalHeader>` when using a custom header.
74+
To add a custom header to a modal, your custom content must be passed as a child of the `<ModalHeader>` component. Do not pass the `title` property to `<ModalHeader>` when using a custom header.
7575

7676
```ts file="./ModalCustomHeader.tsx"
7777

@@ -113,9 +113,9 @@ To guide users through a series of steps in a modal, you can add a [wizard](/com
113113

114114
### With dropdown
115115

116-
To present a menu of actions or links to a user, you can add a [dropdown](/components/menus/dropdown) to a modal.
116+
To present a menu of actions or links to a user, you can add a [dropdown](/components/menus/dropdown) to a modal.
117117

118-
To allow the dropdown to visually break out of the modal container, set the `menuAppendTo` property to parent. Handle the modal’s closing behavior by listening to the `onEscapePress` callback on the `<Modal>` component. This allows the "escape" key to collapse the dropdown without closing the entire modal.
118+
To allow the dropdown to visually break out of the modal container, set the `popperProps appendTo` property to one of the parent content items in the modal. Otherwise you can use `inline` to allow it to scroll within the modal itself. Using the Dropdown's default append location will interfere with keyboard accessibility due to the modal's built-in focus trap. Handle the modal’s closing behavior by listening to the `onEscapePress` callback on the `<Modal>` component. This allows the "escape" key to collapse the dropdown without closing the entire modal.
119119

120120
```ts file="./ModalWithDropdown.tsx"
121121

@@ -141,7 +141,7 @@ To enable form submission from a button in the modal's footer (outside of the `<
141141

142142
### Custom focus
143143

144-
To customize which element inside the modal receives focus when initially opened, use the `elementToFocus` property`.
144+
To customize which element inside the modal receives focus when initially opened, use the `elementToFocus` property`.
145145

146146
```ts file="./ModalCustomFocus.tsx"
147147

packages/react-core/src/components/Modal/examples/ModalWithDropdown.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ export const ModalWithDropdown: React.FunctionComponent = () => {
3232
};
3333

3434
const onFocus = () => {
35-
const element = document.getElementById('modal-dropdown-toggle');
36-
(element as HTMLElement).focus();
35+
if (typeof document !== 'undefined') {
36+
const element = document.getElementById('modal-dropdown-toggle');
37+
(element as HTMLElement)?.focus();
38+
}
3739
};
3840

3941
const onEscapePress = (event: KeyboardEvent) => {
@@ -45,6 +47,16 @@ export const ModalWithDropdown: React.FunctionComponent = () => {
4547
}
4648
};
4749

50+
const getAppendLocation = () => {
51+
// document doesn't exist when PatternFly website docs framework gets pre-rendered
52+
// this is just to avoid that issue - works fine at runtime without it
53+
if (typeof document !== 'undefined' && document.getElementById) {
54+
return document.getElementById('modal-box-body-with-dropdown');
55+
} else {
56+
return 'inline';
57+
}
58+
};
59+
4860
return (
4961
<Fragment>
5062
<Button variant="primary" onClick={handleModalToggle}>
@@ -61,10 +73,13 @@ export const ModalWithDropdown: React.FunctionComponent = () => {
6173
<ModalHeader title="Dropdown modal" labelId="modal-with-dropdown" />
6274
<ModalBody id="modal-box-body-with-dropdown">
6375
<div>
64-
Set the dropdown <strong>menuAppendTo</strong> prop to <em>parent</em> in order to allow the dropdown menu
65-
break out of the modal container. You'll also want to handle closing of the modal yourself, by listening to
66-
the <strong>onEscapePress</strong> callback on the Modal component, so you can close the Dropdown first if
67-
it's open without closing the entire modal.
76+
Set the Dropdown's popperProps appendTo prop to <em>inline</em> if you want the dropdown to scroll within
77+
the modal. Set the popperProps appendTo prop to the id of the <em>parent modal or modal body</em> in order
78+
to allow the dropdown menu to break out of the modal container. Using the default dropdown location will
79+
break keyboard control over the dropdown due to the modal's built-in focus trap. You'll also want to handle
80+
closing of the modal yourself, by listening to the
81+
<strong>onEscapePress</strong> callback on the Modal component, so you can close the Dropdown first if it's
82+
open without closing the entire modal.
6883
</div>
6984
<br />
7085
<div>
@@ -73,10 +88,18 @@ export const ModalWithDropdown: React.FunctionComponent = () => {
7388
onSelect={onSelect}
7489
onOpenChange={(isOpen: boolean) => setIsDropdownOpen(isOpen)}
7590
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
76-
<MenuToggle ref={toggleRef} onClick={handleDropdownToggle} isExpanded={isDropdownOpen}>
91+
<MenuToggle
92+
id="modal-dropdown-toggle"
93+
ref={toggleRef}
94+
onClick={handleDropdownToggle}
95+
isExpanded={isDropdownOpen}
96+
>
7797
Dropdown
7898
</MenuToggle>
7999
)}
100+
popperProps={{
101+
appendTo: getAppendLocation()
102+
}}
80103
>
81104
<DropdownList>
82105
<DropdownItem value={0} key="action">

0 commit comments

Comments
 (0)