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(components/popovers): popovers with a hover trigger display when the trigger element is focused (#1991) #1997

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,65 @@ describe('Popover directive', () => {
expect(popover).toBeNull();
}));

it('should open and close popover via focus when a hover trigger is used', fakeAsync(() => {
fixture.componentInstance.trigger = 'mouseenter';

detectChangesFakeAsync();

const button = getCallerElement();

button?.focus();
SkyAppTestUtility.fireDomEvent(button, 'focusin');

detectChangesFakeAsync();

let popover = getPopoverElement();

expect(isElementVisible(popover)).toEqual(true);

// Simulate moving the mouse to the popover.
SkyAppTestUtility.fireDomEvent(popover, 'mouseenter');

detectChangesFakeAsync();

popover = getPopoverElement();

// Confirm popover is still open.
expect(isElementVisible(popover)).toEqual(true);

// Simulate moving the mouse from the popover to the trigger button.
SkyAppTestUtility.fireDomEvent(popover, 'mouseleave');
tick();
fixture.detectChanges();
SkyAppTestUtility.fireDomEvent(button, 'mouseenter');
tick();
fixture.detectChanges();

popover = getPopoverElement();

// Confirm popover is still open.
expect(isElementVisible(popover)).toEqual(true);

// Simulate mouse leaving the trigger button.
SkyAppTestUtility.fireDomEvent(button, 'mouseleave');

detectChangesFakeAsync();

popover = getPopoverElement();

// Confirm popover is still open.
expect(isElementVisible(popover)).toEqual(true);

SkyAppTestUtility.fireDomEvent(button, 'focusout');

detectChangesFakeAsync();

popover = getPopoverElement();

// Menu should now be closed.
expect(popover).toBeNull();
}));

it('should close popover when clicking outside', fakeAsync(() => {
detectChangesFakeAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,39 @@ export class SkyPopoverDirective implements OnInit, OnDestroy {
this.skyPopover.isActive &&
this.skyPopoverTrigger === 'mouseenter'
) {
// Give the popover a chance to set its isMouseEnter flag before checking to see
// if it should be closed.
setTimeout(() => {
this.#closePopoverOrMarkForClose();
});
if (document.activeElement !== element) {
// Give the popover a chance to set its isMouseEnter flag before checking to see
// if it should be closed.
setTimeout(() => {
this.#closePopoverOrMarkForClose();
});
}
}
}
});

observableFromEvent(element, 'focusin')
.pipe(takeUntil(this.#ngUnsubscribe))
.subscribe(() => {
if (this.skyPopover) {
if (
!this.skyPopover.isActive &&
this.skyPopoverTrigger === 'mouseenter'
) {
this.#sendMessage(SkyPopoverMessageType.Open);
}
}
});

observableFromEvent(element, 'focusout')
.pipe(takeUntil(this.#ngUnsubscribe))
.subscribe(() => {
if (this.skyPopover) {
if (
this.skyPopover.isActive &&
this.skyPopoverTrigger === 'mouseenter'
) {
this.#sendMessage(SkyPopoverMessageType.Close);
}
}
});
Expand Down
Loading