项目作者: jshcrowthe

项目描述 :
An a11y equipped dialog element written for the modern browser with Web Components
高级语言: HTML
项目地址: git://github.com/jshcrowthe/dialog-el.git
创建时间: 2016-07-21T05:40:23Z
项目社区:https://github.com/jshcrowthe/dialog-el

开源协议:

下载


Build Status
Published on webcomponents.org

This repo is a Custom Element for creating accessible dialogs/modals It is heavily inspired by Polymer’s paper-dialog and the A11y Dialog fork from Edenspiekermann.

Dependencies

There are no non-native dependencies in this Web Component. If your browser supports the following two things you are good. If not you will need to make sure you load the Polyfills first

Utilization

Examples:

HTML
<!—

  1. <custom-element-demo>
  2. <template>
  3. <script src="../webcomponentsjs/webcomponents.js"></script>
  4. <link rel="import" href="../paper-button/paper-button.html">
  5. <link rel="import" href="dialog-el.html">
  6. <style>
  7. .centered {
  8. position: relative;
  9. display: flex;
  10. justify-content: center;
  11. align-items: center;
  12. height: 200px
  13. }
  14. .content paper-button {
  15. color: #FFFFFF;
  16. background-color: #5c6bc0;
  17. }
  18. </style>
  19. <div class='centered content'>
  20. <paper-button onclick='dialog.show()'>Open Dialog</paper-button>
  21. <paper-button onclick='modal.show()'>Open Modal</paper-button>
  22. <next-code-block></next-code-block>
  23. </div>
  24. </template>
  25. </custom-element-demo>

—>

  1. <dialog-el id='dialog'>
  2. <h1>A header</h1>
  3. <p>Dialog Content</p>
  4. </dialog-el>
  5. <dialog-el id='modal' modal>
  6. <h1>A modal</h1>
  7. <p>Modal Content</p>
  8. </dialog-el>

JavaScript

  1. var dialogEl = document.createElement('dialog-el');
  2. document.body.appendChild(dialogEl);

Methods

  • dialog.show

    This is the means of displaying the dialog/modal. Simply select the element and call this method to display the dialog/modal.

    Example:

    1. var dialog = document.querySelector('dialog-el');
    2. dialog.show();

    This function returns a Promise that you can use to perform operations after the dialog/modal has displayed.

    NOTE: The show function, if called on an already open dialog, will throw an error

    dialog.show can also take an optional DOM Element as an argument. If passed the dialog will attempt to intelligently
    position itself relative to the passed element.

    Example:

    1. var dialog = document.querySelector('dialog-el');
    2. var button = document.querySelector('#myMagicButton');
    3. dialog.show(button);

    Positioning will be handled based on the existence of a valid arrowPosition value. If the value does not exist
    the dialog will position itself where there is the most available screen real estate. If the value does exist,
    the dialog will be positioned such that the arrow points at the center of the passed element.

    Restrictions

    • If your dialog-el is positioned inside of a container with overflow: hidden this can cause
      the dialog to not be visible on the screen.
    • Your dialog must have a fixed height & width to ensure correct positioning, consider doing
      something like the following:

      1. <style>
      2. .sizeMe {
      3. width: 500px;
      4. height: 300px;
      5. }
      6. </style>
      7. <dialog-el>
      8. <div class='sizeMe'>
      9. <!--Content-->
      10. </div>
      11. </dialog-el>
  • dialog.close

    This is the means of closing an open dialog/modal. Simply select the element and call this method to close it.

    Example:

    1. var dialog = document.querySelector('dialog-el');
    2. dialog.close();

    This function returns a Promise that you can use to perform operations after the dialog/modal has closed.

    NOTE: The close function, if called on an already closed dialog, will throw an error

Properties

  • modal

    If set the dialog will render as a fixed position modal instead of an absolute positioned dialog.

  • arrowDirection

    Based on the value of this property (left, right, top, bottom), it will render an arrow on that side of the dialog. For example, a value of left adds an arrow to the left side of the dialog.

Example:

  1. <dialog-el arrow-direction='left'>
  2. <h1>A header</h1>
  3. <p>Dialog Content</p>
  4. </dialog-el>
  1. var dialog = document.createElement('dialog-el');
  2. dialog.arrowDirection = 'left';
  3. document.body.appendChild(dialog);

NOTE: This does not work with the modal property

Events

  • dialog-opened

    Fired whenever the dialog is opened.

  • dialog-closed

    Fired whenever the dialog is closed.

CSS Custom Properties

Use these to override default styles

Property Name Description Default Value
--dialog-el-padding Used as the padding for the dialog 15px
--dialog-el-background Used for the background property of the dialog #FFFFFF

These defaults effectively wrap your provided local DOM in a 15px white border. Override them to change the appearance.