forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeEditorConfigurationModal.tsx
More file actions
230 lines (217 loc) · 7.28 KB
/
CodeEditorConfigurationModal.tsx
File metadata and controls
230 lines (217 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import CogIcon from '@patternfly/react-icons/dist/esm/icons/cog-icon';
import MapIcon from '@patternfly/react-icons/dist/esm/icons/map-icon';
import MoonIcon from '@patternfly/react-icons/dist/esm/icons/moon-icon';
import HashtagIcon from '@patternfly/react-icons/dist/esm/icons/hashtag-icon';
import FontIcon from '@patternfly/react-icons/dist/esm/icons/font-icon';
import { CodeEditor, CodeEditorControl } from '@patternfly/react-code-editor';
import {
Flex,
FlexItem,
Icon,
Modal,
ModalBody,
ModalHeader,
NumberInput,
Switch,
SwitchProps
} from '@patternfly/react-core';
import { useState } from 'react';
interface ConfigModalItemProps {
/** Icon rendered inside the configuration modal. */
icon?: ReactNode;
/** Description of the configuration option. */
description: string;
/** Title of the configuration option. We assume that titles are unique. */
title: string;
/**
* Optional ID of the configuration option. Also used as a prefix for the following elements:
* - `${id}-title` for the element which contains the title
* - `${id}-description` for the element which contains the description
*/
id?: string;
/**
* Slot to render inside the configuration modal. Remember to add `aria-labelledby` and `aria-describedby` props
* to the control inside the slot, pointing to the title and description ids respectively.
*/
slot?: ReactNode;
}
const ConfigModalItem: React.FunctionComponent<ConfigModalItemProps> = ({
icon = <CogIcon />,
description,
title,
id = `ConfigModalItem-${title.replace(/\s+/g, '-').toLowerCase()}`,
slot
}) => (
<Flex
alignItems={{ default: 'alignItemsCenter' }}
justifyContent={{ default: 'justifyContentSpaceBetween' }}
spaceItems={{ default: 'spaceItemsMd' }}
id={id}
>
<FlexItem flex={{ default: 'flex_1' }}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<Icon isInline>{icon}</Icon>
<strong id={`${id}-title`} className="pf-v6-u-font-weight-bold">
{title}
</strong>
</Flex>
<div id={`${id}-description`}>{description}</div>
</FlexItem>
<FlexItem alignSelf={{ default: 'alignSelfCenter' }}>{slot}</FlexItem>
</Flex>
);
interface ConfigModalSwitchProps extends Omit<ConfigModalItemProps, 'slot'> {
/** Flag indicating whether the option is enabled or disabled. */
isChecked?: SwitchProps['isChecked'];
/** onChange handler for the switch. */
onChange?: SwitchProps['onChange'];
/** Labels for the enabled and disabled states of the switch. */
labels?: {
enabled: string;
disabled: string;
};
}
const ConfigModalSwitch: React.FunctionComponent<ConfigModalSwitchProps> = ({
icon = <CogIcon />,
description,
title,
id = `ConfigModalSwitch-${title.replace(/\s+/g, '-').toLowerCase()}`,
isChecked = false,
onChange,
labels = { enabled: undefined, disabled: undefined }
}) => (
<ConfigModalItem
icon={icon}
description={description}
title={title}
id={id}
slot={
<Switch
aria-labelledby={`${id}-title`}
aria-describedby={`${id}-description`}
ouiaId={`${id}-switch`}
isChecked={isChecked}
isReversed
label={isChecked ? labels.enabled : labels.disabled}
onChange={onChange}
/>
}
/>
);
interface ConfigModalControlProps {
/** Controls to be rendered inside the configuration modal. */
children: React.ReactNode;
/** Title of the configuration modal. */
title?: string;
/** Description of the configuration modal. */
description?: string;
/** OptionalID of the configuration modal. Also used as a prefix for the ids of inner elements and the OUIA id. */
ouiaId?: string;
}
const ConfigModalControl: React.FunctionComponent<ConfigModalControlProps> = ({
children,
title = 'Editor settings',
description = 'Changes apply immediately',
ouiaId = 'CodeEditorConfigurationModal'
}: ConfigModalControlProps) => {
const [isModalOpen, setIsModalOpen] = useState(false);
return (
<>
<Modal
aria-describedby={`${ouiaId}-body`}
aria-labelledby={`${ouiaId}-title`}
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
ouiaId={ouiaId}
variant="small"
>
<ModalHeader title={title} description={description} labelId={`${ouiaId}-title`} />
<ModalBody id={`${ouiaId}-body`}>
<Flex direction={{ default: 'column' }} spaceItems={{ default: 'spaceItemsMd' }}>
{children}
</Flex>
</ModalBody>
</Modal>
<CodeEditorControl
aria-label={title}
aria-haspopup="dialog"
isSettings
onClick={() => setIsModalOpen(true)}
tooltipProps={{ content: title, ariaLive: 'off' }}
/>
</>
);
};
export const CodeEditorConfigurationModal: React.FunctionComponent = () => {
const [code, setCode] = useState('Some example content');
const [isMinimapVisible, setIsMinimapVisible] = useState(true);
const [isDarkTheme, setIsDarkTheme] = useState(false);
const [isLineNumbersVisible, setIsLineNumbersVisible] = useState(true);
const [fontSize, setFontSize] = useState(14);
const onChange = (code: string) => {
setCode(code);
};
const customControl = (
<ConfigModalControl>
<ConfigModalSwitch
key="minimap-switch"
title="Minimap"
description="Show a preview of the full code on the side of the editor"
isChecked={isMinimapVisible}
onChange={(_e, checked) => setIsMinimapVisible(checked)}
icon={<MapIcon />}
/>
<ConfigModalSwitch
key="dark-theme-switch"
title="Dark theme"
description="Switch the editor to a dark color theme"
isChecked={isDarkTheme}
onChange={(_e, checked) => setIsDarkTheme(checked)}
icon={<MoonIcon />}
/>
<ConfigModalSwitch
key="line-numbers-switch"
title="Line numbers"
description="Show line numbers to the left of each line of code"
isChecked={isLineNumbersVisible}
onChange={(_e, checked) => setIsLineNumbersVisible(checked)}
icon={<HashtagIcon />}
/>
<ConfigModalItem
key="font-size"
title="Font size"
description="Adjust the font size of the code editor"
ouia-id="ConfigModalItem-font-size"
icon={<FontIcon />}
slot={
<NumberInput
aria-labelledby="ConfigModalItem-font-size-title"
aria-describedby="ConfigModalItem-font-size-description"
inputAriaLabel="Enter a font size"
minusBtnAriaLabel="Decrease font size"
plusBtnAriaLabel="Increase font size"
role="group" // For screen readers to group the input and buttons as one widget
value={fontSize}
min={5}
onMinus={() => setFontSize((size) => Math.max(5, size - 1))}
onChange={(event) => setFontSize(Number((event.target as HTMLInputElement).value))}
onPlus={() => setFontSize((size) => size + 1)}
widthChars={2}
/>
}
/>
</ConfigModalControl>
);
return (
<CodeEditor
code={code}
customControls={customControl}
height="400px"
isDarkTheme={isDarkTheme}
isLineNumbersVisible={isLineNumbersVisible}
isMinimapVisible={isMinimapVisible}
onChange={onChange}
options={{ fontSize }}
/>
);
};