LoginSignup
4
2

More than 1 year has passed since last update.

v-dialogでよく使うattrsってなに?

Last updated at Posted at 2021-12-11

要約

  • Vuetify2ののactivatorスロットのスロットプロパティの意味を調べる
  • attrsはアクセシビリティのための属性が入ったオブジェクト

動機

Vuetifyにはv-dialogという、ダイアログを実装するためのコンポーネントが用意されており、実装例として、公式では次のコードが紹介されている

<template>
  <div class="text-center">
    <v-dialog
      v-model="dialog"
      width="500"
    >
      <template v-slot:activator="{ on, attrs }">
        <v-btn
          color="red lighten-2"
          dark
          v-bind="attrs"
          v-on="on"
        >
          Click Me
        </v-btn>
      </template>

      <v-card>
        <v-card-title class="text-h5 grey lighten-2">
          Privacy Policy
        </v-card-title>

        <v-card-text>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </v-card-text>

        <v-divider></v-divider>

        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn
            color="primary"
            text
            @click="dialog = false"
          >
            I accept
          </v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        dialog: false,
      }
    },
  }
</script>

v-dialogactivatorスロットにv-btnを与え、スロットプロパティからはonプロパティおよびattrsプロパティを取り出している。
このonプロパティはイベントハンドラであり、v-btnv-onプロパティに与えることで、画面のボタンをクリックするとダイアログが表示される。

それでは、attrsプロパティとはなんだろうか?v-dialogAPIのドキュメントを読んでも、プロパティとしてはonvalueしか説明がない。気になったので、attrsの正体を探した。

結論

このプロパティの正体は、ダイアログ展開のためのトリガとなるコンポーネントに指定すべきWAI-ARIA属性のようだ。
実際v-dialogactivatorスロットでボタンにバインドされたattrsには、ダイアログが閉じている状態では次のオブジェクトが入っている。

{
  "role": "button",
  "aria-haspopup": true,
  "aria-expanded": false
}

aria-expanded属性は、ダイアログを展開するとtrueになる。

WAI-ARIA属性については次が詳しい。
https://developer.mozilla.org/ja/docs/Learn/Accessibility/WAI-ARIA_basics

Vuetifyでは、このような属性を付与しやすくし、アクセシビリティを担保しやすくしているようだ。
もちろん、バインド対象によってroleは変わらない。divinputにバインドしてもroleはbuttonのままである。

4
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2