-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add src/lib/NavPalette.svelte invoked with cmd+k for keyboard-only si…
…te navigation with new demo page showing how it's made bump @sveltejs/kit to ^1.9.2 to fix vitest
- Loading branch information
Showing
8 changed files
with
136 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<script lang="ts"> | ||
import { goto } from '$app/navigation' | ||
import { tick } from 'svelte/internal' | ||
import Select from '.' | ||
export let routes: string[] | { label: string; route: string }[] | ||
export let trigger: string = `k` | ||
let open = false | ||
let dialog: HTMLDialogElement | ||
let input: HTMLInputElement | ||
async function toggle(event: KeyboardEvent) { | ||
if (event.key === trigger && event.metaKey && !open) { | ||
// open on cmd+k | ||
open = true | ||
await tick() // wait for dialog to open and input to be mounted | ||
input?.focus() | ||
} else if (event.key === `Escape` && open) { | ||
// close on escape | ||
open = false | ||
} | ||
} | ||
function close_if_outside(event: MouseEvent) { | ||
if (open && !dialog?.contains(event.target as Node)) { | ||
open = false | ||
} | ||
} | ||
function move( | ||
event: CustomEvent<{ option: string | { label: string; route: string } }> | ||
) { | ||
const { option } = event.detail | ||
if (typeof option == `object`) goto(option.route) | ||
else goto(option) | ||
open = false | ||
} | ||
</script> | ||
|
||
<svelte:window on:keydown={toggle} on:click={close_if_outside} /> | ||
|
||
{#if open} | ||
<dialog class:open bind:this={dialog}> | ||
<Select | ||
options={routes} | ||
bind:input | ||
placeholder="Go to..." | ||
on:add={move} | ||
on:keydown={toggle} | ||
--sms-bg="var(--sms-options-bg)" | ||
--sms-width="min(20em, 90vw)" | ||
--sms-max-width="none" | ||
/> | ||
</dialog> | ||
{/if} | ||
|
||
<style> | ||
dialog { | ||
position: fixed; | ||
top: 30%; | ||
border: none; | ||
padding: 0; | ||
background-color: transparent; | ||
display: flex; | ||
color: white; | ||
z-index: 10; | ||
font-size: 2.4ex; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<script lang="ts"> | ||
export let data: PageServerData | ||
import hljs from 'highlight.js/lib/common' | ||
import 'highlight.js/styles/vs2015.css' | ||
import nav_palette from '$lib/NavPalette.svelte?raw' | ||
</script> | ||
|
||
## Nav Palette | ||
|
||
You can use `<MultiSelect />` to build a navigation palette in just 70 lines of code (40 without styles). | ||
|
||
```svelte stackblitz id="disabled-input-title" | ||
<script> | ||
import { NavPalette } from '$lib' | ||
|
||
const routes = Object.keys(import.meta.glob(`./**/+page.{svx,svelte,md}`)).map( | ||
(filename) => { | ||
const parts = filename.split(`/`).filter((part) => !part.startsWith(`(`)) // remove hidden route segments | ||
return `/${parts.slice(1, -1).join(`/`)}` | ||
} | ||
) | ||
</script> | ||
|
||
<NavPalette {routes} /> | ||
``` | ||
|
||
Here's `<NavPalette />` component | ||
|
||
<pre><code>{@html hljs.highlight(nav_palette, { language: 'typescript' }).value}</code></pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters