Skip to content

Commit

Permalink
Make sure <sl-select> closes when focusing out (#1764)
Browse files Browse the repository at this point in the history
* fixes #1763

* fix comment

* πŸ€·πŸ»β€β™‚οΈ

* whatever wtr
  • Loading branch information
claviska authored Dec 6, 2023
1 parent dd27db5 commit b7eccb1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti
- Fixed more performance issues with focus trapping performance. [#1750]
- Added the `hover-bridge` feature to `<sl-popup>` to support better tooltip accessibility [#1734]
- Fixed a bug in `<sl-input>` and `<sl-textarea>` that made it work differently from `<input>` and `<textarea>` when using defaults [#1746]
- Fixed a bug in `<sl-select>` that prevented it from closing when tabbing to another select inside a shadow root [#1763]
- Improved the accessibility of `<sl-tooltip>` so they persist when hovering over the tooltip and dismiss when pressing [[Esc]] [#1734]

## 2.12.0
Expand Down
19 changes: 13 additions & 6 deletions src/components/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,22 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
}

private addOpenListeners() {
document.addEventListener('focusin', this.handleDocumentFocusIn);
document.addEventListener('keydown', this.handleDocumentKeyDown);
document.addEventListener('mousedown', this.handleDocumentMouseDown);
//
// Listen on the root node instead of the document in case the elements are inside a shadow root
//
// https://github.com/shoelace-style/shoelace/issues/1763
//
const root = this.getRootNode();
root.addEventListener('focusin', this.handleDocumentFocusIn);
root.addEventListener('keydown', this.handleDocumentKeyDown);
root.addEventListener('mousedown', this.handleDocumentMouseDown);
}

private removeOpenListeners() {
document.removeEventListener('focusin', this.handleDocumentFocusIn);
document.removeEventListener('keydown', this.handleDocumentKeyDown);
document.removeEventListener('mousedown', this.handleDocumentMouseDown);
const root = this.getRootNode();
root.removeEventListener('focusin', this.handleDocumentFocusIn);
root.removeEventListener('keydown', this.handleDocumentKeyDown);
root.removeEventListener('mousedown', this.handleDocumentMouseDown);
}

private handleFocus() {
Expand Down
1 change: 1 addition & 0 deletions src/components/select/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe('<sl-select>', () => {
await el.updateComplete;
await sendKeys({ press: 'ArrowDown' }); // move selection to the third option
await el.updateComplete;
el.focus(); // For some reason, the browser loses focus before we press enter. Refocus the select.
await sendKeys({ press: 'Enter' }); // commit the selection
await el.updateComplete;

Expand Down

0 comments on commit b7eccb1

Please sign in to comment.