Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type { MaybeArray } from '$lib/utils.js';
import type { Attachment } from 'svelte/attachments';
import { ensureArray, overrideStyle, switchValue } from '$lib/utils.js';
type Axis = 'horizontal' | 'vertical';
type Direction = 'up' | 'left' | 'right' | 'down';
/**
* Calls the callback when an upward swipe is detected on the attached node.
*/
export function onswipe(
/** Swipe directions to react to */
direction: MaybeArray<Direction | 'horizontal' | 'vertical' | 'any'>,
callback: (data: {
/** The HTML element onSwipe has been attached to */
element: HTMLElement;
/** The direction of the swipe */
direction: Direction;
/** horizontal if direction is left or right, vertical otherwise */
axis: Axis;
/** deltaX (positive if right) or deltaY (positive if down) */
distance: number;
}) => void,
{
minDistance = {},
}: {
/** Use a number directly to specify the same value on both axes. Minimum distance to accept the swipe. If the number is <1, it's interpreted as a %age of the elements width/height (depending on the swiped direction's axis), if it's >=1 it's interpreted as a pixel length. Defaults to 30 on each axis. */
minDistance?: number | Partial<Record<Axis, number>>;
} = {}
): Attachment<HTMLElement> {
const thresholds = {
vertical: typeof minDistance === 'number' ? minDistance : (minDistance.vertical ?? 30),
horizontal: typeof minDistance === 'number' ? minDistance : (minDistance.horizontal ?? 30),
};
const acceptedDirections = new Set<Direction>(
ensureArray(direction)
.map((dir) =>
switchValue(dir, {
up: ['up'],
down: ['down'],
left: ['left'],
right: ['right'],
horizontal: ['left', 'right'],
vertical: ['up', 'down'],
any: ['up', 'down', 'left', 'right'],
})
)
.flat()
);
return (element) => {
const cleanupOverscrollOverride = overrideStyle(
'body, html',
'overscroll-behavior-x',
'none'
);
const { width, height } = element.getBoundingClientRect();
Iif (thresholds.horizontal < 1) thresholds.horizontal *= width;
Iif (thresholds.vertical < 1) thresholds.horizontal *= height;
let touchStartX: number | undefined;
let touchStartY: number | undefined;
const onTouchStart = (event: TouchEvent) => {
const touch = event.touches[0];
Iif (!touch) return;
touchStartX = touch.clientX;
touchStartY = touch.clientY;
};
const onTouchEnd = (event: TouchEvent) => {
Iif (touchStartX === undefined || touchStartY === undefined) return;
const touch = event.changedTouches[0];
Iif (!touch) return;
const deltaX = touch.clientX - touchStartX;
const deltaY = touch.clientY - touchStartY;
touchStartX = undefined;
touchStartY = undefined;
const mostlyVertical = Math.abs(deltaY) > Math.abs(deltaX);
const mostlyHorizontal = Math.abs(deltaX) > Math.abs(deltaY);
const axis = mostlyVertical ? 'vertical' : mostlyHorizontal ? 'horizontal' : undefined;
Iif (!axis) return;
const threshold = thresholds[axis];
const distance = switchValue(axis, {
vertical: deltaY,
horizontal: deltaX,
});
const direction = switchValue(axis, {
vertical: distance > 0 ? 'down' : 'up',
horizontal: distance > 0 ? 'right' : 'left',
});
Iif (Math.abs(distance) < threshold) {
console.debug(
`[onSwipe] ignored swipe ${direction} because of distance threshold: |${distance}| < ${threshold}`,
minDistance
);
return;
}
Iif (!acceptedDirections.has(direction)) return;
callback({ element, direction, axis, distance });
};
const onTouchCancel = () => {
touchStartX = undefined;
touchStartY = undefined;
};
element.addEventListener('touchstart', onTouchStart, { passive: true });
element.addEventListener('touchend', onTouchEnd, { passive: true });
element.addEventListener('touchcancel', onTouchCancel, { passive: true });
return () => {
element.removeEventListener('touchstart', onTouchStart);
element.removeEventListener('touchend', onTouchEnd);
element.removeEventListener('touchcancel', onTouchCancel);
cleanupOverscrollOverride();
};
};
}
if (import.meta.vitest) {
const { describe, it, expect, vi } = import.meta.vitest;
describe('onswipe', () => {
it('should call callback on upward swipe', () => {
const callback = vi.fn();
const node = document.createElement('div');
const detach = onswipe('up', callback)(node);
// Simulate touch events
const touchStartEvent = new TouchEvent('touchstart', {
touches: [{ identifier: 0, target: node, clientX: 100, clientY: 100 }],
});
const touchEndEvent = new TouchEvent('touchend', {
changedTouches: [{ identifier: 0, target: node, clientX: 100, clientY: 50 }],
});
node.dispatchEvent(touchStartEvent);
node.dispatchEvent(touchEndEvent);
expect(callback).toHaveBeenCalled();
detach();
});
});
}
|