|
| 1 | +import { forwardRef, useState } from 'react' |
| 2 | + |
| 3 | +import { |
| 4 | + ConfirmDialog, |
| 5 | + SwipeableDrawer as DefaultSwipeableDrawer, |
| 6 | + IconButton, |
| 7 | + TrashCanIcon, |
| 8 | +} from '@baseapp-frontend/design-system' |
| 9 | + |
| 10 | +import { LoadingButton } from '@mui/lab' |
| 11 | +import { Box, Divider, Typography } from '@mui/material' |
| 12 | +import { LongPressCallbackReason, useLongPress } from 'use-long-press' |
| 13 | + |
| 14 | +import { ActionOverlayContainer, IconButtonContentContainer } from './styled' |
| 15 | +import { ActionOverlayProps, LongPressHandler } from './types' |
| 16 | + |
| 17 | +const ActionsOverlay = forwardRef<HTMLDivElement, ActionOverlayProps>( |
| 18 | + ( |
| 19 | + { |
| 20 | + actions = [], |
| 21 | + children, |
| 22 | + title = 'Item', |
| 23 | + enableDelete = false, |
| 24 | + isDeletingItem = false, |
| 25 | + handleDeleteItem = () => {}, |
| 26 | + offsetTop = 0, |
| 27 | + offsetRight = 0, |
| 28 | + ContainerProps = {}, |
| 29 | + SwipeableDrawerProps = {}, |
| 30 | + SwipeableDrawer = DefaultSwipeableDrawer, |
| 31 | + }, |
| 32 | + ref, |
| 33 | + ) => { |
| 34 | + const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false) |
| 35 | + const [isHoveringItem, setIsHoveringItem] = useState(false) |
| 36 | + const [longPressHandler, setLongPressHandler] = useState<LongPressHandler>({ |
| 37 | + isLongPressingItem: false, |
| 38 | + shouldOpenItemOptions: false, |
| 39 | + }) |
| 40 | + |
| 41 | + const longPressHandlers = useLongPress<HTMLDivElement>( |
| 42 | + (e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>) => { |
| 43 | + e.stopPropagation() |
| 44 | + setLongPressHandler({ isLongPressingItem: true, shouldOpenItemOptions: true }) |
| 45 | + }, |
| 46 | + { |
| 47 | + onCancel: (e, { reason }) => { |
| 48 | + // This is a workaround to prevent the comment options's drawer from closing when the user clicks on the drawer's content. |
| 49 | + // Ideally, we would call setLongPressHandler({ isLongPressingComment: false }) on `onFinished` instead of `onCancel`. |
| 50 | + // But, on mobile google chrome devices, the long press click is being wrongly propagated to the backdrop and closing the comment options's drawer right after it opens. |
| 51 | + const className = (e?.target as HTMLElement)?.className || '' |
| 52 | + const classNameString = typeof className === 'string' ? className : '' |
| 53 | + const isClickOnBackdrop = classNameString.includes('MuiBackdrop') |
| 54 | + if (reason === LongPressCallbackReason.CancelledByRelease && isClickOnBackdrop) { |
| 55 | + setLongPressHandler((prevState) => ({ ...prevState, isLongPressingItem: false })) |
| 56 | + } |
| 57 | + }, |
| 58 | + cancelOutsideElement: true, |
| 59 | + threshold: 400, |
| 60 | + }, |
| 61 | + ) |
| 62 | + const handleLongPressItemOptionsClose = () => { |
| 63 | + setLongPressHandler({ isLongPressingItem: false, shouldOpenItemOptions: false }) |
| 64 | + } |
| 65 | + |
| 66 | + const handleDeleteDialogOpen = () => { |
| 67 | + setIsDeleteDialogOpen(true) |
| 68 | + } |
| 69 | + |
| 70 | + const deviceHasHover = |
| 71 | + typeof window !== 'undefined' && window.matchMedia('(hover: hover)').matches |
| 72 | + |
| 73 | + const onDeleteItemClick = () => { |
| 74 | + setIsDeleteDialogOpen(false) |
| 75 | + handleDeleteItem?.() |
| 76 | + handleLongPressItemOptionsClose() |
| 77 | + } |
| 78 | + |
| 79 | + const renderDeleteDialog = () => ( |
| 80 | + <ConfirmDialog |
| 81 | + title={`Delete ${title}?`} |
| 82 | + content="Are you sure you want to delete? This action cannot be undone." |
| 83 | + action={ |
| 84 | + <LoadingButton |
| 85 | + color="error" |
| 86 | + onClick={onDeleteItemClick} |
| 87 | + disabled={isDeletingItem} |
| 88 | + loading={isDeletingItem} |
| 89 | + > |
| 90 | + Delete |
| 91 | + </LoadingButton> |
| 92 | + } |
| 93 | + onClose={() => setIsDeleteDialogOpen(false)} |
| 94 | + open={isDeleteDialogOpen} |
| 95 | + /> |
| 96 | + ) |
| 97 | + |
| 98 | + const renderActionsOverlay = () => { |
| 99 | + if (!deviceHasHover) { |
| 100 | + const handleDrawerClose = () => { |
| 101 | + if (!longPressHandler.isLongPressingItem) { |
| 102 | + handleLongPressItemOptionsClose() |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return ( |
| 107 | + <SwipeableDrawer |
| 108 | + open={longPressHandler.shouldOpenItemOptions && longPressHandler.isLongPressingItem} |
| 109 | + onClose={handleDrawerClose} |
| 110 | + aria-label="actions overlay" |
| 111 | + {...SwipeableDrawerProps} |
| 112 | + > |
| 113 | + <Box display="grid" gridTemplateColumns="1fr" justifySelf="start" gap={1}> |
| 114 | + {actions?.map(({ label, icon, onClick, disabled, hasPermission, closeOnClick }) => { |
| 115 | + if (!hasPermission) return null |
| 116 | + |
| 117 | + const handleClick = () => { |
| 118 | + onClick?.() |
| 119 | + if (closeOnClick) { |
| 120 | + handleLongPressItemOptionsClose() |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return ( |
| 125 | + <IconButton |
| 126 | + key={label} |
| 127 | + onClick={handleClick} |
| 128 | + disabled={disabled} |
| 129 | + sx={{ width: 'fit-content' }} |
| 130 | + aria-label={label} |
| 131 | + > |
| 132 | + <IconButtonContentContainer> |
| 133 | + <Box display="grid" justifySelf="center" height="min-content"> |
| 134 | + {icon} |
| 135 | + </Box> |
| 136 | + <Typography variant="body2">{label}</Typography> |
| 137 | + </IconButtonContentContainer> |
| 138 | + </IconButton> |
| 139 | + ) |
| 140 | + })} |
| 141 | + {enableDelete && ( |
| 142 | + <> |
| 143 | + <Divider /> |
| 144 | + <IconButton |
| 145 | + onClick={handleDeleteDialogOpen} |
| 146 | + disabled={isDeletingItem} |
| 147 | + sx={{ width: 'fit-content' }} |
| 148 | + aria-label="delete item" |
| 149 | + > |
| 150 | + <IconButtonContentContainer> |
| 151 | + <Box display="grid" justifySelf="center" height="min-content"> |
| 152 | + <TrashCanIcon /> |
| 153 | + </Box> |
| 154 | + <Typography variant="body2" color="error.main"> |
| 155 | + {`Delete ${title}`} |
| 156 | + </Typography> |
| 157 | + </IconButtonContentContainer> |
| 158 | + </IconButton> |
| 159 | + </> |
| 160 | + )} |
| 161 | + </Box> |
| 162 | + </SwipeableDrawer> |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + if (deviceHasHover && isHoveringItem) { |
| 167 | + return ( |
| 168 | + <ActionOverlayContainer |
| 169 | + offsetRight={offsetRight} |
| 170 | + offsetTop={offsetTop} |
| 171 | + aria-label="actions overlay" |
| 172 | + > |
| 173 | + {enableDelete && ( |
| 174 | + <IconButton |
| 175 | + onClick={handleDeleteDialogOpen} |
| 176 | + disabled={isDeletingItem} |
| 177 | + aria-label="delete item" |
| 178 | + > |
| 179 | + <TrashCanIcon /> |
| 180 | + </IconButton> |
| 181 | + )} |
| 182 | + {actions?.map(({ label, icon, onClick, disabled, hasPermission, closeOnClick }) => { |
| 183 | + if (!hasPermission) return null |
| 184 | + |
| 185 | + const handleClick = () => { |
| 186 | + onClick?.() |
| 187 | + if (closeOnClick) { |
| 188 | + handleLongPressItemOptionsClose() |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return ( |
| 193 | + <IconButton |
| 194 | + key={label} |
| 195 | + onClick={handleClick} |
| 196 | + disabled={disabled} |
| 197 | + aria-label={label} |
| 198 | + > |
| 199 | + {icon} |
| 200 | + </IconButton> |
| 201 | + ) |
| 202 | + })} |
| 203 | + </ActionOverlayContainer> |
| 204 | + ) |
| 205 | + } |
| 206 | + return <div /> |
| 207 | + } |
| 208 | + |
| 209 | + return ( |
| 210 | + <Box |
| 211 | + ref={ref} |
| 212 | + onMouseEnter={() => setIsHoveringItem(true)} |
| 213 | + onMouseLeave={() => setIsHoveringItem(false)} |
| 214 | + {...longPressHandlers()} |
| 215 | + {...ContainerProps} |
| 216 | + sx={{ position: 'relative', maxWidth: 'max-content' }} |
| 217 | + > |
| 218 | + {renderDeleteDialog()} |
| 219 | + {renderActionsOverlay()} |
| 220 | + {children} |
| 221 | + </Box> |
| 222 | + ) |
| 223 | + }, |
| 224 | +) |
| 225 | + |
| 226 | +export default ActionsOverlay |
0 commit comments