hn-hlw-app/hooks/useElder.js
2026-07-27 11:26:39 +08:00

47 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { computed, ref, watch } from 'vue';
const cssVar = {
'--text-xs': '20rpx',
'--text-sm': '24rpx',
'--text-base': '28rpx',
'--text-lg': '32rpx',
'--text-xl': '36rpx',
}
const elderCssVar = {
'--text-xs': '28rpx',
'--text-sm': '32rpx',
'--text-base': '42rpx',
'--text-lg': '48rpx',
'--text-xl': '52rpx',
}
export default function useElder() {
const elderMode = ref(String(uni.getStorageSync('elderMode')) === 'true');
const elderVarStyle = computed(() => getStyleString(elderMode.value ? elderCssVar : cssVar));
const elderClass = computed(() => elderMode.value ? 'caring-mode-active' : ''); // caring-mode 关爱模式避免和初版的elder-mode-active类名重复
function getStyleString(cssVarObject) {
return Object.entries(cssVarObject).map(([key, value]) => `${key}:${value}`).join(';');
}
function toggle() {
elderMode.value = !elderMode.value;
}
watch(elderMode, (newVal) => {
uni.setStorageSync('elderMode', newVal);
const app = getApp();
if (app && app.globalData) {
app.globalData.elderMode = elderMode.value;
}
}, { immediate: true });
return {
elderMode,
elderClass,
elderVarStyle,
toggle
};
};