Skip to content

Commit

Permalink
fix(TooltipDefinition): sync hidden state with other tooltip componen…
Browse files Browse the repository at this point in the history
…ts (#8655)

* fix(Button): prevent adding hidden class to TooltipDefinition

* fix(Button): restrict tooltip calls to hasIconOnly variant

* fix(TooltipDefinition): remove not selector, add show/hide logic

* chore(snapshot): update snapshots

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
tw15egan and kodiakhq[bot] authored May 13, 2021
1 parent fa7f540 commit f0b3027
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6415,6 +6415,9 @@ Map {
"id": Object {
"type": "string",
},
"onBlur": Object {
"type": "func",
},
"onFocus": Object {
"type": "func",
},
Expand Down
38 changes: 22 additions & 16 deletions packages/react/src/components/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,40 @@ const Button = React.forwardRef(function Button(
};

const handleFocus = (evt) => {
closeTooltips(evt);
setIsHovered(!isHovered);
setIsFocused(true);
setAllowTooltipVisibility(true);
if (hasIconOnly) {
closeTooltips(evt);
setIsHovered(!isHovered);
setIsFocused(true);
setAllowTooltipVisibility(true);
}
};

const handleBlur = () => {
setIsHovered(false);
setIsFocused(false);
setAllowTooltipVisibility(false);
if (hasIconOnly) {
setIsHovered(false);
setIsFocused(false);
setAllowTooltipVisibility(false);
}
};

const handleMouseEnter = (evt) => {
setIsHovered(true);
tooltipTimeout.current && clearTimeout(tooltipTimeout.current);
if (hasIconOnly) {
setIsHovered(true);
tooltipTimeout.current && clearTimeout(tooltipTimeout.current);

if (evt.target === tooltipRef.current) {
setAllowTooltipVisibility(true);
return;
}
if (evt.target === tooltipRef.current) {
setAllowTooltipVisibility(true);
return;
}

closeTooltips(evt);
closeTooltips(evt);

setAllowTooltipVisibility(true);
setAllowTooltipVisibility(true);
}
};

const handleMouseLeave = () => {
if (!isFocused) {
if (!isFocused && hasIconOnly) {
tooltipTimeout.current = setTimeout(() => {
setAllowTooltipVisibility(false);
setIsHovered(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React from 'react';

import { withKnobs, select, text } from '@storybook/addon-knobs';
import TooltipDefinition from '../TooltipDefinition';
import Button from '../Button';
import mdx from './TooltipDefinition.mdx';

const directions = {
Expand Down Expand Up @@ -54,6 +55,9 @@ export default {
export const Default = () => (
<div style={{ marginTop: '2rem' }}>
<TooltipDefinition {...props()}>Definition Tooltip</TooltipDefinition>
<Button hasIconOnly iconDescription="Filter">
Test
</Button>
</div>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const TooltipDefinition = ({
children,
direction,
align,
onBlur,
onFocus,
onMouseEnter,
onMouseLeave,
Expand All @@ -49,14 +50,25 @@ const TooltipDefinition = ({
[`${prefix}--tooltip--visible`]: tooltipVisible,
}
);

const debounceTooltipVisible = debounce(() => setTooltipVisible(false), 100);
const handleFocus = () => setAllowTooltipVisibility(true);

const handleFocus = () => {
debounceTooltipVisible.cancel();
setAllowTooltipVisibility(true);
setTooltipVisible(true);
};

const handleBlur = debounceTooltipVisible;

const handleMouseEnter = () => {
debounceTooltipVisible.cancel();
setAllowTooltipVisibility(true);
setTooltipVisible(true);
};

const handleMouseLeave = debounceTooltipVisible;

useEffect(() => {
const handleEscKeyDown = (event) => {
if (matches(event, [keys.Escape])) {
Expand All @@ -77,7 +89,8 @@ const TooltipDefinition = ({
type="button"
className={tooltipTriggerClasses}
aria-describedby={tooltipId}
onFocus={composeEventHandlers([onFocus, handleFocus])}>
onFocus={composeEventHandlers([onFocus, handleFocus])}
onBlur={composeEventHandlers([onBlur, handleBlur])}>
{children}
</button>
<div
Expand Down Expand Up @@ -119,6 +132,11 @@ TooltipDefinition.propTypes = {
*/
id: PropTypes.string,

/**
* The event handler for the `blur` event.
*/
onBlur: PropTypes.func,

/**
* The event handler for the `focus` event.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports[`TooltipDefinition should allow the user to specify the direction 1`] =
<button
aria-describedby="definition-tooltip-3"
className="bx--tooltip__trigger bx--tooltip--a11y bx--tooltip__trigger--definition bx--tooltip--top bx--tooltip--align-start"
onBlur={[Function]}
onFocus={[Function]}
type="button"
>
Expand Down Expand Up @@ -46,6 +47,7 @@ exports[`TooltipDefinition should render 1`] = `
<button
aria-describedby="definition-tooltip-1"
className="bx--tooltip__trigger bx--tooltip--a11y bx--tooltip__trigger--definition bx--tooltip--bottom bx--tooltip--align-start"
onBlur={[Function]}
onFocus={[Function]}
type="button"
>
Expand Down Expand Up @@ -78,6 +80,7 @@ exports[`TooltipDefinition should support a custom trigger element class 1`] = `
<button
aria-describedby="definition-tooltip-2"
className="bx--tooltip__trigger bx--tooltip--a11y bx--tooltip__trigger--definition custom-trigger-class bx--tooltip--bottom bx--tooltip--align-start"
onBlur={[Function]}
onFocus={[Function]}
type="button"
>
Expand Down

0 comments on commit f0b3027

Please sign in to comment.