-
Notifications
You must be signed in to change notification settings - Fork 1k
/
HoverForHelp.tsx
130 lines (117 loc) · 3.33 KB
/
HoverForHelp.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
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import * as React from 'react';
import PropTypes from 'prop-types';
import styled, { css } from 'styled-components';
import { OverlayTrigger } from 'components/common';
import type { SizeProp } from 'components/common/Icon';
import Icon from 'components/common/Icon';
const StyledPopover = styled.span(({ theme }) => css`
ul {
padding-left: 0;
}
li {
margin-bottom: 5px;
&:last-child {
margin-bottom: 0;
}
}
h4 {
font-size: ${theme.fonts.size.large};
}
`);
const StyledIcon = styled(Icon)<{ $type: Type, $displayLeftMargin: boolean }>(({ theme, $type, $displayLeftMargin }) => css`
display: inline-flex;
color: ${$type === 'error' ? theme.colors.variant.danger : 'inherit'};
margin: 0;
margin-left: ${$displayLeftMargin ? '0.3em' : 0};
pointer-events: auto !important;
`);
const iconName = (type: Type) => {
switch (type) {
case 'error':
return 'error';
case 'info':
default:
return 'help';
}
};
type Type = 'info' | 'error';
type Props = {
children: React.ReactNode,
className?: string,
displayLeftMargin?: boolean,
id?: string,
placement?: 'top' | 'right' | 'bottom' | 'left',
iconSize?: SizeProp
pullRight?: boolean,
title?: string,
testId?: string,
trigger?: React.ComponentProps<typeof OverlayTrigger>['trigger'],
type?: 'info' | 'error',
};
const HoverForHelp = ({
children,
className,
displayLeftMargin,
title,
id,
pullRight,
placement,
testId,
type,
iconSize,
trigger,
}: Props) => (
<OverlayTrigger trigger={trigger}
placement={placement}
overlay={<StyledPopover id={id}>{children}</StyledPopover>}
title={title}
testId={testId}>
<StyledIcon className={`${className} ${pullRight ? 'pull-right' : ''}`}
name={iconName(type)}
type="regular"
$type={type}
$displayLeftMargin={displayLeftMargin}
size={iconSize} />
</OverlayTrigger>
);
HoverForHelp.propTypes = {
displayLeftMargin: PropTypes.bool,
children: PropTypes.any.isRequired,
className: PropTypes.string,
id: PropTypes.string,
placement: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
pullRight: PropTypes.bool,
title: PropTypes.string,
testId: PropTypes.string,
trigger: PropTypes.arrayOf(PropTypes.oneOf(['click', 'focus', 'hover'])) || PropTypes.oneOf(['click', 'focus', 'hover']),
};
HoverForHelp.defaultProps = {
id: 'help-popover',
className: '',
pullRight: true,
placement: 'bottom',
testId: undefined,
title: undefined,
type: 'info',
iconSize: undefined,
trigger: ['hover', 'focus'],
displayLeftMargin: false,
};
/** @component */
export default HoverForHelp;