-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Viewer bottomSheet for mobile
- Loading branch information
Showing
4 changed files
with
216 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React, { useState, useEffect, useRef } from 'react' | ||
import { BottomSheet } from 'mui-bottom-sheet' | ||
import { makeStyles } from '@material-ui/core/styles' | ||
|
||
const useStyles = ({ isTopPosition }) => ({ | ||
root: { | ||
borderTopLeftRadius: '1rem', | ||
borderTopRightRadius: '1rem', | ||
transition: 'border-radius 0.5s', | ||
boxShadow: '0 6px 16px 0 rgba(0, 0, 0, 0.5)', | ||
...(isTopPosition && { | ||
borderTopLeftRadius: 0, | ||
borderTopRightRadius: 0 | ||
}) | ||
} | ||
}) | ||
|
||
const useClasses = makeStyles(theme => ({ | ||
header: { | ||
height: '2.5rem', | ||
width: '100%', | ||
position: 'relative', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center' | ||
}, | ||
indicator: { | ||
width: '4rem', | ||
height: '0.25rem', | ||
borderRadius: '99px', | ||
backgroundColor: theme.palette.divider | ||
} | ||
})) | ||
|
||
const BottomSheetWrapper = ({ | ||
file, | ||
actionButtonsRef, | ||
toolbarRef, | ||
children | ||
}) => { | ||
const [isTopPosition, setIsTopPosition] = useState(false) | ||
const [peekHeights, setPeekHeights] = useState(null) | ||
const [initPos, setInitPos] = useState(null) | ||
const [bottomSpacerHeight, setBottomSpacerHeight] = useState(0) | ||
const classes = useClasses() | ||
const styles = useStyles({ isTopPosition }) | ||
const innerContentRef = useRef() | ||
const headerRef = useRef() | ||
|
||
const toolbar = toolbarRef.current | ||
|
||
useEffect( | ||
() => { | ||
const maxHeight = toolbar | ||
? window.innerHeight - toolbar.offsetHeight | ||
: window.innerHeight | ||
const mediumHeight = maxHeight * 0.33 | ||
const actionButtonsHeight = parseFloat( | ||
getComputedStyle(actionButtonsRef.current).getPropertyValue('height') | ||
) | ||
// this is the margin of action buttons without bottomSheet | ||
const actionButtonsBottomMargin = 7 | ||
const minHeight = | ||
headerRef.current.offsetHeight + | ||
actionButtonsHeight + | ||
actionButtonsBottomMargin | ||
|
||
// Used so that the bottomSheet can be opened to the top, | ||
// without stopping at the content height | ||
const bottomSpacerHeight = | ||
maxHeight - innerContentRef.current.offsetHeight | ||
|
||
setPeekHeights([minHeight, mediumHeight, maxHeight]) | ||
setInitPos(mediumHeight) | ||
setBottomSpacerHeight(bottomSpacerHeight) | ||
}, | ||
[toolbar, innerContentRef, file, actionButtonsRef] | ||
) | ||
|
||
const handleOnIndexChange = snapIndex => { | ||
const maxHeightSnapIndex = peekHeights.length - 1 | ||
|
||
if (snapIndex === maxHeightSnapIndex && !isTopPosition) { | ||
setIsTopPosition(true) | ||
} | ||
if (snapIndex !== maxHeightSnapIndex && isTopPosition) { | ||
setIsTopPosition(false) | ||
} | ||
} | ||
|
||
return ( | ||
<BottomSheet | ||
peekHeights={peekHeights} | ||
defaultHeight={initPos} | ||
backdrop={false} | ||
fullHeight={false} | ||
onIndexChange={snapIndex => handleOnIndexChange(snapIndex)} | ||
styles={{ root: styles.root }} | ||
> | ||
<div ref={innerContentRef}> | ||
<div | ||
data-testid="bottomSheet-header" | ||
className={classes.header} | ||
ref={headerRef} | ||
> | ||
<div className={classes.indicator} /> | ||
</div> | ||
{children} | ||
</div> | ||
<div style={{ height: bottomSpacerHeight }} /> | ||
</BottomSheet> | ||
) | ||
} | ||
|
||
export default BottomSheetWrapper |
55 changes: 55 additions & 0 deletions
55
src/drive/web/modules/viewer/Footer/BottomSheetContent.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { forwardRef } from 'react' | ||
import PropTypes from 'prop-types' | ||
import cx from 'classnames' | ||
import { makeStyles } from '@material-ui/core/styles' | ||
|
||
import { isMobileApp } from 'cozy-device-helper' | ||
import Paper from 'cozy-ui/transpiled/react/Paper' | ||
import Typography from 'cozy-ui/transpiled/react/Typography' | ||
import Stack from 'cozy-ui/transpiled/react/Stack' | ||
|
||
import Sharing from './Sharing' | ||
import ForwardButton from './ForwardButton' | ||
import DownloadButton from './DownloadButton' | ||
import getPanelBlocks, { panelBlocksSpecs } from '../Panel/getPanelBlocks' | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
stack: { | ||
backgroundColor: theme.palette.background.default | ||
} | ||
})) | ||
|
||
const BottomSheetContent = forwardRef(({ file }, ref) => { | ||
const panelBlocks = getPanelBlocks({ panelBlocksSpecs, file }) | ||
const FileActionButton = isMobileApp() ? ForwardButton : DownloadButton | ||
const styles = useStyles() | ||
|
||
return ( | ||
<Stack | ||
spacing="s" | ||
className={cx('u-flex u-flex-column u-ov-hidden', styles.stack)} | ||
> | ||
<Paper className={'u-flex u-ph-1 u-pb-1'} elevation={2} square ref={ref}> | ||
<Sharing file={file} /> | ||
<FileActionButton file={file} /> | ||
</Paper> | ||
{panelBlocks.map((PanelBlock, index) => ( | ||
<Paper | ||
key={index} | ||
elevation={index === panelBlocks.length - 1 ? 0 : 2} | ||
square | ||
> | ||
<Typography variant="h4"> | ||
<PanelBlock file={file} /> | ||
</Typography> | ||
</Paper> | ||
))} | ||
</Stack> | ||
) | ||
}) | ||
|
||
BottomSheetContent.propTypes = { | ||
file: PropTypes.object.isRequired | ||
} | ||
|
||
export default BottomSheetContent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters