Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watch for [data-inert] elements and turn them inerted as well (#126) #128

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"code": 100,
"tabWidth": 2,
"ignoreUrls": true
}]
}],
"linebreak-style": ["error", "windows"]
}
}
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions src/inert.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ class InertManager {
*/
this._observer = new MutationObserver(this._watchForInert.bind(this));

/**
* Selector for elements that should be inerted
* @type string
* */
this._inertSelector = '[inert], [data-inert]';

// Add inert style.
addInertStyle(document.head || document.body || document.documentElement);

Expand Down Expand Up @@ -580,7 +586,7 @@ class InertManager {
*/
_onDocumentLoaded() {
// Find all inert roots in document and make them actually inert.
const inertElements = slice.call(this._document.querySelectorAll('[inert]'));
const inertElements = slice.call(this._document.querySelectorAll(this._inertSelector));
inertElements.forEach(function(inertElement) {
this.setInert(inertElement, true);
}, this);
Expand All @@ -603,8 +609,8 @@ class InertManager {
if (node.nodeType !== Node.ELEMENT_NODE) {
return;
}
const inertElements = slice.call(node.querySelectorAll('[inert]'));
if (matches.call(node, '[inert]')) {
const inertElements = slice.call(node.querySelectorAll(this._inertSelector));
if (matches.call(node, this._inertSelector)) {
inertElements.unshift(node);
}
inertElements.forEach(function(inertElement) {
Expand Down Expand Up @@ -698,12 +704,12 @@ function addInertStyle(node) {
const style = document.createElement('style');
style.setAttribute('id', 'inert-style');
style.textContent = '\n'+
'[inert] {\n' +
'[inert], [data-inert] {\n' +
' pointer-events: none;\n' +
' cursor: default;\n' +
'}\n' +
'\n' +
'[inert], [inert] * {\n' +
'[inert], [data-inert], [inert] *, [data-inert] * {\n' +
' user-select: none;\n' +
' -webkit-user-select: none;\n' +
' -moz-user-select: none;\n' +
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/data-attribute.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--
This work is licensed under the W3C Software and Document License
(http://www.w3.org/Consortium/Legal/2015/copyright-software-and-document).
-->

<div data-inert>
<button>Click Me</button>
<div id="fake-button" tabindex="0">Click me</div>
</div>
<button id="non-inert">Non-inert button</button>
29 changes: 29 additions & 0 deletions test/specs/data-attribute.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe('Data-Attribute', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add some tests for mutations?

  • Adding an element into an inert subtree
  • Adding data-inert to an existing element
  • Adding an element to the DOM with a data-inert attribute already on it

before(function() {
fixture.setBase('test/fixtures');
});

beforeEach(function(done) {
fixture.load('data-attribute.html');
// Because inert relies on MutationObservers,
// wait till next microtask before running tests.
setTimeout(function() {
done();
}, 0);
});

afterEach(function() {
fixture.cleanup();
});

it('should make implicitly focusable child not focusable', function() {
var button = fixture.el.querySelector('[data-inert] button');
expect(isUnfocusable(button)).to.equal(true);
});

it('should make explicitly focusable child not focusable', function() {
var div = fixture.el.querySelector('#fake-button');
expect(div.hasAttribute('tabindex')).to.equal(false);
expect(isUnfocusable(div)).to.equal(true);
});
});