-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use react-resizable-panel on RequestorPage And also: * tweak the mobile layout * make the AiTestGenerationPanel resizable * Set autoSaveId so the panel layout is saved in localstorage * Fix build * Cleanup * Also migrate routes panel to react-resizable-panel Please enter the commit message for your changes. Lines starting * Cleanup * Remove react-resizable * Remove commented out code * Fix build * Remove import of react-resizable css * Increase minimum size for RequestPanel * Feedback on PR * Remove line * Add missing dependency for effect
- Loading branch information
Showing
15 changed files
with
382 additions
and
338 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
File renamed without changes.
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,6 @@ | ||
export { | ||
ResizableHandle, | ||
ResizablePanel, | ||
ResizablePanelGroup, | ||
} from "./components"; | ||
export { usePanelConstraints } from "./usePanelConstraints"; |
94 changes: 94 additions & 0 deletions
94
frontend/src/components/ui/resizable/usePanelConstraints.ts
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,94 @@ | ||
import { useHandler } from "@fiberplane/hooks"; | ||
import { useLayoutEffect, useState } from "react"; | ||
import { getPanelGroupElement } from "react-resizable-panels"; | ||
|
||
type PanelConstraintOptions = { | ||
groupId: string; | ||
minPixelSize?: number; | ||
maxPixelSize?: number; | ||
/** | ||
* The initial estimated size of the panel in pixels. | ||
*/ | ||
initialGroupSize: number; | ||
/** | ||
* The minimum size of the panel in pixels. If the panel is | ||
* not big enough, there won't be any min/max value applied. | ||
*/ | ||
minimalGroupSize?: number; | ||
}; | ||
|
||
type SizeConstraint = { | ||
/* Size in percentages. If set to undefined, the panel group wasn't big engouh */ | ||
minSize?: number; | ||
maxSize?: number; | ||
}; | ||
|
||
/** | ||
* This function determines the min/max size of a panel group based on the | ||
* initial size of the panel group and the min/max pixel size for a panel | ||
* | ||
* Note: it is assumed the panel is a horizontal panel group | ||
*/ | ||
export function usePanelConstraints( | ||
options: PanelConstraintOptions, | ||
): SizeConstraint { | ||
const { | ||
groupId, | ||
initialGroupSize, | ||
maxPixelSize, | ||
minPixelSize, | ||
minimalGroupSize, | ||
} = options; | ||
|
||
const getConstraint = useHandler((size: number) => { | ||
if (minimalGroupSize && size < minimalGroupSize) { | ||
return {}; | ||
} | ||
const minSize = minPixelSize ? (minPixelSize / size) * 100 : undefined; | ||
const maxSize = maxPixelSize ? (maxPixelSize / size) * 100 : maxPixelSize; | ||
|
||
return { | ||
minSize, | ||
maxSize, | ||
}; | ||
}); | ||
|
||
const [current, setCurrent] = useState(() => getConstraint(initialGroupSize)); | ||
|
||
const updateCurrent = useHandler((size: number) => { | ||
const result = getConstraint(size); | ||
if ( | ||
result.minSize !== current.minSize || | ||
result.maxSize !== current.maxSize | ||
) { | ||
setCurrent(result); | ||
} | ||
}); | ||
|
||
useLayoutEffect(() => { | ||
const group = getPanelGroupElement(groupId); | ||
if (!group) { | ||
return; | ||
} | ||
|
||
if (!group) { | ||
console.warn("Unable to find the group"); | ||
return; | ||
} | ||
|
||
const observer = new ResizeObserver((entries) => { | ||
const width = entries[0].contentRect.width; | ||
updateCurrent(width); | ||
}); | ||
observer.observe(group); | ||
|
||
// Trigger the initial update | ||
updateCurrent(group.offsetWidth); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [groupId, updateCurrent]); | ||
|
||
return current; | ||
} |
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
Oops, something went wrong.