<script setup lang="ts">
import { TanStackDevtools } from '@tanstack/vue-devtools'
import {
HotkeysProvider,
useFormatForDisplay,
useHotkey,
useHotkeyScopesState,
} from '@tanstack/vue-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/vue-hotkeys-devtools'
import { ref } from 'vue'
import EnabledScopes from './EnabledScopes.vue'
import ExclusiveScopes from './ExclusiveScopes.vue'
import NestedScopes from './NestedScopes.vue'
import Palette from './Palette.vue'
const formatForDisplay = useFormatForDisplay()
const helpOpened = ref(0)
useHotkey('Alt+[KeyH]', () => helpOpened.value++)
const scopes = useHotkeyScopesState()
const list = (names: ReadonlyArray<string>) =>
names.length > 0 ? [...names].sort().join(', ') : 'none'
</script>
<template>
<div class="app">
<header>
<h1>Hotkey scopes</h1>
<p>
Each <code>HotkeysProvider</code> with a <code>scope</code> groups the
hotkeys inside it. They fire while the provider is mounted and enabled,
and no exclusive scope suspends them.
</p>
</header>
<main>
<section class="demo-section">
<h2>Global scope</h2>
<p>
<kbd>{{ formatForDisplay('Alt+[KeyH]') }}</kbd> opens help. It's in
<code>global</code>, which is always active. Only an exclusive scope
suspends it.
</p>
<p role="status">
Help opened {{ helpOpened }} {{ helpOpened === 1 ? 'time' : 'times' }}
</p>
</section>
<HotkeysProvider scope="palette">
<Palette />
</HotkeysProvider>
<HotkeysProvider scope="document">
<NestedScopes />
</HotkeysProvider>
<HotkeysProvider scope="mail">
<EnabledScopes />
</HotkeysProvider>
<HotkeysProvider scope="settings">
<ExclusiveScopes />
</HotkeysProvider>
<section class="demo-section">
<h2>Scope state</h2>
<div role="status">
<p>Active: {{ list(scopes.active) }}</p>
<p>Exclusive: {{ scopes.exclusive ?? 'none' }}</p>
<p>Allowed: {{ list(scopes.allowed) }}</p>
<p>Suspended: {{ list(scopes.suspended) }}</p>
</div>
</section>
</main>
</div>
<TanStackDevtools :plugins="[hotkeysDevtoolsPlugin()]" />
</template>