From f9bc6a04e5a40b6fe89d8f257dc5fc4c7fa36b7b Mon Sep 17 00:00:00 2001 From: "Hancock, Matthew" Date: Tue, 11 Apr 2023 10:02:05 -0400 Subject: [PATCH] [fixed] strict matching for tabbable nodes --- examples/index.html | 1 + examples/wc/app.css | 39 ++++++++++++++++ examples/wc/app.js | 88 +++++++++++++++++++++++++++++++++++++ examples/wc/index.html | 19 ++++++++ specs/Modal.helpers.spec.js | 65 ++++++++++++++++++++++++++- src/helpers/tabbable.js | 3 +- 6 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 examples/wc/app.css create mode 100644 examples/wc/app.js create mode 100644 examples/wc/index.html diff --git a/examples/index.html b/examples/index.html index f6207520..66ab8e6f 100644 --- a/examples/index.html +++ b/examples/index.html @@ -14,6 +14,7 @@

an accessible React modal dialog component

Basic Bootstrap + Web Component
Fork me on GitHub diff --git a/examples/wc/app.css b/examples/wc/app.css new file mode 100644 index 00000000..b707a4ec --- /dev/null +++ b/examples/wc/app.css @@ -0,0 +1,39 @@ +.ReactModal__Overlay { + -webkit-perspective: 600; + perspective: 600; + opacity: 0; + overflow-x: hidden; + overflow-y: auto; + background-color: rgba(0, 0, 0, 0.5); +} + +.ReactModal__Overlay--after-open { + opacity: 1; + transition: opacity 150ms ease-out; +} + +.ReactModal__Content { + -webkit-transform: scale(0.5) rotateX(-30deg); + transform: scale(0.5) rotateX(-30deg); +} + +.ReactModal__Content--after-open { + -webkit-transform: scale(1) rotateX(0deg); + transform: scale(1) rotateX(0deg); + transition: all 150ms ease-in; +} + +.ReactModal__Overlay--before-close { + opacity: 0; +} + +.ReactModal__Content--before-close { + -webkit-transform: scale(0.5) rotateX(30deg); + transform: scale(0.5) rotateX(30deg); + transition: all 150ms ease-in; +} + +.ReactModal__Content.modal-dialog { + border: none; + background-color: transparent; +} diff --git a/examples/wc/app.js b/examples/wc/app.js new file mode 100644 index 00000000..7b27bf81 --- /dev/null +++ b/examples/wc/app.js @@ -0,0 +1,88 @@ +import React, { Component } from 'react'; +import ReactDOM from 'react-dom'; +import Modal from 'react-modal'; + +import '@webcomponents/custom-elements/src/native-shim'; + +var appElement = document.getElementById('example'); + +Modal.setAppElement(appElement); + +class App extends Component { + constructor(props) { + super(props); + this.state = { modalIsOpen: false }; + } + + openModal = () => { + this.setState({modalIsOpen: true}); + } + + closeModal = () => { + this.setState({modalIsOpen: false}); + } + + handleModalCloseRequest = () => { + // opportunity to validate something and keep the modal open even if it + // requested to be closed + this.setState({modalIsOpen: false}); + } + + handleSaveClicked = (e) => { + alert('Save button was clicked'); + } + + render() { + return ( +
+ + +
+
+

Modal title

+
+ + +
+
+
+

Really long content...

+

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus

+
+
+ + +
+
+
+
+ ); + } +} + +ReactDOM.render(, appElement); + +class AwesomeButton extends HTMLElement { + constructor() { + super(); + } + + // this shows with no shadow root + connectedCallback() { + this.innerHTML = ` + + `; + } +} + +customElements.define("awesome-button", AwesomeButton); diff --git a/examples/wc/index.html b/examples/wc/index.html new file mode 100644 index 00000000..4835028e --- /dev/null +++ b/examples/wc/index.html @@ -0,0 +1,19 @@ + + + + Bootstrap-Style Example + + + + + + +
+

react-modal

+

an accessible React modal dialog component

+
+
+ Fork me on GitHub + + + diff --git a/specs/Modal.helpers.spec.js b/specs/Modal.helpers.spec.js index 3043cf17..9f073452 100644 --- a/specs/Modal.helpers.spec.js +++ b/specs/Modal.helpers.spec.js @@ -116,7 +116,28 @@ export default () => { tabbable(elem).should.not.containEql(button); }); - describe("inside Web Components", () => { + it("excludes elements that contain reserved node names", () => { + const button = document.createElement("button"); + button.innerHTML = "I am a good button"; + elem.appendChild(button); + + const badButton = document.createElement("bad-button"); + badButton.innerHTML = "I am a bad button"; + elem.appendChild(badButton); + + tabbable(elem).should.deepEqual([button]); + }); + + it("includes elements that contain reserved node names with tabindex", () => { + const trickButton = document.createElement("trick-button"); + trickButton.innerHTML = "I am a good button"; + trickButton.tabIndex = '0'; + elem.appendChild(trickButton); + + tabbable(elem).should.deepEqual([trickButton]); + }); + + describe("inside Web Components with shadow dom", () => { let wc; let input; class TestWebComponent extends HTMLElement { @@ -169,6 +190,48 @@ export default () => { tabbable(elem).should.not.containEql(input); }); }); + + describe("inside Web Components with no shadow dom", () => { + let wc; + let button; + class ButtonWebComponent extends HTMLElement { + constructor() { + super(); + } + + connectedCallback() { + this.innerHTML = ''; + this.style.display = "block"; + this.style.width = "100px"; + this.style.height = "25px"; + } + } + + const registerButtonComponent = () => { + if (window.customElements.get("button-web-component")) { + return; + } + window.customElements.define("button-web-component", ButtonWebComponent); + }; + + beforeEach(() => { + registerButtonComponent(); + wc = document.createElement("button-web-component"); + + elem.appendChild(wc); + }); + + afterEach(() => { + // remove Web Component + elem.removeChild(wc); + }); + + it("includes only focusable elements", () => { + button = wc.querySelector('button'); + + tabbable(elem).should.deepEqual([button]); + }); + }); }); }); }; diff --git a/src/helpers/tabbable.js b/src/helpers/tabbable.js index e9bcaa4c..2cd386b0 100644 --- a/src/helpers/tabbable.js +++ b/src/helpers/tabbable.js @@ -13,7 +13,8 @@ const DISPLAY_NONE = "none"; const DISPLAY_CONTENTS = "contents"; -const tabbableNode = /input|select|textarea|button|object|iframe/; +// match the whole word to prevent fuzzy searching +const tabbableNode = /^(input|select|textarea|button|object|iframe)$/; function isNotOverflowing(element, style) { return (