Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
f6e025d5
Commit
f6e025d5
authored
Oct 08, 2025
by
Claude
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: implement theme management with system preference detection and early application
parent
35141807
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
0 deletions
+48
-0
main.tsx
web/src/main.tsx
+4
-0
theme.ts
web/src/utils/theme.ts
+44
-0
No files found.
web/src/main.tsx
View file @
f6e025d5
...
...
@@ -8,8 +8,12 @@ import "./index.css";
import
router
from
"./router"
;
import
{
initialUserStore
}
from
"./store/user"
;
import
{
initialWorkspaceStore
}
from
"./store/workspace"
;
import
{
applyThemeEarly
}
from
"./utils/theme"
;
import
"leaflet/dist/leaflet.css"
;
// Apply theme early to prevent flash of wrong theme
applyThemeEarly
();
const
Main
=
observer
(()
=>
(
<>
<
RouterProvider
router=
{
router
}
/>
...
...
web/src/utils/theme.ts
View file @
f6e025d5
...
...
@@ -16,6 +16,43 @@ const validateTheme = (theme: string): ValidTheme => {
return
VALID_THEMES
.
includes
(
theme
as
ValidTheme
)
?
(
theme
as
ValidTheme
)
:
"default"
;
};
/**
* Detects system theme preference
*/
export
const
getSystemTheme
=
():
"default"
|
"default-dark"
=>
{
if
(
typeof
window
!==
"undefined"
&&
window
.
matchMedia
)
{
return
window
.
matchMedia
(
"(prefers-color-scheme: dark)"
).
matches
?
"default-dark"
:
"default"
;
}
return
"default"
;
};
/**
* Gets the theme that should be applied on initial load
* Priority: stored user preference -> system preference -> default
*/
export
const
getInitialTheme
=
():
ValidTheme
=>
{
// Try to get stored theme from localStorage (where user settings might be cached)
try
{
const
storedTheme
=
localStorage
.
getItem
(
"memos-theme"
);
if
(
storedTheme
&&
VALID_THEMES
.
includes
(
storedTheme
as
ValidTheme
))
{
return
storedTheme
as
ValidTheme
;
}
}
catch
{
// localStorage might not be available
}
// Fall back to system preference
return
getSystemTheme
();
};
/**
* Applies the theme early to prevent flash of wrong theme
*/
export
const
applyThemeEarly
=
():
void
=>
{
const
theme
=
getInitialTheme
();
loadTheme
(
theme
);
};
export
const
loadTheme
=
(
themeName
:
string
):
void
=>
{
const
validTheme
=
validateTheme
(
themeName
);
...
...
@@ -35,4 +72,11 @@ export const loadTheme = (themeName: string): void => {
// Set data attribute
document
.
documentElement
.
setAttribute
(
"data-theme"
,
validTheme
);
// Store theme preference for future loads
try
{
localStorage
.
setItem
(
"memos-theme"
,
validTheme
);
}
catch
{
// localStorage might not be available
}
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment