/
Bubble.jsx
107 lines (88 loc) · 2.87 KB
/
Bubble.jsx
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
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import Box from '@tds/core-box'
import { colorGreyShark, colorWhite } from '@tds/core-colours'
import { media } from '@tds/core-responsive'
import Text from '@tds/core-text'
import { borders } from '@tds/shared-styles'
const dimensions = {
bubbleTriggerSize: '24px',
bubbleOffset: '7px',
bubbleTriangleHeight: '10px',
bubbleTriangleWidth: '7px',
bubbleTrianglePosition: '12px', // bubbleTriangleWidth + 5
}
const BubbleStyle = styled(({ bubbleDimensions, direction, open, ...rest }) => (
<Box {...rest} />
)).attrs(({ id }) => ({ 'data-testid': 'bubble', id }))(
borders.rounded,
({ bubbleDimensions, direction, open }) => ({
...{ display: open ? undefined : 'none' },
position: 'absolute',
bottom: `calc(100% + ${bubbleDimensions.bubbleTriangleHeight})`,
backgroundColor: colorWhite,
boxShadow: `0 0 2px 0 ${colorGreyShark}, 0 3px 2px 0 rgba(84, 89, 95, 0.25)`,
...{ right: direction === 'left' ? `-${bubbleDimensions.bubbleOffset}` : undefined },
...{
left:
direction === 'right'
? `calc(100% - ${bubbleDimensions.bubbleTriggerSize} - ${bubbleDimensions.bubbleOffset})`
: undefined,
},
'&:before': {
content: `''`,
display: 'block',
position: 'absolute',
bottom: `-${bubbleDimensions.bubbleTriangleWidth}`,
borderWidth: bubbleDimensions.bubbleTriangleWidth,
borderStyle: 'solid',
borderColor: `transparent ${colorWhite} ${colorWhite} transparent`,
backgroundColor: colorWhite,
boxShadow: '2px 2px 3px 0 rgba(42, 42, 44, 0.4)',
transform: 'rotate(45deg)',
...{ right: direction === 'left' ? bubbleDimensions.bubbleTrianglePosition : undefined },
...{ left: direction === 'right' ? bubbleDimensions.bubbleTrianglePosition : undefined },
},
...media.until('sm').css({
maxWidth: '80vw',
}),
...media.from('sm').css({
maxWidth: '50vw',
}),
...media.from('md').css({
maxWidth: '25vw',
}),
})
)
const InnerBubbleStyle = styled.div(({ bubbleWidth }) => ({
...bubbleWidth,
maxWidth: '100%',
}))
const Bubble = ({ id, direction, open, width, children }) => {
return (
<BubbleStyle
vertical={2}
horizontal={3}
bubbleDimensions={dimensions}
direction={direction}
open={open}
role="tooltip"
aria-live="assertive"
aria-hidden={open ? 'false' : 'true'}
id={id}
>
<InnerBubbleStyle bubbleWidth={width}>
<Text size="small">{children}</Text>
</InnerBubbleStyle>
</BubbleStyle>
)
}
Bubble.propTypes = {
id: PropTypes.string.isRequired,
direction: PropTypes.oneOf(['left', 'right']).isRequired,
open: PropTypes.bool.isRequired,
width: PropTypes.object.isRequired,
children: PropTypes.node.isRequired,
}
export default Bubble