ui.render event each time it’s about to draw a render site, and your hook for that event returns what to draw there.
This map shows where a mod can draw in a terminal session:
To look up one prop or limit, see the reference.
Build a pane with tabs
In this section you build a mod that adds a/hello-tabs command, and the command opens a pane. A pane is a sidebar beside the transcript in a wide fullscreen terminal, or a framed region above the prompt otherwise. This pane shows two tabs, and the second tab has a button that adds one to a counter. The count is still there after you restart Claude Code.
The finished mod looks like this. The recording opens the pane, switches to the second tab, presses the button a few times, and returns to the first tab:
1
Create the plugin
A mod is a plugin with a manifest, a Name your entry point in
hooks.json that points to your code, and the code file. Create a mod explains each one. Create a directory named hello-tabs with .claude-plugin and hooks directories inside it, then save the first two files.Save the manifest as hello-tabs/.claude-plugin/plugin.json:hello-tabs/.claude-plugin/plugin.json
hello-tabs/hooks/hooks.json:hello-tabs/hooks/hooks.json
2
Write the code
The code does three jobs, one in each hook:Each hook also does something the code doesn’t make plain:
- Adds the
/hello-tabscommand - Opens the pane when you run that command
- Draws the pane’s content: the row of tabs and the open tab’s body
tab and count, hold the pane’s state.Save this as hello-tabs/hooks/register.js:hello-tabs/hooks/register.js
session.startalso reads the saved count from$.store, a key-value store that persists between sessions.command.runonly tells Claude Code the pane exists. Opening a pane draws nothing by itself: Claude Code then raisesui.renderto ask what goes in it.ui.renderreturns the element tree, aBoxthat holds other boxes, text, and buttons, and builds it again fromtabandcounteach time it runs.
onPress callback, which changes a variable and calls redraw. Claude Code then runs the ui.render hook again, and the hook builds a new tree from the new values. Every interactive drawing uses that render cycle: a callback changes state, and the hook renders again from the new state.3
Open the pane
In your shell, start Claude Code with
claude --plugin-dir ./hello-tabs. At the Claude Code prompt, run /hello-tabs. A pane opens with 1: One and 2: Two across the top. Press 2, then press a, the hotkey for Add one, a few times. The count rises.4
Check that the count was saved
Press Esc to close the pane, then exit the session. In your shell, start Claude Code again with the same
claude --plugin-dir ./hello-tabs command, and at the Claude Code prompt run /hello-tabs. The count is where you left it.To clear the count, have the mod call $.store.delete('count'). Keep state covers how long each kind of value lasts.Pick where to draw
Aui.render hook runs for every render site unless you narrow it to the one you want to draw in. To choose the render site, pass a filter, called a matcher, as the second argument to on. { component: 'Pane' } runs the hook only for panes. In the hook, e.component names the site, e.surface says which app is drawing, and e.props holds the site’s own data. For a pane, e.requestId is the id you opened it with.
Two sites are empty until a mod fills them, the pane and the band. Select a tab to see what each one is and how to draw in it:
- Pane
- Band above the prompt
A pane is a sidebar beside the transcript in a wide fullscreen terminal, or a framed region above the prompt otherwise. With several panes open, each gets a tab that shows its title.A pane appears when your mod calls
$.ui.open with an id you choose, as in $.ui.open({ id: 'hello-tabs' }). Open a pane at the right time covers the other fields and when a pane waits for a wider terminal.To draw in your pane, filter on { component: 'Pane' } and check that e.requestId is your id.Change what Claude Code already draws
Claude Code draws most of its interface itself: messages, tool call rows, the spinner, and more. Each of those parts is a render site too, so a mod can restyle or replace it. To change one, filter yourui.render hook on its name from this table:
At a site Claude Code already draws, your hook has three choices: change a detail, replace the drawing, or leave it alone. Select a tab to see each one applied to the spinner. The examples read a
calls variable that another hook counts, as in the tutorial mod.
- Change a detail
- Replace the drawing
- Leave it alone
To keep Claude Code’s drawing and change one part of it, pass The spinner keeps its animation and its word, and your text follows the word:
next a copy of the event with changed props. This hook changes the text after the spinner’s word:AskUserQuestion, is one, so a mod can change that.
The terminal and the Desktop app don’t raise all the same sites. Pane, AbovePrompt, Spinner, and the transcript sites work in both. A few other status lines are raised in the terminal only. The render sites table lists where each one is raised.
Open a pane at the right time
A pane appears only when your mod opens it. How and when you open it decides whether it takes keyboard focus, how much room it asks for, and whether it shows at all in a narrow terminal. To open a pane, call$.ui.open with an id you choose. The id is the pane’s name: your ui.render hook checks for it, and you pass it again to close the pane.
$.ui.close with the id you opened it with:
id, $.ui.open takes these optional fields:
To let a command open the pane while Claude is working, add
immediate: true when you register the command. Without it, a command typed during a turn waits for the turn to end.
When a pane waits for a wider terminal
A pane your mod opens without being asked doesn’t appear in a narrow terminal, so it can’t take over a small screen. Whether it appears depends on what opened it:- Opened by something the user did, such as a command they ran or a button they pressed, the pane appears at any width
- Opened by your mod acting by itself, such as from a timer or a
turn.starthook, the pane appears only in a terminal at least 144 columns wide. After the user has opened that pane once themselves, 110 columns is enough.
$.ui.open resolves to { isPlaced: true }. When the pane is waiting, isPlaced is false and reason is a string that says why. A waiting pane appears when the user opens it or widens the terminal. To say something is available without opening a pane, call $.ui.toast('Your message'), which shows a small notice that disappears after a few seconds.
Build a tree from elements
What aui.render hook returns is an element tree: a description of what to draw, made of boxes, text, and controls nested inside each other. You describe the drawing, and Claude Code renders it in the terminal or the Desktop app.
To get the elements, call $.ui.resolve(e) in your hook, as in const { Box, Text, Button } = $.ui.resolve(e). Each element is a function. You pass it props, and you put the elements and strings that go inside it in children.
Most drawings use four elements. Select a tab to see each one and how the terminal draws it:
- Text
- Box
- Input
Text draws a string, with optional styling such as bold and color:
If your module is a
.tsx or .jsx file, you can write the tree as JSX. Destructure the elements from $.ui.resolve(e) first, because a hooks module has no element globals.
If a tree uses an element the app doesn’t have, a prop an element doesn’t take, or a child where none goes, Claude Code draws its own version of the site.
In a session started with --plugin-dir, a transcript line says so, such as ui.render (Pane) refused: Text prop "bogusProp" is not allowed; the engine drew its own. The debug log records it as ui.render (Pane): a hook returned a tree that does not validate with the same reason. Nothing else appears in the session, so when a drawing doesn’t show up, check that line or the log.
Draw a grid of colored cells
For a heat map, a sparkline, or a game board in the terminal, draw oneRaster and not a Box for each cell. A Raster takes a key, its size in columns and rows, and cells, which packs every cell into one string. Each cell is three numbers: the character’s code point, its color, and its background color. A color is a hexadecimal number with two digits each for red, green, and blue, such as 0xc62828 for a red, or 0x01000000 for the terminal’s default.
The Desktop app has no Raster, so check e.surface and draw text there. This pane body draws a three by two heat map:
rows array is the part you’d change, and cellsOf turns it into the packed string. The hook draws only in a pane whose id is heat, so open one with $.ui.open({ id: 'heat' }) from a command, as the hello-tabs example opens its pane.
Each character has to be one cell wide. To animate a Raster that’s already on screen, call $.ui.blit with the pane’s id as requestId, the Raster’s key, the same size, and new cells. For this example, that’s $.ui.blit({ requestId: 'heat', key: 'grid', columns: 3, rows: 2, cells: cellsOf(newRows) }). It repaints that one element without running your ui.render hook again.
Respond to presses and typing
When the user presses a button, types into a field, or picks from a list your mod drew, Claude Code calls the function you gave that control, and it runs in your module. Each control takes its own callbacks:Button: takesonPress(e), wheree.surfaceis the app the press came fromInput: takesonSubmit(value)andonInput(value)Select: takesonSelect(value)with its choices inoptions, a list of at least one choice with unique values, such as[{ value: 'sm', label: 'Small' }, { value: 'lg', label: 'Large' }]
key, so give each control one. Each use of a control also fires ui.press, ui.input, or ui.select with the key in e.element, and another mod can hook those events. Its hook runs before your callback, so it sees what the user types into your Input and can change it or answer in place of your callback. The mods API has no method that presses another mod’s button.
Keyboard focus and hotkeys
Your mod never reads the keyboard itself. The user presses a key, Claude Code decides which of your controls it’s for, and that control’s callback runs. Apart from a digit hotkey on the band, that happens only while your pane or band has keyboard focus. The rest of the time, keys go to the prompt.How a pane gets keyboard focus
A pane gets keyboard focus in one of three ways:- Your mod opens it with
focus: truefrom a command or a press - The user presses Ctrl+X then Tab
- The user clicks it
focus: true only while the prompt is empty and nothing else has keyboard focus. A pane that opens while the user is typing doesn’t take their keystrokes.
What each key does
This table lists what a key does while your pane or band has keyboard focus:
A mod can’t bind Tab or the arrow keys to anything else, so a game steers with
w, a, s, and d.
Set a hotkey and the first focus
Two props on a control decide how the keyboard reaches it:hotkey: to let the user press aButtonwith one key, give it ahotkeyof one digit or one lowercase letter, as inhotkey: 'a'autoFocus: to choose which control has the focus when the pane opens, addautoFocus: trueto it. Leave the prop off the others, because Claude Code refusesautoFocus: false.
In the terminal, name the key in a bracketed button’s label, or use
plain: true, so the user can see what to press. The elements reference has the other Button rules: action, digit hotkeys on the band, and two buttons on one hotkey.
Take typed input and draw a row for each item
Many panes are a text field with a list under it. The example in this section is a notes pane: you type a note and press Enter to add it, and each note has anx button that deletes it. With two notes added, the terminal draws the pane this way:
- Take typed input: an
InputcallsonSubmit(value)with the field’s text when the user presses Enter, andonInput(value)on every change - Draw a list: map your data to one row each, and give every row’s button its own
key
- Add a note: type a line and press Enter. The line appears as a new row, and the field empties.
- Delete a note: press Tab until the note’s
xbutton has the focus, then press Enter. Thexis the button’s label and not a hotkey, so typing the letter doesn’t press it.
hello-tabs: the callback changes notes, calls redraw, and saves the list to $.store.
The field empties after each submit because of its value prop. value is the text the field holds when it’s drawn, and the user’s typing replaces it until your hook draws the field again. The example always draws the field with ''.
The example saves the notes and doesn’t load them. To bring them back in the next session, read them in a session.start hook, the way hello-tabs reads count.
Three props make up the field’s line, Note: Type a note and press Enter ⏎ add:
Submitting an
Input doesn’t start a turn unless your callback calls $.prompt.submit.
Redraw a site
A drawing is a snapshot: it shows what yourui.render hook returned the last time the hook ran. To show something new, the hook has to run again. Claude Code runs it again for some changes, and your mod asks for the rest.
When Claude Code redraws without being asked
Claude Code runs yourui.render hook again when the site’s props change or the terminal’s width changes. It doesn’t run the hook on a timer, and it can’t tell when a variable in your module changes.
Redraw when your data changes
To have your sites drawn again after your own data changes, call$.ui.invalidate('ui.render'). This pane counts presses. The button’s callback changes count, then asks for a redraw:
hello-tabs example wraps the same call in its redraw function.
A value you keep in $.state doesn’t need the call, because writing the value redraws the sites that read it.
Redraw on a timer
To keep a clock, a countdown, or a value from outside the session current, redraw on a schedule. Start a timer in the module’ssession.start hook. If the module already has one, as hello-tabs does, add the $.clock.every line to it:
ui.render hook once a second. The timer stops when the module reloads, and the new copy of the module starts its own.
How often a site can redraw
Claude Code limits how often it redraws a site, so your mod can call$.ui.invalidate as often as its data changes. The visible pane and the band have a higher limit than other sites, and the limits table has the numbers.
Calls that come faster than the limit are combined into one redraw. That redraw runs your hook once, and the hook reads your data as it is at that moment, so the latest value shows and the values in between don’t. An animation can’t run faster than the limit.
Keep state
A mod has three places to keep a value, and they differ in how long the value lasts: until the module reloads, until the session ends, or from one session to the next. Choose by how long the value has to last:$.store.get(key) resolves to the value or undefined, and $.store.set(key, value) takes any JSON value.
Keep a value in $.state
$.state holds values for the length of a session, and it redraws for you. It’s reactive state: a ui.render hook that reads a value subscribes to it, so Claude Code redraws that site each time you write the value, and you don’t call $.ui.invalidate. A value in $.state also survives a reload of the module, which a variable doesn’t.
To set it up, declare your values, point your manifest at the declaration, then define and use each value. The examples move the count from hello-tabs into $.state.
Declare the values
Declare the values in a types file. The outer key is your plugin’s name, and each entry under it is a value and its type. Save this ashello-tabs/types/index.d.ts:
hello-tabs/types/index.d.ts
Point the manifest at the declaration
To letclaude plugin validate check your code against that file, add a types field to the manifest with its path:
hello-tabs/.claude-plugin/plugin.json
Define, read, and write a value
In your module, define each value with a default, read it while drawing, and write it from a callback.atom names a value and its default, read returns it, and update writes it. The three helpers call $.state.get and $.state.set for you:
ui.render hook read count, Claude Code runs the hook again each time the button writes it.
Three rules apply to the code:
- Write
pluginandkeyas literal strings:claude plugin validatereads them from your source - Declare every value in the types file: otherwise validation fails with
hello-tabs.count is not declared - Write from a callback or another event’s hook: a
ui.renderhook can read state and can’t write it, so write fromonPress,onSubmit, or a hook for another event
Change hello-tabs to use $.state
To move count in hello-tabs into $.state, change every line that uses it:
- At the top of the module: add the
importline, and replacelet count = 0with theatomline - In the
ui.renderhook: add thereadline beforetabButton, and draw'Count: ' + nin theText - In the Add one button: replace
onPresswith the one in Save from more than one session, which saves the count as well as writing it - In the
session.starthook: replace the two lines that readsavedwith theloadCountcall from Load a saved value again after/clear
redraw for the tab buttons, because tab is still a variable.
Load a saved value again after /clear
If your mod copies a saved value from $.store into $.state at session.start, it has to copy it again after /clear, /resume, or /branch. Those commands put every $.state value back to its default, and session.start doesn’t fire again. classic.SessionStart does fire after each of them, with e.source set to clear, resume, or fork, so copy the value again in a hook on it. Otherwise your drawing shows the default, and a callback that saves the $.state value writes the default over what you stored.
This code loads count from both hooks. It builds on the $.state version of hello-tabs, where count is an atom and update is imported. Put loadCount above register, and add the loadCount call to the session.start hook you already have. classic.SessionStart also fires at startup and after compaction, which doesn’t reset $.state, so the filter on source keeps the hook to the three resets:
/clear and not 0, and the next press of Add one adds to the saved count.
loadCount writes the stored value over the one in $.state, and session.start fires again each time the module reloads. To keep the store from falling behind, save on every change, as the Add one button does.
To check the reload without a session, test the drawing after /clear.
Save from more than one session
Every session on your machine that runs your mod shares one$.store. A get followed by a set isn’t atomic. When two sessions each read a value, change it, and write it back, they race, and the second write replaces the first.
Two choices make that less likely:
- Give each item its own key: a
setchanges only its own key, so sessions that write different keys don’t overwrite each other - Read again right before you write: for a value that several sessions change,
getthe key in the callback and build the new value from that, not from a copy you loaded atsession.start. Another session’s write is still lost if it lands between yourgetand yourset.
Next steps
- React to events: feed your drawing from tool calls and turns
- Use the mods API: feed your drawing from timers and model calls
- Test a drawing: press your buttons from a test, on more than one surface
- Render sites and elements: each site’s props and each element’s props