nb-select

This is a select component for Vue.js 3+.

Loading component...

Installation

Yarn
yarn add @vlalg-nimbus/nb-selects
NPM
npm install @vlalg-nimbus/nb-selects

Usage

Vue 3
import { createApp } from 'vue'
import App from './App.vue'

import NbSelectComponents from '@vlalg-nimbus/nb-selects'
import "@vlalg-nimbus/nb-selects/dist/style.css";

const app = createApp(App)
app.use(NbSelectComponents)
app.mount('#app')
Nuxt 3
import NbSelectComponents from '@vlalg-nimbus/nb-selects'
import "@vlalg-nimbus/nb-selects/dist/style.css";

export default defineNuxtPlugin(context => {
  context.vueApp.use(NbSelectComponents)
})

To use, simply call the component, in this case it will be NbSelect or nb-select.

Mode 1
<template>
  <NbSelect />
</template>
Mode 2
<template>
  <nb-select />
</template>
Mode 3
<template>
  <nb-select></nb-select>
</template>

Preview & Playground

Select the component you want to edit/test

Loading Sandbox...

Props

Items with an (*) mean they are required

nameValue typeDefaultDescription
nbId (*)StringSets the id attribute to differentiate from other components
selectNameString'selectX'Sets the name attribute for the select element
displayString'b'Defines the display type. Accepts ib and b.
themeString'light'Defines the theme. Accepts dark and light. When theme is set, uses theme-specific color props.
hasBorderRadiusBooleanfalseDefines if the select should have border-radius.
borderRadiusNumber0.5Defines border-radius.
disabledBooleanfalseDefines if the select is disabled
requiredBooleanfalseDefines if the select is required
fontFamilyString"'Lato', sans-serif"Defines the font-family
fontSizeString'1.6em'Defines the font-size
fontWeightNumber400Defines the font-weight
textAlignString'left'Defines the text alignment. Accepts left, center, right.
selectWidthNumber200Defines the select width in pixels
inputStyleString'background'Defines the input style. Accepts background, line, border.
optionsArrayDefines the list of options. It is an array of objects with value and text properties (or custom keys via valueKey and textKey props)
selectedOptionOnlyString, Number, Boolean''Defines the selected value for single select. The value must exist in the list options
selectedOptionMultipleArrayDefines the selected values for multiple select. The values must exist in the list options
textKeyString'text'Defines the key name for the text property in options objects
valueKeyString'value'Defines the key name for the value property in options objects
multipleBooleanfalseDefines if the select allows multiple selections
hasEmptyOptionBooleantrueDefines if the select should have an empty option (placeholder)
emptyOptionTextString'Selecione uma opção'Defines the text for the empty option
emptyOptionValueString, NumbernullDefines the value for the empty option
dropdownScrollClassString''Defines a custom CSS class for the dropdown scroll container
hasLabelBooleanfalseDefines if the select should have a label
labelTextString'Select: 'Defines the label text
tabindexNumber, String0Defines the tab index. Set this property to make the select focusable by the keyboard.
lightBgColorString'#f8f8f2'Defines the background color for light theme. Accepts Color name and Hex
lightBgColorFocusString'#eaeaea'Defines the background color on focus for light theme. Accepts Color name and Hex
lightBorderColorString'#353734'Defines the border color for light theme. Accepts Color name and Hex
lightBorderColorFocusString'#272936'Defines the border color on focus for light theme. Accepts Color name and Hex
lightDisabledBgColorString'#dfdfd9'Defines the disabled background color for light theme. Accepts Color name and Hex
lightTextColorString'#000000'Defines the text color for light theme. Accepts Color name and Hex
lightDisabledBorderColorString'rgba(53, 55, 52, 0.3)'Defines the disabled border color for light theme. Accepts Color name and Hex
lightOptionTextColorString'#000000'Defines the option text color for light theme. Accepts Color name and Hex
lightOptionTextColorSelectedString'#000000'Defines the selected option text color for light theme. Accepts Color name and Hex
lightOptionBgColorSelectedString'#e0e0e0'Defines the selected option background color for light theme. Accepts Color name and Hex
lightPlaceholderColorString'#999999'Defines the placeholder color for light theme. Accepts Color name and Hex
darkBgColorString'#353734'Defines the background color for dark theme. Accepts Color name and Hex
darkBgColorFocusString'#272936'Defines the background color on focus for dark theme. Accepts Color name and Hex
darkBorderColorString'#44475a'Defines the border color for dark theme. Accepts Color name and Hex
darkBorderColorFocusString'rgba(68, 71, 90, 0.4)'Defines the border color on focus for dark theme. Accepts Color name and Hex
darkDisabledBgColorString'rgba(40, 42, 54, 1)'Defines the disabled background color for dark theme. Accepts Color name and Hex
darkTextColorString'#ffffff'Defines the text color for dark theme. Accepts Color name and Hex
darkDisabledBorderColorString'rgba(68, 71, 90, 0.3)'Defines the disabled border color for dark theme. Accepts Color name and Hex
darkOptionTextColorString'#ffffff'Defines the option text color for dark theme. Accepts Color name and Hex
darkOptionTextColorSelectedString'#ffffff'Defines the selected option text color for dark theme. Accepts Color name and Hex
darkOptionBgColorSelectedString'#3d3d3d'Defines the selected option background color for dark theme. Accepts Color name and Hex
darkPlaceholderColorString'#999999'Defines the placeholder color for dark theme. Accepts Color name and Hex

