Code Monkey home page Code Monkey 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 <details>.

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

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

flimm avatar garbee avatar moraghughson avatar rstacruz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

details-polyfill's Issues

Summary text doesn't trigger on click if it has children elements

For example:

<details class="faq__accordion-button"> <summary>Donec ac odio tempor orci. Sed sed risus pretium quam celeri?</summary> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Magna eget est lorem ipsum dolor sit. Gravida neque convallis a cras semper. Dolor magna eget est lorem ipsum. Pellentesque adipiscing commodo elit at imperdiet dui. Ut tortor pretium viverra suspendisse.</p> <a href="#">Sample Link</a> </details> is valid and triggers fine in IE11, but
<details class="faq__accordion-button"> <summary><span></span>Donec ac odio tempor orci. Sed sed risus pretium quam celeri?</span></summary> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Magna eget est lorem ipsum dolor sit. Gravida neque convallis a cras semper. Dolor magna eget est lorem ipsum. Pellentesque adipiscing commodo elit at imperdiet dui. Ut tortor pretium viverra suspendisse.</p> <a href="#">Sample Link</a> </details> will not

Need new line inserted when <details> tag opened.

This project is great - have had some users on IE or pre-Chromium-Edge that have benefitted from this. One thing that is not perfect that I thought you might like to know about is that it would be better to have a new line inserted when the <detail> tag is opened. Currently in IE, the content of the <detail> tag is on the same line as the opened tag. When you have nested tags this looks a bit odd.

I have tried changing the inserted style from display:inline-block to display:block, which fixes the newline problem but then the arrow is not on the same line as the rest of the <detail> tag so it's better in one way and worse in another. It could be because IE is not good at display:inline-block, but I thought you'd like to know anyway, in case you can make it better.

trigger "toggle" action

The native <details> element triggers a toggle action whenever it is toggled.

Something like the following might work:

function clickHandler (e) {
  if (e.target.nodeName.toLowerCase() === 'summary') {
    var details = e.target.parentNode
    if (!details) return

    var shouldContinue = dispatchToggleEvent(details);
    if (!shouldContinue) return

    if (details.getAttribute('open')) {
      details.open = false
      details.removeAttribute('open')
    } else {
      details.open = true
      details.setAttribute('open', 'open')
    }
  }
}

function dispatchToggleEvent (details) {
  var toggleEvent;
  try {
    toggleEvent = new CustomEvent('toggle', { bubbles: true, canceable: true });
  } catch(e) {
    toggleEvent = document.createEvent('CustomEvent');
    toggleEvent.initCustomEvent('toggle', true, true, null);
  }

  return details.dispatchEvent(toggleEvent);
}

I don't have access to enough browsers to test this thoroughly, though.

Maintainer/Contributor?

I'd like to add a test suite to this via travis/browserstack. It seems like you're too busy for this project, so I'd like to step in.

Consider switching to unicode escape for triangle arrows

Hi there. Thanks for creating this polyfill. Just the kind of minimalist implementation I was looking for.

However, I'm having a problem with it under Firefox in Linux. The triangle arrows show up as gibberish. I was able to fix it by changing these lines:

'html.no-details ' + DETAILS + ' > ' + SUMMARY + ':before { content: "▶"; display: inline-block; font-size: .8em; width: 1.5em; }\n' +
'html.no-details ' + DETAILS + '[open] > ' + SUMMARY + ':before { content: "▼"; }')

to this:

'html.no-details ' + DETAILS + ' > ' + SUMMARY + ':before { content: "\u25b6"; display: inline-block; font-size: .8em; width: 1.5em; }\n' +
'html.no-details ' + DETAILS + '[open] > ' + SUMMARY + ':before { content: "\u25bc"; }')

Works great for me in Chrome as well. Not sure how it looks in IE/Edge.

Lack of keyboard accessibility

It appears that the polyfill doesn't provide any keyboard accessibility to toggle the details element. I tried adding tabindex=0 to the <summary> element, which makes it focusable but not toggleable.

Both Chrome and Firefox allow the user to focus on the <summary> element and toggle the details using the Space or Enter keys.

It is probably not too difficult to implement, if I do get something working I will send over a pull request

Errors locally

TypeError: null is not an object (evaluating 'document.body.appendChild')

because document.body is currently null due to document.readyState === "loading".

License

In the README and package.json it says MIT but it would be nice to have a LICENSE or LICENSE.md file so that it is detected by GitHub automatically (see https://help.github.com/articles/adding-a-license-to-a-repository/)

Also I wanted to say thanks for creating this polyfill! <details> is still "under consideration" in Edge so it's really useful regardless of whether IE support is necessary.

Strange appearance in IE/Edge

Due to the <details> element not being block-level by default.

Edit: Should this be handled by this library, or by the project? We already have to do such with <article>. Would people think to do the same with <details> and <summary>? I hadn't.

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.