Skip to content

Commit

Permalink
feat: move initialize theme function to ui package
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Shatford <[email protected]>
  • Loading branch information
jordanshatford committed Mar 15, 2024
1 parent 715dc85 commit 22f2617
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changeset/quiet-masks-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@yd/extension": minor
"@yd/ui": minor
---

feat: move initialize theme function to ui package
3 changes: 1 addition & 2 deletions apps/extension/src/lib/components/PageLayout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { IconButton, ExternalLinkIcon, Toasts, Footer } from '@yd/ui';
import { initializeTheme } from '~/lib/theme';
import { IconButton, ExternalLinkIcon, Toasts, Footer, initializeTheme } from '@yd/ui';
import { openWebsite } from '~/lib/website';
import config from '~/lib/config';
Expand Down
3 changes: 1 addition & 2 deletions apps/extension/src/lib/components/PopupLayout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { IconButton, ExternalLinkIcon, GearIcon } from '@yd/ui';
import { initializeTheme } from '~/lib/theme';
import { IconButton, ExternalLinkIcon, GearIcon, initializeTheme } from '@yd/ui';
import { openWebsite } from '~/lib/website';
import { getContextService } from '~/lib/context-service';
import PopupError from '~/lib/components/PopupError.svelte';
Expand Down
14 changes: 0 additions & 14 deletions apps/extension/src/lib/theme.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/ui/src/lib/components/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { theme } from './store';
export { theme, initializeTheme } from './store';
export { default as ThemeToggle } from './ThemeToggle.svelte';
22 changes: 14 additions & 8 deletions packages/ui/src/lib/components/theme/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ function getInferredDefaultTheme(): Theme {
}
}

// Initialize document with a given theme. Tailwind will handle styling dark or light.
export function initializeTheme(override?: Theme): Theme {
const theme = override ? override : getInferredDefaultTheme();
if (theme === 'dark') {
document.querySelector('html')?.classList.add('dark');
} else {
document.querySelector('html')?.classList.remove('dark');
}
return theme;
}

function createThemeStore() {
const { subscribe, set, update } = writable<Theme>(getInferredDefaultTheme());

if (browser()) {
let data = localStorage?.getItem(THEME_LOCALSTORAGE_KEY) as Theme;
if (!data) {
data = getInferredDefaultTheme();
}
set(data);
if (data === 'dark') {
document.querySelector('html')?.classList.add('dark');
}
const data = localStorage?.getItem(THEME_LOCALSTORAGE_KEY) as Theme;
const theme = initializeTheme(data);
set(theme);
}

subscribe((value) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Component exports
export { NavBar } from './components/nav';
export { ThemeToggle, theme } from './components/theme';
export { Toast, Toasts, toast } from './components/toast';
export { Title } from './components/typography';
export * from './components/theme';
export * from './components/toast';
export * from './components/typography';
export { default as ActionIcon } from './components/ActionIcon.svelte';
export { default as Alert, type AlertVariants } from './components/Alert.svelte';
export { default as Badge, type BadgeVariants } from './components/Badge.svelte';
Expand Down

0 comments on commit 22f2617

Please sign in to comment.