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-toggle

Installation

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

Live demo

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>

API

Properties

PropertyMarkupTypeDefaultDescription
variantvariantRCSearchBarVariant'bar'Presentation mode: docked search bar or expandable search view.
debouncedebouncenumber200Debounce window in ms for `rc-search-bar-input`; `0` dispatches synchronously.
clearLabelclear-labelstring'Clear search'Accessible label for the clear button.
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 `clear-label="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