Skip to content

Commit 4259031

Browse files
authored
allows Tooltip to open on hover (#487)
1 parent 5056d03 commit 4259031

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

spec/__snapshots__/Storyshots.test.js.snap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19711,6 +19711,52 @@ exports[`Storyshots Design System/Tooltip With Header 1`] = `
1971119711
</div>
1971219712
`;
1971319713

19714+
exports[`Storyshots Design System/Tooltip With Hover 1`] = `
19715+
<div
19716+
style={
19717+
Object {
19718+
"padding": "1rem",
19719+
}
19720+
}
19721+
>
19722+
<div
19723+
style={
19724+
Object {
19725+
"padding": "4rem",
19726+
}
19727+
}
19728+
>
19729+
<span
19730+
aria-hidden="true"
19731+
className="Tooltip__icon"
19732+
onClick={[Function]}
19733+
onKeyPress={[Function]}
19734+
onMouseEnter={[Function]}
19735+
onMouseLeave={[Function]}
19736+
tabIndex="0"
19737+
>
19738+
<svg
19739+
aria-hidden="true"
19740+
className="svg-inline--fa fa-question-circle fa-w-16 "
19741+
data-icon="question-circle"
19742+
data-prefix="fas"
19743+
focusable="false"
19744+
role="img"
19745+
style={Object {}}
19746+
viewBox="0 0 512 512"
19747+
xmlns="http://www.w3.org/2000/svg"
19748+
>
19749+
<path
19750+
d="M504 256c0 136.997-111.043 248-248 248S8 392.997 8 256C8 119.083 119.043 8 256 8s248 111.083 248 248zM262.655 90c-54.497 0-89.255 22.957-116.549 63.758-3.536 5.286-2.353 12.415 2.715 16.258l34.699 26.31c5.205 3.947 12.621 3.008 16.665-2.122 17.864-22.658 30.113-35.797 57.303-35.797 20.429 0 45.698 13.148 45.698 32.958 0 14.976-12.363 22.667-32.534 33.976C247.128 238.528 216 254.941 216 296v4c0 6.627 5.373 12 12 12h56c6.627 0 12-5.373 12-12v-1.333c0-28.462 83.186-29.647 83.186-106.667 0-58.002-60.165-102-116.531-102zM256 338c-25.365 0-46 20.635-46 46 0 25.364 20.635 46 46 46s46-20.636 46-46c0-25.365-20.635-46-46-46z"
19751+
fill="currentColor"
19752+
style={Object {}}
19753+
/>
19754+
</svg>
19755+
</span>
19756+
</div>
19757+
</div>
19758+
`;
19759+
1971419760
exports[`Storyshots Design System/Tooltip With Html 1`] = `
1971519761
<div
1971619762
style={

src/Tooltip/Tooltip.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class Tooltip extends Component {
4848
}
4949
};
5050

51-
handleToggleTooltip = (event) => {
51+
handleToggleTooltipClick = (event) => {
52+
if (this.props.withHover) return;
53+
5254
event.preventDefault();
5355
this.clickOutsideListener = addClickOutsideListener(
5456
event.target.parentNode,
@@ -58,6 +60,10 @@ class Tooltip extends Component {
5860
this.setState((state) => ({ visible: !state.visible }), this.handleShow);
5961
};
6062

63+
handleToggleTooltipHover = () => {
64+
this.setState((state) => ({ visible: !state.visible }), this.handleShow);
65+
};
66+
6167
render() {
6268
return (
6369
<Popper
@@ -74,8 +80,10 @@ class Tooltip extends Component {
7480
className={classNames('Tooltip__icon', this.props.iconClasses)}
7581
/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */
7682
tabIndex="0"
77-
onClick={this.handleToggleTooltip}
78-
onKeyPress={this.handleToggleTooltip}
83+
onClick={this.handleToggleTooltipClick}
84+
onKeyPress={this.handleToggleTooltipClick}
85+
onMouseEnter={this.props.withHover && this.handleToggleTooltipHover}
86+
onMouseLeave={this.props.withHover && this.handleToggleTooltipHover}
7987
>
8088
<FontAwesomeIcon icon={this.props.icon} />
8189
</span>
@@ -92,6 +100,7 @@ Tooltip.propTypes = {
92100
strategy: PropTypes.string,
93101
text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
94102
theme: PropTypes.string,
103+
withHover: PropTypes.bool,
95104
onShow: PropTypes.func,
96105
};
97106

@@ -101,6 +110,7 @@ Tooltip.defaultProps = {
101110
header: undefined,
102111
strategy: undefined,
103112
theme: 'dark',
113+
withHover: undefined,
104114
onShow: undefined,
105115
};
106116

src/Tooltip/Tooltip.stories.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ export const GrayIcon = () => (
7070
/>
7171
);
7272

73+
export const WithHover = () => (
74+
<Tooltip
75+
placement={radios('Placement', ['right', 'top', 'bottom', 'left', 'auto'], 'right')}
76+
text={text('Tooltip Text', 'Default Tooltip')}
77+
withHover
78+
/>
79+
);
80+
7381
const trackingEvent = {
7482
event: 'test tracking event',
7583
eventData: { userId: 1 },

0 commit comments

Comments
 (0)