Zubra UI
Code for
GitHub

select-field

A labelled dropdown on a native <select>: the platform's own picker on every device, keyboard typeahead, autofill, and form submission under name. options is [{ value, label }], or groups [{ label, options: [...] }]. placeholder adds an empty first choice, and with required the form won't submit until one is chosen. value is two-way.

Try it: events appear here (valuechange).

Source: select-field.zb, and its registry item.

Install

Compiled (any project)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/select-field.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/select-field.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/select-field.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/select-field.json
One file, no dependencies (3.6 KB gzipped)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/select-field.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/select-field.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/select-field.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/select-field.json
.zb source (to edit it with Zubra)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/select-field.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/select-field.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/select-field.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/select-field.json
No build: a script tag, straight from this site
HTML
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/select-field.js"></script>

The compiled install writes src/components/zubra/select-field.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.

Usage

index.html
<select-field
  label="Country"
  name="country"
  placeholder="Choose a country"
  autocomplete="country"
></select-field>

<script type="module">
  import './components/zubra/select-field.js';

  const element = document.querySelector('select-field');
  element.value = 'ca';
  element.options = [
    { value: 'ca', label: 'Canada' },
    { value: 'gb', label: 'United Kingdom' },
    { value: 'us', label: 'United States' },
    { value: 'ie', label: 'Ireland' },
  ];
  element.addEventListener('valuechange', (event) => console.log('valuechange', event.detail));
</script>
Example.jsx
import './components/zubra/select-field.js';

const value = 'ca';
const options = [
  { value: 'ca', label: 'Canada' },
  { value: 'gb', label: 'United Kingdom' },
  { value: 'us', label: 'United States' },
  { value: 'ie', label: 'Ireland' },
];

export function SelectFieldExample() {
  return (
    <select-field
      label="Country"
      name="country"
      placeholder="Choose a country"
      autocomplete="country"
      value={value}
      options={options}
      onvaluechange={(event) => console.log('valuechange', event.detail)}
    ></select-field>
  );
}
Example.vue
<script setup>
import './components/zubra/select-field.js';
const value = 'ca';
const options = [
  { value: 'ca', label: 'Canada' },
  { value: 'gb', label: 'United Kingdom' },
  { value: 'us', label: 'United States' },
  { value: 'ie', label: 'Ireland' },
];
const onValuechange = (event) => console.log('valuechange', event.detail);
</script>

<template>
  <select-field
    label="Country"
    name="country"
    placeholder="Choose a country"
    autocomplete="country"
    :value.prop="value"
    :options.prop="options"
    @valuechange="onValuechange"
  ></select-field>
</template>

<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) -->
Example.svelte
<script>
  import './components/zubra/select-field.js';
  const value = 'ca';
  const options = [
    { value: 'ca', label: 'Canada' },
    { value: 'gb', label: 'United Kingdom' },
    { value: 'us', label: 'United States' },
    { value: 'ie', label: 'Ireland' },
  ];
</script>

<select-field
  label="Country"
  name="country"
  placeholder="Choose a country"
  autocomplete="country"
  {value}
  {options}
  onvaluechange={(event) => console.log('valuechange', event.detail)}
></select-field>
example.component.ts
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/select-field.js';

@Component({
  selector: 'app-select-field-example',
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <select-field
      label="Country"
      name="country"
      placeholder="Choose a country"
      autocomplete="country"
      [value]="value"
      [options]="options"
      (valuechange)="onValuechange($event)"
    ></select-field>
  `,
})
export class SelectFieldExample {
  value = 'ca';

  options = [
    { value: 'ca', label: 'Canada' },
    { value: 'gb', label: 'United Kingdom' },
    { value: 'us', label: 'United States' },
    { value: 'ie', label: 'Ireland' },
  ];

  onValuechange(event: Event) {
    console.log('valuechange', (event as CustomEvent).detail);
  }
}

API

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
valuestring""
options
property only
{ value, label }[][]
labelstring""
descriptionstring""
namestring""
placeholderstring""
requiredbooleanfalse
disabledbooleanfalse
autocompletestring""
Events
EventWhen
valuechangeThe choice changed; detail is the chosen value.

Size

1.3 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 17.6 KB.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour