Skip to content
Patternfly Logo

Code editor

A code editor is a versatile text editor that allows for editing various languages. The code editor is built on top of the Monaco Editor.

Info alert:Beta feature

This Beta component is currently under review and is still open for further evolution. It is available for use in product. Beta components are considered for promotion on a quarterly basis. Please join in and give us your feedback or submit any questions on the PatternFly forum or via Slack. To learn more go to our Beta components page on GitHub.

Note: Code editor lives in its own package at @patternfly/react-code-editor and has required peer deps.

Examples

Basic

JAVASCRIPT

With sizeToFit height, height will grow/shrink with content

With shortcut menu and main header content

These examples below are the shortcuts that we recommend describing in the popover since they are monaco features.

Shortcut Example
JAVASCRIPT

With actions

PLAINTEXT

Start editing

Drag and drop a file or upload one.

With custom control

Props

CodeEditor

*required
NameTypeDefaultDescription
classNamestring''additional classes added to the code editor
codestring''code displayed in code editor
copyButtonAriaLabelstring'Copy code to clipboard'Accessible label for the copy button
copyButtonSuccessTooltipTextstring'Content added to clipboard'Text to display in the tooltip on the copy button after code copied to clipboard
copyButtonToolTipTextstring'Copy to clipboard'Text to display in the tooltip on the copy button before text is copied
customControlsReact.ReactNode | React.ReactNode[]nullA single node or array of nodes - ideally CodeEditorControls - to display above code editor
downloadButtonAriaLabelstring'Download code'Accessible label for the download button
downloadButtonToolTipTextstring'Download'Text to display in the tooltip on the download button
downloadFileNamestringDate.now().toString()Name of the file if user downloads code to local file
emptyStateReact.ReactNode''Content to display in space of the code editor when there is no code to display
emptyStateBodyReact.ReactNode'Drag and drop a file or upload one.'Override default empty state body text
emptyStateButtonReact.ReactNode'Browse'Override default empty state title text
emptyStateLinkReact.ReactNode'Start from scratch'Override default empty state body text
emptyStateTitleReact.ReactNode'Start editing'Override default empty state title text
headerMainContentstring''Editor header main content title
heightstring | 'sizeToFit'''Height of code editor. Defaults to 100%. 'sizeToFit' will automatically change the height to the height of the content.
isCopyEnabledbooleanfalseFlag to add copy button to code editor actions
isDarkThemebooleanfalseFlag indicating the editor is styled using monaco's dark theme
isDownloadEnabledbooleanfalseFlag to add download button to code editor actions
isLanguageLabelVisiblebooleanfalseFlag to include a label indicating the currently configured editor language
isLineNumbersVisiblebooleantrueFlag indicating the editor is displaying line numbers
isMinimapVisiblebooleanfalseFlag to add the minimap to the code editor
isReadOnlybooleanfalseFlag indicating the editor is read only
isUploadEnabledbooleanfalseFlag to add upload button to code editor actions. Also makes the code editor accept a file using drag and drop
languageLanguageLanguage.plaintextlanguage displayed in the editor
loadingReact.ReactNode''The loading screen before the editor will be loaded. Defaults 'loading...'
onChangeChangeHandlerFunction which fires each time the content of the code editor is manually changed. Does not fire when a file is uploaded.
onCodeChange(value: string) => void() => {}Function which fires each time the code changes in the code editor
onEditorDidMountEditorDidMount() => {}Callback which fires after the code editor is mounted containing a reference to the monaco editor and the monaco instance
optionseditor.IStandaloneEditorConstructionOptions{}Refer to Monaco interface {monaco.editor.IStandaloneEditorConstructionOptions}.
overrideServiceseditor.IEditorOverrideServices{}Refer to Monaco interface {monaco.editor.IEditorOverrideServices}.
shortcutsPopoverButtonTextstring'View Shortcuts'Text to show in the button to open the shortcut popover
shortcutsPopoverPropsPopoverProps{ bodyContent: '', 'aria-label': 'Keyboard Shortcuts', ...Popover.defaultProps }Properties for the shortcut popover
showEditorbooleantrueFlag to show the editor
toolTipCopyExitDelaynumber1600The delay before tooltip fades after code copied
toolTipDelaynumber300The entry and exit delay for all tooltips
toolTipMaxWidthstring'100px'The max width of the tooltips on all button
toolTipPositionTooltipPosition | 'auto' | 'top' | 'bottom' | 'left' | 'right' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | 'left-start' | 'left-end' | 'right-start' | 'right-end''top'The position of tooltips on all buttons
uploadButtonAriaLabelstring'Upload code'Accessible label for the upload button
uploadButtonToolTipTextstring'Upload'Text to display in the tooltip on the upload button
widthstring''Width of code editor. Defaults to 100%

CodeEditorControl

*required
NameTypeDefaultDescription
iconrequiredReact.ReactNodeicon rendered inside the code editor control
toolTipTextrequiredReact.ReactNodeText to display in the tooltip
aria-labelstringaccessible label for the code editor control
classNamestringadditional classes added to the Code editor control
entryDelaynumber300Delay in ms before the tooltip appears.
exitDelaynumber0Delay in ms before the tooltip disappears.
isVisiblebooleantrueFlag indicating that the button is visible above the code editor
maxWidthstring'100px'Maximum width of the tooltip (default 150px).
onClick(code: string, event?: any) => void() => {}Event handler for the click of the button
positionPopoverPosition | 'auto' | 'top' | 'bottom' | 'left' | 'right' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | 'left-start' | 'left-end' | 'right-start' | 'right-end''top'Copy button popover position.

Popover

*required
NameTypeDefaultDescription
bodyContentrequiredReact.ReactNode | ((hide: () => void) => React.ReactNode)Body content If you want to close the popover after an action within the bodyContent, you can use the isVisible prop for manual control, or you can provide a function which will receive a callback as an argument to hide the popover i.e. bodyContent={hide => <Button onClick={() => hide()}>Close</Button>}
alertSeverityScreenReaderTextBetastringText announced by screen reader when alert severity variant is set to indicate severity level
alertSeverityVariantBeta'default' | 'info' | 'warning' | 'success' | 'danger'Severity variants for an alert popover. This modifies the color of the header to match the severity.
animationDurationnumber300CSS fade transition animation duration
appendToHTMLElement | ((ref?: HTMLElement) => HTMLElement)() => document.bodyThe element to append the popover to, defaults to body
aria-labelstring''Accessible label, required when header is not present
Deprecated: boundary'scrollParent' | 'window' | 'viewport' | HTMLElement- no longer used. if you want to constrain the popper to a specific element use the appendTo prop instead
childrenReactElement<any>The reference element to which the Popover is relatively placed to. If you cannot wrap the reference with the Popover, you can use the reference prop instead. Usage: <Popover><Button>Reference</Button></Popover>
classNamestring''Popover additional class
closeBtnAriaLabelstring'Close'Aria label for the Close button
distancenumber25Distance of the popover to its target, defaults to 25
enableFlipbooleantrueIf true, tries to keep the popover in view by flipping it if necessary. If the position is set to 'auto', this prop is ignored
flipBehavior'flip' | ( | 'top' | 'bottom' | 'left' | 'right' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | 'left-start' | 'left-end' | 'right-start' | 'right-end' )[]['top', 'right', 'bottom', 'left', 'top', 'right', 'bottom']The desired position to flip the popover to if the initial position is not possible. By setting this prop to 'flip' it attempts to flip the popover to the opposite side if there is no space. You can also pass an array of positions that determines the flip order. It should contain the initial position followed by alternative positions if that position is unavailable. Example: Initial position is 'top'. Button with popover is in the top right corner. 'flipBehavior' is set to ['top', 'right', 'left']. Since there is no space to the top, it checks if right is available. There's also no space to the right, so it finally shows the popover on the left.
footerContentReact.ReactNode | ((hide: () => void) => React.ReactNode)nullFooter content If you want to close the popover after an action within the bodyContent, you can use the isVisible prop for manual control, or you can provide a function which will receive a callback as an argument to hide the popover i.e. footerContent={hide => <Button onClick={() => hide()}>Close</Button>}
hasAutoWidthbooleanfalseRemoves fixed-width and allows width to be defined by contents
hasNoPaddingbooleanfalseAllows content to touch edges of popover container
headerComponent'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6''h6'Sets the heading level to use for the popover header. Default is h6.
headerContentReact.ReactNode | ((hide: () => void) => React.ReactNode)nullSimple header content to be placed within a title. If you want to close the popover after an action within the bodyContent, you can use the isVisible prop for manual control, or you can provide a function which will receive a callback as an argument to hide the popover i.e. headerContent={hide => <Button onClick={() => hide()}>Close</Button>}
headerIconBetaReact.ReactNodenullIcon to be displayed in the popover header *
hideOnOutsideClickbooleantrueHides the popover when a click occurs outside (only works if isVisible is not controlled by the user)
idstringid used as part of the various popover elements (popover-${id}-header/body/footer)
isVisiblebooleannullTrue to show the popover programmatically. Used in conjunction with the shouldClose prop. By default, the popover child element handles click events automatically. If you want to control this programmatically, the popover will not auto-close if the Close button is clicked, ESC key is used, or if a click occurs outside the popover. Instead, the consumer is responsible for closing the popover themselves by adding a callback listener for the shouldClose prop.
maxWidthstringpopoverMaxWidth && popoverMaxWidth.valueMaximum width of the popover (default 18.75rem)
minWidthstringpopoverMinWidth && popoverMinWidth.valueMinimum width of the popover (default 6.25rem)
onHidden(tip?: TippyInstance) => void(): void => nullLifecycle function invoked when the popover has fully transitioned out. Note: The tip argument is no longer passed and has been deprecated.
onHide(tip?: TippyInstance) => void(): void => nullLifecycle function invoked when the popover begins to transition out. Note: The tip argument is no longer passed and has been deprecated.
onMount(tip?: TippyInstance) => void(): void => nullLifecycle function invoked when the popover has been mounted to the DOM. Note: The tip argument is no longer passed and has been deprecated.
onShow(tip?: TippyInstance) => void(): void => nullLifecycle function invoked when the popover begins to transition in. Note: The tip argument is no longer passed and has been deprecated.
onShown(tip?: TippyInstance) => void(): void => nullLifecycle function invoked when the popover has fully transitioned in. Note: The tip argument is no longer passed and has been deprecated.
positionPopoverPosition | 'auto' | 'top' | 'bottom' | 'left' | 'right' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | 'left-start' | 'left-end' | 'right-start' | 'right-end''top'Popover position. Note: With 'enableFlip' set to true, it will change the position if there is not enough space for the starting position. The behavior of where it flips to can be controlled through the flipBehavior prop.
referenceHTMLElement | (() => HTMLElement) | React.RefObject<any>The reference element to which the Popover is relatively placed to. If you can wrap the reference with the Popover, you can use the children prop instead. Usage: <Popover reference={() => document.getElementById('reference-element')} />
shouldClose(tip?: TippyInstance, hideFunction?: () => void, event?: MouseEvent | KeyboardEvent) => void(): void => nullCallback function that is only invoked when isVisible is also controlled. Called when the popover Close button is clicked, Enter key was used on it, or the ESC key is used. Note: The tip argument is no longer passed and has been deprecated.
shouldOpen(showFunction?: () => void, event?: MouseEvent | KeyboardEvent) => void(): void => nullCallback function that is only invoked when isVisible is also controlled. Called when the Enter key is used on the focused trigger
showClosebooleantrueWhether to show the close button
Deprecated: tippyPropsPartial<TippyProps>- no longer used
withFocusTrapbooleanWhether to trap focus in the popover
zIndexnumber9999z-index of the popover

CSS variables

.pf-c-code-editor--pf-c-code-editor__controls--c-button--m-control--Color
#6a6e73
.pf-c-code-editor--pf-c-code-editor__controls--c-button--m-control--hover--Color
#151515
.pf-c-code-editor--pf-c-code-editor__controls--c-button--m-control--focus--Color
#151515
.pf-c-code-editor--pf-c-code-editor__controls--c-button--m-control--disabled--after--BorderBottomColor
#d2d2d2
.pf-c-code-editor--pf-c-code-editor__header--before--BorderBottomWidth
1px
.pf-c-code-editor--pf-c-code-editor__header--before--BorderBottomColor
#d2d2d2
.pf-c-code-editor--pf-c-code-editor__main--BorderColor
#d2d2d2
.pf-c-code-editor--pf-c-code-editor__main--BorderWidth
1px
.pf-c-code-editor--pf-c-code-editor__main--BackgroundColor
#fff
.pf-c-code-editor--pf-c-code-editor--m-read-only__main--BackgroundColor
#f0f0f0
.pf-c-code-editor--pf-c-code-editor__main--m-drag-hover--before--BorderWidth
1px
.pf-c-code-editor--pf-c-code-editor__main--m-drag-hover--before--BorderColor
#06c
.pf-c-code-editor--pf-c-code-editor__main--m-drag-hover--after--BackgroundColor
#06c
.pf-c-code-editor--pf-c-code-editor__main--m-drag-hover--after--Opacity
.1
.pf-c-code-editor--pf-c-code-editor__code--PaddingTop
0.5rem
.pf-c-code-editor--pf-c-code-editor__code--PaddingRight
0.5rem
.pf-c-code-editor--pf-c-code-editor__code--PaddingBottom
0.5rem
.pf-c-code-editor--pf-c-code-editor__code--PaddingLeft
0.5rem
.pf-c-code-editor--pf-c-code-editor__code-pre--FontSize
0.875rem
.pf-c-code-editor--pf-c-code-editor__code-pre--FontFamily
'"Liberation Mono", consolas, "SFMono-Regular", menlo, monaco, "Courier New", monospace'
.pf-c-code-editor--pf-c-code-editor__header-main--PaddingRight
0.5rem
.pf-c-code-editor--pf-c-code-editor__header-main--PaddingLeft
0.5rem
.pf-c-code-editor--pf-c-code-editor__tab--BackgroundColor
#fff
.pf-c-code-editor--pf-c-code-editor__tab--Color
#6a6e73
.pf-c-code-editor--pf-c-code-editor__tab--PaddingTop
0.375rem
.pf-c-code-editor--pf-c-code-editor__tab--PaddingRight
0.5rem
.pf-c-code-editor--pf-c-code-editor__tab--PaddingBottom
0.375rem
.pf-c-code-editor--pf-c-code-editor__tab--PaddingLeft
0.5rem
.pf-c-code-editor--pf-c-code-editor__tab--BorderTopWidth
1px
.pf-c-code-editor--pf-c-code-editor__tab--BorderRightWidth
1px
.pf-c-code-editor--pf-c-code-editor__tab--BorderBottomWidth
0
.pf-c-code-editor--pf-c-code-editor__tab--BorderLeftWidth
1px
.pf-c-code-editor--pf-c-code-editor__tab--BorderColor
#d2d2d2
.pf-c-code-editor--pf-c-code-editor__tab-icon--text--MarginLeft
0.5rem
.pf-c-code-editor.pf-m-read-only--pf-c-code-editor__main--BackgroundColor
#f0f0f0
.pf-c-code-editor__controls .pf-c-button.pf-m-control--pf-c-button--m-control--Color
#6a6e73
.pf-c-code-editor__controls .pf-c-button.pf-m-control:hover--pf-c-code-editor__controls--c-button--m-control--Color
#151515
.pf-c-code-editor__controls .pf-c-button.pf-m-control:focus--pf-c-code-editor__controls--c-button--m-control--Color
#151515

View source on GitHub