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
170 lines (159 loc) · 5.51 KB
/
CodeEditorConfigurationModal.tsx
File metadata and controls
170 lines (159 loc) · 5.51 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
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 { CodeEditor, CodeEditorControl } from '@patternfly/react-code-editor';
import { Flex, FlexItem, Icon, Modal, ModalBody, ModalHeader, Switch, SwitchProps } from '@patternfly/react-core';
import { useState } from 'react';
interface ConfigModalItemProps {
/** Icon rendered inside the configuration modal. */
icon?: React.ReactNode;
/** Description of the configuration option. */
description: string;
/** Flag indicating whether the option is enabled or disabled. */
isChecked?: SwitchProps['isChecked'];
/** onChange handler for the switch. */
onChange?: SwitchProps['onChange'];
/** Title of the configuration option. We assume that titles are unique. */
title: string;
/** Labels for the enabled and disabled states of the switch. */
labels?: {
enabled: string;
disabled: string;
};
/** Optional OUIA ID of the configuration option. Also used as a prefix for the ids of inner elements. */
ouiaId?: string;
}
const ConfigModalItem: React.FunctionComponent<ConfigModalItemProps> = ({
icon = <CogIcon />,
description,
isChecked = false,
labels = { enabled: undefined, disabled: undefined },
onChange,
title,
ouiaId
}) => (
<Flex
alignItems={{ default: 'alignItemsCenter' }}
justifyContent={{ default: 'justifyContentSpaceBetween' }}
spaceItems={{ default: 'spaceItemsMd' }}
>
<FlexItem flex={{ default: 'flex_1' }}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<Icon isInline>{icon}</Icon>
<strong id={`${ouiaId}-title`} className="pf-v6-u-font-weight-bold">
{title}
</strong>
</Flex>
<div id={`${ouiaId}-description`}>{description}</div>
</FlexItem>
<FlexItem alignSelf={{ default: 'alignSelfCenter' }}>
<Switch
aria-labelledby={`${ouiaId}-title`}
aria-describedby={`${ouiaId}-description`}
ouiaId={`${ouiaId}-switch`}
isChecked={isChecked}
isReversed
label={isChecked ? labels.enabled : labels.disabled}
onChange={onChange}
/>
</FlexItem>
</Flex>
);
interface ConfigModalControlProps {
/** Array of configuration controls to be rendered inside the modal. */
controls: ConfigModalItemProps[];
/** Title of the configuration modal. */
title?: string;
/** Description of the configuration modal. */
description?: string;
/** Optional OUIA ID of the configuration modal. Also used as a prefix for the ids of inner elements. */
ouiaId?: string;
}
const ConfigModalControl: React.FunctionComponent<ConfigModalControlProps> = ({
controls,
title = 'Editor settings',
description = 'Settings will be applied immediately',
ouiaId = 'CodeEditorConfigurationModal'
}) => {
const [isModalOpen, setIsModalOpen] = useState(false);
return (
<>
<Modal
aria-describedby={`${ouiaId}-body`}
aria-labelledby={`${ouiaId}-title`}
isOpen={isModalOpen}
onClose={() => setIsModalOpen(!isModalOpen)}
ouiaId={ouiaId}
variant="small"
>
<ModalHeader title={title} description={description} labelId={`${ouiaId}-title`} />
<ModalBody id={`${ouiaId}-body`}>
<Flex direction={{ default: 'column' }} spaceItems={{ default: 'spaceItemsMd' }}>
{controls.map((control) => (
<ConfigModalItem
key={control.title}
ouiaId={`${ouiaId}-${control.title.replace(/\s+/g, '-').toLowerCase()}`}
{...control}
/>
))}
</Flex>
</ModalBody>
</Modal>
<CodeEditorControl
aria-label={title}
aria-haspopup="dialog"
icon={<CogIcon />}
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 onChange = (code: string) => {
setCode(code);
};
const customControl = (
<ConfigModalControl
controls={[
{
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 />
},
{
title: 'Dark theme',
description: 'Switch the editor to a dark color theme',
isChecked: isDarkTheme,
onChange: (_e, checked) => setIsDarkTheme(checked),
icon: <MoonIcon />
},
{
title: 'Line numbers',
description: 'Show line numbers to the left of each line of code',
isChecked: isLineNumbersVisible,
onChange: (_e, checked) => setIsLineNumbersVisible(checked),
icon: <HashtagIcon />
}
]}
/>
);
return (
<CodeEditor
code={code}
customControls={customControl}
height="400px"
isDarkTheme={isDarkTheme}
isLineNumbersVisible={isLineNumbersVisible}
isMinimapVisible={isMinimapVisible}
onChange={onChange}
/>
);
};