Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/functions/isDarkTheme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@
* @return {boolean} - Whether the dark theme is enabled via Nextcloud theme
*/
export function checkIfDarkTheme(el: HTMLElement = document.body): boolean {
// Nextcloud uses --background-invert-if-dark for dark theme filters in CSS
// Values:
// - 'invert(100%)' for dark theme
// - 'no' for light theme
// This is the most reliable way to check for dark theme, including custom themes
const backgroundInvertIfDark = window.getComputedStyle(el).getPropertyValue('--background-invert-if-dark')
if (backgroundInvertIfDark !== undefined) {
return backgroundInvertIfDark === 'invert(100%)'
// Check if the new clean nextcloud-theme-dark variable is set
const isNextcloudThemeDark = window.getComputedStyle(el).getPropertyValue('--nextcloud-theme-dark')
if (isNextcloudThemeDark !== undefined) {
return isNextcloudThemeDark === '1'
}

// Else we check the old ways
// 1. first priority is the data-theme-dark attribute on a parent element
// 2. second priority is the data-themes attribute on the body, aka the user setting
// 3. third priority is the system preference
// 4. lastly is the default light theme
if (el.closest('[data-theme-dark]') !== null) {
return true
}

if (document.body.getAttribute('data-themes')?.includes('dark')) {
return true
}

if (window.matchMedia('(prefers-color-scheme: dark)')?.matches) {
return true
}

// There is no theme? Fallback to the light theme
return false
}

Expand Down
Loading