Skip to main content

rc-search-bar

Search field/view wrapper for a native <input type="search"> with icon chrome, clear button, suggestions, and debounced events.

Package
@rcarls/rc-search-bar
Element
<rc-search-bar>
Native dependency
Requires a direct child native search input
State model
Controlled or uncontrolled search value and search view
Main events
rc-search-bar-inputrc-search-bar-clearrc-search-bar-togglerc-search-bar-suggestion-select

Installation​

npm install @rcarls/rc-webcomponents@next
import '@rcarls/rc-webcomponents/rc-search-bar/define';

Usage​

Wrap a native <input type="search"> and label it. Wrap the bar in <search> when it is the page's search landmark.

<search>
<label for="q" class="visually-hidden">Search recipes</label>
<rc-search-bar placeholder="Search 51 recipes">
<span slot="leading" aria-hidden="true">🔍</span>
<input type="search" id="q" name="q" />
</rc-search-bar>
</search>

Live demo​

Accessibility​

The component writes no ARIA to the input: type="search" already exposes the searchbox role, and author labels are never overwritten. It adds no landmark of its own. The clear button is a real <button> named by the translatable clear message, and activating it returns focus to the input before the button hides. In a search view (variant="view"), Escape in the input closes the open view.

Theming​

The default demo mode shows the component without a package theme. Use the shared preview controls on this page to compare inherited, light, and dark color schemes or to apply the optional Material theme only inside the demo frames.

Search view​

Set variant="view" to enable the expandable search view. The component keeps the direct child <input type="search"> as the value, label, and form source of truth, while open / default-open control the view panel.

Use the suggestions slot for rich Material-style rows. For simple text suggestions, the component can read the native input's list attribute and render the associated <datalist><option> values when the slot is empty. <datalist> remains a progressive enhancement path: browser styling, high-contrast behavior, and screen-reader announcement vary, so slotted suggestions are recommended when you need rich rows or tighter accessibility control.

<rc-search-bar variant="view">
<input type="search" aria-label="Search recipes" list="recipe-suggestions" />
<datalist id="recipe-suggestions">
<option value="pasta" label="Pasta"></option>
<option value="tomato soup"></option>
</datalist>
</rc-search-bar>

Localization​

Built-in text follows the element's own lang, else <html lang>. Override it with messages, or register it for every instance under rc-search-bar: clear. See Localization.

API​

Properties​

PropertyMarkupTypeDefaultDescription
messagesJS property onlyPartial<RCSearchBarMessages> | undefinedNot specifiedBuilt-in text overrides; unset keys fall back to registered text, then English.
variantvariantRCSearchBarVariant'bar'Presentation mode: docked search bar or expandable search view.
debouncedebouncenumber200Debounce window in ms for `rc-search-bar-input`; `0` dispatches synchronously.
allowNativeClearallow-native-clearbooleanfalseWhen set, leaves the browser's native WebKit cancel button visible.
showClearOnFocusshow-clear-on-focusbooleanfalseWhen set, the clear button is visible whenever the input is focused. Matching the Apple HIG "cancel" pattern. Consider setting `messages.clear` to `Cancel` in this mode.
disableddisabledbooleanNot specifiedDisables the component and mirrors the state to the slotted input.
placeholderplaceholderstring | undefinedNot specifiedPlaceholder mirrored onto the native input. Consumer `placeholder` attribute is always honored.
openopenbooleanNot specifiedWhether the search view panel is open. Host writes are silent.
defaultOpendefault-openbooleanNot specifiedInitial uncontrolled search view open state.
valueJS property onlystringNot specifiedThe current search value. Reads from the native input when present. Host writes are silent (no events) and win over slotted author values.
defaultValuedefault-valuestring | undefinedNot specifiedInitial uncontrolled value hint. Applied once, and only when neither an author `value`, a host `value` write, nor user input owns the value.

Methods​

MethodDescription
showView()Opens the search view and fires `rc-search-bar-toggle` when it changes.
closeView()Closes the search view and fires `rc-search-bar-toggle` when it changes.
toggleView()Toggles the search view and fires `rc-search-bar-toggle` when it changes.
clear()Programmatically clears the value and fires rc-search-bar-clear + rc-search-bar-input.

Events​

EventDetail typeDescription
rc-search-bar-inputCustomEventDebounced after typing, immediate on clear; `detail: { value }`
rc-search-bar-toggleCustomEventWhen user interaction opens or closes the search view; `detail: { open }`
rc-search-bar-suggestion-selectCustomEventWhen a datalist-derived suggestion is activated; `detail: { value, label }`
rc-search-bar-clearCustomEventWhen the clear button is activated

Slots​

NameDescription
(default)The required native `<input type="search">`
leadingDecorative leading icon; mark it `aria-hidden="true"`
trailingOptional trailing content after the clear button
clear-iconOptional glyph replacing the default clear glyph
suggestionsRich search view suggestions; takes precedence over datalist-derived suggestions.

CSS Custom Properties​

PropertyDefaultDescription
--rc-search-bar-border1px solid ButtonBorderWrapper border; set to `none` in M3 theme (uses elevation instead)
--rc-search-bar-shadownoneWrapper box-shadow for elevation; M3 theme sets Level 1 at rest
--rc-search-bar-bgFieldWrapper background
--rc-search-bar-colorFieldTextWrapper text color
--rc-search-bar-icon-colorGrayTextLeading icon color
--rc-search-bar-clear-colorGrayTextClear button glyph color
--rc-search-bar-radiusvar(--rc-control-radius,0.125em)Wrapper border radius
--rc-search-bar-heightvar(--rc-control-block-size,2.5rem)Wrapper block size
--rc-search-bar-padding-inlinevar(--rc-control-padding-inline,0.75rem)Wrapper horizontal padding
--rc-search-bar-gapvar(--rc-control-gap,0.25em)Gap between icon, input, and clear button
--rc-search-bar-input-font-sizeNot specifiedInput font size (inherits when unset)
--rc-search-bar-input-font-familyNot specifiedInput font family (inherits when unset)
--rc-search-bar-input-colorNot specifiedInput text color (inherits when unset)
--rc-search-bar-view-bgvar(--rc-search-bar-bg)Search view panel background
--rc-search-bar-view-colorvar(--rc-search-bar-color)Search view panel text color
--rc-search-bar-view-radiusvar(--rc-search-bar-radius)Search view panel radius
--rc-search-bar-view-shadowvar(--rc-search-bar-shadow)Search view panel shadow
--rc-search-bar-view-bordervar(--rc-search-bar-border)Search view panel border
--rc-search-bar-view-offset0.25remGap between the search bar and the view panel below it
--rc-search-bar-view-padding-block0.5remSearch view suggestion list block padding
--rc-search-bar-suggestion-min-block-size2.75remDatalist-derived suggestion row minimum block size
--rc-search-bar-suggestion-padding-inline1remDatalist-derived suggestion inline padding
--rc-search-bar-suggestion-hover-bgButtonFaceDatalist-derived suggestion hover/focus background

CSS Parts​

PartDescription
rootThe wrapper element
leadingWrapper around the leading icon slot
trailingWrapper around the trailing slot
clearThe clear button
viewSearch view panel
suggestionsSuggestions container
suggestionDatalist-derived suggestion button