Events

nameReturn typeDescription
changedvalueFired when the selected value changes, returns the selected value(s).
user-changedvalueFired when the user manually changes the selected value, returns the selected value(s).
clickednothingFired when the select is clicked, returns nothing.

Slot

The component has a slot called slot-select-item where you can customize the display of each option.

<template>
  <NbSelect
    nb-id="select-custom"
    select-name="select-custom"
    :options="selectOptions"
    :selected-option-only="selectedValue"
  >
    <template #slot-select-item="{ propRow, propIndex }">
      <div>
        <strong>{{ propRow.text }}</strong>
        <span style="color: #999;"> - {{ propRow.description }}</span>
      </div>
    </template>
  </NbSelect>
</template>

Example

Light Theme (default)

<template>
  <NbSelect
    nb-id="select-light"
    select-name="select-light"
    theme="light"
    :options="selectOptions"
    :selected-option-only="selectedValue"
    light-bg-color="#dbeafe"
    light-bg-color-focus="#bfdbfe"
    light-border-color="#93c5fd"
    light-border-color-focus="#60a5fa"
    light-text-color="#1e40af"
    light-placeholder-color="#64748b"
    light-option-text-color="#1e40af"
    light-option-text-color-selected="#ffffff"
    light-option-bg-color-selected="#3b82f6"
    :border-radius="0.375"
    @changed="handleChange($event)"
    @user-changed="handleUserChange($event)"
  />
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompSelect',
});

const selectOptions = [
  { value: 'option1', text: 'Option 1' },
  { value: 'option2', text: 'Option 2' },
  { value: 'option3', text: 'Option 3' }
];

const selectedValue = ref('option1');

const handleChange = (value) => {
  selectedValue.value = value;
  console.log('Changed:', value);
};

const handleUserChange = (value) => {
  console.log('User changed:', value);
};
</script>

Dark Theme

<template>
  <NbSelect
    nb-id="select-dark"
    select-name="select-dark"
    theme="dark"
    :options="selectOptions"
    :selected-option-only="selectedValue"
    dark-bg-color="#8b5cf6"
    dark-bg-color-focus="#7c3aed"
    dark-border-color="#a78bfa"
    dark-border-color-focus="#8b5cf6"
    dark-text-color="#f3f4f6"
    dark-placeholder-color="#c4b5fd"
    dark-option-text-color="#f3f4f6"
    dark-option-text-color-selected="#ffffff"
    dark-option-bg-color-selected="#6d28d9"
    :border-radius="0.5"
    @changed="handleChange($event)"
    @user-changed="handleUserChange($event)"
  />
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompSelectDark',
});

const selectOptions = [
  { value: 'option1', text: 'Option 1' },
  { value: 'option2', text: 'Option 2' },
  { value: 'option3', text: 'Option 3' }
];

const selectedValue = ref('option2');

const handleChange = (value) => {
  selectedValue.value = value;
  console.log('Changed:', value);
};

const handleUserChange = (value) => {
  console.log('User changed:', value);
};
</script>

Multiple Selection

<template>
  <NbSelect
    nb-id="select-multiple"
    select-name="select-multiple"
    theme="light"
    :multiple="true"
    :options="selectOptions"
    :selected-option-multiple="selectedValues"
    @changed="handleChange($event)"
    @user-changed="handleUserChange($event)"
  />
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompSelectMultiple',
});

const selectOptions = [
  { value: 'option1', text: 'Option 1' },
  { value: 'option2', text: 'Option 2' },
  { value: 'option3', text: 'Option 3' }
];

const selectedValues = ref(['option1', 'option2']);

const handleChange = (value) => {
  selectedValues.value = value;
  console.log('Changed:', value);
};

const handleUserChange = (value) => {
  console.log('User changed:', value);
};
</script>

Comportamento

Fechamento Automático

O dropdown do componente fecha automaticamente em duas situações:

  1. Quando o foco sai do componente: Ao usar Tab para navegar para fora do componente ou clicar em outro elemento da página, o dropdown fecha automaticamente.
  2. Quando outro select é aberto: Ao abrir um novo select (single ou múltiplo), todos os outros selects abertos fecham automaticamente. Isso garante que apenas um select esteja aberto por vez.

Modo Múltiplo

No modo múltiplo (multiple="true"), o dropdown permanece aberto após selecionar uma opção, permitindo que você selecione múltiplas opções sem precisar reabrir o dropdown. O dropdown só fecha quando:

  • O foco sai do componente
  • Outro select é aberto
  • Você clica fora do componente

Limitações

Overflow Hidden

O dropdown do componente usa position: absolute para posicionamento. Se o componente estiver dentro de um container pai com overflow: hidden, o dropdown pode ser cortado ou ocultado.

Solução: Remova ou ajuste a propriedade overflow: hidden no container pai do componente para permitir que o dropdown seja exibido corretamente.

<!-- ❌ Problema: dropdown será cortado -->
<div style="overflow: hidden;">
  <NbSelect nb-id="select-1" :options="options" />
</div>

<!-- ✅ Solução: remova overflow: hidden ou use overflow: visible -->
<div style="overflow: visible;">
  <NbSelect nb-id="select-1" :options="options" />
</div>