Skip to content

Commit

Permalink
fix(dropdown): properly handle the document click events when the ele…
Browse files Browse the repository at this point in the history
…ment is used in a shadow dom


fix(select): properly handle the document click events when the element is used in a shadow dom
  • Loading branch information
daenub authored Jul 23, 2024
1 parent 2af0886 commit 0c6c5a9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class LeuDropdown extends LeuElement {
}

_documentClickHandler = (event) => {
if (!this.contains(event.target)) {
if (!event.composedPath().includes(this)) {
this.expanded = false
}
}
Expand Down
23 changes: 20 additions & 3 deletions src/components/dropdown/test/dropdown.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { html } from "lit"
import { fixture, expect } from "@open-wc/testing"
import { fixture, expect, elementUpdated } from "@open-wc/testing"

import "../leu-dropdown.js"

async function defaultFixture() {
return fixture(html` <leu-dropdown label="Download">
async function defaultFixture(args = { expanded: false }) {
return fixture(html` <leu-dropdown
label="Download"
?expanded=${args.expanded}
>
<leu-menu>
<leu-menu-item>Als CSV Tabelle</leu-menu-item>
<leu-menu-item>Als XLS Tabelle</leu-menu-item>
Expand All @@ -28,4 +31,18 @@ describe("LeuDropdown", () => {

await expect(el).shadowDom.to.be.accessible()
})

it("closes the popup when the document is clicked outside the component", async () => {
const el = await defaultFixture()

const toggleButton = el.shadowRoot.querySelector("leu-button")
toggleButton.click()
await elementUpdated(el)

expect(el.expanded).to.be.true

document.body.click()

expect(el.expanded).to.be.false
})
})
6 changes: 1 addition & 5 deletions src/components/select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,7 @@ export class LeuSelect extends LeuElement {
* @param {MouseEvent} event
*/
_handleDocumentClick = (event) => {
if (
event.target instanceof Node &&
!this.contains(event.target) &&
this.open
) {
if (!event.composedPath().includes(this) && this.open) {
this._closeDropdown()
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/components/select/test/select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,19 @@ describe("LeuSelect", () => {
const popup = el.shadowRoot.querySelector("leu-popup")
expect(popup.active).to.not.be.true
})

it("closes the popup when the document is clicked outside the component", async () => {
const el = await defaultFixture({
options: MUNICIPALITIES,
label: "Gemeinde",
})

const toggleButton = el.shadowRoot.querySelector(".select-toggle")
toggleButton.click()

document.body.click()

const popup = el.shadowRoot.querySelector("leu-popup")
expect(popup.active).to.not.be.true
})
})

0 comments on commit 0c6c5a9

Please sign in to comment.