-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useIsDarkTheme): add useIsDarkTheme composable
Signed-off-by: Grigorii K. Shartsev <[email protected]>
- Loading branch information
Showing
6 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<!-- | ||
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
- SPDX-License-Identifier: AGPL-3.0-or-later | ||
--> | ||
|
||
```ts static | ||
import { | ||
useIsDarkTheme, | ||
useIsDarkThemeElement, | ||
} from '@nextcloud/vue/dist/Composables/useIsDarkTheme.js' | ||
``` | ||
|
||
Same as `isDarkTheme` functions, but with reactivity. | ||
|
||
## Definition | ||
|
||
```ts static | ||
/** | ||
* Check whether the dark theme is enabled in Nextcloud on a specific element. | ||
* If you need to check a whole page, use `useIsDarkTheme` instead. | ||
* | ||
* @param el - the element to check for the dark theme enabled on | ||
* @return {ComputedRef<boolean>} - computed boolean whether the dark theme is enabled | ||
*/ | ||
declare function useIsDarkThemeElement(el: MaybeRef<HTMLElement> = document.body): ComputedRef<boolean> | ||
|
||
/** | ||
* Shared composable to check whether the dark theme is enabled in Nextcloud | ||
* | ||
* @return {ComputedRef<boolean>} - computed boolean whether the dark theme is enabled | ||
*/ | ||
declare function useIsDarkTheme(): ComputedRef<boolean> | ||
``` | ||
|
||
## Example | ||
|
||
```vue | ||
<template> | ||
<div> | ||
Is dark theme enabled? {{ isDarkTheme }} | ||
<NcButton type="tertiary" @click="switchTheme">Switch theme</NcButton> | ||
</div> | ||
</template> | ||
<script> | ||
export default { | ||
setup() { | ||
const isDarkTheme = useIsDarkTheme() | ||
// For documentation only. Do not use in production. | ||
const switchTheme = () => { | ||
if (isDarkTheme.value) { | ||
document.body.setAttribute('data-theme-light') | ||
document.body.removeAttribute('data-theme-dark') | ||
document.body.setAttribute('data-themes', 'light') | ||
} else { | ||
document.body.setAttribute('data-theme-dark') | ||
document.body.removeAttribute('data-theme-light') | ||
document.body.setAttribute('data-themes', 'dark') | ||
} | ||
} | ||
return { | ||
isDarkTheme, | ||
switchTheme, | ||
} | ||
}, | ||
} | ||
</script> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { computed } from 'vue' | ||
import type { ComputedRef } from 'vue' | ||
import { useCssVar, createSharedComposable } from '@vueuse/core' | ||
import type { MaybeRef } from '@vueuse/core' | ||
|
||
/** | ||
* Check whether the dark theme is enabled in Nextcloud on a specific element. | ||
* If you need to check a whole page, use `useIsDarkTheme` instead. | ||
* | ||
* @param el - the element to check for the dark theme enabled on | ||
* @return {ComputedRef<boolean>} - computed boolean whether the dark theme is enabled | ||
*/ | ||
export function useIsDarkThemeElement(el: MaybeRef<HTMLElement> = document.body): ComputedRef<boolean> { | ||
// Nextcloud uses --background-invert-if-dark for dark theme filters in CSS | ||
// Values: | ||
// - 'invert(100%)' for the dark theme | ||
// - 'no' for the light theme | ||
const backgroundInvertIfDark = useCssVar('--background-invert-if-dark', el) | ||
return computed(() => backgroundInvertIfDark.value === 'invert(100%)') | ||
} | ||
|
||
/** | ||
* Shared composable to check whether the dark theme is enabled in Nextcloud | ||
* | ||
* @return {ComputedRef<boolean>} - computed boolean whether the dark theme is enabled | ||
*/ | ||
export const useIsDarkTheme = createSharedComposable(() => useIsDarkThemeElement()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters