The page navigation is complete. You may now navigate the page content as you wish.
Skip to main content

Code Editor

Added in v4.16.0

A code editor with syntax highlighting, change history, and keyboard navigation.

The Code Editor is used to edit formatted code. It supports syntax highlighting, auto-closing brackets and quotes, and a custom yielded section for additional functions.

Usage

When to use

  • When a user needs to create code from scratch.
  • When code requires modification from an existing source.

When not to use

  • As a read-only reference for code, use the Code Block instead.

Standalone

The isStandalone property increases the portability of the Code Editor to ensure that it can be used in different contexts. For example, a common use case of the Code Editor is in a “standalone” context, which can be part of a form or multi-step process and is generally a part of the normal layout flow.

Code Editor with rounded corners for enabled standalone property

Sometimes it may be necessary to use the Code Editor in a more dense layout or nested within another component.

Code Editor embedded inside of a logs UI, where the corners are squared to fit within container.

An optional header is available within the Code Editor. It consists of three sections that can be toggled as needed:

  • the text content that includes the title and description
  • an actions container for secondary actions
  • a custom content section for adding primary actions

When to use a title and description

A title and description provides additional contextual information for the Code Editor. An accessible name is mandatory and can either be provided by the title or by an external text element.

Do

Provide an accessible name for the Code Editor so that users of assistive technology can understand its purpose.

A Code Editor embedded in a form following a set of radio buttons. It has the heading “Policy code (Sentinel)” immediately before.

Don’t

Assume all users will understand the purpose of the Code Editor without providing an accessible name. Not providing one may cause confusion and is an accessibility failure. A Code Editor embedded in a form following a set of radio buttons. The Code Editor has no title labelling it.

Secondary actions

The secondary actions section supports two optional buttons: the Copy Button, and a “full screen” button. The Copy Button copies the content of the Code Editor to the clipboard, while the full screen button toggles the size of the Code Editor from inline view to full screen.

The secondary actions container is shown twice, both with the Copy Button and full screen button; the first showing the full screen button with the maximize icon, and the second showing it with the minimize icon.

Custom actions

This space is intended for custom primary actions. Primary actions are those which are necessary for the user to complete their work.

The Code Editor with the title “CodeEditor title”. The custom yielded element section shows a placeholder and is between the title and the editor.

Here is an example of a custom action to reveal secrets.

The Code Editor with the title “CodeEditor title”. The custom yielded section has a “Reveal secrets” toggle.

The Code Editor has limited support for dark mode styles. Buttons have pre-defined dark mode styles, but all other components require manual color adjustments until a dark mode theme is released. Please contact the Design Systems Team for help translating components into dark mode.

External elements

Some elements or functions outside the Code Editor may affect the content within the Code Editor. In this case, we recommend turning off the header to visually couple the editor with the nearby controlling elements.

A Code Editor with the external title “Automations and expressions” coupled with a filter input, “Copy” dropdown, “Version” dropdown, and a “Create new version” button.

Active line highlighting

When a user edits a line of code, a highlight will display to show their location within the Code Editor.

The Code Editor's line 1 with the active line highlight, shown with a blinking cursor and border around the entire line.

When a user selects a piece of code, the active line highlight no longer displays although the line number of the last line of the selection will be highlighted.

The Code Editor's line 1 to 3 selected and showing the green selection highlighted color

Bracket highlighting

The Code Editor automatically closes open quotes and brackets as the user types. When the user’s cursor is next to a bracket, the bracket will highlight to show the start and end of the pair.

The Code Editor's line 1 is highlighted and the user's cursor is next to a bracket. That bracket and its pair are both highlighted with a white border.

Language

Language determines how syntax highlighting is applied and formatted within the editor but is handled a bit differently between the Ember and Figma components.

The Ember component uses CodeMirror to handle syntax highlighting and comes with a pre-defined set of languages.

In Figma we provide a handful of example languages intended as visual examples. Syntax highlighting in Figma is a non-trivial process and requires the manual application of color styles to each “type” of code. You are still able to add a custom code snippet to the Figma component by typing/pasting into the text layer, but syntax highlighting will not be automatically applied.

Applying syntax highlighting

If you wish to create custom examples using the Code Editor, we publish all of the relevant syntax highlighting variables in the HDS Components v2.0 library. However, due to the number of languages supported by the component, the color variables use a generic naming schema (e.g., cyan, red, purple) to remain as agnostic as possible when being applied to different languages. For more details around syntax, visit the specifications.

