-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add resizable inline right panels #2512
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
Open
shivamhwp
wants to merge
10
commits into
pingdotgg:main
Choose a base branch
from
shivamhwp:right-sidebar-resize-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3e32677
Add resizable inline right panels
shivamhwp a7d7c20
Fix right panel resize state updates
shivamhwp fdb9ff5
Merge remote-tracking branch 'upstream/main' into right-sidebar-resiz…
shivamhwp f77acbc
fixing the cursor bugbot issues
shivamhwp 385b32c
remove dead conditional
shivamhwp 8a193e5
Merge branch 'main' into right-sidebar-resize-fix
shivamhwp e03ecba
Merge branch 'main' into right-sidebar-resize-fix
shivamhwp 01315d1
Merge branch 'main' into right-sidebar-resize-fix
shivamhwp 9d0cf9d
Merge branch 'main' into right-sidebar-resize-fix
shivamhwp 52721c9
Fix right sidebar resize state handling
shivamhwp 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 hidden or 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 hidden or 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 hidden or 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,174 @@ | ||
| import * as Schema from "effect/Schema"; | ||
| import { | ||
| type PointerEvent as ReactPointerEvent, | ||
| type ReactNode, | ||
| useCallback, | ||
| useEffect, | ||
| useRef, | ||
| useState, | ||
| } from "react"; | ||
|
|
||
| import { getLocalStorageItem, setLocalStorageItem } from "~/hooks/useLocalStorage"; | ||
| import { cn } from "~/lib/utils"; | ||
|
|
||
| const DEFAULT_RATIO = 0.4; | ||
| const MIN_RATIO = 0.3; | ||
| const MAX_RATIO = 0.8; | ||
| let bodyResizeStyleOwner: symbol | null = null; | ||
|
|
||
| const clampRatio = (ratio: number) => Math.max(MIN_RATIO, Math.min(ratio, MAX_RATIO)); | ||
|
|
||
| function readStoredRatio(storageKey: string | undefined) { | ||
| if (!storageKey) return DEFAULT_RATIO; | ||
| try { | ||
| const storedRatio = getLocalStorageItem(storageKey, Schema.Finite); | ||
| return storedRatio === null ? DEFAULT_RATIO : clampRatio(storedRatio); | ||
| } catch (error) { | ||
| console.error("[LOCALSTORAGE] Error:", error); | ||
| return DEFAULT_RATIO; | ||
| } | ||
| } | ||
|
|
||
| const applyBodyResizeStyles = (owner: symbol) => { | ||
| bodyResizeStyleOwner = owner; | ||
| document.body.style.cursor = "col-resize"; | ||
| document.body.style.userSelect = "none"; | ||
| }; | ||
|
|
||
| const clearBodyResizeStyles = (owner: symbol) => { | ||
| if (bodyResizeStyleOwner !== owner) return; | ||
| document.body.style.removeProperty("cursor"); | ||
| document.body.style.removeProperty("user-select"); | ||
| bodyResizeStyleOwner = null; | ||
| }; | ||
|
|
||
| export function ResizableRightPanel({ | ||
| children, | ||
| className, | ||
| storageKey, | ||
| }: { | ||
| children: ReactNode; | ||
| className?: string; | ||
| storageKey?: string; | ||
| }) { | ||
| const [widthRatio, setWidthRatio] = useState(() => readStoredRatio(storageKey)); | ||
| const resizeOwnerRef = useRef(Symbol("ResizableRightPanel")); | ||
| const panelRef = useRef<HTMLDivElement | null>(null); | ||
| const widthRatioRef = useRef(widthRatio); | ||
| const resizeStateRef = useRef<{ | ||
| frameId: number | null; | ||
| handle: HTMLDivElement; | ||
| panel: HTMLDivElement; | ||
| pointerId: number; | ||
| startWidth: number; | ||
| startX: number; | ||
| } | null>(null); | ||
|
|
||
| const commitWidthRatio = useCallback((ratio: number) => { | ||
| widthRatioRef.current = ratio; | ||
| setWidthRatio(ratio); | ||
| }, []); | ||
|
|
||
| const stopResize = useCallback( | ||
| (pointerId: number) => { | ||
| const resizeState = resizeStateRef.current; | ||
| if (!resizeState) return; | ||
| if (resizeState.frameId !== null) { | ||
| window.cancelAnimationFrame(resizeState.frameId); | ||
| } | ||
| if (resizeState.handle.hasPointerCapture(pointerId)) { | ||
| resizeState.handle.releasePointerCapture(pointerId); | ||
| } | ||
| clearBodyResizeStyles(resizeOwnerRef.current); | ||
| resizeStateRef.current = null; | ||
| if (storageKey) { | ||
| setLocalStorageItem(storageKey, widthRatioRef.current, Schema.Finite); | ||
| } | ||
| }, | ||
| [storageKey], | ||
| ); | ||
|
|
||
| const handlePointerDown = useCallback((event: ReactPointerEvent<HTMLDivElement>) => { | ||
| if (event.button !== 0) return; | ||
| const panel = panelRef.current; | ||
| if (!panel) return; | ||
|
|
||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| resizeStateRef.current = { | ||
| frameId: null, | ||
| handle: event.currentTarget, | ||
| panel, | ||
| pointerId: event.pointerId, | ||
| startWidth: panel.getBoundingClientRect().width, | ||
| startX: event.clientX, | ||
| }; | ||
| event.currentTarget.setPointerCapture(event.pointerId); | ||
| applyBodyResizeStyles(resizeOwnerRef.current); | ||
| }, []); | ||
|
|
||
| const handlePointerMove = useCallback( | ||
| (event: ReactPointerEvent<HTMLDivElement>) => { | ||
| const resizeState = resizeStateRef.current; | ||
| if (!resizeState || resizeState.pointerId !== event.pointerId) return; | ||
|
|
||
| event.preventDefault(); | ||
| if (resizeState.frameId !== null) return; | ||
|
|
||
| const clientX = event.clientX; | ||
| resizeState.frameId = window.requestAnimationFrame(() => { | ||
| const activeResizeState = resizeStateRef.current; | ||
| if (!activeResizeState) return; | ||
|
|
||
| activeResizeState.frameId = null; | ||
| const containerWidth = activeResizeState.panel.parentElement?.clientWidth ?? 0; | ||
| if (containerWidth <= 0) return; | ||
|
|
||
| const nextWidth = activeResizeState.startWidth + activeResizeState.startX - clientX; | ||
| commitWidthRatio(clampRatio(nextWidth / containerWidth)); | ||
| }); | ||
| }, | ||
| [commitWidthRatio], | ||
| ); | ||
|
|
||
| const handlePointerUp = useCallback( | ||
| (event: ReactPointerEvent<HTMLDivElement>) => { | ||
| const resizeState = resizeStateRef.current; | ||
| if (!resizeState || resizeState.pointerId !== event.pointerId) return; | ||
| stopResize(event.pointerId); | ||
| }, | ||
| [stopResize], | ||
| ); | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
|
|
||
| useEffect(() => { | ||
| const resizeOwner = resizeOwnerRef.current; | ||
| return () => { | ||
| const resizeState = resizeStateRef.current; | ||
| if (resizeState?.frameId !== null && resizeState?.frameId !== undefined) { | ||
| window.cancelAnimationFrame(resizeState.frameId); | ||
| } | ||
| clearBodyResizeStyles(resizeOwner); | ||
| }; | ||
| }, []); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <div | ||
| className={cn("relative min-h-0 shrink-0", className)} | ||
| ref={panelRef} | ||
| style={{ width: `${widthRatio * 100}%` }} | ||
| > | ||
| <div | ||
| aria-label="Resize right panel" | ||
| className="absolute inset-y-0 left-0 z-20 w-4 -translate-x-1/2 cursor-col-resize touch-none after:absolute after:inset-y-0 after:left-1/2 after:w-px hover:after:bg-border" | ||
| onPointerCancel={handlePointerUp} | ||
| onPointerDown={handlePointerDown} | ||
| onPointerMove={handlePointerMove} | ||
| onPointerUp={handlePointerUp} | ||
| role="separator" | ||
| tabIndex={-1} | ||
| title="Drag to resize right panel" | ||
| /> | ||
| {children} | ||
| </div> | ||
| ); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.