Skip to content

Commit

Permalink
scroll horizontally with the mouse.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Sep 17, 2023
1 parent 06ce593 commit 8fd5b19
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
<script>
<script lang="ts">
import { tabs } from '$lib/state';
import TabElement from '$components/TabElement.svelte';
import Searchbar from '$components/Searchbar.svelte';
import { scrollTabIntoView } from '$lib/utils';
import '../app.postcss';
import { onMount } from 'svelte';
let dragging = false;
let startX: number;
let scrollLeft: number;
onMount(() => {
const slider = document.getElementById('panel');
document.addEventListener('mousedown', onMouseDown);
document.addEventListener('mouseup', onMouseUp);
document.addEventListener('mousemove', onMouseMove);
return () => {
document.removeEventListener('mousedown', onMouseDown);
document.removeEventListener('mouseup', onMouseUp);
document.removeEventListener('mousemove', onMouseMove);
};
function onMouseDown(ev: MouseEvent) {
if (!slider) return;
if (
ev.target instanceof HTMLInputElement ||
ev.target instanceof HTMLTextAreaElement ||
ev.target instanceof HTMLButtonElement
)
return;
ev.preventDefault();
dragging = true;
startX = ev.clientX - slider.offsetLeft;
scrollLeft = slider.scrollLeft;
}
function onMouseUp(ev: MouseEvent) {
if (dragging) {
ev.preventDefault();
ev.stopPropagation();
}
dragging = false;
}
function onMouseMove(ev: MouseEvent) {
if (!slider) return;
if (!dragging) return;
ev.preventDefault();
slider.scrollLeft = scrollLeft + startX - ev.clientX;
}
});
</script>

<svelte:head>
<title>wikistr</title>
</svelte:head>

<div class="flex overflow-x-scroll pb-2 scroll-smooth">
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-static-element-interactions -->
<div class="flex overflow-x-scroll pb-2" id="panel" draggable="false">
{#each $tabs as tab (tab.id)}
<TabElement {tab} />
{/each}
Expand Down

0 comments on commit 8fd5b19

Please sign in to comment.