-
Notifications
You must be signed in to change notification settings - Fork 150
/
ContentWrapper.js
174 lines (154 loc) · 4.79 KB
/
ContentWrapper.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
// @flow
import React, { useRef, useEffect } from "react";
import styled, { css, keyframes } from "styled-components";
import defaultTheme from "../../defaultTheme";
import media from "../../utils/mediaQuery";
import Button from "../../Button";
import resolvePopoverPosition from "../helpers/resolvePopoverPosition";
import resolvePopoverHorizontal from "../helpers/resolvePopoverHorizontal";
import calculatePopoverPosition from "../helpers/calculatePopoverPosition";
import type { Props } from "./ContentWrapper.js.flow";
import useDimensions from "../hooks/useDimensions";
import useVerticalPosition from "../hooks/useVerticalPosition";
import useHorizontalPosition from "../hooks/useHorizontalPosition";
import Translate from "../../Translate";
const showAnimation = keyframes`
from {
transform: translateY(100%);
}
to {
transform: translateY(0);
}
`;
const opacityAnimation = keyframes`
from {
opacity: 0;
}
to {
opacity: 1;
}
`;
const StyledPopoverParent = styled.div`
position: fixed;
bottom: 0;
left: 0;
right: 0;
width: 100%;
box-sizing: border-box;
border-top-left-radius: 9px; /* TODO: Add token */
border-top-right-radius: 9px; /* TODO: Add token */
animation: ${showAnimation} ${({ theme }) => theme.orbit.durationFast} linear;
background-color: ${({ theme }) => theme.orbit.backgroundModal}; // TODO: Add token
padding: ${({ theme, noPadding }) => (noPadding ? 0 : theme.orbit.spaceSmall)};
padding-top: ${({ theme, noPadding }) => (noPadding ? 0 : theme.orbit.spaceMedium)};
box-shadow: ${({ theme }) => theme.orbit.boxShadowElevatedLevel1};
overflow: hidden;
z-index: 1000;
&:focus {
outline: 0;
}
${media.largeMobile(css`
position: absolute;
left: auto;
right: auto;
bottom: auto;
width: ${({ width }) => (width ? `${width}` : "auto")};
animation: ${opacityAnimation} ${({ theme }) => theme.orbit.durationFast} linear;
border-radius: ${({ theme }) => theme.orbit.borderRadiusNormal};
${resolvePopoverPosition}
${resolvePopoverHorizontal}
`)}
`;
StyledPopoverParent.defaultProps = {
theme: defaultTheme,
};
const StyledPopoverContent = styled.div``;
const StyledOverlay = styled.div`
display: block;
position: fixed;
top: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
background-color: rgba(23, 27, 30, 0.6); // TODO: token
animation: ${opacityAnimation} ${({ theme }) => theme.orbit.durationFast} ease-in;
z-index: 999;
${media.largeMobile(css`
background-color: transparent;
`)};
`;
StyledOverlay.defaultProps = {
theme: defaultTheme,
};
const StyledPopoverClose = styled.div`
padding: ${({ theme, noPadding }) => (noPadding ? theme.orbit.spaceSmall : 0)};
padding-top: ${({ theme }) => theme.orbit.spaceMedium};
${media.largeMobile(css`
display: none;
visibility: hidden;
padding-bottom: 0;
`)}
`;
StyledPopoverClose.defaultProps = {
theme: defaultTheme,
};
const PopoverContentWrapper = ({
children,
onClose,
width,
dataTest,
preferredPosition,
containerRef,
noPadding,
}: Props) => {
const popover: { current: React$ElementRef<*> } = useRef(null);
const content: { current: React$ElementRef<*> } = useRef(null);
const overlay: { current: React$ElementRef<*> } = useRef(null);
const position = calculatePopoverPosition(preferredPosition);
const dimensions = useDimensions({ containerRef, popover, content });
const verticalPosition = useVerticalPosition(position[0], dimensions);
const horizontalPosition = useHorizontalPosition(position[1], dimensions);
const handleClick = (ev: SyntheticEvent<HTMLElement>) => {
ev.stopPropagation();
if (ev.target === overlay?.current) {
onClose();
}
};
useEffect(() => {
if (popover.current) {
popover.current.focus();
}
}, []);
return (
<React.Fragment>
<StyledOverlay ref={overlay} onClick={handleClick} />
<StyledPopoverParent
anchor={horizontalPosition}
position={verticalPosition}
containerTop={dimensions.containerTop}
containerLeft={dimensions.containerLeft}
containerHeight={dimensions.containerHeight}
containerWidth={dimensions.containerWidth}
popoverHeight={dimensions.popoverHeight}
popoverWidth={dimensions.popoverWidth}
width={width}
ref={popover}
onClick={handleClick}
tabIndex="0"
data-test={dataTest}
noPadding={noPadding}
>
<StyledPopoverContent ref={content}>
{children}
<StyledPopoverClose noPadding={noPadding}>
<Button type="secondary" block onClick={onClose}>
<Translate tKey="button_close" />
</Button>
</StyledPopoverClose>
</StyledPopoverContent>
</StyledPopoverParent>
</React.Fragment>
);
};
export default PopoverContentWrapper;