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

bugfix(react-tooltip): stops propagation on Escape keydown #24810

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
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Includes Tooltip Escape scenarios on e2e tests",
"packageName": "@fluentui/react-dialog",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "bugfix: stops propagation on Escape key",
"packageName": "@fluentui/react-tooltip",
"email": "[email protected]",
"dependentChangeType": "patch"
}
15 changes: 12 additions & 3 deletions packages/react-components/react-dialog/e2e/Dialog.e2e.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Popover,
PopoverSurface,
PopoverTrigger,
Tooltip,
} from '@fluentui/react-components';
import {
dialogSurfaceSelector,
Expand Down Expand Up @@ -215,23 +216,31 @@ describe('Dialog', () => {
</DialogBody>
<DialogActions>
<DialogTrigger>
<Button id={dialogTriggerCloseId} appearance="secondary">
Close
</Button>
<Tooltip hideDelay={0} showDelay={0} content="Test tooltip" relationship="label">
<Button id={dialogTriggerCloseId} appearance="secondary">
Close
</Button>
</Tooltip>
</DialogTrigger>
<Button appearance="primary">Do Something</Button>
</DialogActions>
</DialogSurface>
</Dialog>,
);
// Open Menu and then close it with Escape
cy.get(dialogTriggerOpenSelector).realClick();
cy.get('#open-menu-btn').realClick();
cy.focused().realType('{esc}');
cy.get(dialogSurfaceSelector).should('exist');

// Open Popover and then close it with Escape
cy.get('#open-popover-btn').realClick();
cy.focused().realType('{esc}');
cy.get(dialogSurfaceSelector).should('exist');

// Open Tooltip, wait for the tooltip to appear and then close it with Escape
cy.get(dialogTriggerCloseSelector).focus().wait(0).realType('{esc}');
cy.get(dialogSurfaceSelector).should('exist');
});
describe('modalType = modal', () => {
it('should close with escape keydown', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-components/react-tooltip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@fluentui/scripts": "^1.0.0"
},
"dependencies": {
"@fluentui/keyboard-keys": "^9.0.0",
"@fluentui/react-portal": "^9.0.5",
"@fluentui/react-positioning": "^9.2.0",
"@fluentui/react-shared-contexts": "^9.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '@fluentui/react-utilities';
import type { TooltipProps, TooltipState, TooltipTriggerProps } from './Tooltip.types';
import { arrowHeight, tooltipBorderRadius } from './private/constants';
import { Escape } from '@fluentui/keyboard-keys';

/**
* Create the state required to render Tooltip.
Expand Down Expand Up @@ -122,19 +123,26 @@ export const useTooltip_unstable = (props: TooltipProps): TooltipState => {
context.visibleTooltip = thisTooltip;

const onDocumentKeyDown = (ev: KeyboardEvent) => {
if (ev.key === 'Escape' || ev.key === 'Esc') {
if (ev.key === Escape) {
thisTooltip.hide();
// stop propagation to avoid conflicting with other elements that listen for `Escape`
// e,g: Dialog, Popover, Menu
ev.stopPropagation();
}
};

targetDocument?.addEventListener('keydown', onDocumentKeyDown);
targetDocument?.addEventListener('keydown', onDocumentKeyDown, {
// As this event is added at targeted document,
// we need to capture the event to be sure keydown handling from tooltip happens first
capture: true,
});

return () => {
if (context.visibleTooltip === thisTooltip) {
context.visibleTooltip = undefined;
}

targetDocument?.removeEventListener('keydown', onDocumentKeyDown);
targetDocument?.removeEventListener('keydown', onDocumentKeyDown, { capture: true });
};
}
}, [context, targetDocument, visible, setVisible]);
Expand Down