Change how we detect touch events

This commit is contained in:
wires 2026-03-07 19:00:15 -05:00
parent bf2d0c968a
commit 417ec134c1
3 changed files with 123 additions and 13 deletions

View file

@ -145,20 +145,29 @@
/** Move the slider */
const move = (e: MouseEvent | TouchEvent) => {
if (sliding && imgOffset) {
const el =
e instanceof TouchEvent && e.touches ? e.touches[0] : (e as MouseEvent);
if (!sliding || !imgOffset) return;
let pointer: MouseEvent | Touch | undefined;
if ('touches' in e) {
pointer = e.touches[0] ?? e.changedTouches[0];
} else {
pointer = e;
}
if (!pointer) return;
const figureOffset =
figure ?
parseInt(window.getComputedStyle(figure).marginLeft.slice(0, -2))
: 0;
let x = el.pageX - figureOffset - imgOffset.left;
let x = pointer.pageX - figureOffset - imgOffset.left;
x =
x < handleMargin ? handleMargin
: x > w - handleMargin ? w - handleMargin
: x;
offset = x / w;
}
};
/** Starts the slider */

View file

@ -0,0 +1,42 @@
<script lang="ts">
import One from './One.svelte';
/* import ScrollerBase from './ScrollerBase.svelte';
import ScrollerDemo from './demo/ScrollerDemo.svelte'; */
import type { PageData } from '../../$types';
import {
EmbedPreviewerLink,
Theme,
} from '@reuters-graphics/graphics-components';
import App from '$lib/App.svelte';
import pkg from '$pkg';
import { dev } from '$app/environment';
import { assets } from '$app/paths';
import { page } from '$app/state';
// Styles
import '@reuters-graphics/graphics-components/scss/main.scss';
import '$lib/styles/global.scss';
let { data }: { data: PageData } = $props();
let content = $derived(data.content);
</script>
<Theme
base="dark"
theme={{
font: {
family: {
serif: 'Sentient',
'sans-serif': "'Atkinson Hyperlegible Next'",
hed: 'Sentient',
subhed: 'Sentient',
body: "'Atkinson Hyperlegible Next'",
note: "'Atkinson Hyperlegible Next'",
},
},
}}
>
<App {content} />
</Theme>
<!-- Only visible in dev! -->
<EmbedPreviewerLink {dev} />

View file

@ -0,0 +1,59 @@
<script lang="ts">
import { ScrollerBase } from '@reuters-graphics/graphics-components';
// Optional: Bind your own variables to use them in your code.
let count = $state(1);
let index = $state(0);
let offset = $state(0);
let progress = $state(0);
let top = $state(0.1);
let threshold = $state(0.5);
let bottom = $state(0.9);
</script>
<ScrollerBase
{top}
{threshold}
{bottom}
bind:count
bind:index
bind:offset
bind:progress
query="div.step-foreground-container"
>
{#snippet backgroundSnippet()}
<!-- Add custom background HTML or component -->
<p class="mb-0">
Current step: <strong>{index + 1}/{count}</strong>
</p>
<progress class="mb-4" value={(index + 1) / count}></progress>
<p class="mb-0">Offset in current step</p>
<progress class="mb-4" value={offset}></progress>
<p class="mb-0">Total progress</p>
<progress class="mb-4" value={progress}></progress>
{/snippet}
{#snippet foregroundSnippet()}
<!-- Add custom foreground HTML or component -->
<div class="step-foreground-container">Step 1</div>
<div class="step-foreground-container">Step 2</div>
<div class="step-foreground-container">Step 3</div>
<div class="step-foreground-container">Step 4</div>
<div class="step-foreground-container">Step 5</div>
{/snippet}
</ScrollerBase>
<style lang="scss">
@use '@reuters-graphics/graphics-components/dist/scss/mixins' as mixins;
.step-foreground-container {
height: 100vh;
width: 50%;
background-color: rgba(0, 0, 0, 0.2);
padding: 1em;
margin: 0 0 2em 0;
position: relative;
left: 50%;
}
</style>