Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

svelte-multiselect

Package Overview
Dependencies
Maintainers
1
Versions
76
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svelte-multiselect

Svelte multi-select component

  • 1.1.10
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
13K
decreased by-31.99%
Maintainers
1
Weekly downloads
 
Created
Source

Svelte MultiSelect

Svelte MultiSelect Netlify Status NPM version

Live demo.

Keyboard-friendly, zero-dependency multi-select Svelte component.

Key Features

  • Single / multiple select: pass maxSelect={1} prop to only allow one selection
  • Dropdowns: scrollable lists for large numbers of options
  • Searchable: start typing to filter options
  • Tagging: selected options are recorded as tags within the text input
  • Server-side rendering: no reliance on browser objects like window or document
  • Configurable: see section props
  • No dependencies: needs only Svelte as dev dependency
  • Keyboard friendly for mouse-less form completion

Installation

yarn add -D svelte-multiselect

Usage

<script>
  import MultiSelect from 'svelte-multiselect'

  const webFrameworks = [`Svelte`, `React`, `Vue`, `Angular`, `Polymer`, `Ruby on Rails`, `ASP.net`, `Laravel`, `Django`, `Express`, `Spring`]

  const name = `webFrameworks`
  const placeholder = `Take your pick...`
  const required = true

  let selected
</script>

Favorite Web Frameworks?

<MultiSelect bind:selected {name} {placeholder} options={webFrameworks} {required} />

Props

Full list of props/bindable variables for this component:

namedefaultdescription
options[required]Array of strings (or integers) that will be listed in the dropdown selection.
maxSelectnullnull or positive integer to allow users to select as many as they like or a maximum number of options, respectively.
selected[] (or '' if maxSelect === 1)Array of currently/pre-selected options when binding/passing as props respectively.
readonlyfalseDisables the input. User won't be able to interact with it.
placeholder''String shown when no option is selected.
requiredfalsePrevents submission in an HTML form when true.
inputundefinedHandle to the DOM node storing the currently selected options in JSON format as its value attribute.
name''Used as reference for associating HTML form labels with this component as well as for the input id. That is, the same DOM node input bindable through <MultiSelect bind:input /> is also retrievable via document.getElementByID(name) e.g. for use in a JS file outside a Svelte component.

Events

MultiSelect.svelte dispatches the following types of events:

  • on:add={(event) => console.log(event.detail.token)}: Triggers when a new token is selected.
  • on:remove={(event) => console.log(event.detail.token)}: Triggers when a selected token is removed.
  • on:change={(event) => console.log(event.detail.token)}: Triggers when a new token is either added or removed.
  • on:blur={yourFunctionHere}: Triggers when the input field looses focus.

Will add a section to the readme as well as a new event on:change that triggers for both add and remove.

Example:

<MultiSelect on:change={(event) => alert(`You selected '${event.detail.token}'`)} />

Want to contribute?

To submit a PR, clone the repo, install dependencies and start the dev server to try out your changes first.

git clone https://github.com/janosh/svelte-multiselect
cd svelte-multiselect
yarn
yarn dev

Styling

There are 3 ways to style this component.

With CSS variables

The first, if you only want to make small adjustments, allows you to pass the following CSS variables directly to the component as props.

  • border: var(--sms-border, 1pt solid lightgray): Border around top-level div.multiselect. Change this to e.g. to 1px solid red to indicate this form field is in an invalid state.
  • border-radius: var(--sms-border-radius, 5pt): div.multiselect border radius.
  • color: var(--sms-text-color, inherit): Input text color.
  • border: var(--sms-focus-border, 1pt solid var(--sms-active-color, cornflowerblue)): div.multiselect border when focused.
  • background: var(--sms-readonly-bg, lightgray): Background when in readonly state.
  • background: var(--sms-token-bg, var(--sms-active-color, cornflowerblue)): Background of selected tokens.
  • color: var(--sms-remove-x-hover-color, lightgray): Hover color of cross icon to remove selected tokens.
  • background: var(--sms-options-bg, white): Background of options list.
  • background: var(--sms-li-selected-bg, inherit): Background of selected list items in options pane.
  • color: var(--sms-li-selected-color, inherit): Text color of selected list items in options pane.
  • background: var(--sms-li-active-bg, var(--sms-active-color, cornflowerblue)): Background of active (currently with arrow keys highlighted) list item.

For example, to change the background color of the options dropdown:

<MultiSelect --sms-options-bg="var(--my-css-var, white)" />

With CSS frameworks

The second method allows you to pass in custom classes to the important DOM elements of this component to target them with frameworks like Tailwind CSS.

  • outerDivClass
  • ulTokensClass
  • liTokenClass
  • ulOptionsClass
  • liOptionClass

This simplified version of the DOM structure of this component shows where these classes are inserted:

<div class={outerDivClass}>
  <ul class={ulTokensClass}>
    <li class={liTokenClass}>First selected tag</li>
    <li class={liTokenClass}>Second selected tag</li>
  </ul>
  <ul class={ulOptionsClass}>
    <li class={liOptionClass}>First available option</li>
    <li class={liOptionClass}>Second available option</li>
  </ul>
</div>

Granular control through global CSS

You can alternatively style every part of this component with more fine-grained control by using the following :global() CSS selectors. Note: Overriding properties that the component already sets internally requires the !important keyword.

:global(.multiselect) {
  /* top-level wrapper div */
}
:global(.multiselect ul.tokens > li) {
  /* the blue tags representing selected options with remove buttons inside the input */
}
:global(.multiselect ul.tokens > li button),
:global(.multiselect button.remove-all) {
  /* buttons to remove a single or all selected options at once */
}
:global(.multiselect ul.options) {
  /* dropdown options */
}
:global(.multiselect ul.options li) {
  /* dropdown options */
}
:global(ul.options li.selected) {
  /* selected options in the dropdown list */
}
:global(ul.options li:not(.selected):hover) {
  /* unselected but hovered options in the dropdown list */
}
:global(ul.options li.selected:hover) {
  /* selected and hovered options in the dropdown list */
  /* probably not necessary to style this state in most cases */
}
:global(ul.options li.active) {
  /* active means element was navigated to with up/down arrow keys */
  /* ready to be selected by pressing enter */
}
:global(ul.options li.selected.active) {
}

Keywords

FAQs

Package last updated on 12 Aug 2021

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc