From 845f85a072598f47c7afe10c4e5ebcc480450113 Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Sat, 2 Nov 2024 14:29:33 +0100 Subject: [PATCH] fix(useKbd): hydration issue Related to #2494 --- playground/app/pages/components/kbd.vue | 2 +- src/runtime/composables/useKbd.ts | 32 ++++++++++--------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/playground/app/pages/components/kbd.vue b/playground/app/pages/components/kbd.vue index b780353d..8eeac651 100644 --- a/playground/app/pages/components/kbd.vue +++ b/playground/app/pages/components/kbd.vue @@ -18,7 +18,7 @@ const kbdKeys = Object.keys(kbdKeysMap)
-
+
diff --git a/src/runtime/composables/useKbd.ts b/src/runtime/composables/useKbd.ts index ab1173d4..68bdab10 100644 --- a/src/runtime/composables/useKbd.ts +++ b/src/runtime/composables/useKbd.ts @@ -1,7 +1,7 @@ +import { reactive, computed, onMounted } from 'vue' import { createSharedComposable } from '@vueuse/core' -import { ref, computed, onMounted } from 'vue' -type KbdSpecificMap = { +type KbdKeysSpecificMap = { meta: string alt: string ctrl: string @@ -31,29 +31,22 @@ export const kbdKeysMap = { end: '↘' } -export const kbdKeysSpecificMap: Record<'macOS' | 'other', KbdSpecificMap> = { - macOS: { - meta: kbdKeysMap.command, - alt: kbdKeysMap.option, - ctrl: '⌃' - }, - other: { - meta: kbdKeysMap.win, - alt: 'alt', - ctrl: 'ctrl' - } -} - export type KbdKey = keyof typeof kbdKeysMap -export type KbdKeySpecific = keyof KbdSpecificMap +export type KbdKeySpecific = keyof KbdKeysSpecificMap const _useKbd = () => { const macOS = computed(() => import.meta.client && navigator && navigator.userAgent && navigator.userAgent.match(/Macintosh;/)) - const metaSymbol = ref(' ') + const kbdKeysSpecificMap = reactive({ + meta: ' ', + alt: ' ', + ctrl: ' ' + }) onMounted(() => { - metaSymbol.value = getKbdKey('meta')! + kbdKeysSpecificMap.meta = macOS.value ? kbdKeysMap.command : kbdKeysMap.win + kbdKeysSpecificMap.alt = macOS.value ? kbdKeysMap.option : 'alt' + kbdKeysSpecificMap.ctrl = macOS.value ? '⌃' : 'ctrl' }) function getKbdKey(value?: KbdKey | string) { @@ -62,7 +55,7 @@ const _useKbd = () => { } if (['meta', 'alt', 'ctrl'].includes(value)) { - return kbdKeysSpecificMap[macOS.value ? 'macOS' : 'other'][value as KbdKeySpecific] + return kbdKeysSpecificMap[value as KbdKeySpecific] } return kbdKeysMap[value as KbdKey] || value.toUpperCase() @@ -70,7 +63,6 @@ const _useKbd = () => { return { macOS, - metaSymbol, getKbdKey } }