Skip to content

🎥 Make videos programmatically with React

Keep sidebar resizing active through collapse

Jonny Burgerbf3b07f92843
diff --git a/packages/example/e2e/sidebar-resize.test.mts b/packages/example/e2e/sidebar-resize.test.mts
new file mode 100644
index 0000000000..81eb9cf5cb
--- /dev/null
+++ b/packages/example/e2e/sidebar-resize.test.mts
@@ -0,0 +1,137 @@
+import {expect, test} from '@playwright/test';
+import {EXPANDED_SIDEBAR_STATE, STUDIO_URL} from './constants.mts';
+import {startStudio, stopStudio} from './studio-server.mts';
+
+test.use({
+ storageState: EXPANDED_SIDEBAR_STATE,
+ viewport: {width: 1440, height: 900},
+});
+
+test.beforeEach(async () => {
+ await startStudio();
+});
+
+test.afterEach(async () => {
+ await stopStudio();
+});
+
+test('sidebars can reopen during a resize and keep their last state when dragging ends', async ({
+ page,
+}) => {
+ await page.goto(`${STUDIO_URL}/AnimatedBarChart`);
+
+ for (const side of ['left', 'right'] as const) {
+ const content =
+ side === 'left'
+ ? page.getByText('Compositions', {exact: true})
+ : page.getByRole('button', {name: 'Width', exact: true});
+ await expect(content).toBeVisible({timeout: 15_000});
+ const icon = page.locator(`[data-sidebar-toggle="${side}"] > div`);
+ await expect(icon).not.toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
+ const handles = page.locator('.remotion-splitter-vertical');
+ const handle = side === 'left' ? handles.first() : handles.nth(1);
+ const box = (await handle.boundingBox())!;
+ const x = box.x + box.width / 2;
+ const y = box.y + box.height / 2;
+ const edge = side === 'left' ? 0 : 1439;
+
+ await page.mouse.move(x, y);
+ await page.mouse.down();
+ for (let attempt = 0; attempt < 2; attempt++) {
+ const initialFrame = await page
+ .getByText('Regional growth', {exact: true})
+ .evaluate((element) => {
+ const {x: frameX, width} = element.getBoundingClientRect();
+ return {x: frameX, width};
+ });
+ const [frames] = await Promise.all([
+ page.getByText('Regional growth', {exact: true}).evaluate(
+ (element) =>
+ new Promise<{x: number; width: number}[]>((resolve) => {
+ const samples: {x: number; width: number}[] = [];
+ const sample = () => {
+ const {x: frameX, width} = element.getBoundingClientRect();
+ samples.push({x: frameX, width});
+ if (samples.length === 12) {
+ resolve(samples);
+ } else {
+ requestAnimationFrame(sample);
+ }
+ };
+ requestAnimationFrame(sample);
+ }),
+ ),
+ page.mouse.move(edge, y),
+ ]);
+ await expect(content).toBeHidden();
+ await expect(icon).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
+ // A collapse may paint the old or new geometry, but no intermediate stage.
+ const finalFrame = frames[frames.length - 1];
+ for (const frame of frames) {
+ expect([initialFrame, finalFrame]).toContainEqual(frame);
+ }
+ await page.mouse.move(x, y);
+ await expect(content).toBeVisible();
+ await expect(icon).not.toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
+ }
+
+ await page.mouse.up();
+ await page.reload();
+ await expect(content).toBeVisible();
+
+ for (const interruption of ['blur', 'lostpointercapture'] as const) {
+ const restoredBox = (await handle.boundingBox())!;
+ await page.mouse.move(
+ restoredBox.x + restoredBox.width / 2,
+ restoredBox.y + restoredBox.height / 2,
+ );
+ await page.mouse.down();
+ await page.mouse.move(edge, y);
+ await expect(content).toBeHidden();
+ await expect(icon).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
+ if (interruption === 'blur') {
+ await page.evaluate(() => window.dispatchEvent(new Event('blur')));
+ } else {
+ await handle.evaluate((element) => element.releasePointerCapture(1));
+ }
+
+ await page.mouse.move(x, y);
+ await page.mouse.up();
+ await expect(content).toBeHidden();
+ await expect(icon).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
+ await page.reload();
+ await expect(content).toBeHidden();
+ await expect(icon).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
+ await page.locator(`[data-sidebar-toggle="${side}"]`).click();
+ await expect(content).toBeVisible();
+ await expect(icon).not.toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
+ }
+
+ const finalBox = (await handle.boundingBox())!;
+ await page.mouse.move(
+ finalBox.x + finalBox.width / 2,
+ finalBox.y + finalBox.height / 2,
+ );
+ await page.mouse.down();
+ await page.mouse.move(edge, y);
+ await expect(content).toBeHidden();
+ // Let the canvas finish reacting to the temporary collapse.
+ await page.evaluate(
+ () =>
+ new Promise<void>((resolve) =>
+ requestAnimationFrame(() => requestAnimationFrame(() => resolve())),
+ ),
+ );
+ const chartTitle = page.getByText('Regional growth', {exact: true});
+ const collapsedTitleBox = await chartTitle.boundingBox();
+ expect(collapsedTitleBox).not.toBeNull();
+ await page.mouse.up();
+ await expect
+ .poll(() => chartTitle.boundingBox())
+ .toEqual(collapsedTitleBox);
+ await page.reload();
+ await expect(content).toBeHidden();
+ await page.locator(`[data-sidebar-toggle="${side}"]`).click();
+ await expect(content).toBeVisible();
+ }
+});
diff --git a/packages/studio/src/components/SidebarCollapserControls.tsx b/packages/studio/src/components/SidebarCollapserControls.tsx
index aa01627760..d12283dfba 100644
--- a/packages/studio/src/components/SidebarCollapserControls.tsx
+++ b/packages/studio/src/components/SidebarCollapserControls.tsx
@@ -27,8 +27,11 @@ const style: React.CSSProperties = {
export const SidebarCollapserControl: React.FC<{
readonly side: 'left' | 'right';
}> = ({side}) => {
- const {setSidebarCollapsedState, sidebarCollapsedStateRight} =
- useContext(SidebarContext);
+ const {
+ setSidebarCollapsedState,
+ sidebarCollapsedStateRight,
+ sidebarCollapsedDuringDrag,
+ } = useContext(SidebarContext);
const keybindings = useKeybinding();
const leftSidebarStatus = useResponsiveSidebarStatus();
@@ -38,10 +41,14 @@ export const SidebarCollapserControl: React.FC<{
width: '35%',
height: '100%',
borderRight: '1px solid ' + color,
- background: leftSidebarStatus === 'expanded' ? color : TRANSPARENT,
+ background:
+ leftSidebarStatus === 'expanded' &&
+ sidebarCollapsedDuringDrag !== 'left'
+ ? color
+ : TRANSPARENT,
};
},
- [leftSidebarStatus],
+ [leftSidebarStatus, sidebarCollapsedDuringDrag],
);
const rightIcon = useCallback(
@@ -53,10 +60,13 @@ export const SidebarCollapserControl: React.FC<{
position: 'absolute',
borderLeft: '1px solid ' + color,
background:
- sidebarCollapsedStateRight === 'expanded' ? color : TRANSPARENT,
+ sidebarCollapsedStateRight === 'expanded' &&
+ sidebarCollapsedDuringDrag !== 'right'
+ ? color
+ : TRANSPARENT,
};
},
- [sidebarCollapsedStateRight],
+ [sidebarCollapsedStateRight, sidebarCollapsedDuringDrag],
);
const toggleLeft = useCallback(() => {
diff --git a/packages/studio/src/components/Splitter/SplitterContainer.tsx b/packages/studio/src/components/Splitter/SplitterContainer.tsx
index 16a9d97edd..7f51f878ab 100644
--- a/packages/studio/src/components/Splitter/SplitterContainer.tsx
+++ b/packages/studio/src/components/Splitter/SplitterContainer.tsx
@@ -1,5 +1,5 @@
import {PlayerInternals} from '@remotion/player';
-import React, {useEffect, useMemo, useRef, useState} from 'react';
+import React, {useLayoutEffect, useMemo, useRef, useState} from 'react';
import {useTimelineFlex} from '../../state/timeline';
import type {
SplitterDragState,
@@ -52,6 +52,10 @@ export const SplitterContainer: React.FC<{
initialTimelineFlex ?? defaultFlex,
);
+ const [collapsedDuringDrag, setCollapsedDuringDrag] = useState<
+ 'left' | 'right' | null
+ >(null);
+
const ref = useRef<HTMLDivElement>(null);
const isDragging = useRef<SplitterDragState>(false);
const size = PlayerInternals.useElementSize(ref, {
@@ -76,6 +80,8 @@ export const SplitterContainer: React.FC<{
const value: TSplitterContext = useMemo(() => {
return {
flexValue: effectiveFlexValue,
+ collapsedDuringDrag,
+ setCollapsedDuringDrag,
ref,
setFlexValue,
isDragging,
@@ -92,6 +98,7 @@ export const SplitterContainer: React.FC<{
};
}, [
defaultFlex,
+ collapsedDuringDrag,
effectiveFlexValue,
id,
maxFlex,
@@ -105,14 +112,9 @@ export const SplitterContainer: React.FC<{
ref,
]);
- useEffect(() => {
- const frame = requestAnimationFrame(() => {
- PlayerInternals.updateAllElementsSizes();
- });
-
- return () => {
- cancelAnimationFrame(frame);
- };
+ useLayoutEffect(() => {
+ // Update the canvas and nested splitters before the resized layout paints.
+ PlayerInternals.updateAllElementsSizes();
});
return (
diff --git a/packages/studio/src/components/Splitter/SplitterContext.tsx b/packages/studio/src/components/Splitter/SplitterContext.tsx
index a44556f49c..81a2754dfc 100644
--- a/packages/studio/src/components/Splitter/SplitterContext.tsx
+++ b/packages/studio/src/components/Splitter/SplitterContext.tsx
@@ -6,6 +6,8 @@ export type SplitterOrientation = 'horizontal' | 'vertical';
export type TSplitterContext = {
flexValue: number;
+ collapsedDuringDrag: 'left' | 'right' | null;
+ setCollapsedDuringDrag: (side: 'left' | 'right' | null) => void;
setFlexValue: React.Dispatch<React.SetStateAction<number>>;
orientation: SplitterOrientation;
ref: React.RefObject<HTMLDivElement | null>;
@@ -82,6 +84,8 @@ export const getClampedSplitterFlex = ({
export const SplitterContext = React.createContext<TSplitterContext>({
flexValue: 1,
+ collapsedDuringDrag: null,
+ setCollapsedDuringDrag: () => undefined,
ref: {current: null},
setFlexValue: () => undefined,
isDragging: {current: false},
diff --git a/packages/studio/src/components/Splitter/SplitterElement.tsx b/packages/studio/src/components/Splitter/SplitterElement.tsx
index 2c16014981..65836effd6 100644
--- a/packages/studio/src/components/Splitter/SplitterElement.tsx
+++ b/packages/studio/src/components/Splitter/SplitterElement.tsx
@@ -19,7 +19,10 @@ export const SplitterElement: React.FC<{
flex:
// Multiply by 1000 because if flex values don't add up to at least 1, they will not fill up the screen
(type === 'flexer' ? context.flexValue : 1 - context.flexValue) * 1000,
- display: 'flex',
+ display:
+ context.collapsedDuringDrag === (type === 'flexer' ? 'left' : 'right')
+ ? 'none'
+ : 'flex',
position: 'relative',
overflow: 'hidden',
flexDirection: 'column',
@@ -36,7 +39,14 @@ export const SplitterElement: React.FC<{
? (minSize ?? undefined)
: undefined,
};
- }, [context.flexValue, context.orientation, maxSize, minSize, type]);
+ }, [
+ context.collapsedDuringDrag,
+ context.flexValue,
+ context.orientation,
+ maxSize,
+ minSize,
+ type,
+ ]);
const stickStyle: React.CSSProperties = useMemo(() => {
return {
diff --git a/packages/studio/src/components/Splitter/SplitterHandle.tsx b/packages/studio/src/components/Splitter/SplitterHandle.tsx
index a58438f30a..df2249d678 100644
--- a/packages/studio/src/components/Splitter/SplitterHandle.tsx
+++ b/packages/studio/src/components/Splitter/SplitterHandle.tsx
@@ -1,6 +1,7 @@
import {PlayerInternals} from '@remotion/player';
import React, {useContext, useEffect, useRef} from 'react';
import {startCapturedPointerSession} from '../../helpers/pointer-session';
+import {SidebarContext} from '../../state/sidebar';
import {
forceSpecificCursor,
stopForcingSpecificCursor,
@@ -13,6 +14,11 @@ const containerRow: React.CSSProperties = {
height: SPLITTER_HANDLE_SIZE,
};
+const collapsedContainer: React.CSSProperties = {
+ width: 0,
+ height: 0,
+};
+
const containerColumn: React.CSSProperties = {
width: SPLITTER_HANDLE_SIZE,
};
@@ -21,6 +27,7 @@ export const SplitterHandle: React.FC<{
readonly allowToCollapse: 'right' | 'left' | 'none';
readonly onCollapse: () => void;
}> = ({allowToCollapse, onCollapse}) => {
+ const {setSidebarCollapsedDuringDrag} = useContext(SidebarContext);
const context = useContext(SplitterContext);
if (!context) {
throw new Error('Cannot find splitter context');
@@ -100,27 +107,41 @@ export const SplitterHandle: React.FC<{
return newFlex;
};
+ const getCollapsedSide = (ev: PointerEvent) => {
+ const collapse = latest.current.allowToCollapse;
+ const unclamped = getNewValue(ev, false);
+ if (collapse === 'left' && unclamped < dragContext.minFlex / 2) {
+ return 'left';
+ }
+
+ if (
+ collapse === 'right' &&
+ 1 - unclamped < (1 - dragContext.maxFlex) / 2
+ ) {
+ return 'right';
+ }
+
+ return null;
+ };
+
+ let lastFlex = startFlex;
+ let lastCollapsedSide: 'left' | 'right' | null = null;
+
const onPointerMove = (ev: PointerEvent) => {
if (!dragContext.isDragging.current) {
return;
}
- dragContext.setFlexValue(getNewValue(ev, true));
-
- const collapse = latest.current.allowToCollapse;
- if (collapse === 'left') {
- const unclamped = getNewValue(ev, false);
- if (unclamped < dragContext.minFlex / 2) {
- endDrag?.();
- latest.current.onCollapse();
- }
- } else if (collapse === 'right') {
- const unclamped = 1 - getNewValue(ev, false);
- if (unclamped < (1 - dragContext.maxFlex) / 2) {
- endDrag?.();
- latest.current.onCollapse();
- }
+ // Keep the handle mounted and captured until release, even while
+ // the panel is hidden, so moving inward can restore it.
+ lastCollapsedSide = getCollapsedSide(ev);
+ lastFlex = getNewValue(ev, true);
+ if (latest.current.allowToCollapse !== 'none') {
+ setSidebarCollapsedDuringDrag(lastCollapsedSide);
}
+
+ dragContext.setCollapsedDuringDrag(lastCollapsedSide);
+ dragContext.setFlexValue(lastFlex);
};
endDrag = startCapturedPointerSession({
@@ -130,12 +151,27 @@ export const SplitterHandle: React.FC<{
onEnd: (reason, endEvent) => {
if (
(reason === 'pointerup' || reason === 'buttons-released') &&
- endEvent &&
- dragContext.isDragging.current
+ endEvent
) {
- dragContext.persistFlex(getNewValue(endEvent, true));
+ lastFlex = getNewValue(endEvent, true);
+ lastCollapsedSide = getCollapsedSide(endEvent);
+ }
+
+ // Capture loss and cancellation may have no usable coordinates.
+ // Commit the last displayed state instead of resetting the panel.
+ if (reason !== 'manual' && dragContext.isDragging.current) {
+ dragContext.setFlexValue(lastFlex);
+ dragContext.persistFlex(lastFlex);
+ if (lastCollapsedSide !== null) {
+ latest.current.onCollapse();
+ }
+ }
+
+ if (latest.current.allowToCollapse !== 'none') {
+ setSidebarCollapsedDuringDrag(null);
}
+ dragContext.setCollapsedDuringDrag(null);
dragContext.isDragging.current = false;
stopForcingSpecificCursor();
endDrag = null;
@@ -150,7 +186,7 @@ export const SplitterHandle: React.FC<{
current.removeEventListener('pointerdown', onPointerDown);
endDrag?.();
};
- }, []);
+ }, [setSidebarCollapsedDuringDrag]);
return (
<div
@@ -162,7 +198,11 @@ export const SplitterHandle: React.FC<{
: 'remotion-splitter-vertical',
].join(' ')}
style={
- context.orientation === 'horizontal' ? containerRow : containerColumn
+ context.collapsedDuringDrag !== null
+ ? collapsedContainer
+ : context.orientation === 'horizontal'
+ ? containerRow
+ : containerColumn
}
/>
);
diff --git a/packages/studio/src/state/sidebar.tsx b/packages/studio/src/state/sidebar.tsx
index a644877d48..ee40b001d0 100644
--- a/packages/studio/src/state/sidebar.tsx
+++ b/packages/studio/src/state/sidebar.tsx
@@ -5,6 +5,8 @@ export type SidebarCollapsedState = 'collapsed' | 'expanded' | 'responsive';
type RightSidebarCollapsedState = Exclude<SidebarCollapsedState, 'responsive'>;
type Context = {
+ sidebarCollapsedDuringDrag: Sidebars | null;
+ setSidebarCollapsedDuringDrag: (side: Sidebars | null) => void;
sidebarCollapsedStateLeft: SidebarCollapsedState;
setSidebarCollapsedState: (options: {
left: null | React.SetStateAction<SidebarCollapsedState>;
@@ -63,6 +65,8 @@ const saveCollapsedState = (type: SidebarCollapsedState, sidebar: Sidebars) => {
};
export const SidebarContext = createContext<Context>({
+ sidebarCollapsedDuringDrag: null,
+ setSidebarCollapsedDuringDrag: () => undefined,
sidebarCollapsedStateLeft: 'collapsed',
setSidebarCollapsedState: () => {
throw new Error('sidebar collapsed state');
@@ -85,8 +89,13 @@ export const SidebarContextProvider: React.FC<{
right: getSavedCollapsedStateRight(isMobileLayout),
}));
+ const [sidebarCollapsedDuringDrag, setSidebarCollapsedDuringDrag] =
+ useState<Sidebars | null>(null);
+
const value: Context = useMemo(() => {
return {
+ sidebarCollapsedDuringDrag,
+ setSidebarCollapsedDuringDrag,
sidebarCollapsedStateLeft: sidebarCollapsedState.left,
sidebarCollapsedStateRight: sidebarCollapsedState.right,
setSidebarCollapsedState: (options: {
@@ -114,7 +123,7 @@ export const SidebarContextProvider: React.FC<{
});
},
};
- }, [sidebarCollapsedState]);
+ }, [sidebarCollapsedState, sidebarCollapsedDuringDrag]);
return (
<SidebarContext.Provider value={value}>{children}</SidebarContext.Provider>