All Projects → berniegp → vue-unique-id

berniegp / vue-unique-id

Licence: ISC license
Component and HTML unique id generation plugin for Vue.js

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to vue-unique-id

v-dropdown-menu
Dropdown menu plugin for vuejs, supported ssr.
Stars: ✭ 23 (-50%)
Mutual labels:  vue-plugin
vue-link
One component to link them all 🔗
Stars: ✭ 65 (+41.3%)
Mutual labels:  vue-plugin
X-Browser-Update-Vue
A Vue.js browser-update plugin
Stars: ✭ 25 (-45.65%)
Mutual labels:  vue-plugin
vue-plausible
Plausible Analytics Vue.js Plugin and NuxtJS Module
Stars: ✭ 107 (+132.61%)
Mutual labels:  vue-plugin
vue-plugin-boilerplate
Vue Plugin Boilerplate
Stars: ✭ 120 (+160.87%)
Mutual labels:  vue-plugin
vue-translator
A deadly simple i18n translate plugin for Vue, ready for Server Side Rendering.
Stars: ✭ 18 (-60.87%)
Mutual labels:  vue-plugin
generator-vue-plugin
Yeoman generator generating vue plugin 🚀
Stars: ✭ 29 (-36.96%)
Mutual labels:  vue-plugin
vue-methods-promise
Let Vue methods support return Promise
Stars: ✭ 35 (-23.91%)
Mutual labels:  vue-plugin
lazyload-vue
Vue Plugin for vanilla-lazyload
Stars: ✭ 29 (-36.96%)
Mutual labels:  vue-plugin
vue-cli-plugin-chrome-ext
vue cli plugin that setup vue project for chrome extension
Stars: ✭ 86 (+86.96%)
Mutual labels:  vue-plugin
vue-package-template
Boilerplate for your next, ES6 Vue.js package. Ready for npm deployments
Stars: ✭ 12 (-73.91%)
Mutual labels:  vue-plugin
vue-drag-zone
Drag Zone component for @vuejs
Stars: ✭ 127 (+176.09%)
Mutual labels:  vue-plugin
vue-next-rx
RxJS integration for Vue next
Stars: ✭ 31 (-32.61%)
Mutual labels:  vue-plugin
vue-img-orientation-changer
A vue plugin that can help you display image in correct orientation.
Stars: ✭ 38 (-17.39%)
Mutual labels:  vue-plugin
vuejs-emoji
vue2.x emoji plugin, autoload fontawesome
Stars: ✭ 32 (-30.43%)
Mutual labels:  vue-plugin
vue-bus
Tiny simple central event bus plugin for Vue.js
Stars: ✭ 50 (+8.7%)
Mutual labels:  vue-plugin
vite-plugin-webfont-dl
⚡ Webfont Download Vite Plugin - Make your Vite site load faster
Stars: ✭ 69 (+50%)
Mutual labels:  vue-plugin
vue-auto-storage
🍻 An automatic storage plugin for Vue2, persist the data with localStorage.
Stars: ✭ 84 (+82.61%)
Mutual labels:  vue-plugin
vue-shell
Component VueJS to simulate a terminal.
Stars: ✭ 36 (-21.74%)
Mutual labels:  vue-plugin
vcrop
A simple Vue image cropping component.
Stars: ✭ 14 (-69.57%)
Mutual labels:  vue-plugin

Build Status

vue-unique-id

Vue.js plugin that generates component-scoped HTML identifiers for use in form inputs, labels, jQuery plugins, etc. This solves the problem of generating unique HTML ids for DOM elements when creating reusable Vue components that can be instantiated multiple times in a page.

As a bonus, this plugin also generates a unique id property (uid by default) for each Vue.js component.

This plugin has no external dependencies.

Installation

via npm (node package manager)

$ npm install vue-unique-id

Then configure Vue to use the plugin:

import UniqueId from 'vue-unique-id';
// or
const UniqueId = require('vue-unique-id');

Vue.use(UniqueId);

Examples

Unique Component Identifier

Each Vue component has a unique uid property.

const MyComponent = {
  created() {
    console.log("This component's unique id is: " + this.uid);
  },
};

Form Inputs with Labels

In order for an <input>'s <label> to work correctly, a unique id must be assigned to the <input> and referenced by the <label>. This example shows how a reusable Vue component containing labelled <input>s can generate a unique id for each of its <input> elements.

Vue template:

<label :for="$id('field1')">Field 1</label>
<input :id="$id('field1')" type="text" />

<label :for="$id('field2')">Field 2</label>
<input :id="$id('field2')" type="text" />

Rendered output:

<label for="uid-42-field1">Field 1</label>
<input id="uid-42-field1" type="text" />

<label for="uid-42-field2">Field 2</label>
<input id="uid-42-field2" type="text" />

Bootstrap Collapse Button

In this example, a unique id is assigned to the div.collapse element and referenced by the toggle buttons.

Vue template:

<p>
  <a class="btn btn-primary" data-toggle="collapse" :href="$idRef('collapseExample')">
    Link with href
  </a>
  <button class="btn btn-primary" type="button" data-toggle="collapse" :data-target="$idRef('collapseExample')">
    Button with data-target
  </button>
</p>
<div class="collapse" :id="$id('collapseExample')">
  <div class="card card-body">
    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
  </div>
</div>

Rendered output:

<p>
  <a class="btn btn-primary" data-toggle="collapse" href="#uid-42-collapseExample">
    Link with href
  </a>
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#uid-42-collapseExample">
    Button with data-target
  </button>
</p>
<div class="collapse" id="uid-42-collapseExample">
  <div class="card card-body">
    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
  </div>
</div>

API

vm.uid

Immutable string containing the component's unique identifier. The uid property name can be changed with the plugin options.

vm.$id(id = '')

Component-scoped HTML id generator. The optional id parameter specifies the id to scope to the component.

vm.$idRef(id = '')

Component-scoped HTML id reference (href) generator. The optional id parameter specifies the id to scope to the component. Prepends '#' to the id generated by vm.$id(id).

Plugin Options

const DEFAULTS = {
  // {string} Property name of the component's unique identifier. Change this if 'vm.uid' conflicts
  // with another plugin or your own props.
  uidProperty: 'uid',

  // {string} Prefix to use when generating HTML ids. Change this to make your ids more unique on a
  // page that already uses or could use a similar naming scheme.
  uidPrefix: 'uid-',
};

Run Unit Tests

$ npm test

Contributing

Contributors are welcome! See here for more info.

License

ISC

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].