Skip to content

Commit

Permalink
Merge branch 'main' into add-escape-to-dismiss
Browse files Browse the repository at this point in the history
* main:
  add changelog
  add focus restoration support
  • Loading branch information
jgerigmeyer committed Mar 14, 2023
2 parents 5800aae + cd20ceb commit b1b153c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- 🚀 NEW: Add support for Escape to dismiss auto popovers --
[#82](https://github.com/oddbird/popover-polyfill/pull/82)
- 🚀 NEW: Add support for focus restoration on popover close --
[#81](https://github.com/oddbird/popover-polyfill/pull/81)

## 0.0.10: 2023-03-03

Expand Down
10 changes: 8 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ <h1>Popover Attribute Polyfill</h1>
<div id="popovers">
<div id="popover1" popover>Popover 1</div>
<div id="popover2" popover="">Popover 2</div>
<div id="popover3" popover="auto">Popover 3</div>
<div id="popover3" popover="auto">
Popover 3. This popover has an autofocus link:
<a href="#" autofocus>I'm a link!</a>
</div>
<div id="popover4" popover="hint">Invalid Popover ("hint")</div>
<div id="popover5" popover="manual">Popover 5</div>
<div id="popover5" popover="manual">
Popover 5. This popover has an autofocus link:
<a href="#" autofocus>I'm a link!</a>
</div>
<div id="popover6" popover="invalid">Invalid Popover ("invalid")</div>
<div id="popover7" popover="auto">Popover 7 (auto)</div>
<div id="popover8" popover="auto">Popover 8 (auto)</div>
Expand Down
16 changes: 12 additions & 4 deletions src/popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const queryAncestorAll = (

export function apply() {
const visibleElements = new WeakSet<HTMLElement>();
let lastFocusedElement: HTMLElement | null = null;

// https://whatpr.org/html/8221/popover.html#check-popover-validity
function checkPopoverValidity(
Expand Down Expand Up @@ -137,11 +138,14 @@ export function apply() {
assertPopoverValidity(this, false);
this.classList.add(':open');
visibleElements.add(this);
const focusEl = this.hasAttribute('autofocus')
? this
: this.querySelector('[autofocus]');
if (this.ownerDocument.activeElement instanceof HTMLElement) {
lastFocusedElement = this.ownerDocument.activeElement;
}
focusEl?.focus();
if (this.popover === 'auto') {
const focusEl = this.hasAttribute('autofocus')
? this
: this.querySelector('[autofocus]');
focusEl?.focus();
for (const invoker of getInvokersFor(this)) {
setInvokerAriaExpanded(invoker);
}
Expand All @@ -166,6 +170,10 @@ export function apply() {
this.classList.remove(':open');
visibleElements.delete(this);
if (this.popover === 'auto') {
if (lastFocusedElement) {
lastFocusedElement.focus();
lastFocusedElement = null;
}
for (const invoker of getInvokersFor(this)) {
setInvokerAriaExpanded(invoker);
}
Expand Down

0 comments on commit b1b153c

Please sign in to comment.