项目作者: morkro

项目描述 :
Vue.js component for a11y-dialog
高级语言: JavaScript
项目地址: git://github.com/morkro/vue-a11y-dialog.git
创建时间: 2018-06-11T08:51:18Z
项目社区:https://github.com/morkro/vue-a11y-dialog

开源协议:MIT License

下载


Vue A11yDialog Build Status

This is a Vue.js wrapper component for a11y-dialog@7.3.0 (Demo on CodeSandbox).

Installation

This library supports both Vue 3 and Vue 2. However, active maintenance is focused on Vue 3. If you still need to support Vue 2, you can stay on version 0.5.2.

Vue 3

  1. npm install vue-a11y-dialog

Vue 2

  1. npm install vue-a11y-dialog@0.5.2

Usage

In your main.js application file, install the component:

  1. import { createApp } from 'vue'
  2. import A11yDialog from 'vue-a11y-dialog'
  3. import App from './App.vue'
  4. createApp(App).use(A11yDialog).mount('#app')

Then use it as follows:

  1. <template>
  2. <div id="app">
  3. <!-- ... -->
  4. <button type="button" @click="openDialog">
  5. Open dialog
  6. </button>
  7. <a11y-dialog
  8. id="app-dialog"
  9. @dialog-ref="assignDialogRef"
  10. >
  11. <template v-slot:title>
  12. <span>Your dialog title</span>
  13. </template>
  14. <div>
  15. <p>Your content</p>
  16. </div>
  17. </a11y-dialog>
  18. </div>
  19. </template>
  1. export default {
  2. name: 'YourComponent',
  3. data: () => ({
  4. dialog: null
  5. }),
  6. methods: {
  7. openDialog() {
  8. if (this.dialog) {
  9. this.dialog.show()
  10. }
  11. },
  12. assignDialogRef(dialog) {
  13. this.dialog = dialog
  14. }
  15. }
  16. }

It’s important to assign the direct reference to the dialog instance via @dialog-ref, otherwise there is no way to call its methods.

Alternatively, you can also import the component directly into your file without installing it first:

  1. import { A11yDialog } from 'vue-a11y-dialog'
  2. export default {
  3. name: 'YourComponent',
  4. components: {
  5. 'a11y-dialog': A11yDialog
  6. },
  7. methods: {
  8. // ...
  9. }
  10. }

Multiple dialogs

It’s possible to use multiple dialogs in the same component, just make sure to assign the different dialog instances separately.

In your <template>:

  1. <template>
  2. <div id="app">
  3. <!-- First dialog -->
  4. <a11y-dialog
  5. id="first-dialog"
  6. @dialog-ref="dialog => assignDialogRef('first', dialog)"
  7. >
  8. <template v-slot:title>
  9. <span>First dialog title</span>
  10. </template>
  11. <div>
  12. <p>Your content</p>
  13. </div>
  14. </a11y-dialog>
  15. <!-- Second dialog -->
  16. <a11y-dialog
  17. id="second-dialog"
  18. @dialog-ref="dialog => assignDialogRef('second', dialog)"
  19. >
  20. <template v-slot:title>
  21. <span>Second dialog title</span>
  22. </template>
  23. <div>
  24. <p>Your content</p>
  25. </div>
  26. </a11y-dialog>
  27. </div>
  28. </template>

In your <script>:

  1. import { A11yDialog } from 'vue-a11y-dialog';
  2. export default {
  3. name: 'YourComponent',
  4. data: () => ({
  5. dialogs: {}
  6. }),
  7. methods: {
  8. assignDialogRef(type, dialog) {
  9. this.dialogs[type] = dialog
  10. }
  11. }
  12. }

API

All a11y-dialog instance methods are available, for further documentation see here.

id

  • Property: id
  • Type: String
  • Required: true
  • Description: The unique HTML id attribute added to the dialog element, internally used by a11y-dialog to manipulate the dialog.
  • Usage:
  1. <a11y-dialog id="main-dialog">
  2. <!-- ... -->
  3. </a11y-dialog>

dialog-root

  • Property: dialog-root
  • Type: String — CSS Selector string.
  • Required: false
  • Default: 'body'
  • Description: The container for the dialog to be rendered into.
  • Usage:
  1. <a11y-dialog dialog-root="#dialog-root">
  2. <!-- ... -->
  3. </a11y-dialog>

class-names

  • Property: class-names
  • Type: Object
  • Required: false
  • Default: {}
  • Description: Object of classes for each HTML element of the dialog element. Keys are: container, overlay, document, title, closeButton. See a11y-dialog docs for reference.
  • Usage:
  1. <a11y-dialog :class-names="{ container: 'container-class', overlay: 'overlay-class' }">
  2. <!-- ... -->
  3. </a11y-dialog>

title-id

  • Property: title-id
  • Type: String
  • Required: false
  • Default: Defaults to id + '-title'.
  • Description: The HTML id attribute of the dialog’s title element, used by assistive technologies to provide context and meaning to the dialog window.
  • Usage:
  1. <a11y-dialog title-id="main-title">
  2. <!-- ... -->
  3. </a11y-dialog>

close-button-label

  • Property: close-button-label
  • Type: String
  • Required: false
  • Default: 'Close this dialog window'
  • Description: The HTML aria-label attribute of the close button, used by assistive technologies to provide extra meaning to the usual cross-mark.
  • Usage:
  1. <a11y-dialog close-button-label="Close dialog">
  2. <!-- ... -->
  3. </a11y-dialog>

role

  • Property: role
  • Type: String
  • Required: false
  • Default: dialog
  • Description: The role attribute of the dialog element, either dialog (default) or alertdialog to make it a modal (preventing closing on click outside of ESC key).
  • Usage:
  1. <a11y-dialog role="alertdialog">
  2. <!-- ... -->
  3. </a11y-dialog>

Events

dialog-ref

  • Returns: An a11y-dialog instance or undefined.
  • Description: This event emits the a11y-dialog instance once the component has been initialised. When it gets destroyed, the event returns undefined. This is needed to call instance methods of the dialog, e.g. this.dialog.show().
  • Usage:
  1. <a11y-dialog @dialog-ref="assignDialogRef">
  2. <!-- ... -->
  3. </a11y-dialog>
  1. export default {
  2. // ...
  3. methods: {
  4. assignDialogRef(dialog) {
  5. this.dialog = dialog
  6. }
  7. }
  8. }

Slots

title

  • Name: title
  • Description: The title of the dialog, mandatory in the document to provide context to assistive technology. Could be hidden with CSS (while remaining accessible).
  • Usage:
  1. <a11y-dialog>
  2. <template v-slot:title>
  3. <span>Your dialog title</span>
  4. </template>
  5. <!-- ... -->
  6. </a11y-dialog>

closeButtonContent

  • Name: closeButtonContent
  • Default: \u00D7 (×)
  • Description: The string that is the inner HTML of the close button.
  • Usage:
  1. <a11y-dialog>
  2. <template v-slot:closeButtonContent>
  3. <span>Close dialog</span>
  4. </template>
  5. <!-- ... -->
  6. </a11y-dialog>

closeButtonPosition

  • Name: closeButtonPosition
  • Default: first
  • Description: One of first, last, or none
  • Usage:
  1. <a11y-dialog close-button-position="last">
  2. <template v-slot:closeButtonContent>
  3. <span>Close dialog</span>
  4. </template>
  5. <!-- ... -->
  6. </a11y-dialog>

Server-side Rendering

This is a client-only component; a11y-dialog won’t render anything on the server and wait for your bundle to be executed on the client.