2022-02-02 13:44:33 +01:00
|
|
|
// @flow
|
2022-02-02 13:48:24 +01:00
|
|
|
import 'scss/component/_swipeable-drawer.scss';
|
|
|
|
|
2022-02-02 13:44:33 +01:00
|
|
|
// $FlowFixMe
|
|
|
|
import { Global } from '@emotion/react';
|
|
|
|
// $FlowFixMe
|
|
|
|
import { grey } from '@mui/material/colors';
|
2022-02-02 13:45:16 +01:00
|
|
|
|
2022-02-23 22:13:22 +01:00
|
|
|
import { HEADER_HEIGHT_MOBILE } from 'component/fileRenderFloating/view';
|
|
|
|
import { PRIMARY_PLAYER_WRAPPER_CLASS, PRIMARY_IMAGE_WRAPPER_CLASS } from 'page/file/view';
|
2022-04-04 14:13:15 +02:00
|
|
|
import { getMaxLandscapeHeight } from 'component/fileRenderFloating/helper-functions';
|
2022-02-02 13:44:33 +01:00
|
|
|
import { SwipeableDrawer as MUIDrawer } from '@mui/material';
|
|
|
|
import * as ICONS from 'constants/icons';
|
|
|
|
import * as React from 'react';
|
|
|
|
import Button from 'component/button';
|
2022-02-07 14:04:25 +01:00
|
|
|
import classnames from 'classnames';
|
2022-02-02 13:44:33 +01:00
|
|
|
|
|
|
|
const DRAWER_PULLER_HEIGHT = 42;
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: Node,
|
2022-02-02 13:45:16 +01:00
|
|
|
title: any,
|
2022-02-07 14:04:25 +01:00
|
|
|
hasSubtitle?: boolean,
|
2022-02-02 13:45:16 +01:00
|
|
|
actions?: any,
|
2022-04-04 14:13:15 +02:00
|
|
|
// -- redux --
|
|
|
|
open: boolean,
|
|
|
|
theme: string,
|
2022-02-02 13:44:33 +01:00
|
|
|
toggleDrawer: () => void,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function SwipeableDrawer(props: Props) {
|
2022-04-04 14:13:15 +02:00
|
|
|
const { title, hasSubtitle, children, open, theme, actions, toggleDrawer } = props;
|
2022-02-02 13:44:33 +01:00
|
|
|
|
|
|
|
const [coverHeight, setCoverHeight] = React.useState();
|
|
|
|
|
2022-04-04 14:13:15 +02:00
|
|
|
const videoHeight = coverHeight || getMaxLandscapeHeight() || 0;
|
2022-02-23 22:13:22 +01:00
|
|
|
|
|
|
|
const handleResize = React.useCallback(() => {
|
|
|
|
const element =
|
|
|
|
document.querySelector(`.${PRIMARY_IMAGE_WRAPPER_CLASS}`) ||
|
|
|
|
document.querySelector(`.${PRIMARY_PLAYER_WRAPPER_CLASS}`);
|
|
|
|
|
|
|
|
if (!element) return;
|
|
|
|
|
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
setCoverHeight(rect.height);
|
|
|
|
}, []);
|
2022-02-02 13:44:33 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-02-23 22:13:22 +01:00
|
|
|
// Drawer will follow the cover image on resize, so it's always visible
|
2022-04-04 14:13:15 +02:00
|
|
|
if (open) {
|
2022-02-23 22:13:22 +01:00
|
|
|
handleResize();
|
|
|
|
|
|
|
|
window.addEventListener('resize', handleResize);
|
2022-02-02 13:44:33 +01:00
|
|
|
|
2022-02-23 22:13:22 +01:00
|
|
|
return () => window.removeEventListener('resize', handleResize);
|
2022-02-02 13:44:33 +01:00
|
|
|
}
|
2022-04-04 14:13:15 +02:00
|
|
|
}, [handleResize, open]);
|
2022-02-02 13:44:33 +01:00
|
|
|
|
2022-02-02 13:49:02 +01:00
|
|
|
// Reset scroll position when opening: avoid broken position where
|
|
|
|
// the drawer is lower than the video
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (open) {
|
|
|
|
const htmlEl = document.querySelector('html');
|
|
|
|
if (htmlEl) htmlEl.scrollTop = 0;
|
|
|
|
}
|
|
|
|
}, [open]);
|
2022-02-02 13:44:33 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-02-02 13:49:02 +01:00
|
|
|
<DrawerGlobalStyles open={open} videoHeight={videoHeight} />
|
2022-02-02 13:44:33 +01:00
|
|
|
|
|
|
|
<MUIDrawer
|
|
|
|
anchor="bottom"
|
|
|
|
open={open}
|
|
|
|
onClose={toggleDrawer}
|
|
|
|
onOpen={toggleDrawer}
|
|
|
|
hideBackdrop
|
|
|
|
disableEnforceFocus
|
|
|
|
disablePortal
|
|
|
|
disableSwipeToOpen
|
|
|
|
ModalProps={{ keepMounted: true }}
|
|
|
|
>
|
2022-02-02 13:45:16 +01:00
|
|
|
{open && (
|
2022-02-02 13:44:33 +01:00
|
|
|
<div className="swipeable-drawer__header" style={{ top: -DRAWER_PULLER_HEIGHT }}>
|
2022-02-02 13:48:24 +01:00
|
|
|
<Puller theme={theme} />
|
2022-02-07 14:04:25 +01:00
|
|
|
<HeaderContents title={title} hasSubtitle={hasSubtitle} actions={actions} toggleDrawer={toggleDrawer} />
|
2022-02-02 13:44:33 +01:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{children}
|
|
|
|
</MUIDrawer>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2022-02-02 13:48:24 +01:00
|
|
|
|
2022-02-02 13:49:02 +01:00
|
|
|
type GlobalStylesProps = {
|
2022-04-04 14:13:15 +02:00
|
|
|
open: boolean,
|
2022-02-02 13:49:02 +01:00
|
|
|
videoHeight: number,
|
|
|
|
};
|
|
|
|
|
2022-04-04 14:13:15 +02:00
|
|
|
const DrawerGlobalStyles = (props: GlobalStylesProps) => {
|
|
|
|
const { open, videoHeight } = props;
|
2022-02-02 13:49:02 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Global
|
|
|
|
styles={{
|
|
|
|
'.main-wrapper__inner--filepage': {
|
|
|
|
overflow: open ? 'hidden' : 'unset',
|
|
|
|
maxHeight: open ? '100vh' : 'unset',
|
|
|
|
},
|
|
|
|
'.main-wrapper .MuiDrawer-root': {
|
|
|
|
top: `calc(${HEADER_HEIGHT_MOBILE}px + ${videoHeight}px) !important`,
|
|
|
|
},
|
|
|
|
'.main-wrapper .MuiDrawer-root > .MuiPaper-root': {
|
|
|
|
overflow: 'visible',
|
|
|
|
color: 'var(--color-text)',
|
|
|
|
position: 'absolute',
|
|
|
|
height: `calc(100% - ${DRAWER_PULLER_HEIGHT}px)`,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-02 13:48:24 +01:00
|
|
|
type PullerProps = {
|
|
|
|
theme: string,
|
|
|
|
};
|
|
|
|
|
2022-04-04 14:13:15 +02:00
|
|
|
const Puller = (props: PullerProps) => {
|
|
|
|
const { theme } = props;
|
2022-02-02 13:48:24 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span className="swipeable-drawer__puller" style={{ backgroundColor: theme === 'light' ? grey[300] : grey[800] }} />
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
type HeaderProps = {
|
|
|
|
title: any,
|
2022-02-07 14:04:25 +01:00
|
|
|
hasSubtitle?: boolean,
|
2022-02-02 13:48:24 +01:00
|
|
|
actions?: any,
|
|
|
|
toggleDrawer: () => void,
|
|
|
|
};
|
|
|
|
|
2022-04-04 14:13:15 +02:00
|
|
|
const HeaderContents = (props: HeaderProps) => {
|
|
|
|
const { title, hasSubtitle, actions, toggleDrawer } = props;
|
2022-02-02 13:48:24 +01:00
|
|
|
|
|
|
|
return (
|
2022-02-07 14:04:25 +01:00
|
|
|
<div
|
|
|
|
className={classnames('swipeable-drawer__header-content', {
|
|
|
|
'swipeable-drawer__header--with-subtitle': hasSubtitle,
|
|
|
|
})}
|
|
|
|
>
|
2022-02-02 13:48:24 +01:00
|
|
|
{title}
|
|
|
|
|
|
|
|
<div className="swipeable-drawer__header-actions">
|
|
|
|
{actions}
|
|
|
|
|
|
|
|
<Button icon={ICONS.REMOVE} iconSize={16} onClick={toggleDrawer} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|