await scrollIntoView(el) — run code after the scroll animation finishes. No guessing with setTimeout.
Calls the real element.scrollIntoView(). Not a custom scroll implementation. You get native browser behavior.
Listens for scroll events on all scrollable ancestors. Resolves when scrolling stops — works with nested containers too.
Configurable timeout (default 3s) ensures the Promise always resolves. Never hangs your async flow.
Pure TypeScript. ~0.6kB gzipped. No bloat, no transitive deps, no supply chain risk.
Pass an AbortSignal to cancel a pending scroll. The Promise rejects with an AbortError.
Each button calls await scrollIntoView(section) and logs when the Promise resolves.
npm install scroll-into-view-promise
yarn add scroll-into-view-promise
pnpm add scroll-into-view-promise
<script type="module"> import scrollIntoView from 'https://esm.sh/scroll-into-view-promise'; </script>
import scrollIntoView from 'scroll-into-view-promise'; const el = document.getElementById('pricing'); await scrollIntoView(el); console.log('Scroll complete!'); // With options await scrollIntoView(el, { block: 'center' });
import scrollIntoView from 'scroll-into-view-promise'; async function walkthrough() { await scrollIntoView(document.getElementById('step-1')); showTooltip('step-1'); await scrollIntoView(document.getElementById('step-2')); showTooltip('step-2'); await scrollIntoView(document.getElementById('step-3')); showTooltip('step-3'); }
import scrollIntoView from 'scroll-into-view-promise'; const input = document.getElementById('email-input'); // Scroll, THEN focus — guaranteed visible await scrollIntoView(input, { block: 'center' }); input.focus();
import scrollIntoView from 'scroll-into-view-promise'; const ctrl = new AbortController(); scrollIntoView(target, { signal: ctrl.signal }) .catch(err => { if (err.name === 'AbortError') console.log('Scroll cancelled'); }); // Cancel after 500ms setTimeout(() => ctrl.abort(), 500);
| Param | Type | Description |
|---|---|---|
| element | Element | The DOM element to scroll into view. |
| options | ScrollIntoViewPromiseOptions | Optional configuration (see below). |
| Option | Type | Default | Description |
|---|---|---|---|
| behavior | 'smooth' | 'instant' | 'auto' | 'smooth' | Scroll behavior, passed to the native API. |
| block | 'start' | 'center' | 'end' | 'nearest' | 'start' | Vertical alignment of the element. |
| inline | 'start' | 'center' | 'end' | 'nearest' | 'nearest' | Horizontal alignment of the element. |
| timeout | number | 3000 | Max wait time (ms) before resolving anyway. |
| signal | AbortSignal | — | Abort signal to cancel and reject the Promise. |