feat: add keyboard shortcuts to main page

This commit is contained in:
Xylight committed 2024-08-05 12:31:08 -07:00
1 parent 4e65bc1113
commit 5129cb36ae
3 files changed
+42 -5

No files matched your search

BIN
View File
Binary file not shown.
+10 -3
View File
@@ -84,7 +84,7 @@
<!-- svelte-ignore a11y-click-events-have-key-events -->
<Material
color={view != 'card' ? 'none' : 'distinct'}
class="post relative max-w-full min-w-0 w-full group cursor-pointer
class="post relative max-w-full min-w-0 w-full cursor-pointer outline-none group
{view != 'card' ? 'bg-transparent !border-0' : 'p-5'} {view == 'compact'
? 'py-3 list-type compact-view'
: view == 'list'
@@ -95,6 +95,11 @@
on:click={(e) => {
onClick(e)
}}
on:keydown={(e) => {
// @ts-ignore
if (e.key == 'Enter') onClick(e)
}}
tabindex="0"
style={$$props.style ?? ''}
>
<PostMeta
@@ -170,9 +175,11 @@
</div>
{/if}
<div
class="absolute overflow-hidden inset-0 group-hover:inset-y-0.5 group-hover:-inset-x-4 group-hover:sm:-inset-x-5
class="absolute overflow-hidden inset-0
sm:rounded-xl bg-slate-50 dark:bg-zinc-900
opacity-0 group-hover:opacity-100 transition-all -z-50 no-list-margin
opacity-0 transition-all -z-50 no-list-margin
group-hover:inset-y-0.5 group-hover:-inset-x-4 group-hover:sm:-inset-x-5 group-hover:opacity-100
group-focus:inset-y-0.5 group-focus:-inset-x-4 group-focus:sm:-inset-x-5 group-focus:opacity-100
duration-150"
/>
</Material>
@@ -38,6 +38,8 @@
import InfiniteScroll from 'svelte-infinite-scroll'
import type { Readable } from 'svelte/motion'
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
import { postLink } from '../helpers'
import { writable } from 'svelte/store'
export let posts: PostView[]
export let community: boolean = false
@@ -177,8 +179,37 @@
await tick()
$virtualizer.measure()
})
// keyboard handling
let selectedIndex = writable(-1)
function handleKeydown(event: KeyboardEvent) {
if (event.key === 'ArrowUp') {
event.preventDefault()
$selectedIndex = Math.max(0, $selectedIndex - 1)
} else if (event.key === 'ArrowDown') {
event.preventDefault()
$selectedIndex = Math.min(posts.length - 1, $selectedIndex + 1)
}
}
onMount(() => {
selectedIndex.subscribe(focusItem)
})
function focusItem(index: number) {
const el =
virtualItemEls.find(
(el) => Number(el?.getAttribute('data-index')) == index
) ?? virtualItemEls[1]
$virtualizer.scrollToIndex(index, { align: 'center', behavior: 'auto' })
el?.firstChild?.firstChild?.parentElement?.focus({})
}
</script>
<svelte:window on:keydown={handleKeydown} />
<ul
class="flex flex-col list-none {$userSettings.view == 'card'
? 'gap-3 md:gap-4'
@@ -223,8 +254,7 @@
<li
bind:this={virtualItemEls[index]}
data-index={row.index}
class="relative post-container"
in:fly={{ y: -16, easing: expoOut, duration: 500, opacity: 0 }}
class="relative post-container pop-in"
>
{#if posts[row.index]}
<Post