Giter VIP home page Giter VIP logo

details-polyfill's Introduction

details-polyill

Polyfill for the HTML5 <details> element, no dependencies


Usage

details-polyfill is available via npm.

npm install --save details-polyfill

Requiring it will immediately inject the needed behaviors.

require('details-polyfill')

The file index.js is also usable as a standalone script.


How it works

Just include the script on any page that uses `
`.

In case the browser doesn't support <details>, it adds the following behaviors:

  • When clicking details > summary, it toggles the open attribute in details.

It also adds these CSS styles:

  • summary:before is styled with a disclosure triangle.
  • details:not([open]) > :not(summary) elements are hidden. (that is: all children of closed details, except summary)
  • The <html> element gets the no-details class.

Limitations

Keep these guidelines in mind.

No loose text

The <details> element must not have loose text inside it. Everything inside it should be in elements.

<!-- ✗ This will NOT work -->
<details>
  <summary>More info...</summary>
  No info available.
</details>
<!-- ✓ But this will -->
<details>
  <summary>More info...</summary>
  <span>No info available.</span>
</details>

Don't style summary::before

...unless it's for a disclosure triangle. This library uses summary::before to create a default triangle.

Summary as first child

The summary element must also be the first child of the details element. Browsers supporting the details element natively will hoist summary elements up, but details-polyfill.js won't.

JavaScript el.open = true

...will not work like how you think it does. You also need to do .setAttribute('open', 'open') or .removeAttribute('open').

Alternatives

Here are a few other polyfills for `
`.

I wrote this because everything else out there either depend on big libraries, or are too complicated. In contrast, details-polyfill has <100 lines of code, and only comes with a .js file.

Thanks

details-polyfill © 2016+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz

details-polyfill's People

Contributors

rstacruz avatar

Watchers

 avatar  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.