How to use this component

The code editor is provided as both a CodeEditor component and as an hds-code-editor Ember modifier. To use this component, you must either include the yielded Title component, provide an @ariaLabel, or specify an @ariaLabelledBy.

Loading
<Hds::CodeEditor @ariaLabel="Basic usage" />

Ember modifier

An Ember modifier is available if your use case does not require a visible title, description, or any additional interactivity beyond editing code.

Modifier used on a div

<div {{hds-code-editor ariaLabel="Ember modifier usage"}} />

Title and description

Optionally, you can pass a title and/or a description using the [CE].Title and [CE].Description components yielded by the Code Editor component.

CodeEditor title

CodeEditor description

Loading
<Hds::CodeEditor @value="Hello, world" as |CE|>
  <CE.Title>
    CodeEditor title
  </CE.Title>
  <CE.Description>
    CodeEditor description
  </CE.Description>
</Hds::CodeEditor>

Title tag

The @tag argument changes the HTML element that wraps the [CE].Title content. When organizing the content on a webpage, the heading levels should reflect the structure of the page. For example, if a Code Editor is within a subsection of the page below a heading level 2, the value should be "h3".

Learn to write functions in Go

Functions are a critical part of learning Go. They are reusable chunks of code that can perform tasks like convert an object to an array.

convertObjectToArray.js

Loading
<div class="doc-code-editor-demo-heading">
  <Hds::Text::Display @tag="h2" @size="300">Learn to write functions in Go</Hds::Text::Display>
  <Hds::Text::Body @tag="p">Functions are a critical part of learning Go. They are reusable chunks of code that can perform tasks like convert an object to an array.</Hds::Text::Body>
</div>
<Hds::CodeEditor
  @language="go"
  @value={{this.goCode}}
  as |CE|>
  <CE.Title @tag="h3">
    convertObjectToArray.js
  </CE.Title>
</Hds::CodeEditor>

The default @tag is "h2", however, the correct value is dependent on the individual page. We strongly encourage consumers to update the @tag to meet WCAG Success Criterion 1.3.1 Info and Relationships as the visual experience should match what is presented to the user with assistive technology.

Language

The language argument sets the syntax highlighting used. We support the following languages: ruby, sentinel, shell, go, hcl, json, sql, and yaml. If you need additional languages, contact the Design Systems Team.

Loading
<Hds::CodeEditor
  @ariaLabel="language"
  @language="go"
  @value={{this.goCode}}
/>

Copy button

Set hasCopyButton to true to display a button for users to copy Code Editor content to their computer clipboard.

Loading
<Hds::CodeEditor @ariaLabel="copy button" @hasCopyButton={{true}} @value={{this.loremIpsum}} />

Full screen mode

Set hasFullScreenButton to true to display a button for users to toggle between a full screen view and normal placement within the page.

Loading
<Hds::CodeEditor @ariaLabel="full screen mode" @hasFullScreenButton={{true}} @value={{this.loremIpsum}} />

Component API

This component uses CodeMirror 6 under the hood.

CodeEditor

<[CE].Title> yielded component
ContentBlock::Title yielded as contextual component (see below).
<[CE].Description> yielded component
ContentBlock::Description yielded as contextual component (see below).
<[CE].Generic> yielded component
ContentBlock::Generic yielded as contextual component (see below).
ariaLabel string
Accepts a localized string. The ariaLabel value is applied to the code editor input element.
ariaLabelledBy string
Accepts a localized string. The ariaLabelledBy value is applied to the code editor input element.
hasCopyButton boolean
  • false (default)
Used to control whether a copy button for copying the code/text content will be displayed.
hasFullScreenButton boolean
  • false (default)
Used to control whether a toggle button for toggling full-screen mode will be displayed.
isStandalone boolean
  • true (default)
Applies rounded borders to the component. When used within another component or when the context requires it, you can turn it off.
language string
  • go
  • hcl
  • json
  • ruby
  • sentinel
  • shell
  • sql
  • yaml
The coding language to use for syntax highlighting. If you need additional languages contact the Design Systems Team.
value string
The text/code content for the Code Editor.
onBlur function
Callback function invoked when focus is removed from the editor.
onInput function
Callback function invoked when the editor receives an input event.
onSetup function
Callback function invoked when the editor completes setup.

Contextual components

[CE].Title

The CodeEditor::Title component, yielded as contextual component.

yield
Accepts complex content, such as logic/conditionals, HTML elements, other Ember components, etc. Styling is applied for simple HTML elements, such as strong, em, a, code/pre. Consumers will need to style other HTML tags if used as children.
tag enum
  • p
  • h1
  • h2 (default)
  • h3
  • h4
  • h5
  • h6
The HTML tag that wraps the content of the "title" block.
…attributes
This component supports use of ...attributes.

[CE].Description

The CodeEditor::Description component, yielded as contextual component.

yield
Supports embedding content such as logic/conditionals, inline HTML elements, and other Ember components. However, since the content is always rendered within a p tag, it must adhere to semantic HTML rules, avoiding block-level elements. Consumers should ensure proper styling to maintain consistent rendering when using custom inline child elements.
…attributes
This component supports use of ...attributes.

[CE].Generic

A generic container, yielded as contextual component.

yield
Elements passed as children are yielded as inner content of the "header content" block, after the "title" and "description".
The content can be a simple string or a more complex/structured one.
Hds::Button components inherit styles from the editor theme.

Anatomy

Element Usage
Title Optional
Description Optional
Custom yielded elements Optional
Copy button Optional
FullScreen button Optional
Code snippet Required

Syntax highlighting

To aid in understanding how the highlighting theme is applied via CodeMirror's tokens, we've provided a high-level, non-exhaustive list of token names and how they might be applied depending on the syntax.

Color Usage
Cyan
Property, url, or operator
Blue
Function, builtins
Orange
Strings, characters
Purple
Booleans, numbers
Green
Keywords, class names, saving the world
Red
Important items
White
Default color within the code block, also used for punctuation (<, { }, =, etc)
Gray
Used for comments across languages

States

Focus with header content

The first tabbed element in the Code Editor is the CopyButton, with it focused.

Focus without header content

The header is turned off and the focused element is the CodeSnippet, and its highlighted with the user cursor is active.

Conformance rating

Conformant

When used as recommended, there should not be any WCAG conformance issues with this component.

Applicable WCAG Success Criteria

This section is for reference only. This component intends to conform to the following WCAG Success Criteria:

  • 1.3.1 Info and Relationships (Level A):
    Information, structure, and relationships conveyed through presentation can be programmatically determined or are available in text.
  • 1.3.2 Meaningful Sequence (Level A):
    When the sequence in which content is presented affects its meaning, a correct reading sequence can be programmatically determined.
  • 1.4.1 Use of Color (Level A):
    Color is not used as the only visual means of conveying information, indicating an action, prompting a response, or distinguishing a visual element.
  • 1.4.10 Reflow (Level AA):
    Content can be presented without loss of information or functionality, and without requiring scrolling in two dimensions.
  • 1.4.11 Non-text Contrast (Level AA):
    The visual presentation of the following have a contrast ratio of at least 3:1 against adjacent color(s): user interface components; graphical objects.
  • 1.4.12 Text Spacing (Level AA):
    No loss of content or functionality occurs by setting all of the following and by changing no other style property: line height set to 1.5; spacing following paragraphs set to at least 2x the font size; letter-spacing set at least 0.12x of the font size, word spacing set to at least 0.16 times the font size.
  • 1.4.3 Minimum Contrast (Level AA):
    The visual presentation of text and images of text has a contrast ratio of at least 4.5:1
  • 1.4.4 Resize Text (Level AA):
    Except for captions and images of text, text can be resized without assistive technology up to 200 percent without loss of content or functionality.
  • 2.1.1 Keyboard (Level A):
    All functionality of the content is operable through a keyboard interface.
  • 2.1.2 No Keyboard Trap (Level A):
    If keyboard focus can be moved to a component of the page using a keyboard interface, then focus can be moved away from that component using only a keyboard interface.
  • 2.4.3 Focus Order (Level A):
    If a Web page can be navigated sequentially and the navigation sequences affect meaning or operation, focusable components receive focus in an order that preserves meaning and operability.
  • 2.4.7 Focus Visible (Level AA):
    Any keyboard operable user interface has a mode of operation where the keyboard focus indicator is visible.
  • 4.1.2 Name, Role, Value (Level A):
    For all user interface components, the name and role can be programmatically determined; states, properties, and values that can be set by the user can be programmatically set; and notification of changes to these items is available to user agents, including assistive technologies.

Support

If any accessibility issues have been found within this component, let us know by submitting an issue.


Related