compute-scroll-into-view
Version:
The engine that powers scroll-into-view-if-needed
89 lines • 4.55 kB
TypeScript
/**
* Which scroll containers to scroll: https://drafts.csswg.org/cssom-view/#enumdef-scrollintoviewcontainer
*/
export type ScrollIntoViewContainer = "all" | "nearest";
export interface Options {
/**
* Control the scroll position in the block direction of the target's [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode).
* In `horizontal-tb` (the default) `block: 'start'` aligns to the top edge and `block: 'end'` to the bottom, in `vertical-rl` they align to the right and left edges.
* @default 'center'
*/
block?: ScrollLogicalPosition;
/**
* Control the scroll position in the inline direction of the target's [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode) and [direction](https://developer.mozilla.org/en-US/docs/Web/CSS/direction). In left-to-right text `inline: 'start'` aligns to the left edge, in right-to-left text to the right edge. Scroll containers that overflow to the left or top get negative `left` or `top` values, like `scrollLeft` in right-to-left scroll containers.
* @default 'nearest'
*/
inline?: ScrollLogicalPosition;
/**
* Like the [`container`](https://drafts.csswg.org/cssom-view/#dom-scrollintoviewoptions-container) option of
* `Element.scrollIntoView()`. `'all'` scrolls every scroll container of the target, up to and including the
* viewport. `'nearest'` only scrolls the target's nearest scroll container, or the viewport if it has none.
* @default 'all'
*/
container?: ScrollIntoViewContainer;
/**
* Change which ancestors are scrolled, and how they are measured. Run in order for every ancestor of the target,
* innermost first, up to the viewport. Without middleware `compute` follows the spec.
* @default []
*/
middleware?: Middleware[];
}
export type ComputeOptions = Options;
export interface ScrollAction {
el: Element;
top: number;
left: number;
}
/** Edges in viewport coordinates, like `getBoundingClientRect()`, or the lengths of the sides of a box in pixels */
export interface SideObject {
top: number;
right: number;
bottom: number;
left: number;
}
/** What a scroll container is aligned with. Everything is measured before anything is scrolled */
export interface Rects {
/** The scroll container's scrollport: its padding box without scrollbars. For the viewport, the visual viewport */
scrollport: SideObject;
/** The target's border box. The scrolling of inner scroll containers is added to it when it's aligned */
target: SideObject;
/** The scroll container's `scroll-padding`, in pixels. For the viewport, the root element's */
scrollPadding: SideObject;
/** The target's `scroll-margin`, in pixels */
scrollMargin: SideObject;
}
export interface MiddlewareState {
/** The element that is scrolled into view */
readonly target: Element;
/** The ancestor of the target that is considered */
readonly container: Element;
/** Whether `container` is the scrolling element of the document, which scrolls the viewport. It's the last one */
readonly viewport: boolean;
/** Whether `container` gets scrolled: it's a scroll container, and no middleware so far returned `scrollable: false` */
readonly scrollable: boolean;
/** The options, with their defaults */
readonly options: Readonly<Required<Pick<Options, "block" | "inline" | "container">>>;
/** Measured the first time it's read, and with what earlier middleware returned in `rects` */
readonly rects: Rects;
/**
* The viewport's `rects`, with what earlier middleware return in `rects` for the viewport. Reading it before the
* viewport is reached calls the earlier middleware for the viewport, so keep them free of side effects
*/
readonly viewportRects: Rects;
}
export interface MiddlewareReturn {
/** Whether `container` gets scrolled. `false` leaves it alone, the target is aligned in the outer ones regardless */
scrollable?: boolean;
/** Don't consider the ancestors of `container`. Together with `scrollable: false`, not `container` either */
stop?: boolean;
/** Use these measurements instead, for the middleware after this one and for aligning the target */
rects?: Partial<Rects>;
}
/** Like the [middleware of Floating UI](https://floating-ui.com/docs/middleware) */
export interface Middleware {
name: string;
/** Returns `{}` to change nothing */
fn: (state: MiddlewareState) => MiddlewareReturn;
}
export declare const compute: (target: Element, options?: Options) => ScrollAction[];
//# sourceMappingURL=index.d.ts.map