Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdd5fb61b4 | ||
|
|
23c5461162 | ||
|
|
82c20a5f10 | ||
|
|
5e43cc03ff | ||
|
|
18fdd1db0b | ||
|
|
98926a8aa9 | ||
|
|
0981fdebd7 |
No files matched your search
+14
-2
@@ -69,10 +69,20 @@ export type AppConfig = {
|
||||
// -------------------
|
||||
|
||||
/**
|
||||
* Application theme.
|
||||
* Default: 'default' to follow the system theme.
|
||||
* Color scheme (dark mode).
|
||||
* Default: 'default' to follow the system settings.
|
||||
*/
|
||||
theme: 'default' | 'dark' | 'light'
|
||||
/**
|
||||
* Contrast preferences: whether to follow the system settings, enable (enforce) or disable (suppress) high contrast.
|
||||
* Default: 'default'.
|
||||
*/
|
||||
highContrast: 'default' | 'enabled' | 'disabled'
|
||||
/**
|
||||
* Whether to enable the dyslexic font (OpenDyslexic) for better readability.
|
||||
* Default: false
|
||||
*/
|
||||
dyslexicFont: boolean
|
||||
/**
|
||||
* Whether to use a custom title bar or the system default.
|
||||
* Default: false.
|
||||
@@ -147,6 +157,8 @@ const defaultAppConfig: AppConfig = {
|
||||
launchAtStartup: false,
|
||||
launchAtStartupInBackground: true,
|
||||
theme: 'default',
|
||||
highContrast: 'default',
|
||||
dyslexicFont: false,
|
||||
systemTitleBar: false,
|
||||
monochromeTrayIcon: isMac,
|
||||
zoomFactor: 1,
|
||||
|
||||
+34
-2
@@ -3,7 +3,7 @@
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { nativeTheme } from 'electron'
|
||||
import { BrowserWindow, ipcMain, nativeTheme } from 'electron'
|
||||
import { getAppConfig, onAppConfigChange } from './AppConfig.ts'
|
||||
|
||||
onAppConfigChange('theme', applyTheme)
|
||||
@@ -14,7 +14,6 @@ onAppConfigChange('theme', applyTheme)
|
||||
export function applyTheme() {
|
||||
const theme = getAppConfig('theme')
|
||||
|
||||
// TODO: add high-contrast themes
|
||||
const configToNativeTheme = {
|
||||
default: 'system',
|
||||
dark: 'dark',
|
||||
@@ -23,3 +22,36 @@ export function applyTheme() {
|
||||
|
||||
nativeTheme.themeSource = configToNativeTheme[theme]
|
||||
}
|
||||
|
||||
// Unlike `prefers-color-scheme`, Electron does not set `prefers-contrast` according to nativeTheme.shouldUseHighContrastColors
|
||||
// Providing it via IPC to enable high contrast theme manually ...
|
||||
|
||||
nativeTheme.on('updated', () => {
|
||||
for (const browserWindow of BrowserWindow.getAllWindows()) {
|
||||
browserWindow.webContents.send('app:prefersContrastMore:change', nativeTheme.shouldUseHighContrastColors)
|
||||
}
|
||||
})
|
||||
|
||||
ipcMain.on('app:prefersContrastMore:get', (event) => {
|
||||
event.returnValue = nativeTheme.shouldUseHighContrastColors
|
||||
})
|
||||
|
||||
/**
|
||||
* Get original system theme.
|
||||
* Once theme is overridden in Electron, it only returns the overridden values.
|
||||
* The only way to get the system theme is to switch to the system and then back.
|
||||
* This API is sync and should not trigger any flickering...
|
||||
*/
|
||||
export function getSystemTheme() {
|
||||
const themeSource = nativeTheme.themeSource
|
||||
|
||||
nativeTheme.themeSource = 'system'
|
||||
const isDark = nativeTheme.shouldUseDarkColors
|
||||
nativeTheme.themeSource = themeSource
|
||||
|
||||
return isDark ? 'dark' : 'light'
|
||||
}
|
||||
|
||||
ipcMain.on('app:systemTheme:get', (event) => {
|
||||
event.returnValue = getSystemTheme()
|
||||
})
|
||||
+4
-1
@@ -19,7 +19,7 @@ const { setupReleaseNotificationScheduler, checkForUpdate } = require('./app/git
|
||||
const { initLaunchAtStartupListener } = require('./app/launchAtStartup.config.ts')
|
||||
const { runMigrations } = require('./app/migration.service.ts')
|
||||
const { systemInfo, isMac, isWindows, isSameExecution, isSquirrel, relaunchApp } = require('./app/system.utils.ts')
|
||||
const { applyTheme } = require('./app/theme.config.ts')
|
||||
const { applyTheme, getSystemTheme } = require('./app/theme.config.ts')
|
||||
const { buildTitle, onReadyToShow } = require('./app/utils.ts')
|
||||
const { enableWebRequestInterceptor, disableWebRequestInterceptor } = require('./app/webRequestInterceptor.js')
|
||||
const { createAuthenticationWindow } = require('./authentication/authentication.window.js')
|
||||
@@ -68,6 +68,9 @@ if (!app.requestSingleInstanceLock()) {
|
||||
|
||||
ipcMain.on('app:quit', () => app.quit())
|
||||
ipcMain.handle('app:getSystemInfo', () => systemInfo)
|
||||
ipcMain.on('app:getSystemTheme', (event) => {
|
||||
event.returnValue = getSystemTheme()
|
||||
})
|
||||
ipcMain.handle('app:buildTitle', (event, title) => buildTitle(title))
|
||||
ipcMain.handle('app:getSystemL10n', () => ({
|
||||
locale: app.getLocale().replace('-', '_') ?? 'en',
|
||||
|
||||
@@ -48,6 +48,29 @@ const TALK_DESKTOP = {
|
||||
* @return {Promise<{ locale: string, language: string }>}
|
||||
*/
|
||||
getSystemL10n: () => ipcRenderer.invoke('app:getSystemL10n'),
|
||||
/**
|
||||
* Get whether (prefers-contrast: more) should match
|
||||
*
|
||||
* @return {boolean} - whether (prefers-contrast: more) should match
|
||||
*/
|
||||
getPrefersContrastMore: () => ipcRenderer.sendSync('app:prefersContrastMore:get'),
|
||||
/**
|
||||
* Subscribe on (prefers-contrast: more) change
|
||||
*
|
||||
* @param {(value: boolean) => void} callback - Callback to be called when (prefers-contrast: more) changes
|
||||
* @return {() => void} - Unsubscribe
|
||||
*/
|
||||
onPrefersContrastMoreChange: (callback) => {
|
||||
const handler = (event, value) => callback(value)
|
||||
ipcRenderer.on('app:prefersContrastMore:change', handler)
|
||||
return () => ipcRenderer.removeListener('app:prefersContrastMore:change', handler)
|
||||
},
|
||||
/**
|
||||
* Get system theme
|
||||
*
|
||||
* @return {Promise<'light' | 'dark'>} - System theme
|
||||
*/
|
||||
getSystemTheme: () => ipcRenderer.sendSync('app:getSystemTheme'),
|
||||
/**
|
||||
* Enable web request intercepting
|
||||
*
|
||||
|
||||
@@ -8,10 +8,11 @@ import { isRTL, register, setLanguage, setLocale } from '@nextcloud/l10n'
|
||||
import { appData } from '../app/AppData.js'
|
||||
import { refetchAppData } from '../app/appData.service.js'
|
||||
import { TITLE_BAR_HEIGHT } from '../constants.js'
|
||||
import { initAppConfig } from './appConfig.service.ts'
|
||||
import { getAppConfigValue, initAppConfig } from './appConfig.service.ts'
|
||||
import { BUILD_CONFIG } from './build.config.ts'
|
||||
import { initGlobals } from './globals/globals.js'
|
||||
import { setupInitialState } from './initialState.service.js'
|
||||
import { setTheming } from './theme.utils.ts'
|
||||
|
||||
/**
|
||||
* @param {string} lang - language code, TS type: `${lang}_${countryCode}`|`${lang}`
|
||||
@@ -258,6 +259,11 @@ export async function setupWebPage() {
|
||||
appData.fromJSON(await window.TALK_DESKTOP.getAppData())
|
||||
await initAppConfig()
|
||||
await applyL10n()
|
||||
setTheming({
|
||||
colorScheme: getAppConfigValue('theme'),
|
||||
highContrast: getAppConfigValue('highContrast'),
|
||||
openDyslexic: getAppConfigValue('dyslexicFont'),
|
||||
})
|
||||
applyInitialState()
|
||||
initGlobals()
|
||||
applyUserData()
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
export function prefersDark() {
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {'light'|'dark'|'default'} theme - Theme, corresponds Nextcloud theme names
|
||||
*/
|
||||
export function applyBodyThemeAttrs(theme = 'default') {
|
||||
delete document.body.dataset.themeLight
|
||||
delete document.body.dataset.themeDark
|
||||
document.body.dataset[`theme${theme[0].toUpperCase() + theme.substring(1)}`] = ''
|
||||
document.body.dataset.themes = theme
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*!
|
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
type NextcloudTheme = 'light' | 'dark' | 'light-highcontrast' | 'dark-highcontrast' | 'opendyslexic'
|
||||
|
||||
/**
|
||||
* Set application theming
|
||||
*
|
||||
* @param options - Theming options
|
||||
* @param options.colorScheme - Color scheme (dark mode)
|
||||
* @param options.highContrast - Whether to follow the system settings, enable (enforce) or disable (suppress) high contrast theme
|
||||
* @param options.defaultColorScheme - System default color scheme to avoid matchMedia call, used to choose concrete high contrast theme
|
||||
* @param options.openDyslexic - Whether to enable a dyslexic font (OpenDyslexic) for better readability
|
||||
*/
|
||||
export function setTheming({
|
||||
colorScheme = 'default',
|
||||
highContrast = 'default',
|
||||
openDyslexic = false,
|
||||
defaultColorScheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light',
|
||||
}: {
|
||||
colorScheme: 'default' | 'light' | 'dark'
|
||||
highContrast: 'default' | 'enabled' | 'disabled'
|
||||
openDyslexic: boolean
|
||||
defaultColorScheme?: 'light' | 'dark'
|
||||
}) {
|
||||
const themes: Set<NextcloudTheme> = new Set()
|
||||
|
||||
if (highContrast === 'default') {
|
||||
// Normal contrast themes
|
||||
if (colorScheme !== 'default') {
|
||||
themes.add(colorScheme)
|
||||
}
|
||||
} else {
|
||||
// High contrast themes
|
||||
const currentColorScheme = colorScheme === 'default' ? defaultColorScheme : colorScheme
|
||||
|
||||
if (highContrast === 'enabled') {
|
||||
// Unlike the color scheme theme, high contrast themes require explicit light/dark setting
|
||||
themes.add(`${currentColorScheme}-highcontrast`)
|
||||
} else if (highContrast === 'disabled' && colorScheme === 'default') {
|
||||
// To suppress the system high contrast theme, base theme must be enabled to override the default
|
||||
// This will override the default themes that follows the system setting
|
||||
themes.add(currentColorScheme)
|
||||
}
|
||||
}
|
||||
|
||||
if (openDyslexic) {
|
||||
themes.add('opendyslexic')
|
||||
}
|
||||
|
||||
applyThemes([...themes])
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply Nextcloud themes on the page
|
||||
*
|
||||
* @param themes - Themes list
|
||||
*/
|
||||
function applyThemes(themes: NextcloudTheme[]) {
|
||||
const currentThemeAttrs = document.body.getAttributeNames().filter((attr) => attr.startsWith('data-theme-'))
|
||||
const newThemeAttrs = themes.map((theme) => `data-theme-${theme}`)
|
||||
|
||||
for (const attr of currentThemeAttrs) {
|
||||
if (!newThemeAttrs.includes(attr)) {
|
||||
document.body.removeAttribute(attr)
|
||||
}
|
||||
}
|
||||
|
||||
for (const attr of newThemeAttrs) {
|
||||
document.body.setAttribute(attr, '')
|
||||
}
|
||||
|
||||
document.body.setAttribute('data-themes', themes.join(','))
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*!
|
||||
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { onScopeDispose, readonly, ref } from 'vue'
|
||||
|
||||
/**
|
||||
* Reactive matchMedia query result. Cheap version of vueuse/core/useMediaQuery
|
||||
*
|
||||
* @param query - Media query to match
|
||||
* @return - Whether the media query matches
|
||||
*/
|
||||
export function useMatchMedia(query: string) {
|
||||
const mediaQueryList = window.matchMedia(query)
|
||||
const matches = ref(mediaQueryList.matches)
|
||||
|
||||
const onChange = () => {
|
||||
matches.value = mediaQueryList.matches
|
||||
}
|
||||
|
||||
mediaQueryList.addEventListener('change', onChange)
|
||||
|
||||
onScopeDispose(() => {
|
||||
mediaQueryList.removeEventListener('change', onChange)
|
||||
})
|
||||
|
||||
return readonly(matches)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*!
|
||||
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { createSharedComposable } from '@vueuse/core'
|
||||
import { onScopeDispose, ref } from 'vue'
|
||||
|
||||
/**
|
||||
* Get reactive (prefers-contrast: more) match media equivalent from Electron API
|
||||
* Unlike `prefers-color-scheme`, Electron does not set `prefers-contrast` according to the system settings ...
|
||||
*/
|
||||
export const usePrefersContrastMore = createSharedComposable(() => {
|
||||
const prefersContrastMore = ref(window.TALK_DESKTOP.getPrefersContrastMore())
|
||||
|
||||
const unsubscribe = window.TALK_DESKTOP.onPrefersContrastMoreChange((value: boolean) => {
|
||||
prefersContrastMore.value = value
|
||||
})
|
||||
|
||||
onScopeDispose(unsubscribe)
|
||||
|
||||
return prefersContrastMore
|
||||
})
|
||||
@@ -5,21 +5,19 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { t } from '@nextcloud/l10n'
|
||||
import { useMediaQuery } from '@vueuse/core'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import NcFormBox from '@nextcloud/vue/components/NcFormBox'
|
||||
import NcFormBoxSwitch from '@nextcloud/vue/components/NcFormBoxSwitch'
|
||||
import NcFormGroup from '@nextcloud/vue/components/NcFormGroup'
|
||||
import NcRadioGroup from '@nextcloud/vue/components/NcRadioGroup'
|
||||
import NcRadioGroupButton from '@nextcloud/vue/components/NcRadioGroupButton'
|
||||
import IconThemeLightDark from 'vue-material-design-icons/ThemeLightDark.vue'
|
||||
import IconWeatherNight from 'vue-material-design-icons/WeatherNight.vue'
|
||||
import IconWeatherSunny from 'vue-material-design-icons/WeatherSunny.vue'
|
||||
import DesktopSettingsSectionRelaunchNote from './components/DesktopSettingsSectionRelaunchNote.vue'
|
||||
import UiFormBoxAudioOutput from './components/UiFormBoxAudioOutput.vue'
|
||||
import UiFormBoxSelectNative from './components/UiFormBoxSelectNative.vue'
|
||||
import UiFormGroupZoom from './components/UiFormGroupZoom.vue'
|
||||
import { usePrefersContrastMore } from '../../../shared/usePrefersContrastMore.ts'
|
||||
import { useAppConfigStore } from './appConfig.store.ts'
|
||||
import { useAppConfigValue } from './useAppConfigValue.ts'
|
||||
import { useTristateToggle } from './useTristateToggle.ts'
|
||||
|
||||
const isLinux = window.systemInfo.isLinux
|
||||
|
||||
@@ -27,7 +25,22 @@ const { isRelaunchRequired } = storeToRefs(useAppConfigStore())
|
||||
|
||||
const launchAtStartup = useAppConfigValue('launchAtStartup')
|
||||
const launchAtStartupInBackground = useAppConfigValue('launchAtStartupInBackground')
|
||||
const theme = useAppConfigValue('theme')
|
||||
|
||||
const themeToggle = useTristateToggle(
|
||||
useAppConfigValue('theme'),
|
||||
// prefers-color-scheme cannot be used here because its value it overridden by Electron on theme change
|
||||
() => window.TALK_DESKTOP.getSystemTheme() === 'dark',
|
||||
['default', 'dark', 'light'],
|
||||
)
|
||||
|
||||
const highContrastToggle = useTristateToggle(
|
||||
useAppConfigValue('highContrast'),
|
||||
usePrefersContrastMore(),
|
||||
['default', 'enabled', 'disabled'],
|
||||
)
|
||||
|
||||
const dyslexicFont = useAppConfigValue('dyslexicFont')
|
||||
|
||||
const systemTitleBar = useAppConfigValue('systemTitleBar')
|
||||
const monochromeTrayIcon = useAppConfigValue('monochromeTrayIcon')
|
||||
const zoomFactor = useAppConfigValue('zoomFactor')
|
||||
@@ -54,31 +67,33 @@ const secondarySpeakerDevice = useAppConfigValue('secondarySpeakerDevice')
|
||||
<NcFormBoxSwitch v-if="launchAtStartup" v-model="launchAtStartupInBackground" :label="t('talk_desktop', 'Launch in background')" />
|
||||
</NcFormBox>
|
||||
|
||||
<NcRadioGroup v-model="theme" :label="t('talk_desktop', 'Theme')">
|
||||
<NcRadioGroupButton :label="t('talk_desktop', 'System default')" value="default">
|
||||
<template #icon>
|
||||
<IconThemeLightDark :size="20" />
|
||||
</template>
|
||||
</NcRadioGroupButton>
|
||||
<NcRadioGroupButton :label="t('talk_desktop', 'Light')" value="light">
|
||||
<template #icon>
|
||||
<IconWeatherSunny :size="20" />
|
||||
</template>
|
||||
</NcRadioGroupButton>
|
||||
<NcRadioGroupButton :label="t('talk_desktop', 'Dark')" value="dark">
|
||||
<template #icon>
|
||||
<IconWeatherNight :size="20" />
|
||||
</template>
|
||||
</NcRadioGroupButton>
|
||||
</NcRadioGroup>
|
||||
|
||||
<NcFormGroup :label="t('talk_desktop', 'Appearance')">
|
||||
<NcFormBox>
|
||||
<NcFormBoxSwitch
|
||||
v-model="themeToggle"
|
||||
:label="t('talk_desktop', 'Dark mode')" />
|
||||
</NcFormBox>
|
||||
</NcFormGroup>
|
||||
|
||||
<NcFormGroup :label="t('talk_desktop', 'System integration')">
|
||||
<NcFormBox>
|
||||
<NcFormBoxSwitch v-model="monochromeTrayIcon" :label="t('talk_desktop', 'Use monochrome tray icon')" />
|
||||
<NcFormBoxSwitch v-model="systemTitleBar" :label="t('talk_desktop', 'Use system title bar')" />
|
||||
</NcFormBox>
|
||||
</NcFormGroup>
|
||||
|
||||
<NcFormGroup :label="t('talk_desktop', 'Accessibility')">
|
||||
<NcFormBox>
|
||||
<NcFormBoxSwitch
|
||||
v-model="highContrastToggle"
|
||||
:label="t('talk_desktop', 'High contrast')" />
|
||||
<NcFormBoxSwitch
|
||||
v-model="dyslexicFont"
|
||||
:label="t('talk_desktop', 'Dyslexia font')"
|
||||
:description="t('talk_desktop', 'Use OpenDyslexic font, created to help with some symptoms of dyslexia')" />
|
||||
</NcFormBox>
|
||||
</NcFormGroup>
|
||||
|
||||
<UiFormGroupZoom v-model="zoomFactor" />
|
||||
|
||||
<NcFormGroup :label="t('talk_desktop', 'Notifications & Sounds')">
|
||||
|
||||
@@ -7,8 +7,11 @@ import type { Ref } from 'vue'
|
||||
import type { AppConfig, AppConfigKey } from '../../../app/AppConfig.ts'
|
||||
|
||||
import { defineStore } from 'pinia'
|
||||
import { readonly, ref, watch } from 'vue'
|
||||
import { readonly, ref, watch, watchEffect } from 'vue'
|
||||
import { getAppConfig } from '../../../shared/appConfig.service.ts'
|
||||
import { setTheming } from '../../../shared/theme.utils.ts'
|
||||
import { useMatchMedia } from '../../../shared/useMatchMedia.ts'
|
||||
import { usePrefersContrastMore } from '../../../shared/usePrefersContrastMore.ts'
|
||||
|
||||
export const useAppConfigStore = defineStore('appConfig', () => {
|
||||
const appConfig: Ref<AppConfig> = ref(getAppConfig())
|
||||
@@ -27,6 +30,21 @@ export const useAppConfigStore = defineStore('appConfig', () => {
|
||||
},
|
||||
)
|
||||
|
||||
const prefersDark = useMatchMedia('(prefers-color-scheme: dark)')
|
||||
const prefersContrastMore = usePrefersContrastMore()
|
||||
|
||||
watchEffect(() => {
|
||||
setTheming({
|
||||
colorScheme: appConfig.value.theme,
|
||||
// Because (prefers-contrast: more) is not set in Electron, default value must be resolved manually
|
||||
highContrast: appConfig.value.highContrast === 'default'
|
||||
? prefersContrastMore.value ? 'enabled' : 'disabled'
|
||||
: appConfig.value.highContrast,
|
||||
openDyslexic: appConfig.value.dyslexicFont,
|
||||
defaultColorScheme: prefersDark.value ? 'dark' : 'light',
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* Get an application config value
|
||||
*
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*!
|
||||
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import type { MaybeRefOrGetter, Ref } from 'vue'
|
||||
|
||||
import { computed, toValue } from 'vue'
|
||||
|
||||
/**
|
||||
* Use a toggle for a tristate value with a "default" or "auto-resolved" option:
|
||||
* - For the end user this is a boolean toggle - either enabled or disabled
|
||||
* - Internally it is tristate value with a third value meaning "default" or some externally resolved, auto option
|
||||
* - Internal value is only non-default when a user explicitly toggle from the auto-resolved default to customize, override the default
|
||||
*
|
||||
* This allows providing a user with a simple "change the default" toggle while keeping the internal complexity with the "default" hidden.
|
||||
*
|
||||
* An example with "high contrast mode":
|
||||
* - By default any app must just follow the system settings (default)
|
||||
* - When everything is good, a user does not go to settings to see that "Follow system" option was chosen
|
||||
* - The user only goes to the setting when the default is wrong and must be TOGGLED. "It is high contrast - the user wants no high contrast".
|
||||
* - When the user changes from a specific fixed value to the value corresponding the system, it very likely can be considered as "system default".
|
||||
* - The user does not need to know the internal complexity of 3 states with a technical "system default" value
|
||||
* - A toggle can be simple, even just an icon, only switch while a tristate requires a radio group with labels
|
||||
*
|
||||
* @param tristate - Internal tristate value
|
||||
* @param defaultToggleValue - The toggle value reflecting the default
|
||||
* @param tristateOptions - Tristate options in the order of [default, true, false]
|
||||
* @param tristateOptions.0 - Tristate option meaning "default", e.g. "default", "auto" or "system"
|
||||
* @param tristateOptions.1 - Tristate option meaning "true" for the toggle, e.g. "enabled" or "on"
|
||||
* @param tristateOptions.2 - Tristate option meaning "false" for the toggle, e.g. "disabled" or "off"
|
||||
* @return Boolean toggle value as a proxy to the tristate value
|
||||
*
|
||||
* @see https://lea.verou.me/blog/2026/dark-mode-toggles/
|
||||
*/
|
||||
export function useTristateToggle<DefaultOption extends string, TrueOption extends string, FalseOption extends string>(
|
||||
tristate: Ref<DefaultOption | TrueOption | FalseOption>,
|
||||
defaultToggleValue: MaybeRefOrGetter<boolean>,
|
||||
[defaultOption, trueOption, falseOption]: [DefaultOption, TrueOption, FalseOption],
|
||||
) {
|
||||
return computed<boolean>({
|
||||
get: () => {
|
||||
return tristate.value === defaultOption
|
||||
? toValue(defaultToggleValue)
|
||||
: tristate.value === trueOption
|
||||
},
|
||||
set: (toggleValue: boolean) => {
|
||||
tristate.value = toggleValue === toValue(defaultToggleValue)
|
||||
? defaultOption
|
||||
: (toggleValue ? trueOption : falseOption)
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user