Search
MaterialComponentsDocs
Components overviewApp barBadgeButtonsButton groupButtonExtended FABFAB menuFABIcon buttonSplit buttonCardCarouselCheckboxChipsDate & time pickersDate pickerTime pickerDialogDividerListLoading & progressLoading indicatorProgressMenuNavigationNavigation barNavigation drawerNavigation railRadio buttonsSearchSelectSheetsBottom sheetSide sheetSliderSnackbarSwitchTabsText fieldToolbarTooltip

Select

Pick an option from a menu. Explore field styles, selection, and validation states.

AppleBananaCherry
FactoryWeb ComponentsReactVueSvelteSolidJS
PublishedUpdated

A select lets people choose one option from a list, in a text field that opens a menu: a country, a role, a sort order. Use it when the options don't need to be visible at once; for a few that do, use radio buttons.

Usage

Each option has an id, which is the select's value, and a text.

Vanilla
import { createSelect } from 'material';

const select = createSelect({
  label: 'Fruit',
  name: 'fruit',
  options: [
    { id: 'apple', text: 'Apple' },
    { id: 'banana', text: 'Banana' },
    { id: 'cherry', text: 'Cherry' },
  ],
});
select.on('change', ({ value }) => choose(value));
document.body.append(select.element);
Web Components
<m-select name="fruit" label="Fruit">
  <m-select-option value="apple">Apple</m-select-option>
  <m-select-option value="banana">Banana</m-select-option>
  <m-select-option value="cherry">Cherry</m-select-option>
</m-select>

<script type="module">
  const select = document.querySelector('m-select');
  select.addEventListener('change', (event) => choose(event.detail.value));
</script>
React
import { Select, SelectOption } from 'material/react';
import { choose } from './app';

export function Example() {
  return (
    <Select onChange={(event) => choose(event.detail.value)} name="fruit" label="Fruit">
      <SelectOption value="apple">Apple</SelectOption>
      <SelectOption value="banana">Banana</SelectOption>
      <SelectOption value="cherry">Cherry</SelectOption>
    </Select>
  );
}
Vue
<script setup lang="ts">
import { MSelect, MSelectOption } from 'material/vue';
import { choose } from './app';
</script>

<template>
  <MSelect @change="choose($event.detail.value)" name="fruit" label="Fruit">
    <MSelectOption value="apple">Apple</MSelectOption>
    <MSelectOption value="banana">Banana</MSelectOption>
    <MSelectOption value="cherry">Cherry</MSelectOption>
  </MSelect>
</template>
Svelte
<script lang="ts">
  import { Select, SelectOption } from 'material/svelte';
  import { choose } from './app';
</script>

<Select onchange={(event) => choose(event.detail.value)} name="fruit" label="Fruit">
  <SelectOption value="apple">Apple</SelectOption>
  <SelectOption value="banana">Banana</SelectOption>
  <SelectOption value="cherry">Cherry</SelectOption>
</Select>
SolidJS
import { Select, SelectOption } from 'material/solid';
import { choose } from './app';

export function Example() {
  return (
    <Select onChange={(event) => choose(event.detail.value)} name="fruit" label="Fruit">
      <SelectOption value="apple">Apple</SelectOption>
      <SelectOption value="banana">Banana</SelectOption>
      <SelectOption value="cherry">Cherry</SelectOption>
    </Select>
  );
}

variant is filled (the default) or outlined, and density: 'compact' lowers the field, as on a text field. A select fills its container's width.

Examples

A chosen option, a disabled one

value selects an option by its id. A disabled option stays in the list and can't be chosen.

Vanilla
import { createSelect } from 'material';

const select = createSelect({
  label: 'Plan',
  variant: 'outlined',
  value: 'pro',
  supportingText: 'Billed monthly',
  options: [
    { id: 'free', text: 'Free' },
    { id: 'pro', text: 'Professional' },
    { id: 'enterprise', text: 'Enterprise', disabled: true },
  ],
});
document.body.append(select.element);
Web Components
<m-select value="pro" variant="outlined" label="Plan" supporting-text="Billed monthly">
  <m-select-option value="free">Free</m-select-option>
  <m-select-option value="pro">Professional</m-select-option>
  <m-select-option value="enterprise" disabled>Enterprise</m-select-option>
</m-select>
React
import { useState } from 'react';
import { Select, SelectOption } from 'material/react';

export function Example() {
  const [value, setValue] = useState("pro");
  return (
    <Select value={value} onChange={(event) => setValue(event.detail.value)} variant="outlined" label="Plan" supportingText="Billed monthly">
      <SelectOption value="free">Free</SelectOption>
      <SelectOption value="pro">Professional</SelectOption>
      <SelectOption value="enterprise" disabled>Enterprise</SelectOption>
    </Select>
  );
}
Vue
<script setup lang="ts">
import { ref } from 'vue';
import { MSelect, MSelectOption } from 'material/vue';

const value = ref("pro");
</script>

<template>
  <MSelect v-model="value" variant="outlined" label="Plan" supporting-text="Billed monthly">
    <MSelectOption value="free">Free</MSelectOption>
    <MSelectOption value="pro">Professional</MSelectOption>
    <MSelectOption value="enterprise" disabled>Enterprise</MSelectOption>
  </MSelect>
</template>
Svelte
<script lang="ts">
  import { Select, SelectOption } from 'material/svelte';

  let value = $state("pro");
</script>

<Select bind:value variant="outlined" label="Plan" supportingText="Billed monthly">
  <SelectOption value="free">Free</SelectOption>
  <SelectOption value="pro">Professional</SelectOption>
  <SelectOption value="enterprise" disabled>Enterprise</SelectOption>
</Select>
SolidJS
import { createSignal } from 'solid-js';
import { Select, SelectOption } from 'material/solid';

export function Example() {
  const [value, setValue] = createSignal("pro");
  return (
    <Select value={value()} onChange={(event) => setValue(event.detail.value)} variant="outlined" label="Plan" supportingText="Billed monthly">
      <SelectOption value="free">Free</SelectOption>
      <SelectOption value="pro">Professional</SelectOption>
      <SelectOption value="enterprise" disabled>Enterprise</SelectOption>
    </Select>
  );
}

Required, in error

setError(true, message) shows a message in place of the supporting text, and clearError() restores it. An action here chooses an option and clears the error.

Vanilla
import { createSelect } from 'material';

const select = createSelect({
  label: 'Role',
  value: '',
  required: true,
  error: true,
  supportingText: 'Choose a role',
  options: [
    { id: 'admin', text: 'Administrator' },
    { id: 'editor', text: 'Editor' },
    { id: 'viewer', text: 'Viewer' },
  ],
});
document.body.append(select.element);

function chooseEditor() {
  select.setValue('editor');
  select.setError(false);
}
Web Components
<m-select label="Role" supporting-text="Choose a role" required error>
  <m-select-option value="admin">Administrator</m-select-option>
  <m-select-option value="editor">Editor</m-select-option>
  <m-select-option value="viewer">Viewer</m-select-option>
</m-select>

<script type="module">
  const select = document.querySelector('m-select');

  function chooseEditor() {
    select.value = "editor";
    select.toggleAttribute('error', false);
  }
</script>
React
import { useState } from 'react';
import { Select, SelectOption } from 'material/react';

export function Example() {
  const [value, setValue] = useState("");
  const [error, setError] = useState(true);
  const chooseEditor = () => {
    setValue("editor");
    setError(false);
  };

  return (
    <Select value={value} onChange={(event) => setValue(event.detail.value)} label="Role" supportingText="Choose a role" required error={error}>
      <SelectOption value="admin">Administrator</SelectOption>
      <SelectOption value="editor">Editor</SelectOption>
      <SelectOption value="viewer">Viewer</SelectOption>
    </Select>
  );
}
Vue
<script setup lang="ts">
import { ref } from 'vue';
import { MSelect, MSelectOption } from 'material/vue';

const value = ref("");
const error = ref(true);

function chooseEditor() {
  value.value = "editor";
  error.value = false;
}
</script>

<template>
  <MSelect v-model="value" label="Role" supporting-text="Choose a role" required :error="error">
    <MSelectOption value="admin">Administrator</MSelectOption>
    <MSelectOption value="editor">Editor</MSelectOption>
    <MSelectOption value="viewer">Viewer</MSelectOption>
  </MSelect>
</template>
Svelte
<script lang="ts">
  import { Select, SelectOption } from 'material/svelte';

  let value = $state("");
  let error = $state(true);

  function chooseEditor() {
    value = "editor";
    error = false;
  }
</script>

<Select bind:value label="Role" supportingText="Choose a role" required error={error}>
  <SelectOption value="admin">Administrator</SelectOption>
  <SelectOption value="editor">Editor</SelectOption>
  <SelectOption value="viewer">Viewer</SelectOption>
</Select>
SolidJS
import { createSignal } from 'solid-js';
import { Select, SelectOption } from 'material/solid';

