Socket
Socket
Sign inDemoInstall

@limetech/mdc-select

Package Overview
Dependencies
18
Maintainers
5
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @limetech/mdc-select

The Material Components web select (text field drop-down) component


Version published
Weekly downloads
24
increased by26.32%
Maintainers
5
Created
Weekly downloads
 

Readme

Source

Select Menus

MDC Select provides Material Design single-option select menus, using the MDC menu. The Select component is fully accessible, and supports RTL rendering.

Design & API Documentation

  • Material Design guidelines: Text Fields
  • Demo

Installation

npm install @limetech/mdc-select

Basic Usage

The select uses an MDCMenu component instance to contain the list of options, but uses the data-value attribute instead of value to represent the options' values.

NOTE: The data-value attribute must be present on each option.

The select requires that you set the width of the mdc-select__anchor element as well as setting the width of the mdc-select__menu element to match. This is best done through the use of another class (e.g. demo-width-class in the example HTML and CSS below).

HTML

<div class="mdc-select">
  <div class="mdc-select__anchor demo-width-class">
    <i class="mdc-select__dropdown-icon"></i>
    <div class="mdc-select__selected-text"></div>
    <span class="mdc-floating-label">Pick a Food Group</span>
    <div class="mdc-line-ripple"></div>
  </div>

  <div class="mdc-select__menu mdc-menu mdc-menu-surface demo-width-class">
    <ul class="mdc-list">
      <li class="mdc-list-item mdc-list-item--selected" data-value="" aria-selected="true"></li>
      <li class="mdc-list-item" data-value="grains">
        Bread, Cereal, Rice, and Pasta
      </li>
      <li class="mdc-list-item" data-value="vegetables">
        Vegetables
      </li>
      <li class="mdc-list-item" data-value="fruit">
        Fruit
      </li>
    </ul>
  </div>
</div>

Styles

When using the select, you will also need to load the Menu and List components' styles.

@import "@limetech/mdc-list/mdc-list";
@import "@limetech/mdc-menu-surface/mdc-menu-surface";
@import "@limetech/mdc-menu/mdc-menu";
@import "@limetech/mdc-select/mdc-select";

.demo-width-class {
  width: 25rem;
}

JavaScript Instantiation

import {MDCSelect} from '@limetech/mdc-select';

const select = new MDCSelect(document.querySelector('.mdc-select'));

select.listen('MDCSelect:change', () => {
  alert(`Selected option at index ${select.selectedIndex} with value "${select.value}"`);
});

See Importing the JS component for more information on how to import JavaScript.

Accessibility (a11y)

In order to have an accessible component for users, it's recommended that you follow the WAI-ARIA example for Collapsible Dropdown Listbox. The following is an example of the select component with all of the necessary aria attributes.

<div class="mdc-select">
  <div class="mdc-select__anchor">
    <i class="mdc-select__dropdown-icon"></i>
    <div id="demo-selected-text" class="mdc-select__selected-text" role="button" aria-haspopup="listbox" aria-labelledby="demo-label demo-selected-text">Vegetables</div>
    <span id="demo-label" class="mdc-floating-label mdc-floating-label--float-above">Pick a Food Group</span>
    <div class="mdc-line-ripple"></div>
  </div>

  <div class="mdc-select__menu mdc-menu mdc-menu-surface" role="listbox">
    <ul class="mdc-list">
      <li class="mdc-list-item mdc-list-item--selected" data-value="" role="option"></li>
      <li class="mdc-list-item" data-value="grains" role="option">
        Bread, Cereal, Rice, and Pasta
      </li>
      <li class="mdc-list-item mdc-list-item--disabled" data-value="vegetables" aria-selected="true" aria-disabled="true" role="option">
        Vegetables
      </li>
      <li class="mdc-list-item" data-value="fruit" role="option">
        Fruit
      </li>
    </ul>
  </div>
</div>

Variants

Outlined Select

The Select Outlined variant uses the mdc-notched-outline in place of the mdc-line-ripple element and adds the mdc-select--outlined modifier class on the root element. All other elements for each type of select remain the same.

<div class="mdc-select mdc-select--outlined">
  <div class="mdc-select__anchor">
    <div class="mdc-notched-outline">
      <div class="mdc-notched-outline__leading"></div>
      <div class="mdc-notched-outline__notch">
        <label class="mdc-floating-label">Pick a Food Group</label>
      </div>
      <div class="mdc-notched-outline__trailing"></div>
    </div>
  </div>

  <!-- Other elements from the select remain. -->
  <div class="mdc-select__menu mdc-menu mdc-menu-surface" role="listbox">...</div>
</div>

Additional Information

Select with pre-selected option

To indicate a select component that has a pre-selected value, use the mdc-list-item--selected class to set the selected item. The select also needs the text from the selected element copied to the mdc-select__selected-text element.

<div class="mdc-select">
  <div class="mdc-select__anchor demo-width-class">
    <i class="mdc-select__dropdown-icon"></i>
    <div class="mdc-select__selected-text">Vegetables</div>
    <span class="mdc-floating-label mdc-floating-label--float-above">Pick a Food Group</span>
    <div class="mdc-line-ripple"></div>
  </div>

  <div class="mdc-select__menu demo-width-class mdc-menu mdc-menu-surface">
    <ul class="mdc-list">
      <li class="mdc-list-item" data-value=""></li>
      <li class="mdc-list-item" data-value="grains">
        Bread, Cereal, Rice, and Pasta
      </li>
      <li class="mdc-list-item mdc-list-item--selected" data-value="vegetables" aria-selected="true">
        Vegetables
      </li>
      <li class="mdc-list-item" data-value="fruit">
        Fruit
      </li>
    </ul>
  </div>
</div>
Using the floating label as the placeholder

Leave the mdc-select__selected-text element empty and don't specify an element as selected. If leaving the field empty should be a valid option, include an mdc-list-item element at the beginning of the list with an empty data-value attribute.

<li class="mdc-list-item mdc-list-item--selected" aria-selected="true" role="option" data-value=""></li>
Required select

To style a select menu as required and enable validation, add the mdc-select--required class to the mdc-select element and set the aria-required attribute on the mdc-select__selected-text element to be "true".

<div class="mdc-select mdc-select--required">
  <div class="mdc-select__anchor">
    <i class="mdc-select__dropdown-icon"></i>
    <div class="mdc-select__selected-text" aria-required="true"></div>
    <span class="mdc-floating-label">Pick a Food Group</span>
    <div class="mdc-line-ripple"></div>
  </div>

  <div class="mdc-select__menu mdc-menu mdc-menu-surface">
    ...
  </div>
</div>

NOTE: To programmatically set a select as required, use the required property in the MDCSelect API.

Disabled select

Add the mdc-select--disabled class to the mdc-select element and and set the aria-disabled attribute on the mdc-select__selected-text element to be "true".

<div class="mdc-select mdc-select--disabled">
  <div class="mdc-select__anchor">
    <i class="mdc-select__dropdown-icon"></i>
    <div class="mdc-select__selected-text" aria-disabled="true"></div>
    <span class="mdc-floating-label">Pick a Food Group</span>
    <div class="mdc-line-ripple"></div>
  </div>

  <div class="mdc-select__menu mdc-menu mdc-menu-surface">
    ...
  </div>
</div>

NOTE: To programmatically set a select as disabled, use the disabled property in the MDCSelect API.

Disabled options

Add the mdc-list-item--disabled class to list items that are disabled. Disabled list items are removed from the list items index and are ignored entirely. You cannot programmatically select a disabled list item.

<div class="mdc-select">
  <div class="mdc-select__anchor">
    ...
  </div>

  <div class="mdc-select__menu mdc-menu mdc-menu-surface">
    <ul class="mdc-list">
      <li class="mdc-list-item" data-value=""></li>
      <li class="mdc-list-item" data-value="grains">
        Bread, Cereal, Rice, and Pasta
      </li>
      <li class="mdc-list-item mdc-list-item--selected mdc-list-item--disabled" data-value="vegetables">
        Vegetables
      </li>
      <li class="mdc-list-item" data-value="fruit">
        Fruit
      </li>
    </ul>
  </div>
</div>

Select with Helper Text

The helper text provides supplemental information and/or validation messages to users. It appears when the select element is focused and disappears on blur by default, or it can be persistent. See here for more information on using helper text.

Select with Leading Icons

Leading icons can be added within the default or outlined variant of MDC Select as visual indicators as well as interaction targets. See here for more information on using icons.

Select with No Label

A label is not required if a separate, adjacent label is provided elsewhere. To correctly style MDC Select without a label, add the class mdc-select--no-label and remove the label from the structure.

Filled
<div class="mdc-select mdc-select--no-label">
  <div class="mdc-select__anchor demo-width-class">
    <i class="mdc-select__dropdown-icon"></i>
    <div class="mdc-select__selected-text"></div>
    <div class="mdc-line-ripple"></div>
  </div>

  <div class="mdc-select__menu mdc-menu mdc-menu-surface demo-width-class">
    <ul class="mdc-list">
      <li class="mdc-list-item mdc-list-item--selected" data-value="" aria-selected="true"></li>
      <li class="mdc-list-item" data-value="grains">
        Bread, Cereal, Rice, and Pasta
      </li>
      <li class="mdc-list-item" data-value="vegetables">
        Vegetables
      </li>
      <li class="mdc-list-item" data-value="fruit">
        Fruit
      </li>
    </ul>
  </div>
</div>
Outlined
<div class="mdc-select mdc-select--outlined mdc-select--no-label">
  <div class="mdc-select__anchor demo-width-class">
    <i class="mdc-select__dropdown-icon"></i>
    <div class="mdc-select__selected-text"></div>
    <div class="mdc-notched-outline">
      <div class="mdc-notched-outline__leading"></div>
      <div class="mdc-notched-outline__trailing"></div>
    </div>
  </div>

  <!-- Other elements from the select remain. -->
  <div class="mdc-select__menu mdc-menu mdc-menu-surface demo-width-class">...</div>
</div>

Style Customization

CSS Classes
ClassDescription
mdc-selectMandatory.
mdc-select__anchorMandatory. This element should be placed within the mdc-select element.
mdc-select__menuMandatory. This class should be placed on the mdc-menu element within the mdc-select element.
mdc-select__dropdown-iconMandatory. Should be placed on an i element within the mdc-select__anchor element. Used for the dropdown arrow svg and animation.
mdc-select__selected-textMandatory. This class should be placed on a div within the mdc-select__anchor element.
mdc-select__iconOptional. Should be placed on an i or svg element within the mdc-select__anchor element. Used for the leading icon.
mdc-select--activatedOptional. Styles the activated state of select. This class will be added automatically when menu is opened.
mdc-select--disabledOptional. Styles the select as disabled. This class should be applied to the root element when the disabled attribute is applied to the <select> element.
mdc-select--outlinedOptional. Styles the select as outlined select.
mdc-select--with-leading-iconStyles the select as a select with a leading icon.
mdc-select--no-labelStyles the select as a select without a label.

NOTE: To further customize the MDCMenu or the MDCList component contained within the select, please refer to their respective documentation.

Sass Mixins

Mixins should be included in the context of a custom class applied to the component's root element, e.g. .my-select.

MixinDescription
mdc-select-ink-color($color)Customizes the color of the selected item displayed in the select.
mdc-select-container-fill-color($color)Customizes the background color of the select.
mdc-select-label-color($color)Customizes the label color of the select in the unfocused state.
mdc-select-focused-label-color($color)Customizes the label color of the select when focused.
mdc-select-bottom-line-color($color)Customizes the color of the default bottom line of the select.
mdc-select-focused-bottom-line-color($color)Customizes the color of the bottom line of the select when focused.
mdc-select-shape-radius($radius, $rtl-reflexive)Sets rounded shape to boxed select variant with given radius size. Set $rtl-reflexive to true to flip radius values in RTL context, defaults to false.
mdc-select-hover-bottom-line-color($color)Customizes the color of the bottom line when the select is hovered.
mdc-select-outline-color($color)Customizes the color of the notched outline.
mdc-select-outline-shape-radius($radius, $rtl-reflexive)Sets the border radius of of the outlined select variant. Set $rtl-reflexive to true to flip radius values in RTL context, defaults to false.
mdc-select-focused-outline-color($color)Customizes the color of the outline of the select when focused.
mdc-select-hover-outline-color($color)Customizes the color of the outline when the select is hovered.

NOTE: To further customize the floating label, please see the floating label documentation.

MDCSelect API

The MDCSelect component API is modeled after a subset of the HTMLSelectElement functionality.

PropertyTypeDescription
valuestringThe value/data-value of the currently selected option.
selectedIndexnumberThe index of the currently selected option. Set to -1 if no option is currently selected. Changing this property will update the select element.
disabledbooleanWhether or not the component is disabled. Setting this sets the disabled state on the component.
validbooleanWhether or not the component is in a valid state. Setting this updates styles on the component, but does not affect the native validity state.
requiredbooleanWhether or not the component is required. Setting this updates the required or aria-required attribute on the component and enables validation.
leadingIconAriaLabelstring (write-only)Proxies to the foundation's setLeadingIconAriaLabel method.
leadingIconContentstring (write-only)Proxies to the foundation's setLeadingIconContent method.
helperTextContentstring (write-only)Proxies to the foundation's setHelperTextContent method when set.
rippleMDCRippleRipple instance attached to outlined select variant, or null for all other variants.

Events

Event NameDataDescription
MDCSelect:change{value: string, index: number}Used to indicate when an element has been selected. This event also includes the value of the item and the index.

Usage within Web Frameworks

If you are using a JavaScript framework, such as React or Angular, you can create a Select for your framework. Depending on your needs, you can use the Simple Approach: Wrapping MDC Web Vanilla Components, or the Advanced Approach: Using Foundations and Adapters. Please follow the instructions here.

MDCSelectAdapter

Method SignatureDescription
addClass(className: string) => voidAdds a class to the select element.
removeClass(className: string) => voidRemoves a class from the select element.
hasClass(className: string) => booleanReturns true if the select element has the className in its classList.
activateBottomLine() => voidActivates the bottom line component.
deactivateBottomLine() => voidDeactivates the bottom line component.
getSelectedMenuItem() => ElementReturns the selected menu item element.
hasLabel() => booleanReturns true if the select contains a label.
floatLabel(value: boolean) => voidFloats or defloats label.
getLabelWidth() => numberReturns the offsetWidth of the label element.
hasOutline() => booleanReturns true if the select has the notched outline element.
notchOutline(labelWidth: number) => voidSwitches the notched outline element to its "notched state."
closeOutline() => voidSwitches the notched outline element to its closed state.
setDisabled(isDisabled: boolean) => voidEnables or disables the select.
setRippleCenter(normalizedX: number) => voidSets the line ripple center to the provided normalizedX value.
notifyChange(value: string) => voidEmits the MDCSelect:change event when an element is selected.
setSelectedText(text: string) => voidSets the text content of the selectedText element to the given string.
isSelectedTextFocused() => booleanReturns whether the selected text element is focused.
getSelectedTextAttr(attr: string) => stringGets the given attribute on the selected text element.
setSelectedTextAttr(attr: string, value: string) => voidSets the given attribute on the selected text element.
openMenu() => voidCauses the menu element in the select to open.
closeMenu() => voidCauses the menu element in the select to close.
getAnchorElement() => ElementReturns the select anchor element.
setMenuAnchorElement(anchorEl: Element) => voidSets the menu anchor element.
setMenuAnchorCorner(anchorCorner: Corner) => voidSets the menu anchor corner.
setMenuWrapFocus(wrapFocus: boolean) => voidSets whether the menu should wrap focus.
setAttributeAtIndex(index: number, attributeName: string, attributeValue: string) => voidSets the attribute on the menu item at the given index.
removeAttributeAtIndex(index: number, attributeName: string) => voidRemoves the attribute on the menu item at the given index.
focusMenuItemAtIndex(index: number) => voidFocuses the menu item at the given index.
getMenuItemValues() => string[]Returns an array representing the VALUE_ATTR attributes of each menu item.
getMenuItemCount() => numberReturns the number of menu items.
getMenuItemAttr(menuItem: Element, attr: string) => stringReturns the given attribute on the the menu item element.
getMenuItemTextAtIndex(index: number) => stringGets the text content of the menu item element at the given index.
addClassAtIndex(menuItem: Element, className: string) => voidAdds the class name on the menu item at the given index.
removeClassAtIndex(menuItem: Element, className: string) => voidRemoves the class name on the menu item at the given index.

MDCSelectFoundation

Method SignatureDescription
notchOutline(openNotch: boolean) => voidOpens/closes the notched outline.
getDisabled() => booleanGets the disabled state.
setDisabled(isDisabled: boolean) => voidUpdates the disabled state.
handleFocus() => voidHandles a focus event on the select element.
handleBlur() => voidHandles a blur event on the select element.
handleClick(normalizedX: number) => voidSets the line ripple center to the normalizedX for the line ripple.
handleMenuOpened() => voidHandles menu or menu surface opened event.
handleMenuClosed() => voidHandles menu or menu surface closed event.
handleMenuItemAction() => voidHandles menu selected event.
handleChange() => voidHandles a change to the select element's value. This must be called both for change events and programmatic changes requested via the component API.
handleKeydown(event: KeyboardEvent) => voidHandles opening the menu when the mdc-select__selected-text element is focused and the user presses the Enter or Space key.
getSelectedIndex() => numberReturns the index of the currently selected menu item.
setSelectedIndex(index: number) => voidHandles setting the mdc-select__selected-text element and closing the menu. Also causes the label to float and outline to notch if needed.
getValue() => stringHandles getting the value through the adapter.
setValue() => stringSets the selected index to the index of the menu item with the given value.
setValid(isValid: boolean) => voidSets the valid state through the adapter.
isValid() => booleanGets the valid state through the adapter's checkValidity API.
setRequired(isRequired: boolean) => voidSets the required state through the adapter.
getRequired() => booleanGets the required state through the adapter.
init() => voidInitializes the foundation.
layout() => voidHandles determining if the notched outline should be notched.
setLeadingIconAriaLabel(label: string) => voidSets the aria label of the leading icon.
setLeadingIconContent(content: string) => voidSets the text content of the leading icon.
setHelperTextContent(content: string) => voidSets the content of the helper text.

MDCSelectFoundation supports multiple optional sub-elements: helper text and icon. The foundations of these sub-elements must be passed in as constructor arguments to MDCSelectFoundation.

Keywords

FAQs

Last updated on 09 Dec 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc