Ask Bar

A bar docked at the bottom of the page that takes a request or a command name, and answers with the exact change it would make.

Source
$npx @dinachi/cli@latest add ask-bar

Rates · December

Sea View Double £162 to £240 · Harbour Twin £138 to £190 · Garden Single £96 to £130

label
suggest
confirm
disabled

Usage

import { AskBar, AskBarContent, AskBarTrigger, type AskPlan } from "@/components/ui/ask-bar"
import type { CommandItem } from "@/components/ui/command-items"

// Your own API: applies the change and returns a function that reverts it.
declare function adjustWeekendRates(pct: number): Promise<() => Promise<void>>

const commands: CommandItem[] = [{ id: "rates", label: "Rates", description: "Page" }]

function resolve(query: string): AskPlan | null {
  if (!/^raise/i.test(query)) return null
  return {
    title: "Raise weekend rates",
    sentence: ["Raise weekend rates by ", { field: "pct" }, "%."],
    fields: [{ id: "pct", label: "Change %", value: "10", inputMode: "decimal" }],
    touches: "8 weekend nights across 3 room types.",
    call: (v) => `rates.adjust('2026-12', 'fri,sat', ${Number(v.pct) / 100})`,
    verb: (v) => `Raise 24 rates by ${v.pct}%`,
    run: async (v) => ({ message: `Raised 24 rates by ${v.pct}%.`, undo: await adjustWeekendRates(Number(v.pct) / 100) }),
  }
}

export function RatesBar() {
  return (
    <AskBar resolve={resolve} commands={commands} suggestions={["raise weekend rates 10%"]}>
      <AskBarTrigger />
      <AskBarContent />
    </AskBar>
  )
}

Examples

Rates · December

Sea View Double £162 to £240 · Harbour Twin £138 to £190 · Garden Single £96 to £130

A rates page's bar. Focus it for suggestions; a request reads back as a plan with its values editable, and a name falls back to commands. Below 640px an Ask button opens it as a sheet.
import { AskBar, AskBarContent, AskBarTrigger, type AskPlan } from '@/components/ui/ask-bar';
import type { CommandItem } from '@/components/ui/command-items';

// Your own API and router.
declare const rates: {
  adjust(month: string, days: string, by: number): Promise<() => Promise<void>>;
};
declare function openPage(id: string): void;

const commands: CommandItem[] = [
  { id: 'rates', label: 'Rates', description: 'Page' },
  { id: 'copy', label: "Copy last year's rates", description: 'Action' },
];

function resolve(query: string): AskPlan | null {
  if (!/^raise/i.test(query)) return null;
  const pct = query.match(/\d+/)?.[0] ?? '10';
  return {
    title: 'Raise weekend rates',
    sentence: ['Raise ', { field: 'rooms' }, ' on ', { field: 'nights' }, ' by ', { field: 'pct' }, '%.'],
    fields: [
      { id: 'nights', label: 'Nights', value: 'Fri and Sat, 5 to 27 Dec', fixed: true },
      { id: 'rooms', label: 'Rooms', value: 'all 3 room types', fixed: true },
      { id: 'pct', label: 'Change %', value: pct, inputMode: 'decimal' },
    ],
    touches: '8 weekend nights across 3 room types.',
    call: (v) => `rates.adjust('2026-12', 'fri,sat', ${Number(v.pct) / 100})`,
    verb: (v) => `Raise 24 rates by ${v.pct}%`,
    run: async (v) => {
      const undo = await rates.adjust('2026-12', 'fri,sat', Number(v.pct) / 100);
      return { message: `Raised 24 rates by ${v.pct}%.`, undo };
    },
  };
}

export function RatesBar() {
  return (
    <AskBar
      resolve={resolve}
      commands={commands}
      suggestions={['raise weekend rates 10% in December']}
      onCommand={(item) => openPage(item.id)}
    >
      <AskBarTrigger />
      <AskBarContent />
    </AskBar>
  );
}

Notes

The plan shape

An AskPlan has a title, a sentence of strings and { field: id } references, its fields (id, label, value, fixed, inputMode), a touches line and the call it makes (each a value, or a function of the values so it follows edits), verb(values) for the button and run(values), which returns { message, undo? }. undo may return a line to announce; otherwise Undone. is read. Fixed values read as plain text. Throwing from run shows the message in place of the touch line and keeps the plan.

Nothing runs on its own

resolve only describes the change. It runs when the person presses the button or Enter, and the button names exactly what it will do with the current values. An agent's press needs a person's approval unless confirm is false.

Motion

