diff --git a/packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md b/packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md index a6c980ab71a..0c3d3681b5d 100644 --- a/packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md +++ b/packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md @@ -9,6 +9,10 @@ Note: Code editor lives in its own package at [@patternfly/react-code-editor](ht import { Fragment, useState } from 'react'; import { CodeEditor, CodeEditorControl, Language } from '@patternfly/react-code-editor'; +import CogIcon from '@patternfly/react-icons/dist/esm/icons/cog-icon'; +import HashtagIcon from '@patternfly/react-icons/dist/esm/icons/hashtag-icon'; +import MapIcon from '@patternfly/react-icons/dist/esm/icons/map-icon'; +import MoonIcon from '@patternfly/react-icons/dist/esm/icons/moon-icon'; import PlayIcon from '@patternfly/react-icons/dist/esm/icons/play-icon'; ## Examples @@ -50,3 +54,9 @@ These examples below are the shortcuts that we recommend describing in the popov ```ts file="CodeEditorCustomControl.tsx" ``` + +### With configuration modal + +```ts file="CodeEditorConfigurationModal.tsx" + +``` diff --git a/packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorConfigurationModal.tsx b/packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorConfigurationModal.tsx new file mode 100644 index 00000000000..1f5cc3d7bca --- /dev/null +++ b/packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorConfigurationModal.tsx @@ -0,0 +1,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 = ({ + icon = , + description, + isChecked = false, + labels = { enabled: undefined, disabled: undefined }, + onChange, + title, + ouiaId +}) => ( + + + + {icon} + + {title} + + + +
{description}
+
+ + + +
+); + +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 = ({ + controls, + title = 'Editor settings', + description = 'Settings will be applied immediately', + ouiaId = 'CodeEditorConfigurationModal' +}) => { + const [isModalOpen, setIsModalOpen] = useState(false); + + return ( + <> + setIsModalOpen(!isModalOpen)} + ouiaId={ouiaId} + variant="small" + > + + + + {controls.map((control) => ( + + ))} + + + + } + 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 = ( + setIsMinimapVisible(checked), + icon: + }, + { + title: 'Dark theme', + description: 'Switch the editor to a dark color theme', + isChecked: isDarkTheme, + onChange: (_e, checked) => setIsDarkTheme(checked), + icon: + }, + { + title: 'Line numbers', + description: 'Show line numbers to the left of each line of code', + isChecked: isLineNumbersVisible, + onChange: (_e, checked) => setIsLineNumbersVisible(checked), + icon: + } + ]} + /> + ); + + return ( + + ); +};