Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(overlay): safari :focus-visible inconsistency when using overlay type modal #4912

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
41 changes: 41 additions & 0 deletions packages/button/src/button.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ governing permissions and limitations under the License.
:host([treatment]:not([disabled]):hover) {
border-color: highlight;
}

:host(.remove-focus-ring-safari-hack:focus-visible):after {
forced-color-adjust: none;
box-shadow: none;
}
}

@keyframes show-progress-circle {
Expand Down Expand Up @@ -99,3 +104,39 @@ sp-progress-circle {
:host([pending]:not([disabled])) #label {
animation: hide-icons-label 0s var(--pending-delay, 1s) forwards;
}

:host(.remove-focus-ring-safari-hack:focus-visible):after {
margin: calc(
-1 * var(--mod-button-focus-indicator-gap, var(--mod-focus-indicator-gap, var(--spectrum-focus-indicator-gap)))
);
box-shadow: none;
}

:host(.remove-focus-ring-safari-hack:focus-visible) {
box-shadow: none;
outline: none;
}

:host(.remove-focus-ring-safari-hack:focus-visible:not(:hover)) {
background-color: var(
--highcontrast-button-background-color-default,
var(
--mod-button-background-color-default,
var(--spectrum-button-background-color-default)
)
);
border-color: var(
--highcontrast-button-border-color-default,
var(
--mod-button-border-color-default,
var(--spectrum-button-border-color-default)
)
);
color: var(
--highcontrast-button-content-color-default,
var(
--mod-button-content-color-default,
var(--spectrum-button-content-color-default)
)
);
}
38 changes: 36 additions & 2 deletions packages/overlay/src/HoverController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ governing permissions and limitations under the License.
*/

import { conditionAttributeWithId } from '@spectrum-web-components/base/src/condition-attribute-with-id.js';
import { isWebKit } from '@spectrum-web-components/shared';
import { randomID } from '@spectrum-web-components/shared/src/random-id.js';

import { noop } from './AbstractOverlay.js';
import {
InteractionController,
InteractionTypes,
lastInteractionType,
} from './InteractionController.js';
import { noop } from './AbstractOverlay.js';

export const SAFARI_FOCUS_RING_CLASS = 'remove-focus-ring-safari-hack';
mizgaionutalexandru marked this conversation as resolved.
Show resolved Hide resolved

const HOVER_DELAY = 300;

Expand All @@ -32,15 +35,33 @@ export class HoverController extends InteractionController {

pointerentered = false;

handleKeyup(event: KeyboardEvent): void {
if (isWebKit() && (event.code === 'Tab' || event.code === 'Escape')) {
this.open = true;
this.removeSafariFocusRingClass();
}
}

mizgaionutalexandru marked this conversation as resolved.
Show resolved Hide resolved
handleTargetFocusin(): void {
if (!this.target.matches(':focus-visible')) {
return;
}

if (
isWebKit() &&
this.target[lastInteractionType] === InteractionTypes.click
) {
this.target.classList.add(SAFARI_FOCUS_RING_CLASS);
return;
}

this.open = true;
this.focusedin = true;
this.removeSafariFocusRingClass();
}

handleTargetFocusout(): void {
this.removeSafariFocusRingClass();
this.focusedin = false;
if (this.pointerentered) return;
this.open = false;
Expand Down Expand Up @@ -138,6 +159,11 @@ export class HoverController extends InteractionController {
this.abortController?.abort();
this.abortController = new AbortController();
const { signal } = this.abortController;
this.target.addEventListener(
'keyup',
(event) => this.handleKeyup(event),
{ signal }
);
this.target.addEventListener(
'focusin',
() => this.handleTargetFocusin(),
Expand Down Expand Up @@ -179,4 +205,12 @@ export class HoverController extends InteractionController {
{ signal }
);
}

private removeSafariFocusRingClass(): void {
if (
isWebKit() &&
this.target.classList.contains(SAFARI_FOCUS_RING_CLASS)
)
this.target.classList.remove(SAFARI_FOCUS_RING_CLASS);
}
}
16 changes: 12 additions & 4 deletions packages/overlay/src/InteractionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ import type { ReactiveController } from '@spectrum-web-components/base';
import { AbstractOverlay } from './AbstractOverlay.js';

export enum InteractionTypes {
'click',
'hover',
'longpress',
click = 'click',
hover = 'hover',
longpress = 'longpress',
}

export const lastInteractionType = Symbol('lastInteractionType');

export type ControllerOptions = {
overlay?: AbstractOverlay;
handleOverlayReady?: (overlay: AbstractOverlay) => void;
isPersistent?: boolean;
};

type InteractionTarget = HTMLElement & {
[lastInteractionType]?: InteractionTypes;
};

export class InteractionController implements ReactiveController {
abortController!: AbortController;

Expand All @@ -50,6 +56,7 @@ export class InteractionController implements ReactiveController {
if (this.overlay) {
// If there already is an Overlay, apply the value of `open` directly.
this.overlay.open = open;
this.target[lastInteractionType] = this.type;
return;
}
if (!open) {
Expand All @@ -65,6 +72,7 @@ export class InteractionController implements ReactiveController {
const { Overlay } = await import('./Overlay.js');
this.overlay = new Overlay();
this.overlay.open = true;
this.target[lastInteractionType] = this.type;
});
import('@spectrum-web-components/overlay/sp-overlay.js');
}
Expand Down Expand Up @@ -93,7 +101,7 @@ export class InteractionController implements ReactiveController {
type!: InteractionTypes;

constructor(
public target: HTMLElement,
public target: InteractionTarget,
{ overlay, isPersistent, handleOverlayReady }: ControllerOptions
) {
this.isPersistent = !!isPersistent;
Expand Down
63 changes: 40 additions & 23 deletions packages/overlay/stories/overlay.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import '@spectrum-web-components/action-button/sp-action-button.js';
import '@spectrum-web-components/action-group/sp-action-group.js';
import { html, TemplateResult } from '@spectrum-web-components/base';
import { ifDefined } from '@spectrum-web-components/base/src/directives.js';
import '@spectrum-web-components/button/sp-button.js';
import { DialogWrapper } from '@spectrum-web-components/dialog';
import '@spectrum-web-components/dialog/sp-dialog-wrapper.js';
import '@spectrum-web-components/dialog/sp-dialog.js';
import '@spectrum-web-components/field-label/sp-field-label.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-magnify.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-open-in.js';
import {
openOverlay,
Overlay,
Expand All @@ -19,40 +28,31 @@ import {
TriggerInteractions,
VirtualTrigger,
} from '@spectrum-web-components/overlay';
import '@spectrum-web-components/action-button/sp-action-button.js';
import '@spectrum-web-components/action-group/sp-action-group.js';
import '@spectrum-web-components/button/sp-button.js';
import '@spectrum-web-components/dialog/sp-dialog.js';
import '@spectrum-web-components/dialog/sp-dialog-wrapper.js';
import { DialogWrapper } from '@spectrum-web-components/dialog';
import '@spectrum-web-components/field-label/sp-field-label.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-magnify.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-open-in.js';
import '@spectrum-web-components/overlay/overlay-trigger.js';

import '@spectrum-web-components/accordion/sp-accordion-item.js';
import '@spectrum-web-components/accordion/sp-accordion.js';
import '@spectrum-web-components/button-group/sp-button-group.js';
import '@spectrum-web-components/menu/sp-menu-divider.js';
import '@spectrum-web-components/menu/sp-menu-group.js';
import '@spectrum-web-components/menu/sp-menu-item.js';
import '@spectrum-web-components/menu/sp-menu.js';
import '@spectrum-web-components/overlay/sp-overlay.js';
import { Picker } from '@spectrum-web-components/picker';
import '@spectrum-web-components/picker/sp-picker.js';
import '@spectrum-web-components/overlay/sp-overlay.js';
import '@spectrum-web-components/menu/sp-menu.js';
import '@spectrum-web-components/menu/sp-menu-item.js';
import '@spectrum-web-components/menu/sp-menu-group.js';
import '@spectrum-web-components/menu/sp-menu-divider.js';
import '@spectrum-web-components/popover/sp-popover.js';
import '@spectrum-web-components/slider/sp-slider.js';
import '@spectrum-web-components/radio/sp-radio.js';
import '@spectrum-web-components/radio/sp-radio-group.js';
import '@spectrum-web-components/tooltip/sp-tooltip.js';
import '@spectrum-web-components/radio/sp-radio.js';
import '@spectrum-web-components/slider/sp-slider.js';
import '@spectrum-web-components/theme/sp-theme.js';
import '@spectrum-web-components/theme/src/themes.js';
import '@spectrum-web-components/accordion/sp-accordion.js';
import '@spectrum-web-components/accordion/sp-accordion-item.js';
import '@spectrum-web-components/button-group/sp-button-group.js';
import '@spectrum-web-components/tooltip/sp-tooltip.js';
import '../../../projects/story-decorator/src/types.js';

import './overlay-story-components.js';
import { render } from 'lit-html';
import { Popover } from '@spectrum-web-components/popover';
import { Button } from '@spectrum-web-components/button';
import { Popover } from '@spectrum-web-components/popover';
import { render } from 'lit-html';
import './overlay-story-components.js';
import { PopoverContent } from './overlay-story-components.js';

const storyStyles = html`
Expand Down Expand Up @@ -297,6 +297,23 @@ accordion.swc_vrt = {
skip: true,
};

export const clickAndHoverTarget = (): TemplateResult => {
return html`
<overlay-trigger type="modal">
<sp-button variant="primary" slot="trigger">Button</sp-button>
<sp-popover slot="click-content" placement="bottom" tip>
Popover content
</sp-popover>
<sp-tooltip slot="hover-content" placement="right">
Tooltip content
</sp-tooltip>
</overlay-trigger>
`;
};
clickAndHoverTarget.swc_vrt = {
skip: true,
};

export const clickAndHoverTargets = (): TemplateResult => {
return html`
<div>
Expand Down
88 changes: 87 additions & 1 deletion packages/overlay/test/overlay-trigger-hover-click.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ import { TriggerInteractions } from '@spectrum-web-components/overlay/src/overla
import '@spectrum-web-components/overlay/overlay-trigger.js';
import { ActionButton } from '@spectrum-web-components/action-button';
import { sendMouse } from '../../../test/plugins/browser.js';
import { clickAndHoverTargets, deep } from '../stories/overlay.stories.js';
import {
clickAndHoverTarget,
clickAndHoverTargets,
deep,
} from '../stories/overlay.stories.js';
import { ignoreResizeObserverLoopError } from '../../../test/testing-helpers.js';
import { Tooltip } from '@spectrum-web-components/tooltip/src/Tooltip.js';
import { sendKeys } from '@web/test-runner-commands';
import { Button } from '@spectrum-web-components/button';
import { isWebKit } from '@spectrum-web-components/shared';
import { SAFARI_FOCUS_RING_CLASS } from '../src/HoverController.js';

ignoreResizeObserverLoopError(before, after);

Expand Down Expand Up @@ -270,4 +276,84 @@ describe('Overlay Trigger - Hover and Click', () => {
expect(el.open, '"click" overlay no longer open').to.be.undefined;
expect(tooltip.open).to.be.false;
});
it('should not open right after closing the click overlay using the mouse', async () => {
const overlayTrigger = await fixture<OverlayTrigger>(
clickAndHoverTarget()
);

await elementUpdated(overlayTrigger);
expect(overlayTrigger.open).to.be.undefined;

const trigger = overlayTrigger.querySelector(
'sp-button[slot="trigger"]'
) as Button;
const rect = trigger.getBoundingClientRect();
const opened = oneEvent(trigger, 'sp-opened');
sendMouse({
steps: [
{
type: 'click',
position: [
rect.left + rect.width / 2,
rect.top + rect.height / 2,
],
},
],
});
await opened;

expect(overlayTrigger.open).to.equal('click');

const closed = oneEvent(trigger, 'sp-closed');
sendMouse({
steps: [
{
type: 'click',
position: [0, 0],
},
],
});
await closed;

// This fails but when manually tested it works in the browser
// in the test it shows that the tooltip is open (overlayTrigger.open === 'hover')
// expect(overlayTrigger.open).to.be.undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would appreciate any feedback/ideas on why would this be flaky, couldn't yet figure out why this would fail if manually tested it works.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iinnnteresting

expect(document.activeElement === trigger, 'trigger focused').to.be
.true;
if (isWebKit())
expect(trigger.classList.contains(SAFARI_FOCUS_RING_CLASS)).to.be
.true;
});

it('should not open right after closing the click overlay using keyboard', async () => {
const overlayTrigger = await fixture<OverlayTrigger>(
clickAndHoverTarget()
);

await elementUpdated(overlayTrigger);
expect(overlayTrigger.open).to.be.undefined;

const trigger = overlayTrigger.querySelector(
'sp-button[slot="trigger"]'
) as Button;

await sendKeys({ press: 'Tab' });
expect(document.activeElement === trigger, 'trigger focused').to.be
.true;

const opened = oneEvent(trigger, 'sp-opened');
await sendKeys({ press: 'Enter' });
await opened;
const closed = oneEvent(trigger, 'sp-closed');
await sendKeys({ press: 'Escape' });
await closed;

// Manually testing this in the browser doesn't fail without the handleKeyup method but it should
// expect(overlayTrigger.open).to.equal('hover');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here as in the previous test.

expect(document.activeElement === trigger, 'trigger focused').to.be
.true;
if (isWebKit())
expect(trigger.classList.contains(SAFARI_FOCUS_RING_CLASS)).to.be
.false;
});
});
Loading