Skip to content

Commit

Permalink
fix: #6680, Tooltip: Escape key not dismissing tooltip (WCAG 2.1 1.4.…
Browse files Browse the repository at this point in the history
…13) (#6687)
  • Loading branch information
akshayaqburst authored May 27, 2024
1 parent 5b6b09b commit f5237b1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
8 changes: 8 additions & 0 deletions components/doc/common/apidoc/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -51151,6 +51151,14 @@
"default": "",
"description": "Content to be displayed in tooltip."
},
{
"name": "closeOnEscape",
"optional": true,
"readonly": false,
"type": "boolean",
"default": "false",
"description": "Specifies if pressing escape key should hide the tooltip."
},
{
"name": "disabled",
"optional": true,
Expand Down
3 changes: 2 additions & 1 deletion components/lib/hooks/useGlobalOnEscapeKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const ESC_KEY_HANDLING_PRIORITIES = {
PASSWORD: 700,
CASCADE_SELECT: 800,
SPLIT_BUTTON: 900,
SPEED_DIAL: 1000
SPEED_DIAL: 1000,
TOOLTIP: 1200
};

/**
Expand Down
9 changes: 8 additions & 1 deletion components/lib/tooltip/Tooltip.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import PrimeReact, { PrimeReactContext } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMergeProps, useMountEffect, useOverlayScrollListener, useResizeListener, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { useMergeProps, useMountEffect, useOverlayScrollListener, useResizeListener, useUnmountEffect, useUpdateEffect, useGlobalOnEscapeKey, ESC_KEY_HANDLING_PRIORITIES } from '../hooks/Hooks';
import { Portal } from '../portal/Portal';
import { DomHandler, ObjectUtils, ZIndexUtils, classNames } from '../utils/Utils';
import { TooltipBase } from './TooltipBase';
Expand Down Expand Up @@ -32,6 +32,13 @@ export const Tooltip = React.memo(
const { ptm, cx, sx, isUnstyled } = TooltipBase.setMetaData(metaData);

useHandleStyle(TooltipBase.css.styles, isUnstyled, { name: 'tooltip' });
useGlobalOnEscapeKey({
callback: () => {
hide();
},
when: props.closeOnEscape,
priority: [ESC_KEY_HANDLING_PRIORITIES.TOOLTIP, 0]
});
const elementRef = React.useRef(null);
const textRef = React.useRef(null);
const currentTargetRef = React.useRef(null);
Expand Down
1 change: 1 addition & 0 deletions components/lib/tooltip/TooltipBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const TooltipBase = ComponentBase.extend({
autoZIndex: true,
baseZIndex: 0,
className: null,
closeOnEscape: false,
content: null,
disabled: false,
event: null,
Expand Down
5 changes: 5 additions & 0 deletions components/lib/tooltip/tooltipoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export interface TooltipOptions {
* Style class of the tooltip.
*/
className?: string | undefined;
/**
* Specifies if pressing escape key should hide the tooltip.
* @defaultValue false
*/
closeOnEscape?: boolean | undefined;
/**
* When present, it specifies that the tooltip should be hidden.
* @defaultValue false
Expand Down

0 comments on commit f5237b1

Please sign in to comment.