Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Start the sidebar closed when it would cover the app

Evan Light425a733eafd4
diff --git a/static/js/app.modular.js b/static/js/app.modular.js
index c1833a8a..32c0ab54 100644
--- a/static/js/app.modular.js
+++ b/static/js/app.modular.js
@@ -1477,6 +1477,17 @@ document.addEventListener('DOMContentLoaded', async () => {
// --- Computed properties needed by composables ---
const isMobileScreen = computed(() => windowWidth.value < 1024);
+ // Collapse the sidebar when the window becomes narrow enough that
+ // it turns into an overlay. Below the lg breakpoint an expanded
+ // sidebar draws a backdrop over the whole app, so a desktop window
+ // dragged narrow -- or, far more often, an iPad turned to portrait
+ // -- would otherwise grey itself out with no hint as to why.
+ // Only the crossing matters, so someone who opens the sidebar on a
+ // narrow screen keeps it open.
+ watch(isMobileScreen, (narrow) => {
+ if (narrow) isSidebarCollapsed.value = true;
+ });
+
// Word-count meta surfaced in the right-rail tab labels. We
// strip HTML and collapse whitespace before counting so
// markdown-rendered summary HTML doesn't inflate the count.
diff --git a/static/js/modules/composables/ui.js b/static/js/modules/composables/ui.js
index 7482926a..f086535e 100644
--- a/static/js/modules/composables/ui.js
+++ b/static/js/modules/composables/ui.js
@@ -5,6 +5,31 @@
import { filenameFromContentDisposition } from '../utils/content-disposition.js';
+/**
+ * Decide whether the sidebar should start collapsed.
+ *
+ * Under Tailwind's `lg` breakpoint the sidebar stops being a column and becomes
+ * an overlay with a half-opaque backdrop across the whole app
+ * (templates/components/sidebar.html renders that backdrop whenever the sidebar
+ * is expanded and the screen is narrow). So on a narrow screen an expanded
+ * sidebar does not just take up room, it greys out everything behind it.
+ *
+ * The stored `sidebarCollapsed` value is a preference someone set at a desktop
+ * width, and restoring it on a narrow screen is what puts a first-time visitor
+ * behind that grey. Narrow screens therefore always start collapsed, and the
+ * stored preference is left alone so it still applies when the window is wide
+ * again.
+ *
+ * @param {string|null} saved raw localStorage value ('true' / 'false' / null)
+ * @param {boolean} isMobileScreen true below the lg breakpoint
+ * @returns {boolean} whether the sidebar should start collapsed
+ */
+export function resolveSidebarCollapsed(saved, isMobileScreen) {
+ if (isMobileScreen) return true;
+ if (saved === null || saved === undefined) return false;
+ return saved === 'true';
+}
+
export function useUI(state, utils, processedTranscription) {
const {
isDarkMode, currentColorScheme, colorSchemes, isSidebarCollapsed,
@@ -136,10 +161,10 @@ export function useUI(state, utils, processedTranscription) {
// Initialize sidebar state
const initializeSidebar = () => {
- const saved = localStorage.getItem('sidebarCollapsed');
- if (saved !== null) {
- isSidebarCollapsed.value = saved === 'true';
- }
+ isSidebarCollapsed.value = resolveSidebarCollapsed(
+ localStorage.getItem('sidebarCollapsed'),
+ isMobileScreen.value
+ );
};
// Switch to upload view. Guarded against silently abandoning an unsaved
diff --git a/static/js/modules/composables/ui.test.js b/static/js/modules/composables/ui.test.js
new file mode 100644
index 00000000..f66d1178
--- /dev/null
+++ b/static/js/modules/composables/ui.test.js
@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest';
+
+import { resolveSidebarCollapsed } from './ui.js';
+
+// Below the lg breakpoint the sidebar is an overlay that draws a backdrop over
+// the whole app, so "expanded" there means "the app is behind grey". iPad
+// portrait is 834px on an 11" and lands on this every time.
+describe('resolveSidebarCollapsed', () => {
+ it('starts collapsed on a narrow screen when nothing is stored', () => {
+ expect(resolveSidebarCollapsed(null, true)).toBe(true);
+ });
+
+ it('starts collapsed on a narrow screen even when a wide screen stored expanded', () => {
+ expect(resolveSidebarCollapsed('false', true)).toBe(true);
+ });
+
+ it('keeps a narrow screen collapsed when that is what was stored', () => {
+ expect(resolveSidebarCollapsed('true', true)).toBe(true);
+ });
+
+ it('restores the stored preference on a wide screen', () => {
+ expect(resolveSidebarCollapsed('true', false)).toBe(true);
+ expect(resolveSidebarCollapsed('false', false)).toBe(false);
+ });
+
+ it('defaults to expanded on a wide screen with nothing stored', () => {
+ expect(resolveSidebarCollapsed(null, false)).toBe(false);
+ expect(resolveSidebarCollapsed(undefined, false)).toBe(false);
+ });
+});