Skip to content

Commit

Permalink
fix(useKbd): hydration issue
Browse files Browse the repository at this point in the history
Related to #2494
  • Loading branch information
benjamincanac committed Nov 2, 2024
1 parent 120eb92 commit 845f85a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion playground/app/pages/components/kbd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const kbdKeys = Object.keys(kbdKeysMap)
<div class="flex items-center gap-1">
<UKbd value="meta" variant="solid" />
</div>
<div class="flex items-center gap-1 ms-[-216px]">
<div class="flex items-center gap-1 ms-[-220px]">
<UKbd v-for="(kdbKey, index) in kbdKeys" :key="index" :value="kdbKey" />
</div>
<div class="flex items-center gap-1 ms-[-22px]">
Expand Down
32 changes: 12 additions & 20 deletions src/runtime/composables/useKbd.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -62,15 +55,14 @@ 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()
}

return {
macOS,
metaSymbol,
getKbdKey
}
}
Expand Down

0 comments on commit 845f85a

Please sign in to comment.