nitro-web 0.3.10 → 0.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/app.tsx +2 -0
- package/client/index.ts +1 -1
- package/components/partials/element/theme-toggle.tsx +34 -16
- package/package.json +1 -1
- package/types.ts +2 -0
package/client/app.tsx
CHANGED
|
@@ -4,6 +4,7 @@ import ReactDOM from 'react-dom/client'
|
|
|
4
4
|
import { axios, camelCase, pick, toArray, setTimeoutPromise } from 'nitro-web/util'
|
|
5
5
|
import { injectedConfig, preloadedStoreData, exposedStoreData } from './index'
|
|
6
6
|
import { Config, Store } from 'nitro-web/types'
|
|
7
|
+
import { applyTheme } from '../components/partials/element/theme-toggle'
|
|
7
8
|
|
|
8
9
|
type StoreContainer = {
|
|
9
10
|
Provider: React.FC<{ children: ReactNode }>
|
|
@@ -46,6 +47,7 @@ export async function setupApp(config: Config, storeContainer: StoreContainer, l
|
|
|
46
47
|
if (!(window as unknown as { useTracked: unknown }).useTracked) {
|
|
47
48
|
throw new Error('useTracked is not defined globally before setupApp()')
|
|
48
49
|
}
|
|
50
|
+
applyTheme()
|
|
49
51
|
|
|
50
52
|
// Fetch state and init app
|
|
51
53
|
const settings: Settings = {
|
package/client/index.ts
CHANGED
|
@@ -40,7 +40,7 @@ export { Modal } from '../components/partials/element/modal'
|
|
|
40
40
|
export { Pagination, type PaginationProps } from '../components/partials/element/pagination'
|
|
41
41
|
export { Sidebar, getIsActive, type SidebarProps } from '../components/partials/element/sidebar'
|
|
42
42
|
export { TimePicker, type TimePickerProps } from '../components/partials/element/timepicker'
|
|
43
|
-
export { ThemeToggle, useTheme, getTheme, type Theme } from '../components/partials/element/theme-toggle'
|
|
43
|
+
export { ThemeToggle, useTheme, getTheme, applyTheme, type Theme } from '../components/partials/element/theme-toggle'
|
|
44
44
|
export { Tooltip, type TooltipProps } from '../components/partials/element/tooltip'
|
|
45
45
|
export { Topbar } from '../components/partials/element/topbar'
|
|
46
46
|
export { Table, type TableColumn, type TableProps, type TableRow, type TableRowType } from '../components/partials/element/table'
|
|
@@ -1,37 +1,55 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react'
|
|
2
|
-
import { Moon, Sun } from 'lucide-react'
|
|
3
|
-
import { twMerge } from 'nitro-web'
|
|
2
|
+
import { Monitor, Moon, Sun } from 'lucide-react'
|
|
3
|
+
import { injectedConfig, twMerge } from 'nitro-web'
|
|
4
4
|
|
|
5
5
|
export type Theme = 'light' | 'dark'
|
|
6
|
+
export type ThemePref = Theme | 'system'
|
|
6
7
|
|
|
7
|
-
//
|
|
8
|
+
// Saved preference (namespaced per app, since dev shares localhost), else the config default (env), else light
|
|
9
|
+
function getPref(): ThemePref {
|
|
10
|
+
return (localStorage.getItem(injectedConfig.themeName) || injectedConfig.theme || 'light') as ThemePref
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Resolves 'system' to the OS preference
|
|
14
|
+
function resolvePref(pref: ThemePref): Theme {
|
|
15
|
+
if (pref == 'system') return matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
|
16
|
+
return pref == 'dark' ? 'dark' : 'light'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// The applied light/dark theme
|
|
8
20
|
export function getTheme(): Theme {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
21
|
+
return resolvePref(getPref())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Applies the theme class to <html>, called in setupApp before React mounts
|
|
25
|
+
export function applyTheme() {
|
|
26
|
+
document.documentElement.classList.toggle('dark', getTheme() == 'dark')
|
|
12
27
|
}
|
|
13
28
|
|
|
14
|
-
// Applies the theme to <html> and persists
|
|
29
|
+
// Applies the theme to <html> and persists the preference. Cycles light -> dark -> system.
|
|
15
30
|
export function useTheme() {
|
|
16
|
-
const [
|
|
31
|
+
const [pref, setTheme] = useState<ThemePref>(getPref)
|
|
17
32
|
useEffect(() => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}, [
|
|
21
|
-
const
|
|
22
|
-
|
|
33
|
+
localStorage.setItem(injectedConfig.themeName, pref)
|
|
34
|
+
applyTheme()
|
|
35
|
+
}, [pref])
|
|
36
|
+
const order: ThemePref[] = ['light', 'dark', 'system']
|
|
37
|
+
const toggle = () => setTheme(order[(order.indexOf(pref) + 1) % order.length])
|
|
38
|
+
const theme = resolvePref(pref)
|
|
39
|
+
return { theme, pref, setTheme, toggle }
|
|
23
40
|
}
|
|
24
41
|
|
|
25
42
|
export function ThemeToggle({ className, size=18 }: { className?: string, size?: number }) {
|
|
26
|
-
const {
|
|
43
|
+
const { pref, toggle } = useTheme()
|
|
44
|
+
const Icon = pref == 'system' ? Monitor : pref == 'dark' ? Moon : Sun
|
|
27
45
|
return (
|
|
28
46
|
<button
|
|
29
47
|
type="button"
|
|
30
48
|
onClick={toggle}
|
|
31
|
-
title=
|
|
49
|
+
title={`Theme: ${pref}`}
|
|
32
50
|
class={twMerge('p-2 rounded-md text-muted-foreground hover:bg-muted hover:text-foreground', className)}
|
|
33
51
|
>
|
|
34
|
-
|
|
52
|
+
<Icon size={size} />
|
|
35
53
|
</button>
|
|
36
54
|
)
|
|
37
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nitro-web",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.12",
|
|
4
4
|
"repository": "github:boycce/nitro-web",
|
|
5
5
|
"homepage": "https://boycce.github.io/nitro-web/",
|
|
6
6
|
"description": "Nitro is a battle-tested, modular base project to turbocharge your projects, styled using Tailwind 🚀",
|
package/types.ts
CHANGED
|
@@ -8,9 +8,11 @@ type InjectedConfig = {
|
|
|
8
8
|
isDemo: boolean // implicitly defined by webpack
|
|
9
9
|
isStatic: boolean // implicitly defined by webpack
|
|
10
10
|
jwtName: string // implicitly defined by webpack
|
|
11
|
+
themeName: string // implicitly defined by webpack
|
|
11
12
|
name: string
|
|
12
13
|
placeholderEmail?: string
|
|
13
14
|
stripePublishableKey?: string
|
|
15
|
+
theme?: 'light' | 'dark' | 'system' // default theme, users can override it via the ThemeToggle
|
|
14
16
|
titleSeparator?: string
|
|
15
17
|
version: string
|
|
16
18
|
}
|