Skip to main content
Elastic UI
Elastic UI
Getting startedComponentsPatternsContentData visualization
EUI ChangelogGitHubFigma
  • Overview
  • Layout
  • Containers
  • Navigation
  • Display
    • Badge
    • Aspect ratio
    • Avatar
    • Beacon
    • Callout
    • Code
    • Comment list
    • Description list
    • Drag and drop
    • Empty prompt
    • Health
    • Icon
    • Image
    • List group
    • Loading
    • Progress
    • Skeleton
    • Stat
    • Text
    • Timeline
    • Title
    • Toast
    • Tooltip
    • Tour
  • Forms
  • Tabular content
  • Templates
  • Editors and syntax
  • EUI
  • Display
  • Code

Code

The EuiCode and EuiCodeBlock components support all language syntaxes available through the prismjs library and ES|QL syntax provided by prismjs-esql plugin. The language prop can also be omitted to simply render formatted but non-highlighted code.

JSX code (often React) has distinct language syntaxes from the base JavaScript and TypeScript languages. For these instances, use language="jsx" or language="tsx".

Components

EuiCode and EuiCodeBlock are intended to render static lines or blocks of code in read-only contexts. If you need capabilities to edit, or want to print long code (e.g., printing JSON from an API), we recommend installing a version of Monaco. If you are building within the Kibana platform, you can use their

CodeEditor.

Inline

EuiCode is for making inline code snippets that can work within or next to bodies of text.

Loading...

Code block

EuiCodeBlock can be used to create multi-line code blocks with configurable font and padding sizes.

Loading...

Usage

Copying

Adding the isCopyable prop allows users to copy the text content of the code block.

Loading...

Scrolling

For long content, you can set an overflowHeight which will scroll if the text exceeds that height, and allows users to view the code in fullscreen mode.

Loading...

Wrapping

By default, the whiteSpace property is set to pre-wrap. This makes the text wrap when needed. You can, however, pass pre to the whiteSpace prop and the text won't wrap unless line breaks are in the content.

Loading...

Line numbers

To render line numbers, you can add lineNumbers as boolean flag.

Loading...

You can customize line number display further with a configuration object containing the following optional settings:

  • start: Changes the starting number of the first line. Defaults to 1.
  • highlight: Visually highlights certain lines (e.g. "3), or ranges of lines (e.g. "5-10"). Accepts a comma-separated string of lines or line ranges.
  • annotations: Displays an informational icon next to certain line numbers, that provides more context via a popover. Pass an object of line numbers with corresponding annotations (accepts strings or React nodes).
Loading...

Code block virtualization

For large blocks of code, add isVirtualized to reduce the number of rendered rows and improve load times. Note that when using virtualization:

  • overflowHeight is required
  • whiteSpace is enforced as pre, and cannot be set to pre-wrap
Loading...

Fill vertical space

In places like flyouts, you can use overflowHeight="100%" to stretch the code block to fill the space. Just be sure that it's parent container is also height: 100%.

Loading...

Props

EuiCode

Extends HTMLAttributes
𐘂𐘂
✄𐘗✄𐘗
Prop
↦
Description and type
↦
Default value
↵
Prop
className#
↦
Description and type
Type: string
↦
Default value
↵
Prop
aria-label#
↦
Description and type

Defines a string value that labels the current element.
@see aria-labelledby.

Type: string
↦
Default value
↵
Prop
data-test-subj#
↦
Description and type
Type: string
↦
Default value
↵
Prop
css#
↦
Description and type
Type: Interpolation<Theme>
↦
Default value
↵
Prop
language#
↦
Description and type

Sets the syntax highlighting for a specific language
@see https://prismjs.com/#supported-languages for options

Type: string
↦
Default value
↵
Prop
transparentBackground#
↦
Description and type
Type: boolean
↦
Default value
false
↵
𐘂𐘂

EuiCodeBlock

Extends HTMLAttributes
𐘂𐘂
✄𐘗✄𐘗
Prop
↦
Description and type
↦
Default value
↵
Prop
className#
↦
Description and type
Type: string
↦
Default value
↵
Prop
aria-label#
↦
Description and type

Defines a string value that labels the current element.
@see aria-labelledby.

Type: string
↦
Default value
↵
Prop
data-test-subj#
↦
Description and type
Type: string
↦
Default value
↵
Prop
css#
↦
Description and type
Type: Interpolation<Theme>
↦
Default value
↵
Prop
language#
↦
Description and type

Sets the syntax highlighting for a specific language
@see https://prismjs.com/#supported-languages for options

Type: string
↦
Default value
↵
Prop
transparentBackground#
↦
Description and type
Type: boolean
↦
Default value
false
↵
Prop
paddingSize#
↦
Description and type
Type: "s" | "m" | "l" | "none"
↦
Default value
l
↵
Prop
fontSize#
↦
Description and type
Type: "s" | "m" | "l"
↦
Default value
s
↵
Prop
whiteSpace#
↦
Description and type

Specify how white-space inside the element is handled.
pre respects line breaks/white space but doesn't force them to wrap the line
pre-wrap respects line breaks/white space but does force them to wrap the line when necessary.

Type: "pre" | "pre-wrap"
↦
Default value
pre-wrap
↵
Prop
isCopyable#
↦
Description and type

Displays an icon button to copy the code snippet to the clipboard.

Type: boolean
↦
Default value
false
↵
Prop
copyAriaLabel#
↦
Description and type

Customizes the aria-label for the copy button.

Type: string
↦
Default value
'Copy'
↵
Prop
lineNumbers#
↦
Description and type

Displays line numbers.
Optionally accepts a configuration object for setting the starting number,
visually highlighting ranges, or annotating specific lines:
{ start: 100, highlight: '1, 5-10, 20-30, 40', annotations: { 6: 'A special note about this line' } }

Type: boolean | LineNumbersConfig
↦
Default value
false
↵
Prop
overflowHeight#
↦
Description and type

Sets the maximum container height.
Accepts a pixel value (300) or a percentage ('100%')
Ensure the container has calcuable height when using a percentage

Type: string | number
↦
Default value
↵
Prop
isVirtualized#
↦
Description and type

Renders code block lines virtually.
Useful for improving load times of large code blocks.

When using this configuration, overflowHeight is required and
whiteSpace can only be pre.

Type: boolean
↦
Default value
↵
𐘂𐘂
Edit this page

Previous
Callout
Next
Comment list
  • Components
    • Inline
    • Code block
  • Usage
    • Copying
    • Scrolling
    • Wrapping
    • Line numbers
    • Code block virtualization
    • Fill vertical space
  • Props
EUI is dual-licensed under Elastic License 2.0 and Server Side Public License, v 1 | Crafted with ❤ by Elastic