|
9 | 9 | * @return {boolean} - Whether the dark theme is enabled via Nextcloud theme
|
10 | 10 | */
|
11 | 11 | export function checkIfDarkTheme(el: HTMLElement = document.body): boolean {
|
12 |
| - // Nextcloud uses --background-invert-if-dark for dark theme filters in CSS |
13 |
| - // Values: |
14 |
| - // - 'invert(100%)' for dark theme |
15 |
| - // - 'no' for light theme |
16 |
| - // This is the most reliable way to check for dark theme, including custom themes |
17 |
| - const backgroundInvertIfDark = window.getComputedStyle(el).getPropertyValue('--background-invert-if-dark') |
18 |
| - if (backgroundInvertIfDark !== undefined) { |
19 |
| - return backgroundInvertIfDark === 'invert(100%)' |
| 12 | + // Check if the new clean nextcloud-theme-dark variable is set |
| 13 | + const isNextcloudThemeDark = window.getComputedStyle(el).getPropertyValue('--nextcloud-theme-dark') |
| 14 | + if (isNextcloudThemeDark !== undefined) { |
| 15 | + return isNextcloudThemeDark === '1' |
20 | 16 | }
|
21 | 17 |
|
22 |
| - // There is no theme? Fallback to the light theme |
23 |
| - return false |
| 18 | + // Else we check the old ways |
| 19 | + // 1. first priority is the data-theme-dark attribute on a parent element |
| 20 | + // 2. second priority is the data-themes attribute on the body, aka the user setting |
| 21 | + // 3. third priority is the system preference |
| 22 | + // 4. lastly is the default light theme |
| 23 | + const darkModeSystemPreference = window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches |
| 24 | + const darkModeAccountSetting = document.body.getAttribute('data-themes')?.includes('dark') |
| 25 | + const darkModeElementOverride = el.closest('[data-theme-dark]') !== null |
| 26 | + return darkModeElementOverride || darkModeAccountSetting || darkModeSystemPreference || false |
24 | 27 | }
|
25 | 28 |
|
26 | 29 | /**
|
|
0 commit comments