-
Notifications
You must be signed in to change notification settings - Fork 0
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
plot resize with draggable panel #352
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { CSSProperties, ReactNode, useEffect, useState } from 'react'; | ||
import { CSSProperties, ReactNode, useEffect, useState, useMemo } from 'react'; | ||
import Draggable, { DraggableEvent, DraggableData } from 'react-draggable'; | ||
import { css } from '@emotion/react'; | ||
import useResizeObserver from 'use-resize-observer'; | ||
|
@@ -102,18 +102,31 @@ export default function DraggablePanel({ | |
} | ||
} | ||
|
||
// convert rem to px | ||
const dragHandleHeightValue = 2; | ||
const dragHandleHeight = useMemo(() => { | ||
return ( | ||
dragHandleHeightValue * | ||
parseFloat(getComputedStyle(document.documentElement).fontSize) | ||
); | ||
}, []); | ||
|
||
const { ref, height, width } = useResizeObserver(); | ||
|
||
useEffect( | ||
function invokeOnPanelResize() { | ||
if (!onPanelResize || !height || !width) return; | ||
|
||
onPanelResize({ | ||
height: height, | ||
// Note: since height from useResizeObserver includes dragHandle's height, 2rem, | ||
// dragHandle's height should be substracted from height to compute main panel size | ||
// without this, the use of useResizeObserver's height may lead to infinite loop | ||
// when changing plot size inside the pannel | ||
height: height - dragHandleHeight, | ||
width: width, | ||
}); | ||
}, | ||
[height, width, onPanelResize] | ||
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. I think this needs to be reinstated. 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. @bobular By having onPanelResize, it causes infinite loop, thus I removed it in the dependencies, only to use [height, width] dependencies in the next line: Line 117. |
||
[height, width] | ||
); | ||
|
||
const finalPosition = confineToParentContainer | ||
|
@@ -162,7 +175,9 @@ export default function DraggablePanel({ | |
background: ${theme?.palette?.primary?.hue[100] ?? gray[100]}; | ||
cursor: ${isDragging ? 'grabbing' : 'grab'}; | ||
display: flex; | ||
height: 2rem; | ||
height: ${dragHandleHeightValue != null | ||
? dragHandleHeightValue + 'rem' | ||
: '2rem'}; | ||
justify-content: center; | ||
// Because the panels are positioned absolutely and overflow auto, | ||
// the handle will get lost when the user scrolls down. We can pin the | ||
|
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.
I think we need to use
useCallback
on the function here and put the dependency back (see below)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.
Or even just
onPanelResize={setPanelDimension}
?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.
useState
setters are guaranteed to be stable