Scopes
invokey’s Scopes feature lets you manage keyboard shortcuts by context. By grouping shortcuts into named contexts, you can enable or disable them based on the current UI state, preventing conflicts and improving user experience.
ScopeProvider Component
Section titled “ScopeProvider Component”The ScopeProvider
component is the main way to manage scopes in your application. It wraps a section of your UI and defines which scopes are active within that section.
Basic Usage
Section titled “Basic Usage”<script lang="ts"> import { ScopeProvider } from '@invokey/svelte'</script>
<ScopeProvider activeScopes={['editor', 'grid']}> <!-- Your app content --></ScopeProvider>
Prop | Type | Default | Description |
---|---|---|---|
children | Snippet | Required | Content to render within the scope context |
activeScopes | string[] | ['*'] | Array of active scope names. '*' represents global scope |
Global Scope
Section titled “Global Scope”The '*'
scope is special and represents the global scope. When this scope is active, all shortcuts will work regardless of their scope. By default, the global scope is always active.
To disable the global scope, you need to explicitly set the scopes you want to be active:
<ScopeProvider activeScopes={['editor']}> <!-- Only editor shortcuts will work here --></ScopeProvider>
Examples
Section titled “Examples”1. Basic Scope Usage
Section titled “1. Basic Scope Usage”The simplest use case with a single scope:
<script lang="ts"> import { ScopeProvider, Combination } from '@invokey/svelte'</script>
<ScopeProvider activeScopes={['editor']}> <Combination combinations="ctrl+s" scope="editor"> <span>Save document</span> </Combination></ScopeProvider>
2. Multiple Scopes
Section titled “2. Multiple Scopes”Handle multiple active scopes:
<script lang="ts"> import { ScopeProvider, Combination } from '@invokey/svelte'</script>
<ScopeProvider activeScopes={['editor', 'grid']}> <Combination combinations="ctrl+s" scope="editor"> <span>Save document</span> </Combination>
<Combination combinations="ctrl+c" scope="grid"> <span>Copy selected cells</span> </Combination></ScopeProvider>
3. Global and Specific Scopes
Section titled “3. Global and Specific Scopes”Mix global and specific scopes:
<script lang="ts"> import { ScopeProvider, Combination } from '@invokey/svelte'</script>
<ScopeProvider activeScopes={['*', 'editor']}> <Combination combinations="ctrl+shift+p" scope="*"> <span>Command palette (works everywhere)</span> </Combination>
<Combination combinations="ctrl+s" scope="editor"> <span>Save document (works in editor)</span> </Combination></ScopeProvider>
4. Dynamic Scopes
Section titled “4. Dynamic Scopes”Change active scopes based on application state:
<script lang="ts"> import { ScopeProvider, Combination } from '@invokey/svelte'
let isEditing = $state(true);</script>
<button on:click={() => (isEditing = !isEditing)}> {`Go to ${isEditing ? 'View' : 'Edit'} mode`}</button><ScopeProvider activeScopes={isEditing ? ['editor'] : ['viewer']}> {#if isEditing} <Combination combinations="ctrl+s" scope="editor"> <span>Changes saved</span> </Combination> {:else} <Combination combinations="ctrl+s" scope="viewer"> <span>Preview saved</span> </Combination> {/if}</ScopeProvider>
Best Practices
Section titled “Best Practices”- Use Descriptive Scope Names: Choose clear, meaningful names for your scopes (e.g., ‘editor’, ‘grid’, ‘preview’).
- Keep Global Scope Minimal: Only put truly global shortcuts in the ’*’ scope.
- Consider User Context: Group shortcuts logically based on user context and workflow.
- Document Scope Hierarchy: Make sure your scope hierarchy is well-documented for other developers.
- Handle Scope Transitions: Consider what happens to shortcuts during scope transitions.
- Test Scope Isolation: Ensure shortcuts don’t leak between scopes unintentionally.
- Use Consistent Naming: Follow a consistent naming convention for your scopes.
- Consider Accessibility: Make sure scope changes don’t affect accessibility features.
- Handle Edge Cases: Consider what happens when multiple scopes are active.
- Clean Up Resources: Disable scopes when components are unmounted to prevent memory leaks.