export function Example() {
  const [value, setValue] = createSignal("");
  const [error, setError] = createSignal(true);
  const chooseEditor = () => {
    setValue("editor");
    setError(false);
  };

  return (
    <Select value={value()} onChange={(event) => setValue(event.detail.value)} label="Role" supportingText="Choose a role" required error={error()}>
      <SelectOption value="admin">Administrator</SelectOption>
      <SelectOption value="editor">Editor</SelectOption>
      <SelectOption value="viewer">Viewer</SelectOption>
    </Select>
  );
}

The menu is mounted in the select's own element. In a scrolling or clipping container, such as a navigation drawer or a sheet, set menu: { container: document.body, maxHeight: '320px' }, or layer: 'top' to show it in the browser's top layer. setOptions() replaces the options, for a list loaded later. Validating a select in a form is planned for Showcase.

API

Options

options

The options

TypeSelectOption[]
Default[]
value

The id of the option selected at first

Typestring
Defaultundefined
variant

The text field's style

Type'filled' | 'outlined'
Default'filled'
density

The field height

Type'default' | 'compact'
Default'default'
label

The floating label

Typestring
Defaultundefined
name

The input's name, for forms

Typestring
Defaultundefined
required

Whether a selection is required

Typeboolean
Defaultfalse
disabled

Whether the select is disabled

Typeboolean
Defaultfalse
supportingText

Helper text under the field

Typestring
Defaultundefined
error

The error state

Typeboolean
Defaultfalse
placement

The menu's placement against the field

Typestring
Default'bottom-start'
menu

Where the menu is mounted, its maximum height, whether it flips above the field, and its variant ('vertical' by default, or 'baseline') and colors

Type{ container?, maxHeight?, autoFlip?, variant?, color? }
Defaultundefined
layer

Renders the menu beside the field and shows it in the top layer; menu.container is then not used

Type'top'
Defaultundefined
on

Event handlers registered at creation

Type{ change?, open?, close? }
Defaultundefined
class

Additional CSS classes

Typestring
Defaultundefined
prefix

Prefix for CSS class names

Typestring
Default'mtrl'

An option

id

The option's value

Typestring
Defaultrequired
text

Its text

Typestring
Defaultrequired
disabled

Whether it can be chosen

Typeboolean
Defaultfalse
icon

HTML, usually an SVG, before the text

Typestring
Defaultundefined
hasSubmenu / submenu

Nested options; a select with them is a menu button rather than a combobox

Typeboolean / SelectOption[]
Defaultundefined
data

Data of your own, carried with the option

Typeunknown
Defaultundefined

Methods

getValue() / setValue(value)

The selected option's id

Parametersvalue: string | null | undefined
Returnsstring | null / SelectComponent
clear()

Clears the selection

Parametersnone
ReturnsSelectComponent
getText()

The selected option's text

Parametersnone
Returnsstring
getSelectedOption()

The selected option

Parametersnone
ReturnsSelectOption | null
getOptions() / setOptions(options)

The options

Parametersoptions: SelectOption[]
ReturnsSelectOption[] / SelectComponent
open(interactionType?) / close()

Opens or closes the menu; a disabled select does not open

ParametersinteractionType?: 'mouse' | 'keyboard'
ReturnsSelectComponent
isOpen()

Whether the menu is open

Parametersnone
Returnsboolean
setDensity(density) / getDensity()

The field height

Parametersdensity: 'default' | 'compact'
ReturnsSelectComponent / string
enable() / disable()

The disabled state

Parametersnone
ReturnsSelectComponent
setError(error, message?) / clearError()

The error state

Parameterserror: boolean, message?: string
ReturnsSelectComponent
on(event, handler) / off(event, handler)

Adds or removes a listener

Parametersevent: string, handler: Function
ReturnsSelectComponent
destroy()

Destroys the select and releases its menu

Parametersnone
Returnsvoid
element

The root, which is the text field's

TypeHTMLElement
textField

The text field

TypeTextFieldComponent

Events

change

The selection changed

Payload{ select, value, text, option, originalEvent? }. value is string | null
open / close

The menu opened or closed, however it was

Payload{ select, originalEvent? }

The web component's change carries { value }.

Styling

The select is a text field: its root carries both sets of classes, and the menu is its child.

.mtrl-select, .mtrl-select--open { }
.mtrl-select.mtrl-text-field--filled, .mtrl-select.mtrl-text-field--outlined { }
.mtrl-select .mtrl-text-field__input, .mtrl-select .mtrl-text-field__label { }
.mtrl-select .mtrl-text-field__trailing-icon { }
.mtrl-select > .mtrl-menu { }