Skip to content

Commit

Permalink
feat(theme): disable transitions during theme switch
Browse files Browse the repository at this point in the history
  • Loading branch information
dvcol committed Feb 10, 2025
1 parent d0512fe commit 58e0d77
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/lib/providers/neo-theme-provider-context.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { wait } from '@dvcol/common-utils/common/promise';
import { getContext, setContext, untrack } from 'svelte';

import {
Expand Down Expand Up @@ -73,14 +74,32 @@ export class NeoThemeProviderContext implements INeoThemeProviderContext {
});
}

private async setTheme(theme: INeoThemeProviderContext['theme']) {
if (!this.root) throw new NeoErrorThemeTargetNotFound();
if (!('setAttribute' in this.root)) throw new NeoErrorThemeInvalidTarget();
if (this.theme === this.root.getAttribute(NeoThemeStorageKey.Theme)) return;

this.root.setAttribute(NeoThemeStorageKey.Transition, 'false');
this.root.setAttribute(NeoThemeStorageKey.Theme, theme);
await wait();
this.root.removeAttribute(NeoThemeStorageKey.Transition);
}

private setSource(source: INeoThemeProviderContext['source']) {
if (!this.root) throw new NeoErrorThemeTargetNotFound();
if (!('setAttribute' in this.root)) throw new NeoErrorThemeInvalidTarget();
if (this.source === this.root.getAttribute(NeoThemeStorageKey.Source)) return;

this.root.setAttribute(NeoThemeStorageKey.Source, source);
}

sync() {
if (!this.root) throw new NeoErrorThemeTargetNotFound();
if (!('setAttribute' in this.root)) throw new NeoErrorThemeInvalidTarget();

this.root.setAttribute(NeoThemeRoot, '');

this.root.setAttribute(NeoThemeStorageKey.Theme, this.theme);
this.root.setAttribute(NeoThemeStorageKey.Source, this.source);
this.setTheme(this.theme);
this.setSource(this.source);

if (this.reset) this.root.setAttribute(NeoThemeStorageKey.Reset, '');
else this.root.removeAttribute(NeoThemeStorageKey.Reset);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/providers/neo-theme-provider.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ export type NeoThemeProviderProps = {

export const NeoThemeRoot = 'neo-theme-root';

export const NeoThemeStorageKey: Record<string, `neo-${keyof INeoThemeProviderContext}`> = {
export const NeoThemeStorageKey: Record<string, `neo-${keyof INeoThemeProviderContext | 'transition'}`> = {
Reset: 'neo-reset' as const,
Theme: 'neo-theme' as const,
Source: 'neo-source' as const,
Remember: 'neo-remember' as const,
Transition: 'neo-transition' as const,
} as const;

export type NeoThemeStorageKeys = (typeof NeoThemeStorageKey)[keyof typeof NeoThemeStorageKey];
Expand Down
8 changes: 8 additions & 0 deletions src/lib/styles/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
background-color: var(--neo-text-highlight-color);
}

&[neo-transition='false'] {
*,
*::before,
*::after {
transition: none !important;
}
}

/* override semantic color if dark mode is enabled */
&[neo-theme='dark'] {
@include colors.dark-theme;
Expand Down

0 comments on commit 58e0d77

Please sign in to comment.