Programmatic Control
Control the select component from external code. Open, close, toggle, clear, and focus the select from buttons, keyboard shortcuts, or any application logic.
Compound Component: Template Ref
Use a template ref on SelectRoot to access exposed methods. This is the simplest approach when using the compound component API.
API Setup (Template Ref Control)
<script setup>
import { ref } from 'vue'
import { SelectRoot } from 'vue-superselect'
const selectRef = ref(null)
const selected = ref(null)
function clearAndFocus() {
selectRef.value?.clear()
selectRef.value?.focus()
}
</script>Exposed Methods
SelectRoot exposes these methods via template ref:
| Method | Signature | Description |
|---|---|---|
open() | () => void | Opens the dropdown |
close() | () => void | Closes the dropdown |
toggle() | () => void | Toggles the dropdown open/closed |
clear() | () => void | Resets the value to null (single) or [] (multi) |
focus() | () => void | Focuses the input element |
<script setup>
import { ref } from 'vue'
import { SelectRoot } from 'vue-superselect'
const selectRef = ref(null)
const selected = ref(null)
function openAndFocus() {
selectRef.value?.open()
selectRef.value?.focus()
}
</script>
<template>
<button @click="openAndFocus">Open Select</button>
<button @click="selectRef?.clear()">Clear</button>
<SelectRoot ref="selectRef" v-model="selected">
<!-- ... -->
</SelectRoot>
</template>Options API uses the same pattern via this.$refs.selectRef?.open() / focus() / clear().
Composable API: Full Control
The useSelect composable returns the same control methods directly, plus reactive state you can read and write to. This gives you maximum flexibility for complex use cases.
Composable Methods and State
useSelect() returns control methods as plain functions, no template ref needed:
const {
// Control methods
open, // () => void
close, // () => void
toggle, // () => void
clear, // () => void
focus, // () => void
dismiss, // () => void
// Reactive state (readable and writable)
value, // Ref<T | T[] | null>
isOpen, // Ref<boolean>
query, // Ref<string>
// Read-only state
visibleItems, // Ref<CollectionItem<T>[]>
activeId, // Ref<string | null>
isAtMax, // Ref<boolean>
disabled, // Ref<boolean>
} = useSelect({ items, labelKey: 'label', valueKey: 'id' })Setting Value Programmatically
With the composable, you can set the value directly:
// Select a specific item
value.value = 'some-id'
// Select random
const random = items[Math.floor(Math.random() * items.length)]
value.value = random.id
// Clear
clear() // or: value.value = nullWith the compound component API, use v-model reactivity:
// The v-model ref controls the value
const selected = ref(null)
// Set programmatically
selected.value = 'some-id'
// Clear
selected.value = nullCommon Patterns
Open on External Event
// Open the select when a keyboard shortcut is pressed
function onKeyDown(event: KeyboardEvent) {
if (event.key === 'k' && (event.metaKey || event.ctrlKey)) {
event.preventDefault()
selectRef.value?.open()
selectRef.value?.focus()
}
}Reset on Form Submit
function onSubmit() {
// Process the selected value
saveData(selected.value)
// Reset the select
selectRef.value?.clear()
}Controlled Open State
If you need external control over open/closed state, use v-model:open.
Full controlled/uncontrolled patterns are covered in Controlled State.