Giter VIP home page Giter VIP logo

inert-polyfill's Introduction

Polyfill for the inert HTML attribute. Check out a small demo.

The inert attribute is a draft feature of HTML. As of Feb 2017, no browser has a native implementation of inert. However, there is active development inside Chrome, and other browsers have shown some renewed interest.

From the HTML spec-

A node (in particular elements and text nodes) can be marked as inert. When a node is inert, then the user agent must act as if the node was absent for the purposes of targeting user interaction events, may ignore the node for the purposes of text search user interfaces (commonly known as "find in page"), and may prevent the user from selecting text in that node.

While not a replacement for proper state management, web developers can use the inert attribute to prevent access to subtrees. This could be useful to prevent access to active HTML forms, to enable modal-like popovers, or to block user interaction while awaiting the result of an asynchronous operation.

Example

<div inert>
  <button>I'm unclickable!</button>
  <input type="text" placeholder="I'm unfocusable!" />
</div>

Usage

Include the inert-polyfill script at the end of your page. There are no other initialization steps.

The polyfill prevents tab-focusing, using the accessKey to access an element, the click event (mostly for sanity, as elements should be unfocusable), and any other approach to focus.

Installation

You may optionally install via NPM or Bower-

$ npm install inert-polyfill
$ bower install inert-polyfill

Supports

This polyfill works on modern versions of all major browsers. It also supports IE9 and above, although links and buttons may appear clickable (IE9 and IE10 do not support pointer-events, although there is a polyfill).

Limitations

Most limitations revolve around keyboard access and the tab key-

  • Most browsers don't support emulating tab events, so positive values of tabIndex may be ignored
    • Avoid relying on a specific tab order
  • Inert elements at the very start or end of a page may prevent tab access to the browser's chrome
  • While tabbing over inert elements, inner elements may still receive intermediate focus and blur events

Other limitations include-

  • Content within an inert element may still be searched for (using the browser's Find box) or selected
  • Focused elements that become inert due to surrounding HTML changes will remain focused
  • Inert will work within a shadow root, but the CSS to support it is not included

If these limitations do not work for your project, there is also a WICG polyfill, which uses MutationObserver to recursively walk HTML trees to clear tabIndex (clearing or setting to -1). This is more correct, but will incur a performance hit when inert is enabled or disabled. The GoogleChrome hosted polyfill simply overloads focus and related events to prevent focus.

Release

Compile code with Closure Compiler.

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name inert-polyfill.min.js
// ==/ClosureCompiler==

// code here

inert-polyfill's People

Contributors

alexanderotavka avatar arkkimaagi avatar naokigoogle avatar samthor 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

inert-polyfill's Issues

More clearly indicate status/relationship to WICG polyfill

When doing a search for "inert polyfill" results usually point to both this repo and the https://github.com/WICG/inert one, causing some confusion for developers.

while there is a note in https://github.com/GoogleChrome/inert-polyfill#limitations it would be good if the readme started more directly with some statement about the status of this polyfill, its relationship with the WICG one, and suggestion to developers on which one they should use/look at.

Inert elements not hidden from screen readers

When a tree is marked inert, it is still visible to screen readers.

Should be given aria-hidden="true" attribute otherwise a screen-reader user can navigate to and manipulate things they shouldn't be able to (e.g. when a modal dialog is covering the rest of the UI they can get at things "under" the modal dialog).

Can see this on the simple demo page, also in our project: rstudio/rstudio#6075

Warning in Google Chromium 52

Using the inert attribute causes following error while tabbing through the page:
"A DOM event generated from JavaScript has triggered a default action inside the browser. This behavior is non-standard and will be removed in M53, around September 2016. See https://www.chromestatus.com/features/5718803933560832 for more details."

It seems to be caused by the following line:

document.dispatchEvent(ev);

Inert elements are searchable in browser search

If I understand the inert property correctly, apart from actually rendering, the element should appear as if display: none was active. The browser search function (cmd/ctrl+f) does not list hidden elements, but it does list inert ones. This is the native behaviour of Chrome Canary as well, but after a quick chat with Rob Dodson and Alice Boxhall, this should perhaps be fixed there as well.

As for a solution to this problem, I'm not of much help. The only text I can think of that isn't searchable is that added as CSS content. To remove the text from the DOM to put it in the content I guess would trigger a repaint, which would render the inert property useless in many cases.

A quick demo: https://codepen.io/sindre/pen/BZGbrK

Polyfill Disabling $.on('focus')

When you just add the polyfill on your page this disable the focus on some elements, at now my issue was with input element that is not working $.on('focus'), but when i remove the polyfill it back to work again.

Parents with absolute child are pushed after pressing tab

Hi there. Hope you are doing fine. So, I'm having this weird problem. Was wondering if this is an issue or do you have a workaround for it.

Basically, the problem is that if you have a parent div with position relative and two children. If the second child has the attribute inert set and position=absolute top = 100% to place it outside the parent, when you change your focus from the first child to an element outside the parent div, the content of the parent is pushed and reveals the second child that should be hidden.

It is kind of difficult to explain, so I created this code pen: https://codepen.io/afallas9/pen/pKYdgX

One more thing that I didn't mention is that if you are reading the screen with Jaws, and you go through the page with the down arrow key, it still reads the link in the inert div

Regards

Support?

I know this is a polyfill, but as a general question:

How likely is it that browser vendors implement it?
I saw the Chrome issue saying they won't continue implementing this.
I saw the Firefox issue that they think this is no more relevant as it's removed from the spec.
It's not even listed on chromestatus.com.

I see the great advantage, but why aren't they all not implementing it?

Setting inert before the load event

Setting the inert property on an element before the load event fires fails to update the attribute. This is a problem for custom elements, as the connectedCallback is called before load fires.

Throws error trying to focus SVGElements in Edge

Microsoft Edge lacks support for SVGElement.prototype.focus, so if you have a situation like

<a href="#">Normal link</a>
<a inert href="#">Inert link</a>
<svg tabindex="0" />

When you forward-tab from “Normal-link,” the polyfill will try to find the next suitable element to focus and call element.focus() on it. So in this case, in Edge, element.focus is undefined.

CodePen demo: https://codepen.io/atcb/pen/RLzjPX

I’ve already fixed this in my fork, but didn’t open a PR earlier because my fork exists for the purpose of swapping the attribute trigger from inert to data-inert, as React <= v15 doesn’t allow passing custom attributes to elements.

Anyway, if you’re interested in accepting a PR, I’ll gladly re-apply my fix to master. My fix is here: andrewbranch@88c8e0c

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.