Focusing, the chips, the plan and its button label all change at once: they follow typing, and a plan never replays an entrance as values change. The done line fades in on opacity over 200ms with the ease-out token, and keeps the fade under reduced motion since nothing moves. The phone sheet slides in with the Drawer's own curve and duration, and leaves on the fast duration so dismissing never waits.

On a phone

Below 640px the Ask button opens a sheet with the field above the keyboard. A suggestion sends on tap. Sending drops the keyboard and the plan takes the sheet: the request with Edit, the fields as rows and the button at the bottom, by the thumb.

With a Command Palette

Both take Cmd+K by default, and the one mounted first wins. On a page with both, set shortcut to false on one, or give it another letter. A command an agent picks with select goes through onCommand, not confirm.

In these docs

The site's search holds Cmd+K, so the demos here take no shortcut. The docs agent has no allow, so the playground starts with confirm off for Run it and Undo to work; switch it on to see the run refused.

API

resolve
agent
(query, { signal }) => AskPlan | null | Promise<…>
Turns the text into a plan, or null. Called after each change to the text, never to run; only the answer for the latest text is kept, and signal aborts once the text moves on, so a fetch can be cancelled. A sync answer lands before paint; a promise marks the plan busy until it settles.
commands
agent
CommandItem[]
Listed when the text resolves to no plan. The Command Palette's item shape.
onCommand
agent
(command, action, details) => void
Runs a picked command with its first action, or the one asked for. details.cancel() refuses it.
filter(command, query) => boolean
Decides which commands match. The default needs every word, then falls back to any word.
limitnumber = 5
How many commands to list.
suggestionsstring[]
Requests to offer as chips.
suggest"focus" | "typing" = "focus"
focus: every chip while the empty field has focus. typing: only chips that contain the text.
valuestring
Controlled text.
defaultValuestring = ""
Starting text.
onValueChange
agent
(value, details) => void
Runs as the text changes, from either side. details.cancel() refuses it.
shortcutstring | false = "k"
The letter that focuses it with Cmd (Mac) or Ctrl. false turns it off.
label
agent
string = "Ask"
Names the bar and its field. The agent finds the bar by it.
confirm
agent
string | false = "Changes many records at once."
Why an agent's run needs a person's approval. false lets an agent run plans unasked. A plan's own confirm overrides it.
disabled
agent
boolean = false
Blocks the field, the shortcut and the agent.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as combobox.

Reads

label

The bar's label.

value

The text in the field.

items

The commands listed, when the text resolved to no plan.

disabled

True when the bar is blocked.

Actions

search("raise weekend rates 10%")

Types the request and returns the plan: title, sentence, fields, touches, call and the button's label, all for the current values. With no plan, returns the matching commands.

select("Rates")

Runs a listed command, with its first action or the one named.

setValue on a field, by its label

Edits a plan value. Fixed values are read-only.

press on the plan's button, by its label

Runs the plan and returns the done message. The button is in the snapshot while a plan shows, labelled for the current values. Needs a person's approval unless confirm is false.

press on "Undo"

Puts the last run back.

The text runs through onValueChange and commands through onCommand; isAgentChange(details) is true for each and details.cancel() refuses it. The plan's button carries confirm, so without an allow on the agent the step is refused and only the person can run it; with one, the person is asked. A label that changes while approval is pending refuses the step. disabled refuses everything.

await agent.act({
  steps: [
    { ref: "Ask", action: "search", args: ["raise weekend rates 10% in December"] },
    { ref: "Change %", action: "setValue", args: ["12"] },
    { ref: "Raise 24 rates by 12%", action: "press" },
  ],
})

Accessibility

  • The dock is a region named by label, ringed while focus is inside it; on a phone the field's pill takes the ring. The field has role combobox; commands and chips are listboxes under one highlight, and aria-activedescendant keeps focus in the field.
  • The plan is a group named by its title. Each value is an input named by its field's label, so the sentence reads in order with its values.
  • One polite live region per bar reads the plan with its button label once the text has been still for 700ms, so plans that only exist mid-word are never read. It then reads the done message, and the undo line. A failed run or undo is an alert.
  • After a run, focus moves to Undo, or back to the field when there is none. After undo it returns to the field. While a run is in flight the field and values are read-only.
  • Show call and Undo have hit areas of at least 24px. Each value is at least 24px wide and one line tall, an inline target within the sentence.
  • On a phone the sheet is a modal dialog (aria-modal) and the rest of the page is hidden from assistive tech while it is open; sending moves focus to the plan so the keyboard drops.