-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
WithTooltip.tsx
159 lines (144 loc) Β· 4.33 KB
/
WithTooltip.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, { FunctionComponent, ReactNode, useState, useEffect } from 'react';
import { styled } from '@storybook/theming';
import { logger } from '@storybook/client-logger';
// import { withState, lifecycle } from 'recompose';
import { document } from 'global';
import TooltipTrigger from 'react-popper-tooltip';
import { Modifiers, Placement } from 'popper.js';
import { Tooltip } from './Tooltip';
// A target that doesn't speak popper
const TargetContainer = styled.div<{ mode: string }>`
display: inline-block;
cursor: ${props => (props.mode === 'hover' ? 'default' : 'pointer')};
`;
const TargetSvgContainer = styled.g<{ mode: string }>`
cursor: ${props => (props.mode === 'hover' ? 'default' : 'pointer')};
`;
interface WithHideFn {
onHide: () => void;
}
export interface WithTooltipPureProps {
svg?: boolean;
trigger?: 'none' | 'hover' | 'click' | 'right-click';
closeOnClick?: boolean;
placement?: Placement;
modifiers?: Modifiers;
hasChrome?: boolean;
tooltip: ReactNode | ((p: WithHideFn) => ReactNode);
children: ReactNode;
tooltipShown?: boolean;
onVisibilityChange?: (visibility: boolean) => void;
onDoubleClick?: () => void;
}
// Pure, does not bind to the body
const WithTooltipPure: FunctionComponent<WithTooltipPureProps> = ({
svg,
trigger,
closeOnClick,
placement,
modifiers,
hasChrome,
tooltip,
children,
tooltipShown,
onVisibilityChange,
...props
}) => {
const Container = svg ? TargetSvgContainer : TargetContainer;
return (
<TooltipTrigger
placement={placement}
trigger={trigger}
modifiers={modifiers}
tooltipShown={tooltipShown}
onVisibilityChange={onVisibilityChange}
tooltip={({
getTooltipProps,
getArrowProps,
tooltipRef,
arrowRef,
placement: tooltipPlacement,
}) => (
<Tooltip
hasChrome={hasChrome}
placement={tooltipPlacement}
tooltipRef={tooltipRef}
arrowRef={arrowRef}
arrowProps={getArrowProps()}
{...getTooltipProps()}
>
{typeof tooltip === 'function'
? tooltip({ onHide: () => onVisibilityChange(false) })
: tooltip}
</Tooltip>
)}
>
{({ getTriggerProps, triggerRef }) => (
// @ts-ignore
<Container ref={triggerRef} {...getTriggerProps()} {...props}>
{children}
</Container>
)}
</TooltipTrigger>
);
};
WithTooltipPure.defaultProps = {
svg: false,
trigger: 'hover',
closeOnClick: false,
placement: 'top',
modifiers: {},
hasChrome: true,
tooltipShown: false,
};
const WithToolTipState: FunctionComponent<
WithTooltipPureProps & {
startOpen?: boolean;
}
> = ({ startOpen, ...rest }) => {
const [tooltipShown, onVisibilityChange] = useState(startOpen || false);
useEffect(() => {
const hide = () => onVisibilityChange(false);
document.addEventListener('keydown', hide, false);
// Find all iframes on the screen and bind to clicks inside them (waiting until the iframe is ready)
const iframes: HTMLIFrameElement[] = Array.from(document.getElementsByTagName('iframe'));
const unbinders: (() => void)[] = [];
iframes.forEach(iframe => {
const bind = () => {
try {
if (iframe.contentWindow.document) {
iframe.contentWindow.document.addEventListener('click', hide);
unbinders.push(() => {
try {
iframe.contentWindow.document.removeEventListener('click', hide);
} catch (e) {
logger.warn('Removing a click listener from iframe failed: ', e);
}
});
}
} catch (e) {
logger.warn('Adding a click listener to iframe failed: ', e);
}
};
bind(); // I don't know how to find out if it's already loaded so I potentially will bind twice
iframe.addEventListener('load', bind);
unbinders.push(() => {
iframe.removeEventListener('load', bind);
});
});
return () => {
document.removeEventListener('keydown', hide);
unbinders.forEach(unbind => {
unbind();
});
};
});
return (
<WithTooltipPure
{...rest}
tooltipShown={tooltipShown}
onVisibilityChange={onVisibilityChange}
/>
);
};
export { WithTooltipPure, WithToolTipState, WithToolTipState as WithTooltip };