-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathCenteredContainer.js
51 lines (46 loc) · 1.36 KB
/
CenteredContainer.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
import React, { useRef, useLayoutEffect } from 'react';
import PropTypes from 'prop-types';
import scrollParent from '../../util/getScrollParent';
import Layout from '../Layout';
import css from './MCLRenderer.css';
const CenteredContainer = ({ innerRef, visible, children, role, style: styleProp }) => {
const wrappingElement = useRef(innerRef || null);
// useLayoutEffect will give us the DOM nodes for measuring just
// before the browser re-paints the screen, preventing potential flash of
// the incorrect/ un-centered element.
useLayoutEffect(() => {
if (wrappingElement.current) {
const sp = scrollParent(wrappingElement.current);
if (sp) {
wrappingElement.current.style.width = `${sp.offsetWidth}px`;
}
}
});
return (
<div
ref={wrappingElement}
className={css.mclCenteredContainer}
style={
{
visibility: `${visible ? 'visible' : 'hidden'}`,
height: `${visible ? null : 0}`,
padding: `${visible ? null : 0}`,
...styleProp
}
}
role={role}
>
<Layout className="textCentered">
{children}
</Layout>
</div>
);
};
CenteredContainer.propTypes = {
children: PropTypes.node,
innerRef: PropTypes.object,
role: PropTypes.string,
style: PropTypes.object,
visible: PropTypes.bool,
};
export default CenteredContainer;