docs(shortcuts): add config details (#155)

This commit is contained in:
Sylvain Marroufin
2023-05-16 12:48:10 +02:00
committed by GitHub
parent 10f3c0c271
commit c78d5ab0d3

View File

@@ -48,6 +48,65 @@ defineShortcuts({
</script>
```
### `usingInput`
Prop: `usingInput?: string | boolean`
By default, `usingInput` is `false`, meaning it will only trigger when no input is focused. When set to `true`, the shortcut will also trigger when any input is focused.
For a more advanced behavior, `usingInput` can be set to the name of an input, so it only triggers when focusing this specific input.
```vue
<template>
<UInput v-model="query" name="queryInput" />
</template>
<script setup lang="ts">
const query = ref('')
defineShortcuts({
enter: {
usingInput: 'queryInput',
handler: () => {
// TODO
}
}
})
</script>
```
_`enter` shortcut will only trigger when `queryInput` is focused._
### `whenever`
Prop: `whenever?: WatchSource<Boolean>[]`
`whenever` allows to add constraints on the shortcut triggering behavior. This array can contain `Ref<Boolean>`, `ComputedRef<Boolean>` or `() => Boolean`.
```ts
defineShortcuts({
meta_k: {
usingInput: true,
handler: () => { isOpen.value = !isOpen.value }
},
escape: {
usingInput: true,
whenever: [isOpen],
handler: () => { isOpen.value = false }
}
})
```
_`escape` shortcut will only trigger when `isOpen` is `true`._
### Simple shortcut
In case the shortcut does not need any config, it can be written as a function.
```ts
defineShortcuts({
'?': () => openHelpModal()
})
```
## `useShortcuts`
To display shortcuts in your app according to the user's OS, you can use the `useShortcuts` composable.
@@ -61,3 +120,4 @@ const { metaSymbol } = useShortcuts()
<UKbd>{{ metaSymbol }}</UKbd>
</template>
```
_`metaSymbol` will display either `⌘` on MacOS or `Ctrl` on any other OS_