npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-unique-id

v3.2.1

Published

Component and HTML unique id generation plugin for Vue.js

Downloads

24,626

Readme

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