OverviewInstallationMigrating to 1.0Themes and layersKanok designCustomizingIconsForms and validationFrameworksUsing with AIButtonText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderModalSide panelTearsheetNotificationToastTooltipDefinition tooltipPopoverToggletipMenuData tableStructured listPaginationContained listTree viewTabsBreadcrumbLinkPagination navContent switcherAccordionProgress indicatorUI shellTagTileCardProduct cardCode snippetCopy buttonUser avatarTruncated textListLoadingInline loadingProgress barSkeletonBadge indicatorStatus indicatorGridHeadingLayerAction setPage headerBig numberEdit in placeIntroductionText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderTabsPagination navContent switcherTooltipDefinition tooltipPopoverMenuAccordionProgress indicatorUI shellModalSide panelTearsheetNotificationToastData tableStructured listPaginationTree viewCode snippetCopy buttonTruncated textLoadingTagCardProduct cardEdit in place
Combobox
A combobox is a text box with a list: people type to find an option instead of scrolling. Use it for long lists, server lookups, and fields that also take free text.
npmshadcn · Web componentshadcn · React + TailwindManualnpminstall@purpurabog/uinpxshadcn@4add@kanok-ui/comboboxnpxshadcn@4add@kanok-ui/tw-combobox# Web component# 1. Install the dependenciesnpminstalllit@^3.3.0# 2. Copy each file's "content" from# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/combobox.json# to its target:# components/ui/primitives/combobox/combobox.styles.ts# components/ui/primitives/combobox/combobox.ts# 3. Do the same for the items it needs (registry/<name>.json):# core-combobox, core-listbox, core-popup, shared-element, shared-events, shared-field, shared-form-control, shared-listbox, shared-localize, shared-position, shared-quiet-focus, shared-slots, tokens# React + Tailwind# 1. Install the dependenciesnpminstall@purpurabog/ui@^2.3.0# 2. Copy each file's "content" from# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-combobox.json# to its target:# components/ui/react-tw/combobox.tsx# 3. Do the same for the items it needs (registry/<name>.json):# core-field, core-listbox, tw-field, tw-i18n, tw-list, tw-theme, tw-utils
npm gives you every component from the package. shadcn copies the source into your app instead: a copy pinned to this version that you can change. The shadcn commands need the @kanok-ui registry in your components.json once, see Installation.
Typing opens the list and highlights the first option whose label contains the text. filter decides whether the other options stay:
filter
The list while typing
none (default, as Carbon)
Every option stays; only the highlight moves
contains
Only options whose label contains the text, anywhere
starts-with
Only options whose label starts with the text
Matching ignores case. When the filter leaves no option, the list shows "No matching items". Type "chiang" in both fields below to compare.
HTMLReactReact + Tailwind<ui-comboboxlabel="Province (filter none)"placeholder="Type a province">
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
<optionvalue="cri">Chiang Rai</option>
<optionvalue="hkt">Phuket</option>
</ui-combobox>
<ui-comboboxlabel="Province (filter starts-with)"placeholder="Type a province"filter="starts-with">
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
<optionvalue="cri">Chiang Rai</option>
<optionvalue="hkt">Phuket</option>
</ui-combobox><UiComboboxlabel="Province (filter none)"placeholder="Type a province">…</UiCombobox>
<UiComboboxlabel="Province (filter starts-with)"placeholder="Type a province"filter="starts-with">…</UiCombobox><Comboboxlabel="Province (filter none)"placeholder="Type a province"items={provinces} />
<Comboboxlabel="Province (filter starts-with)"placeholder="Type a province"items={provinces} filter="starts-with" />
Free text or a listed option
When the user leaves the field, text that exactly matches an option's label picks that option. Other text snaps back to the option chosen before, so the value is always one of the options.
allow-custom-value keeps the typed text instead: it becomes the value and is submitted with the form. Use it when the list is only a suggestion, such as job titles.
HTMLReactReact + Tailwind<ui-comboboxlabel="Job title"name="title"allow-custom-valuehelper-text="Pick a title or type a new one">
<optionvalue="Lawyer">Lawyer</option>
<optionvalue="Paralegal">Paralegal</option>
<optionvalue="Legal secretary">Legal secretary</option>
</ui-combobox><UiComboboxlabel="Job title"name="title"allowCustomValuehelperText="Pick a title or type a new one">
<optionvalue="Lawyer">Lawyer</option>
<optionvalue="Paralegal">Paralegal</option>
<optionvalue="Legal secretary">Legal secretary</option>
</UiCombobox>const titles = [
{ value:'Lawyer', label:'Lawyer' },
{ value:'Paralegal', label:'Paralegal' },
{ value:'Legal secretary', label:'Legal secretary' },
];
<Comboboxlabel="Job title"name="title"items={titles} allowCustomValuehelperText="Pick a title or type a new one" />
Read the value
The committed choice is value, and change fires when it changes. The text the user currently sees is inputValue on the web component. Type, pick, then leave the field to watch both.
In React + Tailwind, onInputChange gets the text on every keystroke.
Search on a server
Listen to the text as the user types, fetch the matches, replace the options, and set loading while the request runs. The list shows "Searching…" meanwhile. Keep filter="none" (the default), as the server has already filtered. In the demo, a short timer stands in for the server.
HTMLReactReact + Tailwind<ui-comboboxid="combobox-company"label="Counterparty"name="company"placeholder="Type a company name"helper-text="Searches the company register as you type"></ui-combobox>
<scripttype="module">
// stands in for fetch(`/api/companies?q=${encodeURIComponent(text)}`).then((response) => response.json())const register = ['Siam Trading Co., Ltd.', 'Siam Logistics PCL', 'Chao Phraya Holdings Co., Ltd.', 'Lanna Foods Co., Ltd.', 'Andaman Resorts Co., Ltd.'];
constsearch= (text) =>new Promise((resolve) =>setTimeout(() =>resolve(register.filter((name) => name.toLowerCase().includes(text.toLowerCase()))), 600),
);
const company = document.getElementById('combobox-company');
company.addEventListener('input', async () => {
const text = company.inputValue;
company.loading =true;
const found =awaitsearch(text);
if (company.inputValue !== text) return; // a newer search is running
company.replaceChildren(...found.map((name) =>newOption(name, name)));
company.loading =false;
});
</script>const [companies, setCompanies] =useState<string[]>([]);
const [loading, setLoading] =useState(false);
constonText=async (text:string) => {
setLoading(true);
setCompanies(awaitsearch(text));
setLoading(false);
};
<UiComboboxlabel="Counterparty"name="company"placeholder="Type a company name"loading={loading} onInput={(e) =>onText((e.target asComboboxElement).inputValue)}>
{companies.map((name) => <optionkey={name} value={name}>{name}</option>)}
</UiCombobox><Comboboxlabel="Counterparty"name="company"placeholder="Type a company name"items={companies.map((name) => ({ value: name, label: name }))} loading={loading} onInputChange={onText} />
companies, loading, onText and search are the same in every React version.
Groups and descriptions
As in Dropdown: <optgroup label> groups options, data-description adds a second line, and disabled options are shown but can't be picked. React + Tailwind takes groups { label, options } and description in items. Typing only searches the labels, never the descriptions.
size: sm 32px, md 40px (the default), lg 48px. direction="top" opens the list above when there is room.
HTMLReactReact + Tailwind<ui-comboboxlabel="Small"size="sm"placeholder="Type a province">
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
</ui-combobox>
<ui-comboboxlabel="Medium"placeholder="Type a province">
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
</ui-combobox>
<ui-comboboxlabel="Large, opens above"size="lg"direction="top"placeholder="Type a province">
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
</ui-combobox><UiComboboxlabel="Small"size="sm"placeholder="Type a province">…</UiCombobox>
<UiComboboxlabel="Medium"placeholder="Type a province">…</UiCombobox>
<UiComboboxlabel="Large, opens above"size="lg"direction="top"placeholder="Type a province">…</UiCombobox><Comboboxlabel="Small"items={provinces} size="sm"placeholder="Type a province" />
<Comboboxlabel="Medium"items={provinces} placeholder="Type a province" />
<Comboboxlabel="Large, opens above"items={provinces} size="lg"direction="top"placeholder="Type a province" />
Fluid
fluid puts the label inside a 64px box (Carbon's FluidComboBox); the open list keeps the usual row height. A fluid form around the field does the same; see fluid forms.
HTMLReactReact + Tailwind<ui-comboboxfluidlabel="Province"name="province"placeholder="Type a province"filter="contains">
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
<optionvalue="hkt">Phuket</option>
</ui-combobox><UiComboboxfluidlabel="Province"name="province"placeholder="Type a province"filter="contains">…</UiCombobox><Comboboxfluidlabel="Province"name="province"placeholder="Type a province"filter="contains"items={provinces} />
Outlined (Kanok design)
In the Kanok design, fields are filled by default: a soft grey fill, no edge. Put data-field-variant="outlined" on an element around the field (a <form>, or <html> for the whole app) for a 1px edge on no fill; focus is still the soft blue halo. The attribute does nothing in the Carbon design, so the example below sits inside data-design="kanok". See Kanok design for the details.
One line shows under the field at a time: an error beats a warning, and a warning beats the helper text. warn doesn't block the form; invalid forces the error.
HTMLReactReact + Tailwind<ui-comboboxlabel="Counterparty"name="party"placeholder="Type to search"helper-text="Type part of the name, then pick with ↑ ↓">
<optionvalue="1">A Company Limited</option>
<optionvalue="2">B Holdings PCL</option>
</ui-combobox>
<ui-comboboxlabel="Counterparty"name="party2"value="2"warnwarn-text="This company's certificate expires this month">
<optionvalue="1">A Company Limited</option>
<optionvalue="2">B Holdings PCL</option>
</ui-combobox>
<ui-comboboxlabel="Counterparty"name="party3"value="1"invalidinvalid-text="This company is already a party to the contract">
<optionvalue="1">A Company Limited</option>
<optionvalue="2">B Holdings PCL</option>
</ui-combobox><UiComboboxlabel="Counterparty"name="party"placeholder="Type to search"helperText="Type part of the name, then pick with ↑ ↓">…</UiCombobox>
<UiComboboxlabel="Counterparty"name="party2"value="2"warnwarnText="This company's certificate expires this month">…</UiCombobox>
<UiComboboxlabel="Counterparty"name="party3"value="1"invalidinvalidText="This company is already a party to the contract">…</UiCombobox><Comboboxlabel="Counterparty"name="party"items={parties} placeholder="Type to search"helperText="Type part of the name, then pick with ↑ ↓" />
<Comboboxlabel="Counterparty"name="party2"items={parties} defaultValue="2"warnwarnText="This company's certificate expires this month" />
<Comboboxlabel="Counterparty"name="party3"items={parties} defaultValue="1"invalidinvalidText="This company is already a party to the contract" />
Read-only and disabled
readonly (readOnly in React + Tailwind) shows and submits the value but blocks changes. disabled greys the field out, and it isn't submitted.
HTMLReactReact + Tailwind<ui-comboboxlabel="Province"value="bkk"readonly>
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
</ui-combobox>
<ui-comboboxlabel="Province"placeholder="Type a province"disabled>
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
</ui-combobox><UiComboboxlabel="Province"value="bkk"readonly>…</UiCombobox>
<UiComboboxlabel="Province"placeholder="Type a province"disabled>…</UiCombobox><Comboboxlabel="Province"items={provinces} defaultValue="bkk"readOnly />
<Comboboxlabel="Province"items={provinces} placeholder="Type a province"disabled />
Required in a form
required blocks the form until a value is committed. The error waits until the user leaves the field or submits. Press Submit with the field empty to see it.
Submitted:
HTMLReactReact + Tailwind<formid="combobox-form"style="display: grid; gap: 16px">
<ui-comboboxlabel="Province of the property"name="province"placeholder="Type a province"filter="contains"requiredinvalid-text="Choose a province from the list">
<optionvalue="bkk">Bangkok</option>
<optionvalue="cnx">Chiang Mai</option>
<optionvalue="hkt">Phuket</option>
</ui-combobox>
<div><ui-buttontype="submit">Submit</ui-button></div>
</form>
<p>Submitted: <outputid="combobox-form-out"></output></p>
<scripttype="module">
const form = document.getElementById('combobox-form');
const out = document.getElementById('combobox-form-out');
form.addEventListener('submit', (event) => {
event.preventDefault();
out.value = [...newFormData(form)].map(([key, value]) =>`${key}=${value}`).join(', ');
});
</script><formonSubmit={submit}>
<UiComboboxlabel="Province of the property"name="province"placeholder="Type a province"filter="contains"requiredinvalidText="Choose a province from the list">…</UiCombobox>
<UiButtontype="submit">Submit</UiButton>
</form>
<p>Submitted: <output>{sent}</output></p>// validation is your app's: `tried` is set on the first submit
<formonSubmit={submit}>
<Comboboxlabel="Province of the property"name="province"placeholder="Type a province"filter="contains"items={provinces} value={province} onChange={setProvince} requiredinvalid={tried &&!province} invalidText="Choose a province from the list" />
<Buttontype="submit">Submit</Button>
</form>
<p>Submitted: <output>{sent}</output></p>
submit prevents the navigation and puts new FormData(form) into sent, as in the HTML. More: Forms and validation.
Keyboard
Key
Does
Typing
Opens the list and highlights the first option containing the text
↓ / ↑
Opens the list on the chosen option (else the first / last); then moves the highlight (no wrap)
Alt+↓
Opens the list without a highlight
Alt+↑
Picks the highlighted option and closes
Enter
Picks the highlighted option. With no highlight, picks the first option containing the text (or keeps the text with allow-custom-value)
Esc
Closes the list; on a closed list, clears the field
Tab
Moves on without picking the highlight. The text is committed or snaps back, as described above
Accessibility
It follows the WAI-ARIA combobox pattern with list autocomplete: the text box has role="combobox", the list is a listbox, and the highlight is announced through aria-activedescendant.
The × and the chevron are not Tab stops (as in Carbon), so Tab goes straight to the next field. Esc clears from the keyboard.
"Searching…" and "No matching items" are announced to screen readers.
Generated from the package. In React, pass a property as a prop of the same name; events are props too (the React column).
<ui-combobox> · React UiCombobox
Attributes
Attribute
Property
Type
Default
Description
label
label
string
''
Visible label text; the label slot replaces it when markup is needed.
hide-label
hideLabel
boolean
false
Keep the label for screen readers but don't show it.
helper-text
helperText
string
''
Helper text under the field, shown while there is no error or warning.
invalid-text
invalidText
string
''
Error text shown while invalid or while the field fails validation.
invalid
invalid
boolean
false
Forces the error state, e.g. after server-side validation.
warn
warn
boolean
false
Non-blocking warning; shown when there is no error.
warn-text
warnText
string
''
Text of the warning shown while warn.
placeholder
placeholder
string
''
value
value
string
''
Committed value: the chosen option's value, or free text with allow-custom-value.
name
name
string
''
Reflected: form-associated elements submit under the name attribute.
size
size
'sm' | 'md' | 'lg'
'md'
Field height: sm 32px, md 40px, lg 48px (tokens --ui-field-height-*).
direction
direction
'bottom' | 'top'
'bottom'
Preferred side for the list; it flips when there is no room.
filter
filter
ComboboxFilter
'none'
allow-custom-value
allowCustomValue
boolean
false
Keep text that matches no option as the value instead of snapping back.
hide-clear
hideClear
boolean
false
No × button (Carbon always shows one while there is text); Escape on a closed list still clears.
loading
loading
boolean
false
Shows a "Searching…" row while a server lookup is running.
disabled
disabled
boolean
false
Disables the control; a disabled <fieldset> ancestor does the same.
required
required
boolean
false
Blocks form submission while the field is empty.
readonly
readonly
boolean
false
Shows and submits the value but blocks changes.
fluid
fluid
boolean
false
Carbon FluidComboBox: the label inside a 64px box, an error / warning inside it. <ui-fluid-form> sets it for every field inside.
Properties
Property
Type
Description
inputValue
What the user currently sees in the text box.
willValidate
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively.
labels
(read-only) The <label>s that point at this field.
Methods
Method
Description
setCustomValidity(message)
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''.
Events
Event
React
Description
ui-toggle
onUiToggle
detail: { open } after the list opens or closes
input
onInput
Whenever value changes, as ui-dropdown and ui-multi-select; also native and composed as the user types, where value is unchanged (kept for compatibility: listen to ui-input for the typing)
change
onChange
When the committed value changes
ui-input
onUiInput
detail: { value } the typed text (inputValue), as the user types
Slots
Slot
Description
label
Label content with markup; the label attribute is the plain-text fallback
helper-text
Helper text with markup (links, bold); overrides helper-text
warn-text
Warning text shown while warn; overrides warn-text
invalid-text
Error text shown while invalid; overrides invalid-text
CSS parts
Part
Description
option-description
Second line of an option (<option data-description>)
base
The field box
label
The label
input
The text box (role=combobox)
clear-button
The × button
toggle
The chevron button
status-icon
The error / warning icon
helper-text
The helper line
warning-text
The warning line
warn-text
Same line as warning-text (named after the warn-text attribute / slot)
error-text
The error line
invalid-text
Same line as error-text (named after the invalid-text attribute / slot)
menu
The floating list
option
Each row
group-label
An <optgroup> heading
empty
The "searching" / "no results" row
CSS custom properties
Custom property
Default
Description
--ui-field-border
Border on every side of the box (default none: Carbon's underline only; Kanok: a 1px outline)
--ui-field-disabled-border-color
Border colour when disabled (default: the underline transparent)
--ui-field-readonly-border-color
Border colour when read-only (default: a hairline underline)
--ui-field-radius
Corner radius of the box (default 0; Kanok: 6px)
--ui-field-background
Fill of the box (default --ui-surface-1; Kanok: transparent)
--ui-label-font
Font of the label (default --ui-font-caption; Kanok: 14px semibold)
--ui-label-color
Colour of the label (default --ui-ink-muted; Kanok: --ui-ink)
--ui-menu-radius
Corner radius of the open list (default 0; Kanok: 8px)
--ui-menu-border
Border around the list (default none; Kanok: a hairline)
--ui-menu-shadow
box-shadow of the list (default Carbon's 0 2px 6px)
--ui-menu-padding
Padding of the open list (default 0; Kanok: 4px, rows inset)
--ui-menu-item-radius
Corner radius of a row (default 0; Kanok: 6px)
--ui-menu-item-ink
Text of a row at rest (default --ui-ink-muted; Kanok: --ui-ink)
--ui-menu-divider
Rule between rows (default --ui-row-divider; Kanok: none)
--ui-menu-item-selected
Fill of the chosen row (default --ui-surface-2; Kanok: --ui-highlight)
--ui-menu-item-selected-hover
The chosen row under the pointer (default --ui-layer-selected-hover; Kanok: the highlight a shade deeper)
--ui-field-readonly-background
The box's fill when read-only (default transparent; Kanok: a faint ink tint, so read-only reads as set and never weaker than disabled)
--ui-fluid-background
/ --ui-fluid-gap - A fluid box's fill and the space under it (default the layer's fill, 0: boxes that touch; Kanok: transparent, 8px)
--ui-menu-item-active-outline
/ --ui-menu-item-active-background - The keyboard-highlighted row: its ring and its fill (default focus, transparent)
--ui-icon-control-radius
Corners of its small icon buttons, their hover fill and ring (default 0; Kanok: round)
--ui-menu-item-pressed
Fill of a pressed option (default --ui-surface-2; Kanok: the 20% pressed tint)