Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: persist sidebar across refreshes (#1527)

Myles Scolnicka855ae77a277
diff --git a/frontend/src/components/debug/indicator.tsx b/frontend/src/components/debug/indicator.tsx
index cdca1628d4..1d62e96014 100644
--- a/frontend/src/components/debug/indicator.tsx
+++ b/frontend/src/components/debug/indicator.tsx
@@ -5,7 +5,7 @@ export const TailwindIndicator = () => {
}
return (
- <div className="fixed bottom-2 right-[240px] z-50 flex size-5 items-center justify-center rounded-lg bg-gray-800 py-3 px-4 font-mono text-xs text-white">
+ <div className="fixed bottom-2 left-[60px] z-50 flex size-5 items-center justify-center rounded-lg bg-gray-800 py-3 px-4 font-mono text-xs text-white">
<div className="block sm:hidden">xs</div>
<div className="hidden sm:block md:hidden lg:hidden xl:hidden 2xl:hidden">
sm
diff --git a/frontend/src/components/editor/chrome/state.ts b/frontend/src/components/editor/chrome/state.ts
index 19ad82157f..d367809e34 100644
--- a/frontend/src/components/editor/chrome/state.ts
+++ b/frontend/src/components/editor/chrome/state.ts
@@ -2,6 +2,8 @@
import { createReducerAndAtoms } from "@/utils/createReducer";
import { useAtomValue } from "jotai";
import { PanelType } from "./types";
+import { ZodLocalStorage } from "@/utils/localStorage";
+import { z } from "zod";
export interface ChromeState {
selectedPanel: PanelType | undefined;
@@ -9,6 +11,19 @@ export interface ChromeState {
panelLocation: "left" | "bottom";
}
+const storage = new ZodLocalStorage<ChromeState>(
+ "marimo:sidebar",
+ z.object({
+ selectedPanel: z
+ .string()
+ .optional()
+ .transform((v) => v as PanelType),
+ isOpen: z.boolean(),
+ panelLocation: z.union([z.literal("left"), z.literal("bottom")]),
+ }),
+ initialState,
+);
+
function initialState(): ChromeState {
return {
selectedPanel: "variables", // initial panel
@@ -22,23 +37,27 @@ const {
createActions,
valueAtom: chromeAtom,
useActions,
-} = createReducerAndAtoms(initialState, {
- openApplication: (state, selectedPanel: PanelType) => ({
- ...state,
- selectedPanel,
- // If it was closed, open it
- // If it was open, keep it open unless it was the same application
- isOpen: state.isOpen ? state.selectedPanel !== selectedPanel : true,
- }),
- openPanel: (state) => ({ ...state, isOpen: true }),
- closePanel: (state) => ({ ...state, isOpen: false }),
- togglePanel: (state) => ({ ...state, isOpen: !state.isOpen }),
- setIsOpen: (state, isOpen: boolean) => ({ ...state, isOpen }),
- changePanelLocation: (state, panelLocation: "left" | "bottom") => ({
- ...state,
- panelLocation,
- }),
-});
+} = createReducerAndAtoms(
+ () => storage.get(),
+ {
+ openApplication: (state, selectedPanel: PanelType) => ({
+ ...state,
+ selectedPanel,
+ // If it was closed, open it
+ // If it was open, keep it open unless it was the same application
+ isOpen: state.isOpen ? state.selectedPanel !== selectedPanel : true,
+ }),
+ openPanel: (state) => ({ ...state, isOpen: true }),
+ closePanel: (state) => ({ ...state, isOpen: false }),
+ togglePanel: (state) => ({ ...state, isOpen: !state.isOpen }),
+ setIsOpen: (state, isOpen: boolean) => ({ ...state, isOpen }),
+ changePanelLocation: (state, panelLocation: "left" | "bottom") => ({
+ ...state,
+ panelLocation,
+ }),
+ },
+ [(_prevState, newState) => storage.set(newState)],
+);
export const useChromeState = () => {
const state = useAtomValue(chromeAtom);
diff --git a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
index 10d2d3ea03..1d67ef5762 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
@@ -16,7 +16,7 @@ export const VerticalLayoutWrapper: React.FC<PropsWithChildren<Props>> = ({
children,
}) => {
return (
- <div className={cn("px-1 sm:px-16 md:px-32", className)}>
+ <div className={cn("px-1 sm:px-16 md:px-20 xl:px-24", className)}>
<div
className={cn(
// Large mobile bottom padding due to mobile browser navigation bar
diff --git a/frontend/src/css/app/Cell.css b/frontend/src/css/app/Cell.css
index 79eecf97fd..7c62efaac9 100644
--- a/frontend/src/css/app/Cell.css
+++ b/frontend/src/css/app/Cell.css
@@ -207,7 +207,7 @@
justify-content: flex-end;
height: 100%;
position: absolute;
- right: -98px;
+ right: -94px;
width: 80px;
gap: 4px;
z-index: 2;
diff --git a/frontend/src/utils/createReducer.ts b/frontend/src/utils/createReducer.ts
index f81dc67f06..270e8ca463 100644
--- a/frontend/src/utils/createReducer.ts
+++ b/frontend/src/utils/createReducer.ts
@@ -68,12 +68,32 @@ export function createReducer<
};
}
+type Middleware<State> = (
+ prevState: State,
+ newState: State,
+ action: ReducerAction<any>,
+) => void;
+
export function createReducerAndAtoms<
State,
RH extends ReducerHandlers<NoInfer<State>>,
->(initialState: () => State, reducers: RH) {
+>(
+ initialState: () => State,
+ reducers: RH,
+ middleware?: Array<Middleware<State>>,
+) {
const { reducer, createActions } = createReducer(initialState, reducers);
+ const reducerWithMiddleware = (state: State, action: ReducerAction<any>) => {
+ const newState = reducer(state, action);
+ if (middleware) {
+ for (const mw of middleware) {
+ mw(state, newState, action);
+ }
+ }
+ return newState;
+ };
+
const valueAtom = atom(initialState());
function useActions() {
@@ -81,11 +101,16 @@ export function createReducerAndAtoms<
return useMemo(() => {
const actions = createActions((action) => {
- setState((state) => reducer(state, action));
+ setState((state) => reducerWithMiddleware(state, action));
});
return actions;
}, [setState]);
}
- return { reducer, createActions, valueAtom, useActions };
+ return {
+ reducer: reducerWithMiddleware,
+ createActions,
+ valueAtom,
+ useActions,
+ };
}