2.0.5 โ€ข Published 2 years ago

@jonasclement/vue-accessible-select v2.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

vue-accessible-select

Vue.js accessible select component made according to WAI-ARIA practices.

โœจ Features

  • fully accessible;
  • โŒจ๏ธ keyboard navigation (Page Up/Down, Home, End, Esc);
  • ๐Ÿ”ฃ type-ahead to select option that starts with typed symbols;
  • ๐Ÿ’… style agnostic, so you can style it whatever you like (but including core.scss is highly encouraged).

๐Ÿ’ฟ Installation

๐Ÿ“ฆ Via NPM

$ npm install @jonasclement/vue-accessible-select --save

๐Ÿงถ Via Yarn

$ yarn add @jonasclement/vue-accessible-select

Initialization

As a plugin

import { createApp } from 'vue'
import VueAccessibleSelect from 'vue-accessible-select'

const app = createApp()
app.use(VueAccessibleSelect)

As a global component

import { createApp } from 'vue'
import { VueAccessibleSelect } from 'vue-accessible-select'

app.component('VueAccessibleSelect', VueAccessibleSelect)

As a local component

import { VueAccessibleSelect } from 'vue-accessible-select'

export default {
  name: 'YourAwesomeComponent',
  components: {
    VueAccessibleSelect,
  },
}

โ„น๏ธ Note to set global options (for example transition for each select component), you should do the following:

import { config } from 'vue-accessible-select'

config.transition = {
  name: 'foo',
}

โš ๏ธ Options passed locally via props will always take precedence over global config options.

Default config.js:

export default {
  transition: null,
}

๐Ÿš€ Usage

Template

<template>
  <vue-accessible-select
    v-model="value"
    :options="options"
    :transition="{ name: 'foo' }"
    label="foo"
    placeholder="bar"
    disabled
  ></vue-accessible-select>
</template>

Script

export default {
  // ...
  data() {
    return {
      value: undefined,
      options: [
        {
          value: 0,
          label: '๐Ÿ‡ Grape',
        },
        {
          value: { foo: 'bar' },
          label: '๐Ÿ‰ Watermelon',
        },
        {
          value: { foo: 'bar' },
          label: '๐Ÿฅ Kiwi',
        },
        {
          value: false,
          label: '๐Ÿฅญ Mango',
        },
        {
          value: true,
          label: '๐Ÿ“ Strawberry',
        },
        {
          value: 'lemon',
          label: '๐Ÿ‹ Lemon',
        },
      ],
    }
  },
  // ...
}

๐ŸŽจ Styles

Then don't forget to include core styles. Also library is sipped with default theme styles you can use.

SASS:

// recommended
@import 'vue-accessible-select/src/styles/core.scss';

// optional
@import 'vue-accessible-select/src/styles/themes/default.scss';

Or already compiled CSS:

/* recommended */
@import 'vue-accessible-select/dist/styles/core.scss';

/* optional */
@import 'vue-accessible-select/dist/styles/themes/default.scss';

โš ๏ธ Note that when you import already compiled CSS you don't have ability to override SASS variables during build process, so it is preferable to use .scss file.

When importing core.scss, there are SASS variables you can override during build process:

$v-select-menu-position-top: 100% !default;
$v-select-arrow-size: 8px !default;

API

โš™๏ธ Props

<vue-accessible-select> accepts some props:

PropDescription
options: arrayrequired Array of select options. Should be an array of objects that match the following pattern { value: any, label: string }
value: anyrequired Current value of select. When value is undefined, it is considered that select has no current value.
label: stringSelect label
placeholder: stringSelect placeholder
disabled: booleanWhether select is disabled
transition: objectThrough this object you can configure the transition of .v-select__menu entrance and leave. Should match the following pattern { name: string, mode: string? }

๐Ÿ•ณ๏ธ Slots

<vue-accessible-select> provides you with some slots and scopedSlots you can use to fit your needs.

SlotScopeDescription
labelLabel slot
prependPrepend slot
placeholder{ placeholder }Placeholder slot
selected{ value, option }Selected slot
arrowArrow slot
option{ value, option }Option slot
no-optionsNo options slot

Example of possible usage of slots and scopedSlots

<vue-accessible-select>
  <template v-slot:label>
    <strong>๐Ÿ˜‹ Just a label slot:</strong>
  </template>
  <template v-slot:prepend>
    <svg viewBox="0 0 54 54">
      <path d="M27 1l8 17 19 3-14 13 4 19-17-9-17 9 3-19L0 21l19-3z" />
    </svg>
  </template>
  <template v-slot:placeholder
    >๐ŸŽƒ I am a placeholder slot</template
  >
  <template v-slot:selected="{ value, option }"
    >๐Ÿ”ฅ Woooow, {{ option.label }}</template
  >
  <template v-slot:option="{ value, option }"
    >Option: {{ option.label }}</template
  >
  <template v-slot:no-options
    >Keine Optionen</template
  >
</vue-accessible-select>

โŒจ๏ธ Keyboard shortcuts

<vue-accessisble-select> is fully accessible when it comes to keyboard interaction.

Here is some useful keys and their appropriate actions:

  • Down Arrow โ€“ Moves focus and selection to the next option.
  • Up Arrow โ€“ Moves focus and selection to the previous option.
  • Home โ€“ Moves focus and selection to the first option.
  • End โ€“ Moves focus and selection to the last option.
  • Esc โ€“ Closes menu.

Type ahead:

  • Type a character: focus and selection moves to the next option with a label that starts with the typed character;
  • Type multiple characters in rapid succession: focus and selection moves to the next option with a label that starts with the string of characters typed.

Note for developers

For this Vue 3-compatible version, the demo does not build correctly.

Powered by

  • Vite;
  • SASS
  • PostCSS;
  • Autoprefixer.

๐Ÿ”’ License

MIT

2.0.3

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago