Skip to content

Visualize Your Ideas With Code

fix(ui): remember state of custom tabs (#900)

Jacobeac45b88ed09
diff --git a/packages/2d/src/editor/InspectorTabConfig.tsx b/packages/2d/src/editor/InspectorTabConfig.tsx
index 6a62e129..f4313497 100644
--- a/packages/2d/src/editor/InspectorTabConfig.tsx
+++ b/packages/2d/src/editor/InspectorTabConfig.tsx
@@ -9,19 +9,20 @@ import {
PluginTabConfig,
PluginTabProps,
Separator,
- SidebarPanel,
Tab,
emphasize,
findAndOpenFirstUserFile,
useApplication,
useCurrentFrame,
useCurrentScene,
+ usePanels,
useReducedMotion,
} from '@motion-canvas/ui';
import {useEffect, useMemo, useRef} from 'preact/hooks';
import {useInspection} from './Provider';
function TabComponent({tab}: PluginTabProps) {
+ const {sidebar} = usePanels();
const inspectorTab = useRef<HTMLButtonElement>(null);
const reducedMotion = useReducedMotion();
const {nodeKey} = useInspection();
@@ -31,7 +32,7 @@ function TabComponent({tab}: PluginTabProps) {
() =>
logger.onInspected.subscribe(key => {
nodeKey.value = key;
- SidebarPanel.value = tab;
+ sidebar.set(tab);
}),
[nodeKey, tab],
);
@@ -39,7 +40,7 @@ function TabComponent({tab}: PluginTabProps) {
useEffect(() => {
if (
nodeKey.value &&
- SidebarPanel.value !== tab &&
+ sidebar.current.value !== tab &&
!reducedMotion &&
inspectorTab.current &&
inspectorTab.current.getAnimations().length < 2
diff --git a/packages/ui/src/Editor.tsx b/packages/ui/src/Editor.tsx
index 6cbe1c1c..f3e8d991 100644
--- a/packages/ui/src/Editor.tsx
+++ b/packages/ui/src/Editor.tsx
@@ -7,28 +7,30 @@ import {PresentationMode} from './components/presentation';
import {Settings, Threads, VideoSettings} from './components/sidebar';
import {Timeline} from './components/timeline';
import {Viewport} from './components/viewport';
+import {usePanels} from './contexts';
import {usePresenterState} from './hooks';
-import {BottomPanel, EditorPanel, SidebarPanel} from './signals';
+import {EditorPanel} from './signals';
export function Editor() {
const state = usePresenterState();
+ const {sidebar, bottom} = usePanels();
return state === PresenterState.Initial ? (
<div className={styles.root}>
<Navigation />
<ResizeableLayout
id={`main-timeline`}
- hidden={BottomPanel.isHidden}
+ hidden={bottom.isHidden}
offset={-160}
vertical
>
<ResizeableLayout
id={`sidebar-viewport`}
- hidden={SidebarPanel.isHidden}
+ hidden={sidebar.isHidden}
offset={400}
>
<ElementSwitch
- value={SidebarPanel.value}
+ value={sidebar.current.value}
cases={{
[EditorPanel.VideoSettings]: VideoSettings,
[EditorPanel.Threads]: Threads,
@@ -39,7 +41,7 @@ export function Editor() {
<Viewport />
</ResizeableLayout>
<ElementSwitch
- value={BottomPanel.value}
+ value={bottom.current.value}
cases={{
[EditorPanel.Timeline]: Timeline,
}}
diff --git a/packages/ui/src/components/layout/ElementSwitch.tsx b/packages/ui/src/components/layout/ElementSwitch.tsx
index 525519df..b30e0ce4 100644
--- a/packages/ui/src/components/layout/ElementSwitch.tsx
+++ b/packages/ui/src/components/layout/ElementSwitch.tsx
@@ -1,6 +1,6 @@
import {Fragment} from 'preact';
import {useMemo} from 'preact/hooks';
-import {useApplication} from '../../contexts';
+import {usePanels} from '../../contexts';
import {PluginTabConfig} from '../../plugin';
export interface ElementSwitchProps<T extends string> {
@@ -12,24 +12,21 @@ export function ElementSwitch<T extends string>({
value,
cases,
}: ElementSwitchProps<T>) {
- const {plugins} = useApplication();
+ const {tabs} = usePanels();
const lookup = useMemo(() => {
const lookup = new Map<string, PluginTabConfig['paneComponent']>();
-
for (const [key, value] of Object.entries<PluginTabConfig['paneComponent']>(
cases,
)) {
lookup.set(key, value);
}
- for (const plugin of plugins) {
- for (const tab of plugin.tabs ?? []) {
- lookup.set(`${plugin.name}-${tab.name}`, tab.paneComponent);
- }
+ for (const tab of tabs) {
+ lookup.set(tab.name, tab.paneComponent);
}
return lookup;
- }, [plugins, cases]);
+ }, [tabs, cases]);
if (lookup.has(value)) {
const Element = lookup.get(value);
diff --git a/packages/ui/src/components/layout/Navigation.tsx b/packages/ui/src/components/layout/Navigation.tsx
index c6130b52..080dcbd5 100644
--- a/packages/ui/src/components/layout/Navigation.tsx
+++ b/packages/ui/src/components/layout/Navigation.tsx
@@ -1,9 +1,8 @@
import {LogLevel} from '@motion-canvas/core';
-import {useEffect, useMemo, useRef, useState} from 'preact/hooks';
-import {useApplication} from '../../contexts';
+import {useEffect, useRef, useState} from 'preact/hooks';
+import {useApplication, usePanels} from '../../contexts';
import {useReducedMotion} from '../../hooks';
-import {PluginTabConfig} from '../../plugin';
-import {BottomPanel, EditorPanel, SidebarPanel} from '../../signals';
+import {EditorPanel} from '../../signals';
import {shake} from '../animations';
import {
Bug,
@@ -19,22 +18,11 @@ import {Badge, Space, Tab, TabGroup, TabLink, Tabs} from '../tabs';
import styles from './Navigation.module.scss';
export function Navigation() {
- const {project, plugins, logger} = useApplication();
+ const {project, logger} = useApplication();
+ const {tabs, sidebar, bottom} = usePanels();
const reducedMotion = useReducedMotion();
const badge = useRef<HTMLDivElement>();
const [errorCount, setErrorCount] = useState(logger.onErrorLogged.current);
- const tabs = useMemo(() => {
- const tabs: [string, PluginTabConfig['tabComponent']][] = [];
-
- for (const plugin of plugins) {
- for (const tab of plugin.tabs ?? []) {
- const name = `${plugin.name}-${tab.name}`;
- tabs.push([name, tab.tabComponent]);
- }
- }
-
- return tabs;
- }, [plugins]);
useEffect(
() =>
@@ -58,12 +46,7 @@ export function Navigation() {
>
<MotionCanvas />
</TabLink>
- <TabGroup
- tab={SidebarPanel.value}
- setTab={tab => {
- SidebarPanel.value = tab;
- }}
- >
+ <TabGroup tab={sidebar.current.value} setTab={tab => sidebar.set(tab)}>
<Tab
title="Video Settings"
id="rendering-tab"
@@ -72,7 +55,7 @@ export function Navigation() {
<Videocam />
</Tab>
{/* eslint-disable-next-line @typescript-eslint/naming-convention */}
- {tabs.map(([name, Component]) => (
+ {tabs.map(({name, tabComponent: Component}) => (
<Component tab={name} />
))}
<Tab title="Thread Debugger" id="threads-tab" tab={EditorPanel.Threads}>
@@ -114,12 +97,7 @@ export function Navigation() {
>
<School />
</TabLink>
- <TabGroup
- tab={BottomPanel.value}
- setTab={tab => {
- BottomPanel.value = tab;
- }}
- >
+ <TabGroup tab={bottom.current.value} setTab={tab => bottom.set(tab)}>
<Tab title="Timeline" id="timeline-tab" tab={EditorPanel.Timeline}>
<Movie />
</Tab>
diff --git a/packages/ui/src/contexts/index.ts b/packages/ui/src/contexts/index.ts
index 8bb279e3..a5799c5a 100644
--- a/packages/ui/src/contexts/index.ts
+++ b/packages/ui/src/contexts/index.ts
@@ -1,3 +1,4 @@
export * from './application';
+export * from './panels';
export * from './timeline';
export * from './viewport';
diff --git a/packages/ui/src/contexts/panels.tsx b/packages/ui/src/contexts/panels.tsx
new file mode 100644
index 00000000..51e32daa
--- /dev/null
+++ b/packages/ui/src/contexts/panels.tsx
@@ -0,0 +1,82 @@
+import {computed, ReadonlySignal} from '@preact/signals';
+import {ComponentChildren, createContext} from 'preact';
+import {useContext, useMemo} from 'preact/hooks';
+import {PluginTabConfig} from '../plugin';
+import {EditorPanel, storedSignal} from '../signals';
+import {useApplication} from './application';
+
+interface Panel {
+ current: ReadonlySignal<string | null>;
+ isHidden: ReadonlySignal<boolean>;
+ set(value: string | null): void;
+}
+
+interface Panels {
+ sidebar: Panel;
+ bottom: Panel;
+ tabs: PluginTabConfig[];
+}
+
+const PanelsContext = createContext<Panels | null>(null);
+
+export function usePanels(): Panels {
+ return useContext(PanelsContext);
+}
+
+export function PanelsProvider({children}: {children: ComponentChildren}) {
+ const {plugins} = useApplication();
+ const tabs = useMemo(() => {
+ const tabs: PluginTabConfig[] = [];
+ for (const plugin of plugins) {
+ for (const tab of plugin.tabs ?? []) {
+ tabs.push({
+ ...tab,
+ name: `${plugin.name}-${tab.name}`,
+ });
+ }
+ }
+
+ return tabs;
+ }, [plugins]);
+
+ const options = useMemo(() => {
+ const options = tabs.map(tab => tab.name);
+ options.push(...Object.values(EditorPanel));
+ return options;
+ }, [tabs]);
+
+ const sidebar = panelSignal(EditorPanel.VideoSettings, 'sidebar', options);
+ const bottom = panelSignal(EditorPanel.Timeline, 'bottom-panel', options);
+
+ return (
+ <PanelsContext.Provider
+ value={{
+ sidebar,
+ bottom,
+ tabs,
+ }}
+ >
+ {children}
+ </PanelsContext.Provider>
+ );
+}
+
+function panelSignal(initial: string, id: string, options: string[]): Panel {
+ const stored = storedSignal<string>(initial, id, value =>
+ options.includes(value) ? value : initial,
+ );
+ const isHidden = storedSignal(stored.value === null, `${id}-hidden`);
+ const current = computed(() => (isHidden.value ? null : stored.value));
+ return {
+ current,
+ isHidden,
+ set(value) {
+ if (value === null) {
+ isHidden.value = true;
+ } else {
+ stored.value = value;
+ isHidden.value = false;
+ }
+ },
+ };
+}
diff --git a/packages/ui/src/main.tsx b/packages/ui/src/main.tsx
index 66f13c82..567baa03 100644
--- a/packages/ui/src/main.tsx
+++ b/packages/ui/src/main.tsx
@@ -10,7 +10,7 @@ import {
import {ComponentChild, render} from 'preact';
import {Editor} from './Editor';
import {ProjectData, ProjectSelection} from './ProjectSelection';
-import {ApplicationProvider} from './contexts';
+import {ApplicationProvider, PanelsProvider} from './contexts';
import {ShortcutsProvider} from './contexts/shortcuts';
import GridPlugin from './plugin/GridPlugin';
import {projectNameSignal} from './signals';
@@ -134,9 +134,11 @@ export function editor(project: Project) {
plugins,
}}
>
- <ShortcutsProvider>
- <Editor />
- </ShortcutsProvider>
+ <PanelsProvider>
+ <ShortcutsProvider>
+ <Editor />
+ </ShortcutsProvider>
+ </PanelsProvider>
</ApplicationProvider>,
);
}
diff --git a/packages/ui/src/signals/index.ts b/packages/ui/src/signals/index.ts
index b4376921..7f559a40 100644
--- a/packages/ui/src/signals/index.ts
+++ b/packages/ui/src/signals/index.ts
@@ -1,5 +1,4 @@
export * from './EditorPanel';
export * from './labelClip';
-export * from './panels';
export * from './projectName';
export * from './storedSignal';
diff --git a/packages/ui/src/signals/panels.ts b/packages/ui/src/signals/panels.ts
deleted file mode 100644
index 0e635eb0..00000000
--- a/packages/ui/src/signals/panels.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import {computed} from '@preact/signals';
-import {EditorPanel, isEditorPanel} from './EditorPanel';
-import {storedSignal} from './storedSignal';
-
-function panelSignals(initial: EditorPanel, id: string) {
- const storedPanel = storedSignal<string>(initial, id, value => {
- return isEditorPanel(value) ? value : initial;
- });
- const isHidden = storedSignal(storedPanel.value === null, `${id}-hidden`);
- const current = computed(() => (isHidden.value ? null : storedPanel.value));
-
- return {
- get value() {
- return current.value;
- },
- set value(value: string | null) {
- if (value === null) {
- isHidden.value = true;
- } else {
- storedPanel.value = value;
- isHidden.value = false;
- }
- },
- isHidden,
- };
-}
-
-export const SidebarPanel = panelSignals(EditorPanel.VideoSettings, 'sidebar');
-export const BottomPanel = panelSignals(EditorPanel.Timeline, 'bottom-panel');
diff --git a/packages/ui/src/signals/storedSignal.ts b/packages/ui/src/signals/storedSignal.ts
index a85b6be7..dd3bbc66 100644
--- a/packages/ui/src/signals/storedSignal.ts
+++ b/packages/ui/src/signals/storedSignal.ts
@@ -1,4 +1,4 @@
-import {signal, Signal} from '@preact/signals';
+import {effect, signal, Signal} from '@preact/signals';
import {projectNameSignal} from './projectName';
export function storedSignal<T>(
@@ -7,7 +7,9 @@ export function storedSignal<T>(
parse?: (value: any) => T,
): Signal<T> {
const stored = signal(value);
- projectNameSignal.subscribe(name => {
+
+ effect(() => {
+ const name = projectNameSignal.value;
if (name !== null) {
const storedValue = localStorage.getItem(`${name}-${id}`);
if (storedValue !== null) {
@@ -20,7 +22,9 @@ export function storedSignal<T>(
}
}
});
- stored.subscribe(value => {
+
+ effect(() => {
+ const value = stored.value;
const name = projectNameSignal.value;
if (name !== null) {
localStorage.setItem(`${name}-${id}`, JSON.stringify(value));