Overview
The editor is a standard textarea field with one more class, nds-editor, on its container. When the page loads, the script adds a toolbar and an editing surface in front of the textarea. The textarea stays the form value: it holds the HTML that the user writes, and it submits with the form.
The editor is in beta. Its API and its markup can change before it is stable.
Pick another component when:
Markup
Variants
Every state row goes on the field’s <textarea>. The editor reads it from there when it starts.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Standard (default) | — | — | Images from a URL only. Uploads nothing |
| Structure | Image upload (id: upload) | canon #editor-upload |
— | The image button also takes a file, and sends it to your server. See Image Upload |
| Structure | Embedded images (id: embed) (hint: Uploaded files are saved inside the text, not on a server) | canon #editor-embed |
— | The image button also takes a file, and puts it in the value as a data: URL. Only for a field with no upload server. See Embedded Images |
| Toolbar | Full (default) | — | — | Every command except h1. Use it for long articles and page content |
| Toolbar | Short (not: upload, embed) | [data-editor-toolbar="bold italic underline \| link \| ul ol \| source"] |
.nds-editor |
A short set for comments and notes. See Toolbar |
| Toolbar | None (not: upload, embed) | [data-editor-toolbar="none"] |
.nds-editor |
No toolbar. The keyboard shortcuts and paste still work |
| State (any) | Read-only | [readonly] |
textarea:not([disabled]) |
The user can read and copy the text, and open the source view if the toolbar has it, but cannot change it. The value submits. Not with Disabled |
| State (any) | Disabled | [disabled] |
textarea:not([readonly]) |
The field is off and its value does not submit. Not with Read-only |
| State (any) | Required (hint: Press Validate with the field empty) | [required] |
textarea |
The form needs text in the field. The forms script adds the required mark |
| Field states | Label, info, feedback, required | — | — | Shared by every form field. See Forms |
Behavior
Toolbar
data-editor-toolbar on .nds-editor picks the toolbar buttons. Write the command names with spaces or commas between them, and | to start a new button group. Without the attribute, the toolbar has the full set. The command names are in the API.
Pasted Components
NDS component markup pasted or typed in the source view stays a styled component. It keeps its nds- classes, data-status, data-state, ARIA attributes and knobs such as --card-width. The user edits the text inside it, but cannot break it apart. Backspace and Delete work inside each part of it, and stop at the part’s edge. A selection that covers the whole component deletes it. Enter adds a line break inside a card or an alert, and moves the caret out of a tag, a chip or a button. The Remove button (remove) lists the component at the caret and each one around it, and removes the one the user picks.
Images
The image button inserts an image from a URL, with alt text, a width and a height. Pasted images keep a safe src, their alt text, and their width and height when these are numbers. A click on an image selects it. The image button then edits it, and the link button links it. By default the editor uploads nothing and embeds nothing: a pasted screenshot shows a message on the field.
Image Upload
data-editor-upload-url with your server’s URL adds a file picker to the image popover. With data-editor-upload-auto-upload, the picker sends each file to the server, the way File Upload does. It then inserts the URL that the server returns. A file that fails a check shows its message in the picker.
Embedded Images
data-editor-upload-url="embed" adds a file picker that sends nothing. The file goes in the value as a data: URL, and a pasted screenshot does too. A screenshot that fails a check shows its message on the field. Use it only for a field with no upload server: see Best Practices.
Read-only and Disabled
readonly on the textarea keeps the text selectable, and the source view still opens. The commands are off, and the value submits. disabled turns the whole field off, and its value does not submit. To change the state after load, call NDS.state.add(el, 'disabled') or NDS.state.remove(el, 'disabled'), where el is .nds-editor. The same goes for 'readonly'. The editor updates the textarea to match.
Built-in Features
-
Automatic Setup
Every
nds-editorfield starts when the page loads. The script makes the toolbar, the editing surface and the popovers, so you write only the textarea field. -
Form Integration
The textarea holds the value as formatted HTML. Saved HTML written in the textarea shows formatted when the editor starts. Its label and placeholder become the editing surface's name and placeholder. It submits with the form, takes
required, and firesinputon each edit andchangewhen the user leaves a changed field. -
Paste Conversion
Text from Word and Google Docs keeps its lists, bold, italic and underline. A pasted table becomes an NDS table. HTML source pasted as text becomes formatted content.
-
Safe Value
The editor cleans every paste, every drop and every source edit. It removes scripts, styles, event attributes and unsafe URLs such as
javascript:. A link that opens a new tab always getsrel="noopener noreferrer". -
Link Popover
The link button opens a form for the text and the URL, with Open in new tab, Hide external badge and Colored link options. On a link, it also shows Unlink.
-
HTML Source View
The source button shows the formatted HTML for direct edits. The text that the user selected stays selected in the source. The formatting buttons are disabled while the source shows.
-
Undo History
The editor keeps its own undo history, so undo also reverses a removed component and a popover edit.
-
Arabic and English Labels
The toolbar labels, tooltips and popover text follow the page language.
-
Loading Placeholder
Until the script starts, the field shows a pulsing bar for the toolbar and a pulsing box for the text, at the editor's size.
Best Practices
- Use the editor for text that a user writes for a page: announcements, articles, service descriptions.
- Give each field only the commands it needs. A comment field needs
bold italic | ul ol, not the full set. - Leave
h1out unless the field writes a whole page. The page already has its<h1>. - Read and write the value on the textarea, never on the editing surface.
- Do not use the editor to write code. The source view is for small markup fixes.
- Clean the value on your server too. The editor cleans what the user enters, but a request can skip it.
- Send image uploads to your server with
data-editor-upload-url. Useembedonly when there is no upload server: adata:URL makes the value about a third larger than the file, and the browser cannot cache it. - Set
--editor-max-sizeon a tall field. The text then scrolls inside the field, and the toolbar stays in view. - For the label, info text, feedback and the required mark, see Forms. They work the same on every field.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-editor-toolbar |
.nds-editor |
The toolbar commands, from the Toolbar Commands table, separated by spaces or commas. \| starts a new group. none removes the toolbar |
data-editor-upload-url |
.nds-editor |
Adds a file picker to the image popover. Your server’s URL, or embed to put the file in the value as a data: URL. The server returns { "url": "…" } or the URL as text |
data-editor-upload-auto-upload |
.nds-editor |
Sends the file as soon as the user picks it. On when present; false, 0, no or off keeps it off. Set it with a server URL. Ignored with embed |
data-editor-upload-max-file-size |
.nds-editor |
The largest file in bytes. Default 2097152 (2 MB). With embed, it also limits a pasted screenshot |
data-editor-upload-allowed-types |
.nds-editor |
The file extensions the picker takes, separated by spaces or commas. Default jpg,jpeg,png,gif,webp,svg. List jpg and jpeg both to take either |
data-editor-upload-allowed-mime-types |
.nds-editor |
The MIME types the picker takes, such as image/*. A second check after the extension |
data-state~="readonly" |
.nds-editor |
Set by the script at load when the textarea has readonly. Add or remove it with NDS.state after load. See Read-only and Disabled |
data-state~="disabled" |
.nds-editor |
Set by the script at load when the textarea has disabled. Add or remove it with NDS.state after load. See Read-only and Disabled |
Toolbar Commands
| Command | Effect |
|---|---|
undo, redo |
Steps back or forward in the editor’s history |
bold, italic, underline, strike |
Turns the format on or off for the selection |
clear |
Removes the formats from the selection |
link |
Opens the link popover |
image |
Opens the image popover. See Images |
h1, h2, h3, h4 |
Turns the line into that heading, or back into a paragraph. h1 is not in the full set |
align-left, align-center, align-right, align-justify |
Aligns the line. Left and right stay on that side in both languages. A second click removes the alignment |
dir-ltr, dir-rtl |
Sets the text direction of the line. A second click removes it, and the line follows the page again |
ul, ol |
Turns the line into a bulleted or a numbered list |
remove |
Removes a pasted component. It is on only while the caret is in one. See Pasted Components |
source |
Opens the HTML source view. It always sits at the end of the bar |
The full set is undo redo | bold italic underline strike clear | link image | h2 h3 h4 | align-left align-center align-right align-justify | dir-ltr dir-rtl | ul ol | remove | source.
Keyboard
| Key | Effect |
|---|---|
| Ctrl or Cmd + B, I, U | Bold, italic, underline |
| Ctrl or Cmd + Z | Undo |
| Ctrl or Cmd + Y, or Ctrl or Cmd + Shift + Z | Redo |
| Enter | Starts a new paragraph. See Pasted Components for Enter in a component |
| Tab, Shift + Tab | In a list item: nests the item, or moves it out one level. Elsewhere, Tab moves the focus out of the field |
| Tab, Shift + Tab in the source view | Indents or outdents the line or the selected lines |
| Escape, then Tab in the source view | Moves the focus out of the field |
CSS Custom Properties
Set these on .nds-editor.
| Property | Default | Controls |
|---|---|---|
--editor-min-size |
12rem |
Minimum height of the editing surface and the source view |
--editor-max-size |
70vh |
Maximum height. Past it, the text scrolls inside the field |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_editor.scss.
| Token | Preview | Value |
|---|---|---|
--focus-outlineRead by outline | var(--colors-base-black)Dark mode var(--colors-base-white) | |
--icon-default-disabledRead by --btn-color-disabled | var(--colors-neutral-400) | |
--form-field-text-placeholderRead by color | var(--text-secondary-paragraph)Dark mode var(--colors-neutral-400) | |
--form-field-background-darkerRead by background | var(--colors-neutral-100)Dark mode var(--colors-neutral-900) |
JavaScript
The NDS.Editor script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
| Method | Effect |
|---|---|
NDS.Editor.init() |
Starts every .nds-editor on the page that has not started |
NDS.Editor.create(el) |
Starts one editor and returns it. Returns the running editor when el already has one, and null when the field has no textarea |
NDS.Editor.destroy(el) |
Removes the toolbar and the editing surface. The plain textarea field stays, with its value |
NDS.Editor.getValue(el) |
Returns the sanitized HTML, or '' when the editor is not found. el is the .nds-editor, its textarea, its id or a selector |
NDS.Editor.setValue(el, html) |
Fills the editing surface and the textarea with html. Returns true, or false when the editor is not found |
NDS.Editor.clear(el) |
Empties the editor. Returns true, or false when the editor is not found |
el.ndsEditor.getValue(), .setValue(html), .clear() |
The same, on an editor that has started |
instance.setImageUpload(config) |
Sets the image popover’s file picker from code. config takes the data-editor-upload-* options in camel case: uploadUrl, autoUpload, maxFileSize, allowedTypes, allowedMimeTypes. null or false removes an option. Returns the instance |
| Event | Fired on | Detail |
|---|---|---|
nds:editor:ready |
.nds-editor (bubbles) |
{ instance }. Fires when the editor has started |
The running editor is also el.ndsEditor. File Upload events, such as nds:upload:error, bubble from the image popover.
A form reset puts the textarea’s first value back in the surface.
The full API is in the banner of _js/nds-editor.js.