PolymerLabs/blocking-elements

Name: blocking-elements

Owner: PolymerLabs

Description: Expose a stack of blocking elements https://github.com/whatwg/html/issues/897

Created: 2016-07-21 19:26:30.0

Updated: 2018-03-24 01:43:52.0

Pushed: 2018-03-24 01:43:50.0

Homepage: null

Size: 944

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status

Blocking Elements stack API

Implementation of proposal https://github.com/whatwg/html/issues/897

The polyfill chooses a non-colliding name (document.$blockingElements instead of document.blockingElements) as the proposal is still work in progress and hasn't yet reached consensus on the semantics and functionality (see this discussion for more details).

document.$blockingElements manages a stack of elements that inert the interaction outside them.

This polyfill will:

Use this polyfill together with the wicg-inert polyfill to disable interactions on the rest of the document. See the demo page as an example.

Why not listening to events that trigger focus change?

Another approach could be to listen for events that trigger focus change (e.g. focus, blur, keydown) and prevent those if focus moves out of the blocking element.

Wrapping the focus requires to find all the focusable nodes within the top blocking element, eventually sort by tabindex, in order to find first and last focusable node.

This approach doesn't allow the focus to move outside the window (e.g. to the browser's url bar, dev console if opened, etc.), and is less robust when used with assistive technology (e.g. android talkback allows to move focus with swipe on screen, Apple Voiceover allows to move focus with special keyboard combinations).

Install & use

Blocking Elements relies on the inert attribute and uses Set objects, so make sure to include their polyfills as needed.

install --save babel-polyfill
install --save wicg-inert
install --save blocking-elements
tml
ipt src="./node_modules/babel-polyfill/dist/polyfill.min.js"></script>
ipt src="./node_modules/wicg-inert/dist/inert.min.js"></script>
ipt src="./node_modules/blocking-elements/dist/blocking-elements.min.js"></script>

 id="container">
utton onclick="makeBlocking(container)">make blocking</button> 
utton onclick="undoBlocking(container)">undo blocking</button> 
v>

ton>some button</button>

ipt>
nction makeBlocking(element) {
document.$blockingElements.push(element);

nction undoBlocking(element) {
document.$blockingElements.remove(element);

ript>
Local development

Install the dependencies with npm install and serve the resources.

Run the tests locally by navigating to http://localhost:8080/test/

Performance

Performance is dependent on the inert polyfill performance. Chrome recently landed the inert attribute implementation behind a flag.

Let's compare the how long it takes to toggle the deepest x-trap-focus inside nested x-b of the demo page (http://localhost:8080/demo/ce.html?ce=v1)

results.

document.$blockingElements with native inert is ~15x faster than polyfilled inert ? ? ?

| with polyfilled inert (M58) | with native inert (M60) | |———-|——–| | polyfill-inert-1.png | native-inert-1.png | | polyfill-inert-2.png | native-inert-2.png | | polyfill-inert-3.png | native-inert-3.png | | polyfill-inert-4.png | native-inert-4.png |


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.