Skip to content

Data Input

EditableText

Text that turns into an input on demand, keeping the typography of its display element.

import { EditableText } from '@var-ui/react';

Road trip

EditableText

Text that turns into an input on demand. Wrap the display element (a Heading, Text, or any element that accepts children) and the input takes its place, inheriting the same font, size and color so nothing shifts.

Examples

Default

Double-click the heading, or focus it and press Enter or F2. Enter or clicking away commits; Escape cancels.

Road trip

Demo.tsxTSX
import { useState } from 'react';
import { EditableText, Heading } from '@var-ui/react';

function PlaylistName() {
  const [name, setName] = useState('Road trip');
  return (
    <EditableText value={name} onCommit={setName} aria-label="Playlist name">
      <Heading level={2}>{name}</Heading>
    </EditableText>
  );
}

Activation

activation controls what starts editing with the pointer: doubleClick (default), click, or focus. Enter and F2 start editing from the keyboard in every mode.

Props

Accessibility

While not editing, the root is a focusable role="group" so keyboard users can reach it and press Enter or F2 to edit. While editing, the input is labelled by aria-label (falling back to the current value). Focus returns to the root after Enter or Escape; blur commits without moving focus. Disabled and read-only instances render the display element as-is and are not focusable.

onCommit is not called when the text is unchanged. Empty text is passed through, so reject or restore it in onCommit if blank values are not allowed.