IconPicker
Opens a panel for choosing an icon. The bound value is the icon name used by <HuiIcon name>, making it suitable for menu or category form fields. Search by name at the top of the panel. Built-in icons from @nscode/icons are grouped below, or you can restrict selection to a supplied list.
Import
import { HuiIconPicker } from '@nscode/handy-ui'
Basic Usage
Bind the icon name with v-model. The trigger shows placeholder text when empty, then the selected icon and its name.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIcon, HuiIconPicker, HuiSpace } from '@nscode/handy-ui'
const icon = ref<string | null>('')
</script>
<template>
<HuiSpace wrap>
<HuiIconPicker v-model="icon" style="width: 240px" />
<HuiIcon v-if="icon" :name="icon" />
</HuiSpace>
</template>
Built-in icons have seven groups: Direction and Navigation, Actions, Status, Users and General, Files, Media, and Editing, Cropping and Conversation. These match the icon list. Icons within each group are sorted by name. Hovering shows the name, and the selected icon is highlighted.
Internal and Controlled Values
Without modelValue, the component stores selection internally starting from an empty string; clearing sets it to null. Listening only to update:modelValue also uses internal state. Providing the prop makes it controlled, with explicit undefined or null displayed as empty. After a selection or clear request, the actual display follows the value accepted by the parent. If the parent rejects the update, the icon and panel selection remain unchanged.
Compare an omitted prop, update listener only, explicit empty value, rejected parent updates and two-way binding below. After selecting, click Parent redraw: omitted-prop and listener-only values remain intact. Removing a supplied model prop restores the earlier internal value. Dynamically switching control modes produces a development diagnostic and is not recommended for routine use.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiIconPicker, HuiSpace } from '@nscode/handy-ui'
const stateIcons = ['home', 'mail', 'search']
const stateBound = ref<string | null>('home')
const stateRedraw = ref(0)
const stateLast = ref('None yet')
const recordState = (value: string | null) => {
stateLast.value = value || 'Empty value null'
}
</script>
<template>
<HuiSpace class="icon-picker-q1-demo" direction="vertical" fill :data-parent="stateRedraw">
<HuiSpace wrap>
<HuiButton class="icon-redraw" @click="stateRedraw++"
>Parent redraw {{ stateRedraw }}</HuiButton
>
<span
>Omitted values use internal state; supplied values let the parent decide the actual
icon.</span
>
</HuiSpace>
<HuiSpace class="icon-local" wrap>
<span>Model prop omitted</span>
<HuiIconPicker :icons="stateIcons" clearable style="width: 220px" />
</HuiSpace>
<HuiSpace class="icon-listener" wrap>
<span>Update listener only</span>
<HuiIconPicker
:icons="stateIcons"
clearable
style="width: 220px"
@update:model-value="recordState"
/>
</HuiSpace>
<HuiSpace class="icon-empty" wrap>
<span>Explicit undefined</span>
<HuiIconPicker :model-value="undefined" :icons="stateIcons" clearable style="width: 220px" />
</HuiSpace>
<HuiSpace class="icon-fixed" wrap>
<span>Fixed home; parent rejects updates</span>
<HuiIconPicker model-value="home" :icons="stateIcons" clearable style="width: 220px" />
</HuiSpace>
<HuiSpace class="icon-bound" wrap>
<span>Two-way binding</span>
<HuiIconPicker v-model="stateBound" :icons="stateIcons" clearable style="width: 220px" />
</HuiSpace>
<span class="icon-status"
>Observed request: {{ stateLast }}; bound icon: {{ stateBound || 'Empty value null' }}</span
>
</HuiSpace>
</template>
Search
Opening the panel focuses the search input. Typing filters icon names without regard to case, ignoring surrounding spaces. Groups with no matches are hidden. If no icons match, emptyText is displayed. Press Enter after entering a query to select the first matching icon.
Icon names use English, such as arrow, user and file. The search field is empty each time the panel opens.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIconPicker } from '@nscode/handy-ui'
const icon = ref<string | null>('')
</script>
<template>
<HuiIconPicker v-model="icon" placeholder="Open and type arrow to search" style="width: 240px" />
</template>
Clearable
Enable clearable to show a clear button on the trigger when it has a value. The button is hidden when empty or disabled. Clearing changes the value to null. The clear button is not reachable with Tab; with the picker focused, press Backspace or Delete to clear it with the same events. These keys do nothing without clearable or a value.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIconPicker } from '@nscode/handy-ui'
const icon = ref<string | null>('user')
</script>
<template>
<HuiIconPicker v-model="icon" clearable style="width: 240px" />
<p style="margin-top: 12px">Bound value: {{ JSON.stringify(icon) }}</p>
</template>
Restricting Available Icons
Pass icon names through icons to show only those icons. They appear in the supplied order without groups. Duplicate names appear only once.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIconPicker } from '@nscode/handy-ui'
const icon = ref<string | null>('user')
const menuIcons = ['home', 'user', 'users', 'setting', 'table', 'file', 'folder', 'bell', 'mail']
</script>
<template>
<HuiIconPicker v-model="icon" :icons="menuIcons" style="width: 240px" />
</template>
With an empty array, no icons are available and the panel displays emptyText:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIconPicker } from '@nscode/handy-ui'
const icon = ref<string | null>('')
</script>
<template>
<HuiIconPicker
v-model="icon"
:icons="[]"
empty-text="No icons available yet"
style="width: 240px"
/>
</template>
Custom Icons
icons can include names outside @nscode/icons. Draw those icons with the icon slot. The slot is used for each panel cell and for the selected icon in the trigger. Its scope argument, icon, is the icon name.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIcon, HuiIconPicker } from '@nscode/handy-ui'
const icon = ref('brand-cube')
const custom: Record<string, string> = {
'brand-cube': 'M12 2 3 7v10l9 5 9-5V7l-9-5zm0 2.3L18.6 8 12 11.7 5.4 8 12 4.3z'
}
</script>
<template>
<HuiIconPicker v-model="icon" :icons="['brand-cube', 'home', 'star']">
<template #icon="{ icon }">
<HuiIcon :name="icon">
<svg v-if="custom[icon]" viewBox="0 0 24 24" fill="currentColor">
<path :d="custom[icon]" />
</svg>
</HuiIcon>
</template>
</HuiIconPicker>
</template>
Slot icons follow the cell size. Icons using 1em, such as HuiIcon, scale automatically without an explicit size.
Custom Text
All built-in text can be overridden through props:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIconPicker } from '@nscode/handy-ui'
const icon = ref<string | null>('')
</script>
<template>
<HuiIconPicker
v-model="icon"
placeholder="Choose a menu icon"
filter-placeholder="Enter an English name, such as arrow"
empty-text="No matching icon"
:group-labels="{ navigation: 'Navigation', user: 'Users' }"
/>
</template>
groupLabels overrides only the supplied group names; others retain their localized headings. Use groupLabels.editor for the Editing, Cropping and Conversation group. An explicit empty string is preserved; omitted values use the active locale. See IconPickerGroupName under Types for the group keys.
Sizes
size accepts large, default or small. Trigger height matches Select of the same size, and the panel scales accordingly:
| Size | Trigger Height | Font Size | Radius | Arrow and Selected Icon | Panel Cell | Cell Icon | Cell Radius | Panel Padding |
|---|---|---|---|---|---|---|---|---|
| large | 40px | 16px | 8px | 18px | 40px | 20px | 8px | 12px |
| default | 32px | 14px | 6px | 16px | 32px | 18px | 6px | 8px |
| small | 24px | 12px | 4px | 14px | 24px | 16px | 4px | 8px |
Cells have a 4px gap, with eight per row. The search input uses the same size. When omitted, size follows HuiFormItem, then HuiForm, then HuiConfigProvider, and finally default.
Each row compares a clearable icon picker, Select and Button of the same size:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiIconPicker, HuiSelect, HuiSpace } from '@nscode/handy-ui'
const icon = ref<string | null>('home')
const value = ref<string | null>('home')
const options = [
{ label: 'Home', value: 'home' },
{ label: 'Settings', value: 'setting' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiIconPicker v-model="icon" size="large" clearable style="width: 200px" />
<HuiSelect v-model="value" size="large" :options="options" style="width: 160px" />
<HuiButton size="large">Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiIconPicker v-model="icon" clearable style="width: 200px" />
<HuiSelect v-model="value" :options="options" style="width: 160px" />
<HuiButton>Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiIconPicker v-model="icon" size="small" clearable style="width: 200px" />
<HuiSelect v-model="value" size="small" :options="options" style="width: 160px" />
<HuiButton size="small">Button</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Cell Size Variable
A public variable sets the side length of the panel cells for all three sizes. The panel width follows it:
| Variable | Default | Description |
|---|---|---|
--hui-icon-picker-cell-size | 40/32/24px for large/default/small | Panel cell side length |
The panel is mounted under body, so set the variable on :root, body or the panel itself (.hui-icon-picker__dropdown). Setting it on a container of the picker does not reach the panel.
:root {
--hui-icon-picker-cell-size: 36px;
}
Disabled
A disabled picker cannot receive focus or open the panel, and its clear button is hidden. It follows the enclosing HuiForm's disabled state. Below are all three sizes, including pickers with no selected icon:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIconPicker, HuiSpace } from '@nscode/handy-ui'
const icon = ref<string | null>('home')
const empty = ref<string | null>('')
</script>
<template>
<HuiSpace wrap>
<HuiIconPicker v-model="icon" size="large" disabled clearable style="width: 180px" />
<HuiIconPicker v-model="icon" disabled clearable style="width: 180px" />
<HuiIconPicker v-model="icon" size="small" disabled clearable style="width: 180px" />
<HuiIconPicker v-model="empty" disabled style="width: 180px" />
</HuiSpace>
</template>
Within a Form
Inside HuiFormItem, the label is associated with the picker through aria-labelledby. Value changes trigger change rules, and closing the panel triggers blur rules. Set validateEvent to false to disable both validation triggers.
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiIconPicker } from '@nscode/handy-ui'
const form = reactive<{ icon: string | null }>({ icon: '' })
const rules = {
icon: [{ required: true, message: 'Choose a menu icon', trigger: 'change' }]
}
</script>
<template>
<HuiForm :model="form" :rules="rules">
<HuiFormItem label="Menu icon" prop="icon">
<HuiIconPicker v-model="form.icon" clearable />
</HuiFormItem>
</HuiForm>
</template>
Events
Selecting a panel icon, including the current icon, emits update:modelValue → change. Clearing emits update:modelValue(null) → clear → change(null). Opening or closing emits visible-change. focus and blur represent entering or leaving the entire picker; moving among the trigger, search input and icon cells does not repeat them. Recent events appear below:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIconPicker, HuiSpace } from '@nscode/handy-ui'
const icon = ref<string | null>('home')
const logs = ref<string[]>([])
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onChange = (value: string | null) => log(`change:${JSON.stringify(value)}`)
const onVisibleChange = (visible: boolean) => log(`visible-change:${visible}`)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiIconPicker
v-model="icon"
clearable
style="width: 240px"
@change="onChange"
@clear="log('clear')"
@visible-change="onVisibleChange"
@focus="log('focus')"
@blur="log('blur')"
/>
<div class="logs">
<div v-if="logs.length === 0">No events yet</div>
<div v-for="(item, index) in logs" :key="index">{{ item }}</div>
</div>
</HuiSpace>
</template>
<style scoped>
.logs {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Calling Methods
Call focus(), blur() and clear() through a ref. focus() focuses the trigger without opening the panel. blur() blurs the actual focus within the picker, without affecting external focus or panel state. clear() does not depend on clearable and emits the same events as the clear button. It has no effect when disabled, under the form's read-only fallback or after unmounting.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiIconPicker, HuiSpace } from '@nscode/handy-ui'
import type { IconPickerExpose } from '@nscode/handy-ui'
const pickerRef = ref<IconPickerExpose>()
const icon = ref<string | null>('home')
const disabled = ref(false)
const events = ref<string[]>([])
const log = (value: string) => {
events.value = [...events.value, value].slice(-8)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiIconPicker
ref="pickerRef"
v-model="icon"
:disabled="disabled"
:icons="['home', 'mail', 'search']"
style="width: 240px"
@focus="log('focus')"
@blur="log('blur')"
@clear="log('clear')"
/>
<HuiSpace wrap>
<HuiButton @click="pickerRef?.focus()">Focus (focus)</HuiButton>
<HuiButton @mousedown.prevent @click="pickerRef?.blur()">Blur (blur)</HuiButton>
<HuiButton @click="pickerRef?.clear()">Clear (clear)</HuiButton>
<HuiButton @click="disabled = !disabled">Disabled: {{ disabled }}</HuiButton>
</HuiSpace>
<span>Current value: {{ JSON.stringify(icon) }}; events: {{ events.join(' → ') }}</span>
</HuiSpace>
</template>
Keyboard Interaction
Use Tab to focus the picker. Once focused:
| Key | Action |
|---|---|
| Enter / Space | Opens the panel and focuses search; pressing again closes it |
| ↓ / ↑ | Opens the panel and focuses search |
| Esc | Closes an open panel |
| Tab | Closes an open panel and moves focus to the next element |
| Backspace / Delete | Clears the value when clearable is set and a value exists, without opening the panel |
With the panel open:
| Location | Key | Action |
|---|---|---|
| Search input | ↓ | Enters the icon grid at the selected icon, or the first icon if none is selected |
| Search input | Enter | Selects the first match when a query is present. Enter used to commit IME composition is excluded |
| Icon grid | ← / → | Moves through icons in order across rows and groups, stopping at either end |
| Icon grid | ↑ / ↓ | Moves to the same column in the previous or next row, or its last icon if that row is shorter |
| Icon grid | ↑ on the first row | Returns to search |
| Icon grid | Home / End | Moves to the first or last icon |
| Icon grid | Enter / Space | Selects the icon, closes the panel and returns focus to the picker |
| Any location | Esc | Closes the panel and returns focus to the picker without closing an enclosing dialog |
| Any location | Tab | Moves through the panel. Leaving backward from the first item or forward from the last closes it and returns to the trigger |
Only one grid cell is in the Tab order. Opening uses the actual selected value, then navigation follows the last focused icon. Left/Right and Home/End track icon names; Up/Down move through each group's eight-column rows. Navigation does not wrap. Reordering keeps the same icon rather than its old index. Removing the focused cell moves to a nearby icon, or to search if none remain. When focus is already outside the grid, data updates adjust its Tab stop without taking focus back.
IME composition, key code 229, Ctrl/Meta/Alt shortcuts and already-consumed events do not open, navigate or select. Home/End and PageUp/PageDown in search retain their native editing behavior. Inputs inside a custom icon slot also retain their own keys.
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiButton, HuiIconPicker, HuiSpace } from '@nscode/handy-ui'
const value = ref<string | null>('home')
const last = ref<string | null>('No selection')
const reversed = ref(false)
const commonOnly = ref(false)
const all = [
'home',
'user',
'users',
'setting',
'table',
'file',
'folder',
'bell',
'mail',
'search',
'check',
'close'
]
const icons = computed(() => {
const result = commonOnly.value
? all.filter((name) => ['home', 'mail', 'search'].includes(name))
: [...all]
return reversed.value ? result.reverse() : result
})
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="reversed = !reversed">Reverse icon order</HuiButton>
<HuiButton @click="commonOnly = !commonOnly">{{
commonOnly ? 'Show all icons' : 'Show common icons'
}}</HuiButton>
</HuiSpace>
<HuiIconPicker v-model="value" :icons="icons" @change="last = $event" />
<span role="status">Current: {{ value }}; last selection: {{ last }}</span>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | null | undefined (internal '' when omitted) | Icon name. Controlled when provided, including undefined/null. Otherwise stores selection internally, even with an update listener |
| icons | string[] | - | Available names, displayed in supplied order without groups. Omit to use all grouped built-in icons |
| clearable | boolean | false | Whether to show a clear button when a value exists and allow clearing with Backspace or Delete |
| disabled | boolean | false | Whether the picker is disabled |
| placeholder | string | Localized icon-selection prompt | Placeholder with no selected icon |
| filterPlaceholder | string | Localized icon-search prompt | Search placeholder and search input aria-label |
| emptyText | string | Localized no-matching-icons message | Text when no icons match |
| groupLabels | Partial<Record<IconPickerGroupName, string>> | - | Group headings. Unspecified groups use the locale. Has no effect when icons is supplied because grouping is disabled |
| size | 'large' | 'default' | 'small' | - | Follows FormItem, Form and ConfigProvider, then default |
| validateEvent | boolean | true | Whether value changes and panel closing trigger form-item validation |
Events
| Event | Callback Arguments | Description |
|---|---|---|
| update:modelValue | (value: string | null) | Selection or clear request, used by v-model. Null when clearing |
| change | (value: string | null) | Panel selection, including the same value, or clearing. Null when clearing |
| clear | - | Emitted when the clear button is clicked or Backspace/Delete clears the value |
| visibleChange | (visible: boolean) | Emitted when the panel opens or closes |
| focus | (event: FocusEvent) | Focus enters the trigger or panel. Internal movement does not repeat it |
| blur | (event: FocusEvent) | Focus leaves the whole picker. Does not add another form blur validation |
Listen to visibleChange in templates as @visible-change.
Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| icon | { icon: string } | Custom rendering for panel cells and the selected trigger icon. Defaults to <HuiIcon :name="icon" /> |
The scope argument is icon, not name: name on <slot> identifies the slot and cannot serve as a scope argument.
Methods
Call through an HuiIconPicker ref.
| Method | Arguments | Return Value | Description |
|---|---|---|---|
| focus | - | void | Focuses the picker |
| blur | - | void | Blurs the picker |
| clear | - | void | Requests null without requiring clearable. Does not take external focus or change panel state |
Types
import type {
IconPickerProps,
IconPickerEmits,
IconPickerSlots,
IconPickerSize,
IconPickerGroupName,
IconPickerExpose
} from '@nscode/handy-ui'
type IconPickerSize = 'large' | 'default' | 'small'
// Built-in groups, matching the keys of iconGroups exported by @nscode/icons
type IconPickerGroupName = 'navigation' | 'action' | 'status' | 'user' | 'file' | 'media' | 'editor'
interface IconPickerExpose {
focus: () => void
blur: () => void
clear: () => void
}