Debian Codegraph @

node/@patternfly/react-core/src/components/Modal/examples/ModalWithDropdown.tsx

raw
1 import { Fragment, useState } from 'react';
2 import {
3 Button,
4 Dropdown,
5 DropdownList,
6 DropdownItem,
7 MenuToggle,
8 MenuToggleElement,
9 Modal,
10 ModalBody,
11 ModalHeader,
12 ModalFooter,
13 ModalVariant
14 } from '@patternfly/react-core';
15
16 export const ModalWithDropdown: React.FunctionComponent = () => {
17 const [isModalOpen, setIsModalOpen] = useState(false);
18 const [isDropdownOpen, setIsDropdownOpen] = useState(false);
19
20 const handleModalToggle = (_event: KeyboardEvent | React.MouseEvent) => {
21 setIsModalOpen(!isModalOpen);
22 setIsDropdownOpen(false);
23 };
24
25 const handleDropdownToggle = () => {
26 setIsDropdownOpen(!isDropdownOpen);
27 };
28
29 const onSelect = () => {
30 setIsDropdownOpen(!isDropdownOpen);
31 onFocus();
32 };
33
34 const onFocus = () => {
35 if (typeof document !== 'undefined') {
36 const element = document.getElementById('modal-dropdown-toggle');
37 (element as HTMLElement)?.focus();
38 }
39 };
40
41 const onEscapePress = (event: KeyboardEvent) => {
42 if (isDropdownOpen) {
43 setIsDropdownOpen(!isDropdownOpen);
44 onFocus();
45 } else {
46 handleModalToggle(event);
47 }
48 };
49
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-with-dropdown-focus-trap');
55 } else {
56 return 'inline';
57 }
58 };
59
60 return (
61 <Fragment>
62 <Button variant="primary" onClick={handleModalToggle}>
63 Show dropdown modal
64 </Button>
65 <Modal
66 variant={ModalVariant.small}
67 isOpen={isModalOpen}
68 onClose={handleModalToggle}
69 onEscapePress={onEscapePress}
70 aria-labelledby="modal-with-dropdown"
71 aria-describedby="modal-box-body-with-dropdown"
72 focusTrapId="modal-with-dropdown-focus-trap"
73 >
74 <ModalHeader title="Dropdown modal" labelId="modal-with-dropdown" />
75 <ModalBody id="modal-box-body-with-dropdown">
76 <div>
77 Using the Dropdown's default append location will interfere with keyboard accessibility due to the modal's
78 built-in focus trap. To allow the dropdown to visually break out of the modal container, set the Dropdown's
79 popperProps appendTo property to id of the focus trap for the modal. This can be done by adding prop
80 focusTrapId to the modal, and then setting the append location to that as per this example. Otherwise you
81 can use "inline" to allow it to scroll within the modal itself. Appending to the focus trap should allow the
82 menu to expand visually outside the Modal (no scrollbar created in the Modal itself), and still allow
83 focusing the content within that menu via keyboard. You should also handle the modal's closing behavior by
84 listening to the
85 <em>onEscapePress</em> callback on the Modal component. This allows the "escape" key to collapse the
86 dropdown without closing the entire modal.
87 </div>
88 <br />
89 <div>
90 <Dropdown
91 isOpen={isDropdownOpen}
92 onSelect={onSelect}
93 onOpenChange={(isOpen: boolean) => setIsDropdownOpen(isOpen)}
94 toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
95 <MenuToggle
96 id="modal-dropdown-toggle"
97 ref={toggleRef}
98 onClick={handleDropdownToggle}
99 isExpanded={isDropdownOpen}
100 >
101 Dropdown
102 </MenuToggle>
103 )}
104 popperProps={{
105 appendTo: getAppendLocation()
106 }}
107 >
108 <DropdownList>
109 <DropdownItem value={0} key="action">
110 Action
111 </DropdownItem>
112 <DropdownItem
113 value={1}
114 key="link"
115 to="#default-link2"
116 // Prevent the default onClick functionality for example purposes
117 onClick={(ev: any) => ev.preventDefault()}
118 >
119 Link
120 </DropdownItem>
121 <DropdownItem value={2} isDisabled key="disabled action">
122 Disabled Action
123 </DropdownItem>
124 <DropdownItem value={3} isDisabled key="disabled link" to="#default-link4">
125 Disabled Link
126 </DropdownItem>
127 </DropdownList>
128 </Dropdown>
129 </div>
130 </ModalBody>
131 <ModalFooter>
132 <Button key="confirm" variant="primary" onClick={handleModalToggle}>
133 Confirm
134 </Button>
135 <Button key="cancel" variant="link" onClick={handleModalToggle}>
136 Cancel
137 </Button>
138 </ModalFooter>
139 </Modal>
140 </Fragment>
141 );
142 };