diff --git a/src/components/ui/side-panel.tsx b/src/components/ui/side-panel.tsx index d41d0b3..4460f50 100644 --- a/src/components/ui/side-panel.tsx +++ b/src/components/ui/side-panel.tsx @@ -1,12 +1,38 @@ import { Dialog as DialogPrimitive } from "@kobalte/core/dialog"; import { createEffect, createSignal, onCleanup, type ParentProps } from "solid-js"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; -import { IconAlert, IconX } from "@/components/ui/icons"; +import { IconAlert, IconMaximize, IconMinimize, IconX } from "@/components/ui/icons"; import { cn } from "@/lib/cn"; import { useT } from "@/stores/preferences-context"; let openPanelCount = 0; +/** Width the user dragged or expanded a panel to; one value for every panel. */ +const WIDTH_KEY = "hezarfen.sidePanel.width"; +const MIN_WIDTH = 360; +/** Room kept free on the left so the page behind still reads as "behind". */ +const PAGE_GUTTER = 64; +const EXPANDED_WIDTH = 1120; + +const maxPanelWidth = () => (typeof window === "undefined" ? EXPANDED_WIDTH : Math.max(MIN_WIDTH, window.innerWidth - PAGE_GUTTER)); +const clampWidth = (value: number) => Math.round(Math.min(Math.max(value, MIN_WIDTH), maxPanelWidth())); +const readStoredWidth = (): number | null => { + try { + const value = Number(localStorage.getItem(WIDTH_KEY)); + return Number.isFinite(value) && value > 0 ? value : null; + } catch { + return null; + } +}; +const storeWidth = (value: number | null) => { + try { + if (value == null) localStorage.removeItem(WIDTH_KEY); + else localStorage.setItem(WIDTH_KEY, String(value)); + } catch { + // Private mode: the width lasts for this page only. + } +}; + export function SidePanel( props: ParentProps<{ open: boolean; @@ -71,6 +97,65 @@ export function SidePanel( return "max-w-[min(34rem,100vw)]"; }; + // A user-chosen width, dragged from the left edge or set by the expand + // button, overrides the size preset on screens wide enough to have a page + // beside the panel. Phones keep the full-screen panel. + const [customWidth, setCustomWidth] = createSignal(readStoredWidth()); + const roomyQuery = () => + typeof window !== "undefined" && typeof window.matchMedia === "function" ? window.matchMedia("(min-width: 640px)") : null; + const [roomy, setRoomy] = createSignal(roomyQuery()?.matches ?? false); + createEffect(() => { + if (!props.open) return; + const query = roomyQuery(); + if (!query) return; + const sync = () => setRoomy(query.matches); + sync(); + query.addEventListener("change", sync); + onCleanup(() => query.removeEventListener("change", sync)); + }); + const appliedWidth = () => { + const value = customWidth(); + return roomy() && value != null ? clampWidth(value) : null; + }; + const expanded = () => (appliedWidth() ?? 0) >= clampWidth(EXPANDED_WIDTH) - 8; + const setWidth = (value: number | null) => { + setCustomWidth(value == null ? null : clampWidth(value)); + storeWidth(value == null ? null : clampWidth(value)); + }; + const toggleExpanded = () => setWidth(expanded() ? null : EXPANDED_WIDTH); + const startResize = (event: PointerEvent) => { + if (event.button !== 0) return; + event.preventDefault(); + const handle = event.currentTarget as HTMLElement; + try { + handle.setPointerCapture(event.pointerId); + } catch { + // No active pointer to capture (synthetic events); moves still arrive. + } + const previousCursor = document.body.style.cursor; + document.body.style.cursor = "col-resize"; + const move = (moveEvent: PointerEvent) => setCustomWidth(clampWidth(window.innerWidth - moveEvent.clientX)); + const stop = () => { + handle.removeEventListener("pointermove", move); + handle.removeEventListener("pointerup", stop); + handle.removeEventListener("pointercancel", stop); + document.body.style.cursor = previousCursor; + storeWidth(customWidth()); + }; + handle.addEventListener("pointermove", move); + handle.addEventListener("pointerup", stop); + handle.addEventListener("pointercancel", stop); + }; + const resizeByKey = (event: KeyboardEvent) => { + const step = event.shiftKey ? 80 : 24; + const current = appliedWidth() ?? (event.currentTarget as HTMLElement).parentElement?.getBoundingClientRect().width ?? MIN_WIDTH; + if (event.key === "ArrowLeft") setWidth(current + step); + else if (event.key === "ArrowRight") setWidth(current - step); + else if (event.key === "Home") setWidth(null); + else return; + event.preventDefault(); + }; + return ( <> @@ -102,8 +187,9 @@ export function SidePanel( width(), props.class, )} + style={appliedWidth() != null ? { width: `${appliedWidth()}px`, "max-width": "100vw" } : undefined} > -
+
{props.title} @@ -114,6 +200,15 @@ export function SidePanel( )}
+ {props.children}
+ {/* Drag the left edge to resize; double-click or Home resets. Last in + the DOM so opening the panel does not focus it first. */} + diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts index 3594455..4d91fae 100644 --- a/src/i18n/locales/en.ts +++ b/src/i18n/locales/en.ts @@ -789,6 +789,9 @@ export const en: Dict = { "sidePanel.discardSummary": "What you entered in this form has not been saved and will be lost.", "sidePanel.discard": "Discard changes", "sidePanel.keepEditing": "Keep editing", + "sidePanel.expand": "Expand panel", + "sidePanel.collapse": "Restore panel width", + "sidePanel.resize": "Resize panel", "dashboard.partialError": "Some panels could not load and show no data.", "common.breadcrumb": "Breadcrumb", "common.truncatedNotice": "Showing the first {shown} of {total} records.", diff --git a/src/i18n/locales/tr.ts b/src/i18n/locales/tr.ts index 7f84af7..aebd121 100644 --- a/src/i18n/locales/tr.ts +++ b/src/i18n/locales/tr.ts @@ -785,6 +785,9 @@ export const tr: Dict = { "sidePanel.discardSummary": "Bu forma girdikleriniz kaydedilmedi ve kaybolacak.", "sidePanel.discard": "Değişiklikleri at", "sidePanel.keepEditing": "Düzenlemeye devam et", + "sidePanel.expand": "Paneli genişlet", + "sidePanel.collapse": "Paneli daralt", + "sidePanel.resize": "Panel genişliğini ayarla", "dashboard.partialError": "Bazı bölümler yüklenemedi ve veri göstermiyor.", "common.breadcrumb": "Sayfa konumu", "common.truncatedNotice": "{total} kayıttan ilk {shown} tanesi gösteriliyor.", diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index f33337c..6ce886e 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -776,6 +776,9 @@ export type MessageKey = | "sidePanel.discardSummary" | "sidePanel.discard" | "sidePanel.keepEditing" + | "sidePanel.expand" + | "sidePanel.collapse" + | "sidePanel.resize" | "dashboard.partialError" | "common.breadcrumb" | "common.truncatedNotice"