-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
194 additions
and
16 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
106 changes: 106 additions & 0 deletions
106
...ages/extension-polkagate/src/fullscreen/governance/components/DraggableModalWithTitle.tsx
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,106 @@ | ||
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/* eslint-disable react/jsx-max-props-per-line */ | ||
|
||
import type { IconDefinition } from '@fortawesome/fontawesome-svg-core'; | ||
|
||
import { Box, Modal, useTheme } from '@mui/material'; | ||
import React, { useCallback, useMemo, useState } from 'react'; | ||
|
||
import ModalTitleWithDrag from '../../partials/ModalTitleWithDrag'; | ||
|
||
interface Props { | ||
width?: number; | ||
maxHeight?: number; | ||
minHeight?: number; | ||
children: React.ReactElement; | ||
open: boolean; | ||
onClose: () => void | ||
title: string; | ||
icon?: string | IconDefinition; | ||
} | ||
|
||
export function DraggableModalWithTitle ({ children, icon, maxHeight = 740, minHeight = 615, onClose, open, title, width = 500 }: Props): React.ReactElement<Props> { | ||
const theme = useTheme(); | ||
|
||
const isDarkMode = useMemo(() => theme.palette.mode === 'dark', [theme.palette.mode]); | ||
|
||
const [isDragging, setIsDragging] = useState(false); | ||
const initialX = (window.innerWidth - width) / 2; | ||
const initialY = (window.innerHeight - maxHeight) / 2; | ||
|
||
const [modalPosition, setModalPosition] = useState({ x: initialX, y: initialY }); | ||
const [dragStartPosition, setDragStartPosition] = useState({ x: 0, y: 0 }); | ||
|
||
const handleMouseDown = useCallback((e: { clientX: number; clientY: number; }) => { | ||
setIsDragging(true); | ||
setDragStartPosition({ x: e.clientX, y: e.clientY }); | ||
}, []); | ||
|
||
const _onClose = useCallback((_event: unknown, reason: string) => { | ||
if (reason && reason === 'backdropClick') { | ||
return; | ||
} | ||
|
||
onClose(); | ||
}, [onClose]); | ||
|
||
const handleMouseMove = useCallback((e: { clientX: number; clientY: number; }) => { | ||
if (isDragging) { | ||
const dx = e.clientX - dragStartPosition.x; | ||
const dy = e.clientY - dragStartPosition.y; | ||
|
||
setModalPosition((prevPosition) => ({ | ||
x: prevPosition.x + dx, | ||
y: prevPosition.y + dy | ||
})); | ||
setDragStartPosition({ x: e.clientX, y: e.clientY }); | ||
} | ||
}, [dragStartPosition, isDragging]); | ||
|
||
const handleMouseUp = useCallback(() => { | ||
setIsDragging(false); | ||
}, []); | ||
|
||
const style = { | ||
'&:focus': { | ||
outline: 'none' // Remove outline when Box is focused | ||
}, | ||
bgcolor: 'background.default', | ||
border: isDarkMode ? '0.5px solid' : 'none', | ||
borderColor: 'secondary.light', | ||
borderRadius: '10px', | ||
boxShadow: 24, | ||
cursor: isDragging ? 'grabbing' : 'grab', | ||
left: modalPosition.x, | ||
maxHeight: `${maxHeight}px`, | ||
minHeight: `${minHeight}px`, | ||
pb: 3, | ||
position: 'absolute', | ||
pt: 2, | ||
px: 4, | ||
top: modalPosition.y, | ||
width: `${width}px` | ||
}; | ||
|
||
return ( | ||
<Modal onClose={_onClose} open={open}> | ||
<Box | ||
onMouseLeave={handleMouseUp} | ||
onMouseMove={handleMouseMove} | ||
onMouseUp={handleMouseUp} | ||
sx={{ ...style }} | ||
|
||
> | ||
<ModalTitleWithDrag | ||
icon={icon} | ||
onClose={onClose} | ||
onMouseDown={handleMouseDown} | ||
title= {title} | ||
/> | ||
{children} | ||
</Box> | ||
</Modal> | ||
); | ||
} |
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
70 changes: 70 additions & 0 deletions
70
packages/extension-polkagate/src/fullscreen/partials/ModalTitleWithDrag.tsx
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,70 @@ | ||
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/* eslint-disable react/jsx-max-props-per-line */ | ||
|
||
import type { IconDefinition, IconProp } from '@fortawesome/fontawesome-svg-core'; | ||
|
||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { Close as CloseIcon, DragIndicator as DragIndicatorIcon } from '@mui/icons-material'; | ||
import { Divider, Grid, IconButton, Typography, useTheme } from '@mui/material'; | ||
import React from 'react'; | ||
|
||
import { Infotip2, VaadinIcon } from '../../components'; | ||
import { useTranslation } from '../../hooks'; | ||
|
||
interface Props { | ||
onClose: () => void; | ||
title: string; | ||
icon?: string | IconDefinition; | ||
onMouseDown: (e: { clientX: number; clientY: number; }) => void; | ||
onMouseMove?: (e: { clientX: number; clientY: number; }) => void; | ||
onMouseUp?: () => void | ||
} | ||
|
||
export default function ModalTitleWithDrag ({ icon, onClose, onMouseDown, onMouseMove, onMouseUp, title }: Props): React.ReactElement { | ||
const theme = useTheme(); | ||
const { t } = useTranslation(); | ||
|
||
const isIconVaadin = typeof icon === 'string' && icon?.startsWith('vaadin'); | ||
const isIconFontAwesome = !!icon; | ||
|
||
return ( | ||
<Grid alignItems='center' container justifyContent='space-between' pt='5px'> | ||
<Grid alignItems='center' container item justifyContent='flex-start' width='fit-content'> | ||
{icon && | ||
<> | ||
{isIconVaadin | ||
? <VaadinIcon icon={icon} style={{ color: `${theme.palette.text.primary}`, height: '22px', marginRight: '10px' }} /> | ||
: isIconFontAwesome | ||
? <FontAwesomeIcon | ||
color={`${theme.palette.text.primary}`} | ||
fontSize='22px' | ||
icon={icon as IconProp} | ||
style={{ marginRight: '10px' }} | ||
/> | ||
: <></> | ||
} | ||
</>} | ||
<Typography display='contents' fontSize='22px' fontWeight={700} pl='10px'> | ||
{title} | ||
</Typography> | ||
</Grid> | ||
<Grid item> | ||
<IconButton | ||
onMouseDown={onMouseDown} | ||
onMouseMove={onMouseMove} | ||
onMouseUp={onMouseUp} | ||
> | ||
<Infotip2 text={t('Drag to move the window.')}> | ||
<DragIndicatorIcon sx={{ color: 'primary.light', cursor: 'pointer', stroke: theme.palette.primary.light, strokeWidth: 1.5 }} /> | ||
</Infotip2> | ||
</IconButton> | ||
<IconButton onClick={onClose}> | ||
<CloseIcon sx={{ color: 'primary.main', cursor: 'pointer', stroke: theme.palette.primary.main, strokeWidth: 1.5 }} /> | ||
</IconButton> | ||
</Grid> | ||
<Divider sx={{ mt: '5px', width: '100%' }} /> | ||
</Grid> | ||
); | ||
} |