Skip to main content

Search field

A query field with a search icon and a shortcut hint.

<mm-search-field name="q" label="Search the catalog" hide-label shortcut="/" global-shortcut
placeholder="Search assets, columns and glossary terms"></mm-search-field>

Guidance​

Search fields filter or find: a leading search icon, the query, and a keycap hint for the shortcut that focuses them (/ or mod+k, shown as ⌘ K on Apple and Ctrl K elsewhere). With global-shortcut the shortcut works from anywhere on the page, except while someone is typing in another field (unless the shortcut uses the mod key). Enter fires mm-search; Escape clears the query, and a second Escape leaves the field. A clear button appears once there is a query.

Form-associated, so a search field in a GET form submits its query under name, and Enter submits the form unless mm-search is cancelled.

Properties​

PropertyAttributeTypeDefaultDescription
valuevaluestring''The current query. The value attribute is the default the form resets to.
placeholderplaceholderstring''
shortcutshortcutstring''Shortcut shown as keycaps, such as / or mod+k (⌘ K on Apple, Ctrl K elsewhere).
globalShortcutglobal-shortcutbooleanfalseRegisters shortcut on the window so it focuses this field from anywhere on the page.
minlengthminlengthnumber | undefined
maxlengthmaxlengthnumber | undefined
labellabelstring''Field label. Rendered above the control unless hide-label, and always the accessible name.
helphelpstring''Help text under the control.
errorerrorstring''Error text: a sentence saying what to fix. Shows the crimson border, an icon and sets aria-invalid.
hideLabelhide-labelbooleanfalseHides the label visually; it stays the accessible name.
optionaloptionalbooleanfalseMarks the field as optional, in words.
namenamestring''Name submitted with the form.
disableddisabledbooleanfalseDisables the control and excludes it from submission.
requiredrequiredbooleanfalseRequires a value before the form submits.
formHTMLFormElement | null
validityValidityState
validationMessagestring
willValidateboolean
isDisabledbooleanTrue when disabled directly or by a disabled ancestor fieldset.

Methods​

MethodDescription
formResetCallback()Restore the initial value when the owning form resets.

Events​

EventDetailDescription
mm-inputCustomEvent<MmValueDetail>On every edit, and when Escape or the clear button empties the field.
mm-changeCustomEvent<MmValueDetail>When the query is committed (blur after editing, or clearing).
mm-searchCustomEvent<MmValueDetail>On Enter. Cancel it to stop the owning form submitting.
inputInputEventNative, composed; on every edit.
changeEventNative, re-dispatched on the host when the query is committed.

CSS parts​

PartDescription
labelThe label.
controlThe bordered input box.
inputThe native input (role searchbox).
keysThe keycap hint.
helpThe help line.
errorThe error line.

Examples​

Every state of mm-search-field in Storybook, with keyboard and screen reader tests.