-
Notifications
You must be signed in to change notification settings - Fork 91
/
Tooltip.js
184 lines (167 loc) · 5.03 KB
/
Tooltip.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import React, { cloneElement } from 'react';
import { createPortal } from 'react-dom';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { useTooltip } from '@zendeskgarden/container-tooltip';
import { composeEventHandlers } from '@zendeskgarden/container-utilities';
import { isRtl, withTheme, getDocument } from '@zendeskgarden/react-theming';
import { Manager, Popper, Target } from 'react-popper';
import { getPopperPlacement, getRtlPopperPlacement } from '../utils/gardenPlacements';
import TooltipView from '../views/TooltipView';
import LightTooltip from '../views/LightTooltip';
/**
* This container must provide a wrapper for the provided tooltip
* due to constraints in our arrow css. We must ensure that the container
* of the tooltip can retain it's relative positioning. Without this
* container Popper would apply absolute positioning.
*/
const TooltipWrapper = styled.div`
z-index: ${props => props.zIndex};
&[aria-hidden='true'] {
display: none;
}
`;
const SIZE = {
SMALL: 'small',
MEDIUM: 'medium',
LARGE: 'large',
EXTRA_LARGE: 'extra-large'
};
const TYPE = {
LIGHT: 'light',
DARK: 'dark'
};
/**
* Due to Popper.JS needing a reference to a component we provide a simple wrapper
* to ensure the correct reference is provided.
*/
const TriggerWrapper = styled.div`
display: inline-block;
`;
const Tooltip = ({
appendToBody,
arrow,
children,
delayMilliseconds,
eventsEnabled,
id,
trigger,
placement,
popperModifiers,
size,
type,
zIndex,
initialIsVisible,
...otherProps
}) => {
const { isVisible, getTooltipProps, getTriggerProps, openTooltip, closeTooltip } = useTooltip({
id,
delayMilliseconds,
isVisible: initialIsVisible
});
const popperPlacement = isRtl(otherProps)
? getRtlPopperPlacement(placement)
: getPopperPlacement(placement);
return (
<Manager tag={false}>
<>
<Target>
{({ targetProps }) => {
const triggerElement = cloneElement(trigger, getTriggerProps(trigger.props));
return <TriggerWrapper ref={targetProps.ref}>{triggerElement}</TriggerWrapper>;
}}
</Target>
<Popper
placement={popperPlacement}
eventsEnabled={eventsEnabled}
modifiers={popperModifiers}
>
{({ popperProps }) => {
const { onFocus, onBlur, ...otherTooltipProps } = otherProps;
const tooltipProps = {
arrow,
placement: popperProps['data-placement'],
size,
onFocus: composeEventHandlers(onFocus, () => {
openTooltip();
}),
onBlur: composeEventHandlers(onBlur, () => {
closeTooltip(0);
}),
...otherTooltipProps
};
const TooltipElem = type === TYPE.LIGHT ? LightTooltip : TooltipView;
const tooltip = (
<TooltipWrapper
ref={popperProps.ref}
style={popperProps.style}
zIndex={zIndex}
aria-hidden={!isVisible}
>
<TooltipElem {...getTooltipProps(tooltipProps)}>{children}</TooltipElem>
</TooltipWrapper>
);
if (appendToBody) {
return createPortal(tooltip, getDocument(otherProps).body);
}
return tooltip;
}}
</Popper>
</>
</Manager>
);
};
Tooltip.propTypes = {
/** Appends the tooltip to the body element */
appendToBody: PropTypes.bool,
arrow: PropTypes.bool,
children: PropTypes.node,
/** Milliseconds of delay before open/close of tooltip is initiated */
delayMilliseconds: PropTypes.number,
/** Whether Popper.js should update based on DOM resize events */
eventsEnabled: PropTypes.bool,
id: PropTypes.string,
trigger: PropTypes.node,
/**
* These placements differ from the default naming of Popper.JS placements to help
* assist with RTL layouts.
**/
placement: PropTypes.oneOf([
'auto',
'top',
'top-start',
'top-end',
'end',
'end-top',
'end-bottom',
'bottom',
'bottom-start',
'bottom-end',
'start',
'start-top',
'start-bottom'
]),
/** Passes options to [Popper.JS Instance](https://github.com/FezVrasta/popper.js/blob/master/docs/_includes/popper-documentation.md#new-popperreference-popper-options) */
popperModifiers: PropTypes.object,
size: PropTypes.oneOf([SIZE.SMALL, SIZE.MEDIUM, SIZE.LARGE, SIZE.EXTRA_LARGE]),
type: PropTypes.oneOf([TYPE.LIGHT, TYPE.DARK]),
/**
* The z-index of the popper.js placement container
*/
zIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
initialIsVisible: PropTypes.bool
};
Tooltip.defaultProps = {
arrow: true,
eventsEnabled: true,
type: TYPE.DARK,
placement: 'top',
delayMilliseconds: 500
};
export default withTheme(Tooltip);