Skip to main content

Focus Shortcut

Lets the slash key (/) move focus into one field from anywhere on the page, and shows a / hint where you place it. It wraps any focusable field: Input, InputCapture, TextArea, Combobox.Input, or Query.Input.

Desktop viewport
Tablet viewport
Mobile viewport

When to use

  • Let / focus the one primary filter or text box on a page, and show a / hint beside it.
  • Prefer Kbd to show a key in copy or a tooltip, with no shortcut behind it.
  • Prefer Command for a ⌘K palette that runs an action or navigates.

Query

Wrap Query.Input in FocusShortcut.Field, and pass FocusShortcut.Hint as a child of Query.Input. The hint then sits at the field's right edge, before any inline actions.

Desktop viewport
Tablet viewport
Mobile viewport

projectFilterGrammar and projects are the ones from the Query page.

Combobox

Combobox.Input renders its own chrome and takes no children, so you place the hint. FocusShortcut.Root renders no element, so it can hold a wrapper that positions the hint over the field.

Desktop viewport
Tablet viewport
Mobile viewport

Composition

FocusShortcut.Root
├── FocusShortcut.Field
└── FocusShortcut.Hint

Polymorphism

FocusShortcut.Hint takes asChild. Use it to draw the hint with your own element, such as a muted <span> in a dense toolbar. The hint keeps aria-hidden, translate="no", and the hidden attribute that shows and hides it.

FocusShortcut.Field always merges onto its one child, so it needs no asChild.

Inside Field.Control, put FocusShortcut.Root outside the control and FocusShortcut.Field inside it, around the field. The id, name, and ARIA that Field.Item owns then reach the field, and the field still names / in its aria-keyshortcuts.

Behavior

  • / still types a slash while focus is in any text control, including one inside an open shadow root. On a checkbox, a radio, or a button, / moves focus to the field.
  • / does nothing with Meta, Ctrl, or Alt held, while an IME composes, or on a key repeat.
  • A field that is disabled (natively, by a fieldset, or with aria-disabled on it or a group around it), not displayed, or behind a modal (inert or aria-hidden="true" on an ancestor) does not take the key. The key then goes to the next field with the shortcut.
  • When two fields have the shortcut, / goes to the field that mounted first and can take focus. Give the shortcut to one field per page.
  • The hint follows typing, focus, disabled, and a controlled value. When your code sets the field's value property directly, dispatch an input event so the hint updates.
  • To let a user turn the shortcut off, such as from a keyboard-shortcuts setting, render the field without FocusShortcut.Root while the setting is off.
  • A listener that calls preventDefault() on the key first keeps the key. FocusShortcut.Field calls preventDefault() when it takes the key, so the slash does not type into the field.

Accessibility

  • FocusShortcut.Field adds / to the field's aria-keyshortcuts, and keeps the shortcuts you set on its direct child. Put FocusShortcut.Field directly around the element that takes focus, such as the InputCapture inside an Input, and set other shortcuts on that element.
  • FocusShortcut.Hint is aria-hidden, because aria-keyshortcuts names the key to assistive tech.
  • A device that cannot hover, such as a phone, usually has no key to press, so the hint does not show there. The shortcut stays.

API Reference

FocusShortcut.Root

Connects one FocusShortcut.Field to its FocusShortcut.Hint. It renders no element.

PropTypeDefaultDescription
childrenReactNodenoneOne FocusShortcut.Field and, optionally, one FocusShortcut.Hint.

FocusShortcut.Field

Makes / move focus into its child. It renders no element of its own, and merges its props onto the child. The child's ref must reach the element that takes focus.

PropTypeDefaultDescription
childrenReactElementnoneThe one field that / focuses, such as InputCapture, TextArea, Combobox.Input, or Query.Input. Required.
Data AttributeValueDescription
data-slot"focus-shortcut-field"Joined onto the child's own data-slot chain.
aria-keyshortcuts"/"Added to the child's own aria-keyshortcuts, which stay first.

FocusShortcut.Hint

The / key, rendered as a Kbd. It accepts every kbd prop except children, hidden, and translate.

PropTypeDefaultDescription
asChildbooleanfalseRender your own element in place of the Kbd, and merge the props onto it.
classNamestringnoneClasses for the hint, such as its position.
Data AttributeValueDescription
data-slot"focus-shortcut-hint kbd"On the <kbd>. Kbd adds kbd to the chain.
hiddenpresentWhile the field has focus, has a value, or is disabled, and on the server. Style the shown state with not-[hidden]:.