CamoSearchField
Concept
1. Why
Search is a text field with its own conventions:
- a search icon at the start;
- a clear button that appears when there's text;
- the keyboard's "Search" action;
- often a pill shape in a top bar.
Every app rebuilds this from a text field. CamoSearchField packages it, reusing CamoTextField's editable core. Suggestions and results stay the app's content, shown below the field or in a CamoMenu.
2. Shape
Material mapping preview: the M3 search bar.
- 56 tall,
surfaceContainerHigh,shapes.full. - The leading search icon and the placeholder are
onSurfaceVariant. - The trailing slot holds an avatar or an action icon.
3. API
Contract
fun CamoSearchField(
query: String,
onQueryChange: (String) -> Unit,
onSearch: (String) -> Unit, // the keyboard's Search action / Enter
placeholder: String = "Search",
leading: Widget? = null, // null → the skin's search icon; pass e.g. a back CamoFieldAction when active
trailing: Widget? = null, // e.g. a mic or avatar; the clear button is automatic
enabled: Boolean = true,
onFocusChanged: ((Boolean) -> Unit)? = null,
)
- The clear button appears when
queryisn't empty. It callsonQueryChange("")and keeps focus. - Debouncing is app-side. Your convention of 500 ms and cancelling in-flight requests belongs to the notifier/ViewModel, not the field.
State → renderer: SearchFieldState(query, placeholder, enabled, focused, isEmpty). Slots: SearchFieldSlots(leading?, trailing?, clear?, input). The input is core's editable node, as in CamoTextField.
Tokens used
colors.surfaceContainerHigh,onSurface,onSurfaceVariantshapes.full,typography.bodyLarge- icon size 24
Component theme
theme.components.searchField: SearchFieldTheme?. Every field is nullable in the theme. The skin's defaults(theme) fills whatever isn't set (Material defaults shown). See Theme §3.10.
| Field | Type | Material default | What it's for |
|---|---|---|---|
height | Dp | 56 | field height |
shape | Shape | shapes.full | container corners |
textStyle / placeholderStyle | CamoTextStyle | bodyLarge | input and placeholder |
iconSize | Dp | 24 | leading, trailing and clear icons |
searchIcon / clearIcon | Icon | the skin's magnifier / × | the leading icon and the clear action's icon (added in the M5 build) |
colors | SearchFieldColors(container, text, placeholder, icon) | surfaceContainerHigh, onSurface, onSurfaceVariant, onSurfaceVariant | every color it uses |
Accessibility
- A text field with a search role/hint and name =
placeholder. The IME action is "Search". - The clear button is a
CamoFieldActionlabelled "Clear search" (no container, so the field keeps its height; see the text field note). - Results that update while typing should be announced by the app ("12 results") through a polite live region. The docs show the pattern.
4. Build steps
CamoSearchFieldon top of the text-field input engine (IME action, clear logic, semantics).SearchFieldRenderer, and the DebugSkin renderer.- Sample: a country search with app-side 500 ms debounce, results in a list below, and a clear button.
- Tests:
- typing calls
onQueryChange; - the IME Search action calls
onSearch; - clear resets and keeps focus;
- the placeholder is the accessible name.
- typing calls
Done when
- The search sample works with software and hardware keyboards on every platform.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Expanded full-screen search (M3 "search view") | Not in v1. Compose: navigate to a search route (Navigation 3). Flutter: push a page. The field is reused inside it. |
| Suggestions dropdown | App content, either below the field or in a CamoMenu anchored to it. |
| Voice search | The app passes a mic CamoFieldAction as trailing, so the field keeps its height. |
Implementation
1. Why
A search field is a text field with a search keyboard action, an automatic clear button and a search role. Kotlin reuses core's editable node from CamoTextField (BasicTextField with Compose's TextFieldState), and adds the IME action and the clear button.
2. Shape
3. API
@Composable
public fun CamoSearchField(
query: String,
onQueryChange: (String) -> Unit,
onSearch: (String) -> Unit,
modifier: Modifier = Modifier,
placeholder: String = "Search",
leading: (@Composable () -> Unit)? = null, // null → the skin's search icon
trailing: (@Composable () -> Unit)? = null,
enabled: Boolean = true,
onFocusChanged: ((Boolean) -> Unit)? = null,
interactionSource: MutableInteractionSource? = null,
)
- Controlled text: the same two-way sync as
CamoTextField: an internalTextFieldStatemirrorsquery; edits callonQueryChange; external changes toqueryreplace the text and keep the cursor at the end. - IME:
KeyboardOptions(imeAction = ImeAction.Search, keyboardType = KeyboardType.Text, autoCorrectEnabled = false)andonKeyboardAction { onSearch(query) }. On desktop and web, Enter does the same (onKeyEvent). - Clear: core builds a
CamoFieldAction(contentDescription = "Clear search")(no container, so the field keeps its height) shown whenquery.isNotEmpty(); it callsonQueryChange("")and keeps focus in the field. - Semantics:
semantics { contentDescription = placeholder }when there's no visible label; Compose exposes the field as an editable text; the clear button is a separate focusable node after the input. - Debounce is not in the field: apps (or
CamoAsyncSelect) debounceonQueryChangethemselves.
4. Build steps
SearchFieldTheme/Style,CamoSearchField, the DebugSkin renderer.commonTest: the search action firesonSearch; the clear button appears only with text, clears and keeps focus; externalquerychanges update the field; Esc (desktop) clears when non-empty.
Done when
- A search screen with a back button as
leadingsearches on the keyboard action on Android and iOS, and on Enter on desktop and web.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Esc on desktop | Clears the query when non-empty; otherwise it's passed on (so a dialog or a search overlay can close). |
| IME composition (Chinese, Japanese) | onQueryChange receives committed and composing text (Compose's behavior); onSearch isn't called during composition. |