mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-23 16:30:45 +01:00
feat(useKbd): new composable (#73)
This commit is contained in:
53
src/runtime/composables/useKbd.ts
Normal file
53
src/runtime/composables/useKbd.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { createSharedComposable } from '@vueuse/core'
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
|
||||
export const kbdKeysMap = {
|
||||
meta: '',
|
||||
command: '⌘',
|
||||
shift: '⇧',
|
||||
ctrl: '⌃',
|
||||
option: '⌥',
|
||||
alt: '⎇',
|
||||
enter: '↵',
|
||||
delete: '⌦',
|
||||
backspace: '⌫',
|
||||
escape: '⎋',
|
||||
tab: '⇥',
|
||||
capslock: '⇪',
|
||||
arrowup: '↑',
|
||||
arrowright: '→',
|
||||
arrowdown: '↓',
|
||||
arrowleft: '←',
|
||||
pageup: '⇞',
|
||||
pagedown: '⇟',
|
||||
home: '↖',
|
||||
end: '↘'
|
||||
}
|
||||
|
||||
export type KbdKey = keyof typeof kbdKeysMap
|
||||
|
||||
const _useKbd = () => {
|
||||
const macOS = computed(() => import.meta.client && navigator && navigator.userAgent && navigator.userAgent.match(/Macintosh;/))
|
||||
|
||||
const metaSymbol = ref(' ')
|
||||
|
||||
onMounted(() => {
|
||||
metaSymbol.value = macOS.value ? kbdKeysMap.command : kbdKeysMap.ctrl
|
||||
})
|
||||
|
||||
function getKbdKey(value: KbdKey | string) {
|
||||
if (value === 'meta') {
|
||||
return metaSymbol.value
|
||||
}
|
||||
|
||||
return kbdKeysMap[value as KbdKey] || value.toUpperCase()
|
||||
}
|
||||
|
||||
return {
|
||||
macOS,
|
||||
metaSymbol,
|
||||
getKbdKey
|
||||
}
|
||||
}
|
||||
|
||||
export const useKbd = createSharedComposable(_useKbd)
|
||||
Reference in New Issue
Block a user