Skip to content
Open
Changes from all 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
26 changes: 23 additions & 3 deletions focus/internal/focus-ring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
/**
* Events that the focus ring listens to.
*/
const EVENTS = ['focusin', 'focusout', 'pointerdown'];
const EVENTS = ['focusin', 'focusout', 'pointerdown', 'keydown'];

let hadKeyboardEvent = false;

/**
* A focus ring component.
Expand Down Expand Up @@ -79,11 +81,22 @@ export class FocusRing extends LitElement implements Attachable {
switch (event.type) {
default:
return;
case 'keydown':
hadKeyboardEvent = true;
break;
case 'focusin':
this.visible = this.control?.matches(':focus-visible') ?? false;
// Only use hadKeyboardEvent when using safari.
// It works around an issue with focus-visible not working
// properly.
this.visible = this.isSafari() ?
hadKeyboardEvent :
this.control?.matches(':focus-visible') ?? false;
break;
case 'focusout':
case 'pointerdown':
hadKeyboardEvent = false;
this.visible = false;
break;
case 'focusout':
this.visible = false;
break;
}
Expand All @@ -108,6 +121,13 @@ export class FocusRing extends LitElement implements Attachable {
}
super.update(changed);
}

private isSafari() {
return (
/^((?!chrome|android).)*safari/i.test(navigator.userAgent) ||
/iPad|iPhone|iPod/.test(navigator.userAgent)
);
}
}

const HANDLED_BY_FOCUS_RING = Symbol('handledByFocusRing');
Expand Down