-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
SpeedDialAction.js
226 lines (213 loc) · 5.55 KB
/
SpeedDialAction.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// @inheritedComponent Tooltip
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { emphasize, withStyles } from '@material-ui/core/styles';
import Fab from '@material-ui/core/Fab';
import Tooltip from '@material-ui/core/Tooltip';
import { capitalize } from '@material-ui/core/utils';
export const styles = theme => ({
/* Styles applied to the Fab component. */
fab: {
margin: 8,
color: theme.palette.text.secondary,
backgroundColor: theme.palette.background.paper,
'&:hover': {
backgroundColor: emphasize(theme.palette.background.paper, 0.15),
},
transition: `${theme.transitions.create('transform', {
duration: theme.transitions.duration.shorter,
})}, opacity 0.8s`,
opacity: 1,
},
/* Styles applied to the Fab component if `open={false}`. */
fabClosed: {
opacity: 0,
transform: 'scale(0)',
},
/* Styles applied to the root element if `tooltipOpen={true}`. */
staticTooltip: {
position: 'relative',
display: 'flex',
'& $staticTooltipLabel': {
transition: theme.transitions.create(['transform', 'opacity'], {
duration: theme.transitions.duration.shorter,
}),
opacity: 1,
},
},
/* Styles applied to the root element if `tooltipOpen={true}` and `open={false}`. */
staticTooltipClosed: {
'& $staticTooltipLabel': {
opacity: 0,
transform: 'scale(0.5)',
},
},
/* Styles applied to the static tooltip label if `tooltipOpen={true}`. */
staticTooltipLabel: {
position: 'absolute',
...theme.typography.body1,
backgroundColor: theme.palette.background.paper,
borderRadius: theme.shape.borderRadius,
boxShadow: theme.shadows[1],
color: theme.palette.text.secondary,
padding: '4px 16px',
},
/* Styles applied to the root if `tooltipOpen={true}` and `tooltipPlacement="left"`` */
tooltipPlacementLeft: {
alignItems: 'center',
'& $staticTooltipLabel': {
transformOrigin: '100% 50%',
right: '100%',
marginRight: 8,
},
},
/* Styles applied to the root if `tooltipOpen={true}` and `tooltipPlacement="right"`` */
tooltipPlacementRight: {
alignItems: 'center',
'& $staticTooltipLabel': {
transformOrigin: '0% 50%',
left: '100%',
marginLeft: 8,
},
},
});
const SpeedDialAction = React.forwardRef(function SpeedDialAction(props, ref) {
const {
classes,
className,
delay = 0,
FabProps,
icon,
id,
open,
TooltipClasses,
tooltipOpen: tooltipOpenProp = false,
tooltipPlacement = 'left',
tooltipTitle,
...other
} = props;
const [tooltipOpen, setTooltipOpen] = React.useState(tooltipOpenProp);
const handleTooltipClose = () => {
setTooltipOpen(false);
};
const handleTooltipOpen = () => {
setTooltipOpen(true);
};
const transitionStyle = { transitionDelay: `${delay}ms` };
if (FabProps && FabProps.style) {
FabProps.style.transitionDelay = `${delay}ms`;
}
const fab = (
<Fab
size="small"
className={clsx(classes.fab, !open && classes.fabClosed, className)}
tabIndex={-1}
role="menuitem"
style={transitionStyle}
aria-describedby={`${id}-label`}
{...FabProps}
>
{icon}
</Fab>
);
if (tooltipOpenProp) {
return (
<span
id={id}
ref={ref}
className={clsx(
classes.staticTooltip,
!open && classes.staticTooltipClosed,
classes[`tooltipPlacement${capitalize(tooltipPlacement)}`],
)}
{...other}
>
<span style={transitionStyle} id={`${id}-label`} className={classes.staticTooltipLabel}>
{tooltipTitle}
</span>
{fab}
</span>
);
}
return (
<Tooltip
id={id}
ref={ref}
title={tooltipTitle}
placement={tooltipPlacement}
onClose={handleTooltipClose}
onOpen={handleTooltipOpen}
open={open && tooltipOpen}
classes={TooltipClasses}
{...other}
>
{fab}
</Tooltip>
);
});
SpeedDialAction.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* Adds a transition delay, to allow a series of SpeedDialActions to be animated.
*/
delay: PropTypes.number,
/**
* Props applied to the [`Fab`](/api/fab/) component.
*/
FabProps: PropTypes.object,
/**
* The Icon to display in the SpeedDial Fab.
*/
icon: PropTypes.node,
/**
* @ignore
*/
id: PropTypes.string,
/**
* @ignore
*/
open: PropTypes.bool,
/**
* Classes applied to the [`Tooltip`](/api/tooltip/) element.
*/
TooltipClasses: PropTypes.object,
/**
* Make the tooltip always visible when the SpeedDial is open.
*/
tooltipOpen: PropTypes.bool,
/**
* Placement of the tooltip.
*/
tooltipPlacement: PropTypes.oneOf([
'bottom-end',
'bottom-start',
'bottom',
'left-end',
'left-start',
'left',
'right-end',
'right-start',
'right',
'top-end',
'top-start',
'top',
]),
/**
* Label to display in the tooltip.
*/
tooltipTitle: PropTypes.node,
};
export default withStyles(styles, { name: 'MuiSpeedDialAction' })(SpeedDialAction);