scroll-snap
Version:
Snap page when user stops scrolling, with a customizable configuration and a consistent cross browser behaviour
89 lines (88 loc) • 3.68 kB
TypeScript
declare module "types" {
export interface ScrollSnapSettings {
snapDestinationX?: string | number;
snapDestinationY?: string | number;
timeout?: number;
duration?: number;
threshold?: number;
snapStop?: boolean;
easing?: (t: number) => number;
showArrows?: boolean;
enableKeyboard?: boolean;
}
export interface SnapLength {
value: number;
unit: string;
}
export interface SnapCoordinates {
y: SnapLength;
x: SnapLength;
}
export interface Coordinates {
y?: number;
x?: number;
}
export interface EventHandlers {
element: HTMLElement | Window;
event: string;
handler: EventListener;
}
}
declare module "animations" {
import { Coordinates } from "types";
export function smoothScrollAxis(obj: HTMLElement, end: Coordinates, axis: 'x' | 'y', easing: (t: number) => number, duration: number, animationFrame: {
x: number;
y: number;
}, callback: () => void): void;
export function smoothScroll(obj: HTMLElement, end: Coordinates, easing: (t: number) => number, duration: number, animationFrame: {
x: number;
y: number;
}, callback: () => void): void;
}
declare module "arrows" {
export function getBackgroundBrightness(element: HTMLElement): 'light' | 'dark';
export function createArrowElements(target: HTMLElement): {
up: HTMLDivElement;
down: HTMLDivElement;
left: HTMLDivElement;
right: HTMLDivElement;
};
export function updateArrowsPosition(element: HTMLElement, arrows: Record<string, HTMLElement>, arrowContainer: HTMLDivElement | null): void;
}
declare module "constants" {
export const TIMEOUT_MIN = 50;
export const TIMEOUT_DEFAULT = 100;
export const DURATION_DEFAULT = 300;
export const THRESHOLD_DEFAULT = 0.2;
export const SNAPSTOP_DEFAULT = false;
export const SHOW_ARROWS_DEFAULT = false;
export const ENABLE_KEYBOARD_DEFAULT = true;
export const NOOP: () => void;
export function easeInOutQuad(t: number): number;
}
declare module "handlers" {
import { EventHandlers } from "types";
export function addEventHandler(element: HTMLElement | Window, event: string, handler: EventListener, activeHandlers: EventHandlers[]): void;
export function cleanupEventHandlers(activeHandlers: EventHandlers[]): void;
export function handleKeydown(e: Event, target: HTMLElement, enableKeyboard: boolean, scrollToDirection: (direction: 'up' | 'down' | 'left' | 'right') => void): void;
}
declare module "utils" {
import { ScrollSnapSettings, SnapCoordinates, SnapLength } from "types";
export function parseSnapCoordinatesValue(x: ScrollSnapSettings['snapDestinationX'], y: ScrollSnapSettings['snapDestinationY']): SnapCoordinates;
export function getYSnapLength(obj: HTMLElement, declaration: SnapLength): number;
export function getXSnapLength(obj: HTMLElement, declaration: SnapLength): number;
export function stayInBounds(min: number, max: number, destined: number): number;
export function roundToNearestSnapPoint(value: number, snapLength: number): number;
}
declare module "validation" {
import { ScrollSnapSettings } from "types";
export function validateSettings(settings: ScrollSnapSettings): void;
}
declare module "index" {
import { ScrollSnapSettings } from "types";
export type { ScrollSnapSettings };
export default function createScrollSnap(element: HTMLElement, settings?: ScrollSnapSettings, callback?: () => void): {
bind: () => void;
unbind: () => void;
};
}