-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(widget-builder): Make widget builder preview draggable #82038
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import {Fragment, useEffect} from 'react'; | ||
import {css} from '@emotion/react'; | ||
import {Fragment, useEffect, useState} from 'react'; | ||
import {DndContext, type Translate, useDraggable} from '@dnd-kit/core'; | ||
import {css, useTheme} from '@emotion/react'; | ||
import styled from '@emotion/styled'; | ||
import {AnimatePresence, motion} from 'framer-motion'; | ||
|
||
|
@@ -36,6 +37,8 @@ type WidgetBuilderV2Props = { | |
onSave: ({index, widget}: {index: number; widget: Widget}) => void; | ||
}; | ||
|
||
const WIDGET_PREVIEW_DRAG_ID = 'widget-preview-draggable'; | ||
|
||
function WidgetBuilderV2({ | ||
isOpen, | ||
onClose, | ||
|
@@ -47,6 +50,14 @@ function WidgetBuilderV2({ | |
const organization = useOrganization(); | ||
const {selection} = usePageFilters(); | ||
|
||
const [{translate}, setTranslate] = useState<{ | ||
initialTranslate: Translate; | ||
translate: Translate; | ||
}>({ | ||
initialTranslate: {x: 0, y: 0}, | ||
translate: {x: 0, y: 0}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (escapeKeyPressed) { | ||
if (isOpen) { | ||
|
@@ -55,6 +66,25 @@ function WidgetBuilderV2({ | |
} | ||
}, [escapeKeyPressed, isOpen, onClose]); | ||
|
||
const handleDragEnd = () => { | ||
setTranslate(({translate: newTranslate}) => { | ||
return { | ||
translate: newTranslate, | ||
initialTranslate: newTranslate, | ||
}; | ||
}); | ||
}; | ||
|
||
const handleDragMove = ({delta}) => { | ||
setTranslate(({initialTranslate}) => ({ | ||
initialTranslate, | ||
translate: { | ||
x: initialTranslate.x + delta.x, | ||
y: initialTranslate.y + delta.y, | ||
}, | ||
})); | ||
}; | ||
|
||
return ( | ||
<Fragment> | ||
{isOpen && <Backdrop style={{opacity: 0.5, pointerEvents: 'auto'}} />} | ||
|
@@ -70,13 +100,22 @@ function WidgetBuilderV2({ | |
<WidgetBuilderContainer> | ||
<WidgetBuilderSlideout | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
onClose={() => { | ||
onClose(); | ||
setTranslate({ | ||
initialTranslate: {x: 0, y: 0}, | ||
translate: {x: 0, y: 0}, | ||
}); | ||
}} | ||
onSave={onSave} | ||
/> | ||
<WidgetPreviewContainer | ||
dashboardFilters={dashboardFilters} | ||
dashboard={dashboard} | ||
/> | ||
<DndContext onDragEnd={handleDragEnd} onDragMove={handleDragMove}> | ||
<WidgetPreviewContainer | ||
dashboardFilters={dashboardFilters} | ||
dashboard={dashboard} | ||
translate={translate} | ||
/> | ||
</DndContext> | ||
</WidgetBuilderContainer> | ||
</ContainerWithoutSidebar> | ||
</SpanTagsProvider> | ||
|
@@ -93,13 +132,25 @@ export default WidgetBuilderV2; | |
function WidgetPreviewContainer({ | ||
dashboardFilters, | ||
dashboard, | ||
translate, | ||
}: { | ||
dashboard: DashboardDetails; | ||
dashboardFilters: DashboardFilters; | ||
translate: Translate; | ||
}) { | ||
const {state} = useWidgetBuilderContext(); | ||
const organization = useOrganization(); | ||
const location = useLocation(); | ||
const theme = useTheme(); | ||
|
||
const isDragEnabled = | ||
window.innerWidth < parseInt(theme.breakpoints.small.replace('px', ''), 10); | ||
|
||
const {attributes, listeners, setNodeRef, isDragging} = useDraggable({ | ||
id: WIDGET_PREVIEW_DRAG_ID, | ||
disabled: !isDragEnabled, | ||
// May need to add 'handle' prop if we want to drag the preview by a specific area | ||
}); | ||
|
||
return ( | ||
<DashboardsMEPProvider> | ||
|
@@ -115,22 +166,35 @@ function WidgetPreviewContainer({ | |
location={location} | ||
forceTransactions={metricsDataSide.forceTransactionsOnly} | ||
> | ||
<SampleWidgetCard | ||
initial={{opacity: 0, x: '50%', y: 0}} | ||
animate={{opacity: 1, x: 0, y: 0}} | ||
exit={{opacity: 0, x: '50%', y: 0}} | ||
transition={{ | ||
type: 'spring', | ||
stiffness: 500, | ||
damping: 50, | ||
<DraggableWidgetContainer | ||
ref={setNodeRef} | ||
id={WIDGET_PREVIEW_DRAG_ID} | ||
style={{ | ||
transform: isDragEnabled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might be able to eek out some small perf gains by telling the browser these values will change. i.e. you can probably add |
||
? `translate3d(${translate?.x ?? 0}px, ${translate?.y ?? 0}px, 0)` | ||
: undefined, | ||
opacity: isDragging ? 0.5 : 1, | ||
}} | ||
isTable={state.displayType === DisplayType.TABLE} | ||
{...attributes} | ||
{...listeners} | ||
> | ||
<WidgetPreview | ||
dashboardFilters={dashboardFilters} | ||
dashboard={dashboard} | ||
/> | ||
</SampleWidgetCard> | ||
<SampleWidgetCard | ||
initial={{opacity: 0, x: '50%', y: 0}} | ||
animate={{opacity: 1, x: 0, y: 0}} | ||
exit={{opacity: 0, x: '50%', y: 0}} | ||
transition={{ | ||
type: 'spring', | ||
stiffness: 500, | ||
damping: 50, | ||
}} | ||
isTable={state.displayType === DisplayType.TABLE} | ||
> | ||
<WidgetPreview | ||
dashboardFilters={dashboardFilters} | ||
dashboard={dashboard} | ||
/> | ||
</SampleWidgetCard> | ||
</DraggableWidgetContainer> | ||
</MEPSettingProvider> | ||
)} | ||
</MetricsDataSwitcher> | ||
|
@@ -164,10 +228,31 @@ const SampleWidgetCard = styled(motion.div)<{isTable: boolean}>` | |
border: 2px dashed ${p => p.theme.border}; | ||
border-radius: ${p => p.theme.borderRadius}; | ||
background-color: ${p => p.theme.background}; | ||
z-index: ${p => p.theme.zIndex.modal}; | ||
position: relative; | ||
`; | ||
|
||
const DraggableWidgetContainer = styled(`div`)` | ||
align-content: center; | ||
z-index: ${p => p.theme.zIndex.modal}; | ||
position: relative; | ||
margin: auto; | ||
|
||
touch-action: none; /* Prevents touch scrolling while dragging */ | ||
cursor: grab; | ||
|
||
&:active { | ||
cursor: grabbing; | ||
} | ||
|
||
@media (min-width: ${p => p.theme.breakpoints.small}) { | ||
transform: none; | ||
cursor: auto; | ||
|
||
&:active { | ||
cursor: auto; | ||
} | ||
} | ||
`; | ||
|
||
const ContainerWithoutSidebar = styled('div')` | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some kind of constant we could use in place of the
54px
? Just trying to see if there's something a bit more semantic