aiellie

Search

Find a component, template or page

GitHub
Design

Editable Title

A heading you click to rename in place: Enter keeps the new name, Escape keeps the old one. Also exports the hook behind the field, for renaming anywhere else.

Click the title to rename it.

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/editable-title"

shadcn needs the @aiellie registry in components.json first. npx aiellie add editable-title adds it on its first run, then installs the same way.

Editable title is a page or chat heading that renames in place. Click it and it becomes a field with the name selected; Enter or clicking away keeps the new name, Escape or an empty field keeps the old one. Without onRename it is plain text, for something that can't be renamed yet.

Getting started

  1. Hold the title and the editing state

    Both are controlled, so something else, like a Rename menu item, can open the field too.

    components/chat-title.tsx
    "use client"
    
    import * as React from "react"
    
    import { EditableTitle } from "@/components/ui/editable-title"
    
    export function ChatTitle({
      title,
      onRename,
    }: {
      title: string
      onRename: (title: string) => void
    }) {
      const [editing, setEditing] = React.useState(false)
    
      return (
        <EditableTitle
          title={title}
          label="Chat name"
          editing={editing}
          onEditingChange={setEditing}
          onRename={onRename}
        />
      )
    }

Anatomy

<h1 data-slot="editable-title"> {/* render, an h1 by default */}
  <button title="Rename"> {/* only with onRename */}
    <span>{title}</span>
  </button>
</h1>

{/* while editing, in place of the heading: */}
<input data-slot="editable-title-input" aria-label={label} />

The heading is set at 14px in label weight, and the button inside it gets the accent fill on hover. While editing, the field takes the heading's place rather than sitting inside it, so there's never an input inside an h1. It takes focus a frame late, so a menu that opened it can hand focus back to its trigger first without stealing it. onRename only fires for a name that changed.

useRenameInput is exported too: the same Enter, Escape and blur handling for a rename field of your own, like one in a sidebar row.

Examples

Rename from a menu

Set editing from a menu item. render makes it an h2 here, since the page has its own h1.

Your own rename field

useRenameInput returns the props for an input: its ref, value and handlers. onDone gets the trimmed name, or nothing when it was cancelled or emptied.

function RowRename({ title, onDone }: { title: string; onDone: (title?: string) => void }) {
  return <Input {...useRenameInput(title, onDone)} aria-label="Chat name" />
}

API reference

EditableTitle

PropTypeDefaultDescription
titlestringrequiredThe name shown, and what the field starts from.
editingbooleanrequiredWhether the field is open.
onEditingChange(editing: boolean) => voidrequiredCalled to open the field on a click, and to close it when done.
onRename(title: string) => voidCalled with a new name. Left out for something that can't be renamed yet, which shows plain text.
labelstring"Name"What the field is called for screen readers, like Chat name.
renderReactElement<h1 />The heading's element.
classNamestringMerged onto the heading.

useRenameInput

ExportTypeDescription
useRenameInput(title, onDone)(title: string, onDone: (title?: string) => void) => InputPropsProps for a rename input: focused and selected on mount, Enter keeps, Escape cancels, blur keeps.