Skip to main content
Login

Sign in to access your account

Editor - National Design System

A rich text field that turns a textarea into an editing surface with a toolbar, and submits clean NDS markup

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:

  • the value is plain text: a textarea in Forms
  • the value is one line: a text field in Forms
  • the value is a list of words: Tag Input

Markup

Preview
<div class="nds-form-container nds-textarea nds-editor"> <div class="nds-form-header"> <label for="editor-content"><span class="nds-label">Content</span></label> </div> <div class="nds-form-control"> <textarea class="nds-textarea" name="content" id="editor-content" placeholder="Write here"></textarea> </div> </div>

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-editor field 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 fires input on each edit and change when 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 gets rel="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 h1 out 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. Use embed only when there is no upload server: a data: URL makes the value about a third larger than the file, and the browser cannot cache it.
  • Set --editor-max-size on 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.

TokenPreviewValue
--focus-outline
Read by outline

var(--colors-base-black)
Dark mode var(--colors-base-white)
--icon-default-disabled
Read by --btn-color-disabled
var(--colors-neutral-400)
--form-field-text-placeholder
Read by color

var(--text-secondary-paragraph)
Dark mode var(--colors-neutral-400)
--form-field-background-darker
Read 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.

document.addEventListener('nds:editor:ready', function (e) { e.detail.instance.setImageUpload({ maxFileSize: 5 * 1024 * 1024 }); }); var textarea = document.querySelector('#editor-content'); textarea.addEventListener('change', function () { console.log(textarea.value); });

The full API is in the banner of _js/nds-editor.js.

Last Modified Date: 05/11/2026 - 12:02 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks