logo polydile dile-components

dile-select

This component uses the native <select> element to create a dropdown select interface.

The diferences between the native <select> are:

Installation

npm i @dile/dile-select

dile-select Usage

Import the component.

import '@dile/dile-select/dile-select.js';

Use the component.

<dile-select id="select1">
  <select slot="select">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
</dile-select>

Properties

Methods

Events

CSS custom properties

You can customize the selector using the CSS custom properties bellow.

Custom propertyDescriptionDefault
--dile-select-font-sizeSelect element font size0.875em
--dile-input-label-font-sizeFont size for the label1em
--dile-input-label-colorColor for the label text#59e
--dile-input-label-font-weightLabel text font weightnormal
--dile-input-label-margin-bottomLabel marging bottom4px
--dile-input-widthSelect element width100%
--dile-input-border-widthSelect element border width1px
--dile-input-border-colorSelect element border color#888
--dile-input-border-radiusSelect element border radius5px
--dile-input-paddingPadding for the select5px
--dile-input-colorInput text color#303030
--dile-input-background-colorColor for the background select element#fff
--dile-input-focus-border-colorInput element border on focus#6af
--dile-input-error-border-colorInput element border on errored property = true#c00
--dile-input-disabled-border-colorInput element border when disabled#eee
--dile-input-message-padding-topSpace from input to message4px
--dile-input-message-font-sizeMessage font size0.875em
--dile-input-message-colorMessage text color#888
--dile-input-message-error-colorMessage text color on errored state#c00

When --dile-input-background-color is configured to a dark color the component has the posibility to put a "dark" class in the <select> tag to create a drop icon with suficient contrast. You need to put this clas by yourself.

<dile-select label="Select one option" class="styled">
  <select slot="select" class="dark">
    <option value="1">Option 1</option>
    <option value="2" disabled>Option 2</option>
    <option value="3">Option 3</option>
  </select>
</dile-select>

dile-select demos

Default dile-select

class MyComponent extends LitElement {

  render() {
    return html`
      <dile-select id="select1" value="3">
        <select slot="select">
          <option value="1">Option 1</option>
          <option value="2">Option 2</option>
          <option value="3">Option 3</option>
        </select>
      </dile-select>
      <p id="msg1"></p>
    `
  }
  firstUpdated() {
    this.shadowRoot.getElementById('select1').addEventListener('element-changed', (e) => {
      let textElement = this.shadowRoot.getElementById('msg1');
      textElement.innerText = "The value selected is: " + e.detail.value;
    });
  }
}
customElements.define('my-component', MyComponent);
export const JsStory = () => html`<my-component></my-component>`;

Disabled

<dile-select disabled value="c">
  <select slot="select">
    <option value="a">Option a</option>
    <option value="b">Option b</option>
    <option value="c">Option c</option>
  </select>
</dile-select>

Styled with label

<style>
  dile-select.styled {
    --dile-input-background-color: red;
    --dile-input-color: #fff;
  }
</style>
<dile-select label="Select one option" class="styled">
  <select slot="select" class="dark">
    <option value="1">Option 1</option>
    <option value="2" disabled>Option 2</option>
    <option value="3">Option 3</option>
  </select>
</dile-select>

Errored

<dile-select errored message="One message..." hideErrorOnInput>
  <select slot="select">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
</dile-select>