Skip to content

Commit 4294055

Browse files
zamoorealex-juKristinLBradleymajedelassjorytindall
authored
Hds::CodeEditor Website updates (#2626)
Co-authored-by: Alex <alex-ju@users.noreply.github.com> Co-authored-by: Kristin Bradley <kristin.bradley@hashicorp.com> Co-authored-by: Majed Elass <majed.elass@hashicorp.com> Co-authored-by: Majed <156002572+majedelass@users.noreply.github.com> Co-authored-by: Jory Tindall <jory.tindall@hashicorp.com> Co-authored-by: Lee White <lee.white@hashicorp.com> Co-authored-by: Dylan Hyun <dylan.hyun@hashicorp.com> Co-authored-by: Melanie Sumner <melanie@hashicorp.com>
1 parent 2dbe8b7 commit 4294055

28 files changed

+419
-0
lines changed

website/app/styles/app.scss

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
@use "pages/components/application-state" as components-application-state;
3636
@use "pages/components/button" as components-button;
3737
@use "pages/components/code-block" as components-code-block;
38+
@use "pages/components/code-editor" as components-code-editor;
3839
@use "pages/components/copy-snippet" as components-copy-snippet;
3940
@use "pages/components/copy-button" as components-copy-button;
4041
@use "pages/components/dropdown" as components-dropdown;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
7+
// COMPONENTS > CODE EDITOR
8+
9+
#show-content-components-code-editor {
10+
.doc-code-editor-syntax-color-preview {
11+
display: inline-block;
12+
width: 18px;
13+
margin-right: 4px;
14+
vertical-align: text-bottom;
15+
border-radius: 3px;
16+
aspect-ratio: 1/1;
17+
}
18+
19+
.doc-code-editor-demo-heading {
20+
margin-bottom: 1rem;
21+
22+
h2 {
23+
margin-bottom: 0.5rem;
24+
}
25+
}
26+
}

website/cspell-config/project-words.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
affordance
2+
Automations
23
Autoscaler
34
borderless
45
codemod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import Component from '@glimmer/component';
7+
8+
export default class Index extends Component {
9+
loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
10+
11+
goCode = `func convertObjectToArray(obj map[string]interface{}) []interface{} {
12+
var arr []interface{}
13+
for key, value := range obj {
14+
arr = append(arr, key, value)
15+
}
16+
sort.Slice(arr, func(i, j int) bool {
17+
return fmt.Sprintf("%v", arr[i]) < fmt.Sprintf("%v", arr[j])
18+
})
19+
return arr
20+
}`;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Code Editor
3+
description: A code editor with syntax highlighting, change history, and keyboard navigation.
4+
caption: A code editor with syntax highlighting, change history, and keyboard navigation.
5+
links:
6+
figma: https://www.figma.com/design/iweq3r2Pi8xiJfD9e6lOhF/HDS-Components-v2.0?m=auto&node-id=69795-4433&t=WuWetCw0HQ2E5TJa-1
7+
github: https://github.com/hashicorp/design-system/tree/main/packages/components/src/components/hds/code-editor
8+
related: ['components/code-block', 'components/copy/button', 'components/copy/snippet']
9+
previewImage: assets/illustrations/components/code-editor.jpg
10+
navigation:
11+
keywords: ['code', 'snippet', 'copy', 'text', 'editor', 'language', 'example', 'syntax', 'highlight', 'block']
12+
status:
13+
added: 4.16.0
14+
---
15+
16+
<section data-tab="Guidelines">
17+
@include "partials/guidelines/overview.md"
18+
@include "partials/guidelines/guidelines.md"
19+
</section>
20+
21+
<section data-tab="Code">
22+
@include "partials/code/how-to-use.md"
23+
@include "partials/code/component-api.md"
24+
</section>
25+
26+
<section data-tab="Specifications">
27+
@include "partials/specifications/anatomy.md"
28+
@include "partials/specifications/states.md"
29+
</section>
30+
31+
<section data-tab="Accessibility">
32+
@include "partials/accessibility/accessibility.md"
33+
</section>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Conformance rating
2+
3+
<Doc::Badge @type="success">Conformant</Doc::Badge>
4+
5+
When used as recommended, there should not be any WCAG conformance issues with this component.
6+
7+
## Applicable WCAG Success Criteria
8+
9+
This section is for reference only. This component intends to conform to the following WCAG Success Criteria:
10+
11+
<Doc::WcagList @criteriaList={{array "1.3.1" "1.3.2" "1.4.1" "1.4.3" "1.4.4" "1.4.10" "1.4.11" "1.4.12" "2.1.1" "2.1.2" "2.4.3" "2.4.7" "4.1.2" }} />
12+
13+
---
14+
15+
<Doc::A11ySupport />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## Component API
2+
3+
This component uses [CodeMirror 6](https://codemirror.net/) under the hood.
4+
5+
### CodeEditor
6+
7+
<Doc::ComponentApi as |C|>
8+
<C.Property @name="<[CE].Title>" @type="yielded component">
9+
`ContentBlock::Title` yielded as contextual component (see below).
10+
</C.Property>
11+
<C.Property @name="<[CE].Description>" @type="yielded component">
12+
`ContentBlock::Description` yielded as contextual component (see below).
13+
</C.Property>
14+
<C.Property @name="<[CE].Generic>" @type="yielded component">
15+
`ContentBlock::Generic` yielded as contextual component (see below).
16+
</C.Property>
17+
<C.Property @name="ariaLabel" @type="string">
18+
Accepts a localized string. The `ariaLabel` value is applied to the code editor input element.
19+
</C.Property>
20+
<C.Property @name="ariaLabelledBy" @type="string">
21+
Accepts a localized string. The `ariaLabelledBy` value is applied to the code editor input element.
22+
</C.Property>
23+
<C.Property @name="hasCopyButton" @type="boolean" @default="false">
24+
Used to control whether a copy button for copying the code/text content will be displayed.
25+
</C.Property>
26+
<C.Property @name="hasFullScreenButton" @type="boolean" @default="false">
27+
Used to control whether a toggle button for toggling full-screen mode will be displayed.
28+
</C.Property>
29+
<C.Property @name="isStandalone" @type="boolean" @default="true">
30+
Applies rounded borders to the component. When used within another component or when the context requires it, you can turn it off.
31+
</C.Property>
32+
<C.Property @name="language" @type="string" @values={{array "go" "hcl" "json" "ruby" "sentinel" "shell" "sql" "yaml"}}>
33+
The coding language to use for syntax highlighting. If you need additional languages <LinkTo class="doc-link-generic" @route="show" @model="about/support">contact the Design Systems Team</LinkTo>.
34+
</C.Property>
35+
<C.Property @name="value" @type="string">
36+
The text/code content for the Code Editor.
37+
</C.Property>
38+
<C.Property @name="onBlur" @type="function">
39+
Callback function invoked when focus is removed from the editor.
40+
</C.Property>
41+
<C.Property @name="onInput" @type="function">
42+
Callback function invoked when the editor receives an input event.
43+
</C.Property>
44+
<C.Property @name="onSetup" @type="function">
45+
Callback function invoked when the editor completes setup.
46+
</C.Property>
47+
</Doc::ComponentApi>
48+
49+
### Contextual components
50+
51+
#### [CE].Title
52+
53+
The `CodeEditor::Title` component, yielded as contextual component.
54+
55+
<Doc::ComponentApi as |C|>
56+
<C.Property @name="yield">
57+
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.
58+
</C.Property>
59+
<C.Property @name="tag" @type="enum" @values={{array "p" "h1" "h2" "h3" "h4" "h5" "h6"}} @default="h2">
60+
The HTML tag that wraps the content of the "title" block.
61+
</C.Property>
62+
<C.Property @name="...attributes">
63+
This component supports use of [`...attributes`](https://guides.emberjs.com/release/in-depth-topics/patterns-for-components/#toc_attribute-ordering).
64+
</C.Property>
65+
</Doc::ComponentApi>
66+
67+
#### [CE].Description
68+
69+
The `CodeEditor::Description` component, yielded as contextual component.
70+
71+
<Doc::ComponentApi as |C|>
72+
<C.Property @name="yield">
73+
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.
74+
</C.Property>
75+
<C.Property @name="...attributes">
76+
This component supports use of [`...attributes`](https://guides.emberjs.com/release/in-depth-topics/patterns-for-components/#toc_attribute-ordering).
77+
</C.Property>
78+
</Doc::ComponentApi>
79+
80+
#### [CE].Generic
81+
82+
A generic container, yielded as contextual component.
83+
84+
<Doc::ComponentApi as |C|>
85+
<C.Property @name="yield">
86+
Elements passed as children are yielded as inner content of the "header content" block, after the "title" and "description".
87+
<br/>The content can be a simple string or a more complex/structured one.
88+
<br/>`Hds::Button` components inherit styles from the editor theme.
89+
</C.Property>
90+
</Doc::ComponentApi>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
## How to use this component
2+
3+
The code editor is provided as both a `CodeEditor` component and as an `hds-code-editor` Ember [modifier](/components/code-editor?tab=code#ember-modifier). To use this component, you must either include the yielded `Title` component, provide an `@ariaLabel`, or specify an `@ariaLabelledBy`.
4+
5+
6+
```handlebars
7+
<Hds::CodeEditor @ariaLabel="Basic usage" />
8+
```
9+
10+
### Ember modifier
11+
12+
An Ember modifier is available if your use case does not require a visible title, description, or any additional interactivity beyond editing code.
13+
14+
#### Modifier used on a `div`
15+
16+
```handlebars
17+
<div {{hds-code-editor ariaLabel="Ember modifier usage"}} />
18+
```
19+
20+
### Title and description
21+
22+
Optionally, you can pass a title and/or a description using the `[CE].Title` and `[CE].Description` components yielded by the Code Editor component.
23+
24+
```handlebars
25+
<Hds::CodeEditor @value="Hello, world" as |CE|>
26+
<CE.Title>
27+
CodeEditor title
28+
</CE.Title>
29+
<CE.Description>
30+
CodeEditor description
31+
</CE.Description>
32+
</Hds::CodeEditor>
33+
```
34+
35+
### Title tag
36+
37+
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"`.
38+
39+
```handlebars
40+
<div class="doc-code-editor-demo-heading">
41+
<Hds::Text::Display @tag="h2" @size="300">Learn to write functions in Go</Hds::Text::Display>
42+
<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>
43+
</div>
44+
<Hds::CodeEditor
45+
@language="go"
46+
@value={{this.goCode}}
47+
as |CE|>
48+
<CE.Title @tag="h3">
49+
convertObjectToArray.js
50+
</CE.Title>
51+
</Hds::CodeEditor>
52+
```
53+
54+
!!! Insight
55+
56+
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](https://www.w3.org/WAI/WCAG22/Understanding/info-and-relationships.html) as the visual experience should match what is presented to the user with assistive technology.
57+
58+
!!!
59+
60+
### Language
61+
62+
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, <LinkTo class="doc-link-generic" @route="show" @model="about/support">contact the Design Systems Team</LinkTo>.
63+
64+
```handlebars
65+
<Hds::CodeEditor
66+
@ariaLabel="language"
67+
@language="go"
68+
@value={{this.goCode}}
69+
/>
70+
```
71+
72+
### Copy button
73+
74+
Set `hasCopyButton` to `true` to display a button for users to copy Code Editor content to their computer clipboard.
75+
76+
```handlebars
77+
<Hds::CodeEditor @ariaLabel="copy button" @hasCopyButton={{true}} @value={{this.loremIpsum}} />
78+
```
79+
80+
81+
### Full screen mode
82+
83+
Set `hasFullScreenButton` to `true` to display a button for users to toggle between a full screen view and normal placement within the page.
84+
85+
```handlebars
86+
<Hds::CodeEditor @ariaLabel="full screen mode" @hasFullScreenButton={{true}} @value={{this.loremIpsum}} />
87+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
## Usage
2+
3+
### When to use
4+
5+
- When a user needs to create code from scratch.
6+
- When code requires modification from an existing source.
7+
8+
### When not to use
9+
10+
- As a read-only reference for code, use the [Code Block](/components/code-block) instead.
11+
12+
## Standalone
13+
14+
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.
15+
16+
![Code Editor with rounded corners for enabled standalone property](/assets/components/code-editor/code-editor-rounded-standalone.png)
17+
18+
Sometimes it may be necessary to use the Code Editor in a more dense layout or nested within another component.
19+
20+
![Code Editor embedded inside of a logs UI, where the corners are squared to fit within container.](/assets/components/code-editor/code-editor-block-level.png)
21+
22+
## Header
23+
24+
An optional header is available within the Code Editor. It consists of three sections that can be toggled as needed:
25+
26+
- the text content that includes the title and description
27+
- an actions container for secondary actions
28+
- a custom content section for adding primary actions
29+
30+
### When to use a title and description
31+
32+
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.
33+
34+
!!! Do
35+
36+
Provide an accessible name for the Code Editor so that users of assistive technology can understand its purpose.
37+
38+
![A Code Editor embedded in a form following a set of radio buttons. It has the heading “Policy code (Sentinel)” immediately before.](/assets/components/code-editor/code-editor-external-do-accessible-name.png)
39+
40+
!!!
41+
42+
!!! Dont
43+
44+
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.
45+
![A Code Editor embedded in a form following a set of radio buttons. The Code Editor has no title labelling it.](/assets/components/code-editor/code-editor-dont-external-accessible-name.png)
46+
47+
!!!
48+
49+
## Secondary actions
50+
51+
The secondary actions section supports two optional buttons: the [Copy Button](/components/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.
52+
53+
![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.](/assets/components/code-editor/code-editor-secondary-actions.png)
54+
55+
## Custom actions
56+
57+
This space is intended for custom primary actions. Primary actions are those which are necessary for the user to complete their work.
58+
59+
![The Code Editor with the title “CodeEditor title”. The custom yielded element section shows a placeholder and is between the title and the editor.](/assets/components/code-editor/code-editor-yielded-actions-placeholder.png)
60+
61+
Here is an example of a custom action to reveal secrets.
62+
63+
![The Code Editor with the title “CodeEditor title”. The custom yielded section has a “Reveal secrets” toggle.](/assets/components/code-editor/code-editor-primary-yielded-actions.png)
64+
65+
66+
!!! Warning
67+
68+
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](about/support) for help translating components into dark mode.
69+
70+
!!!
71+
72+
## External elements
73+
74+
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.
75+
76+
![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.](/assets/components/code-editor/code-editor-external-functions.png)
77+
78+
79+
## Active line highlighting
80+
81+
When a user edits a line of code, a highlight will display to show their location within the Code Editor.
82+
83+
![The Code Editor's line 1 with the active line highlight, shown with a blinking cursor and border around the entire line.](/assets/components/code-editor/code-editor-code-active-line.png)
84+
85+
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.
86+
87+
![The Code Editor's line 1 to 3 selected and showing the green selection highlighted color](/assets/components/code-editor/code-editor-line-selection.png)
88+
89+
## Bracket highlighting
90+
91+
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.
92+
93+
![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.](/assets/components/code-editor/code-editor-bracket-highlighting.png)
94+
95+
## Language
96+
97+
Language determines how syntax highlighting is applied and formatted within the editor but is handled a bit differently between the Ember and Figma components.
98+
99+
The **Ember** component uses [CodeMirror](https://codemirror.net/) to handle syntax highlighting and comes with a pre-defined set of [languages](/components/code-editor?tab=code#language-1).
100+
101+
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.
102+
103+
### Applying syntax highlighting
104+
105+
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](https://www.figma.com/design/iweq3r2Pi8xiJfD9e6lOhF/HDS-Components-v2.0?node-id=67166-37020&t=gWdKy44MzTP4cTRo-1) 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.
106+
For more details around syntax, visit the [specifications](/components/code-editor?tab=specifications).

0 commit comments

Comments
 (0)