-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract out PopupBase and use it in Alert + Popup
- Loading branch information
1 parent
bfd56e3
commit ccd0e60
Showing
6 changed files
with
134 additions
and
81 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { PropsWithChildren } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { alertActionCreator as alert } from '../actions/alert' | ||
import { clearMulticursorsActionCreator as clearMulticursors } from '../actions/clearMulticursors' | ||
import { deleteResumableFile } from '../actions/importFiles' | ||
import { isTouch } from '../browser' | ||
import { AlertType } from '../constants' | ||
import useCombinedRefs from '../hooks/useCombinedRefs' | ||
import usePositionFixed from '../hooks/usePositionFixed' | ||
import useSwipeToDismiss from '../hooks/useSwipeToDismiss' | ||
import syncStatusStore from '../stores/syncStatus' | ||
import fastClick from '../util/fastClick' | ||
import CloseButton from './CloseButton' | ||
|
||
export type PopupBaseProps = PropsWithChildren<{ | ||
// used to cancel imports | ||
importFileId?: string | ||
/** If defined, will show a small x in the upper right corner. */ | ||
onClose?: () => void | ||
disableTop?: boolean | ||
className?: string | ||
style?: React.CSSProperties | ||
closeButtonSize?: 'sm' | 'md' | ||
}> | ||
|
||
/** A popup component that can be dismissed. */ | ||
const PopupBase = React.forwardRef<HTMLDivElement, PopupBaseProps>( | ||
({ children, importFileId, onClose, className, style, disableTop = false, closeButtonSize }, ref) => { | ||
const dispatch = useDispatch() | ||
|
||
const fontSize = useSelector(state => state.fontSize) | ||
const multicursor = useSelector(state => state.alert?.alertType === AlertType.MulticursorActive) | ||
const positionFixedStyles = usePositionFixed({ disableTop }) | ||
const useSwipeToDismissProps = useSwipeToDismiss({ | ||
// dismiss after animation is complete to avoid touch events going to the Toolbar | ||
onDismissEnd: () => { | ||
dispatch(alert(null)) | ||
}, | ||
}) | ||
|
||
const combinedRefs = useCombinedRefs(isTouch ? [useSwipeToDismissProps.ref, ref] : [ref]) | ||
|
||
return ( | ||
<div | ||
className={className} | ||
{...(isTouch ? useSwipeToDismissProps : null)} | ||
ref={combinedRefs} | ||
// merge style with useSwipeToDismissProps.style (transform, transition, and touchAction for sticking to user's touch) | ||
style={{ | ||
...positionFixedStyles, | ||
fontSize, | ||
...(isTouch ? useSwipeToDismissProps.style : null), | ||
...style, | ||
}} | ||
> | ||
{children} | ||
{importFileId && ( | ||
<a | ||
onClick={() => { | ||
deleteResumableFile(importFileId!) | ||
syncStatusStore.update({ importProgress: 1 }) | ||
onClose?.() | ||
}} | ||
> | ||
cancel | ||
</a> | ||
)} | ||
{multicursor && ( | ||
<a | ||
{...fastClick(() => { | ||
dispatch(clearMulticursors()) | ||
onClose?.() | ||
})} | ||
> | ||
cancel | ||
</a> | ||
)} | ||
{onClose ? <CloseButton size={closeButtonSize} onClose={onClose} disableSwipeToDismiss /> : null} | ||
</div> | ||
) | ||
}, | ||
) | ||
|
||
PopupBase.displayName = 'Popup' | ||
|
||
export default PopupBase |
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