From 9b84b4483ed6551248cc1776c8f86db17a3ea283 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sun, 27 Aug 2023 11:07:04 +1000 Subject: [PATCH] long sidebar style --- src/lib/css_var.ts | 39 ++++++++++++++++++++++++++++ src/lib/global_styles.scss | 4 ++- src/widget/Sidebar/Sidebar.svelte | 34 +++++++++++++++++++++++- src/widget/Sidebar/style.module.scss | 13 +++++----- 4 files changed, 81 insertions(+), 9 deletions(-) diff --git a/src/lib/css_var.ts b/src/lib/css_var.ts index 43e6056..2b900ee 100644 --- a/src/lib/css_var.ts +++ b/src/lib/css_var.ts @@ -1,4 +1,16 @@ +let events: CSSVariableEventHandler[] = []; + +export interface CSSVariableEvent { + name: string + newValue: string, + oldValue: string +} + +export interface CSSVariableEventHandler { + (event: CSSVariableEvent): any | void | Promise +} + /** * Set CSS variable * @@ -10,6 +22,12 @@ export function setCSSVariable(name: string, val: string | number): void { if (typeof val == 'number') val = `${val}px`; if (!name.startsWith('--')) name = `--${name}`; + callEvent({ + name, + newValue: val, + oldValue: getCSSVariable(name) + }); + // @ts-ignore document.querySelector(':root').style.setProperty(name, val); } @@ -38,4 +56,25 @@ export function setCSSVariableFor(name: string, val: string | number, timeout: n const old = getCSSVariable(name); setCSSVariable(name, val); setTimeout(() => setCSSVariable(name, old), timeout); +} + +/** + * Register an event handler that would watch for + * CSS variable changes + * + * @param handler Handler to be called + */ +export function registerEventHandler(handler: CSSVariableEventHandler) { + events.push(handler) +} + +async function callEvent(e: CSSVariableEvent) { + let promises = []; + + for (const event of events) { + promises.push(event(e)); + } + + // trick to execute all promises concurrently + await Promise.all(promises); } \ No newline at end of file diff --git a/src/lib/global_styles.scss b/src/lib/global_styles.scss index 9656a5a..819b57b 100644 --- a/src/lib/global_styles.scss +++ b/src/lib/global_styles.scss @@ -1,7 +1,9 @@ :root { --sidebar-width: 60px; --sidebar-button-margin: 8px; - --sidebar-button-icon-size: 16px; + --sidebar-button-icon-size: 11pt; + --sidebar-button-font-size: 9pt; + --sidebar-button-size: 60px; } input[type=text] { diff --git a/src/widget/Sidebar/Sidebar.svelte b/src/widget/Sidebar/Sidebar.svelte index 69e099e..972a931 100644 --- a/src/widget/Sidebar/Sidebar.svelte +++ b/src/widget/Sidebar/Sidebar.svelte @@ -6,21 +6,53 @@ import VscArrowLeft from "svelte-icons-pack/vsc/VscArrowLeft"; import BsGearFill from 'svelte-icons-pack/bs/BsGearFill'; import { route, back } from '../../store/RouteStore'; + import { onMount } from 'svelte'; + + import { registerEventHandler } from '../../lib/css_var'; + + let self: HTMLElement; + let longStyle = false; + + const checkLongStyle = () => { + if (self.clientWidth >= 120) { + longStyle = true; + } else { + longStyle = false; + } + } + + registerEventHandler((e) => { + if (e.name !== '--sidebar-width') return; + checkLongStyle(); + }) + + onMount(() => { + checkLongStyle() + }) -
+
\ No newline at end of file diff --git a/src/widget/Sidebar/style.module.scss b/src/widget/Sidebar/style.module.scss index 508481c..5d787c1 100644 --- a/src/widget/Sidebar/style.module.scss +++ b/src/widget/Sidebar/style.module.scss @@ -20,11 +20,13 @@ .button { transition: 650ms ease; width: calc(var(--sidebar-width) - (var(--sidebar-button-margin) * 2)); - height: calc(var(--sidebar-width) - (var(--sidebar-button-margin) * 2)); + height: calc(var(--sidebar-button-size) - (var(--sidebar-button-margin) * 2)); margin: var(--sidebar-button-margin); margin-bottom: 0; border: 3px solid #272222; border-radius: 9px; + text-align: center; + line-height: calc(var(--sidebar-button-size) / 3); &:hover { border: 3px solid #444; @@ -39,9 +41,10 @@ &[active] { border: 3px solid #777; } + font-size: var(--sidebar-button-font-size); svg { - transform: translate(-1px, 1px); + transform: translate(-2px, 2px); height: var(--sidebar-button-icon-size); width: var(--sidebar-button-icon-size); pointer-events: none; @@ -50,16 +53,12 @@ .back { height: fit-content !important; border-width: 1px; - padding: calc(var(--sidebar-button-margin) / 2); + padding: calc(var(--sidebar-button-margin) * .6) calc(var(--sidebar-button-margin) * 1.5); margin-top: calc(var(--sidebar-button-margin) + 4px); &:hover, &:active { margin-top: var(--sidebar-button-margin); } - - svg { - transform: translate(0, 1px); - } } .bottom { position: absolute;