GithubHelp home page GithubHelp logo

alexxnica / dialog-polyfill Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlechrome/dialog-polyfill

0.0 2.0 0.0 199 KB

Polyfill for the HTML dialog element

License: BSD 3-Clause "New" or "Revised" License

CSS 0.92% JavaScript 65.43% HTML 33.65%

dialog-polyfill's Introduction

dialog-polyfill.js is a polyfill for <dialog>. Check out some demos!

<dialog> is an element for a popup box in a web page, including a modal option which will make the rest of the page inert during use. This could be useful to block a user's interaction until they give you a response, or to confirm an action. See the HTML spec.

Usage

Installation

You may optionally install via NPM or Bower-

$ npm install dialog-polyfill
$ bower install dialog-polyfill

Supports

This polyfill works on modern versions of all major browsers. It also supports IE9 and above.

Steps

  1. Include the CSS in the <head> of your document, and the Javascript anywhere before referencing dialogPolyfill.
  2. Create your dialog elements within the document. See limitations for more details.
  3. Register the elements using dialogPolyfill.registerDialog(), passing it one node at a time. This polyfill won't replace native support.
  4. Use your <dialog> elements!

Example

<head>
  <link rel="stylesheet" type="text/css" href="dialog-polyfill.css" />
</head>
<body>
  <dialog>
    I'm a dialog!
    <form method="dialog">
      <input type="submit" value="Close" />
    </form>
  </dialog>
  <script src="dialog-polyfill.js"></script>
  <script>
    var dialog = document.querySelector('dialog');
    dialogPolyfill.registerDialog(dialog);
    // Now dialog acts like a native <dialog>.
    dialog.showModal();
  </script>
</body>

::backdrop

In native <dialog>, the backdrop is a pseudo-element. When using the polyfill, the backdrop will be an adjacent element:

dialog::backdrop { /* native */
  background-color: green;
}
dialog + .backdrop { /* polyfill */
  background-color: green;
}

Limitations

In the polyfill, modal dialogs have limitations-

  • They should not be contained by parents that create a stacking context, see below
  • The browser's chrome may not always be accessible via the tab key
  • Changes to the CSS top/bottom values while open aren't retained

Stacking Context

The major limitation of the polyfill is that dialogs should not have parents that create a stacking context. The easiest way to solve this is to move your <dialog> element to be a child of <body>.

If this isn't possible you may still be able to use the dialog. However, you may want to resolve it for two major reasons-

  1. The polyfill can't guarantee that the dialog will be the top-most element of your page
  2. The dialog may be positioned incorrectly as they are positioned as part of the page layout where they are opened (defined by spec), and not at a fixed position in the user's browser.

To position a dialog in the center (regardless of user scroll position or stacking context), you can specify the following CSS-

dialog {
  position: fixed;
  top: 50%;
  transform: translate(0, -50%);
}

dialog-polyfill's People

Contributors

addyosmani avatar agektmr avatar blanchg avatar brunoeberhard avatar craigfrancis avatar cvrebert avatar davidlehn avatar dinoboff avatar ebidel avatar ffuubarbuzz avatar flimm avatar garbee avatar jf10r avatar karlhorky avatar krman009 avatar lfbayer avatar matzko avatar merih avatar mfalken avatar niksy avatar puf avatar rowno avatar samthor avatar staugaard avatar suhajdab avatar timoxley avatar uniqname avatar xballoy avatar xixora avatar zmitroc avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.