Skip to content

Expose _uid as public value in Vue instances #5886

@benjarwar

Description

@benjarwar

What problem does this feature solve?

I'm adding aria roles for accessibility to a Vue component. This requires setting a unique id attribute on an HTML element in my component's template. I don't have any additional requirements for the id, aside from it being unique on the page.

Rather than manage these instance attributes from a higher level, I'd like the component itself to create an id and render the appropriate aria-controls and id attributes. A good example is a collapsible/accordion-style UI, the template of which could look like this:

<template>
  <div class="collapsible">
    <button @click.prevent="toggle" :aria-controls="uid">
      <slot name="toggle"></slot>
    </button>
    <div :id="uid" v-show="expanded">
      <slot name="content"></slot>
    </div>
  </div>
</template>

What does the proposed API look like?

Using the template above, I'd have a method that sets the uid value. I don't like proposed solutions that reference this._uid, since that's a private value and subject to change in the future.

Instead, I'd like to set the uid value based upon a publicly available identifier at vm.$uid, something like this:

export default {
  data: () => {
    return {
      uid: ''
    }
  },
  created () {
    this.uid = `my-component-label-${this.$uid}`;
  }
}

That way, if I have multiple instances of the same component on the page, they get unique id attributes without needing to first declare those values from the parent application, or resorting to more complex means of generating random strings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions