|
8 | 8 |
|
9 | 9 | 
|
10 | 10 |
|
11 |
| -Lower level API that is used by the [ponyfill](https://ponyfill.com) [scroll-into-view-if-needed](https://github.com/stipsan/scroll-into-view-if-needed) to compute where (if needed) elements should scroll based on [options defined in the spec](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView). |
| 11 | +Lower level API that is used by the [ponyfill](https://ponyfill.com) [scroll-into-view-if-needed](https://github.com/stipsan/scroll-into-view-if-needed) to compute where (if needed) elements should scroll based on [options defined in the spec](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) and the [`scrollMode: "if-needed"` draft spec proposal](https://github.com/w3c/csswg-drafts/pull/1805). |
12 | 12 | Use this if you want the smallest possible bundlesize and is ok with implementing the actual scrolling yourself.
|
13 | 13 |
|
| 14 | +Scrolling SVG elements are supported, as well as Shadow DOM elements. The [VisualViewport](https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport) API is also supported, ensuring scrolling works properly on modern devices. Quirksmode is also supported as long as you polyfill `document.scrollingElement`. |
| 15 | + |
14 | 16 | ## Install
|
15 | 17 |
|
16 | 18 | ```bash
|
@@ -54,6 +56,76 @@ actions.forEach(({ el, top, left }) => {
|
54 | 56 | })
|
55 | 57 | ```
|
56 | 58 |
|
| 59 | +## API |
| 60 | + |
| 61 | +### computeScrollIntoView(target, options) |
| 62 | + |
| 63 | +### options |
| 64 | + |
| 65 | +Type: `Object` |
| 66 | + |
| 67 | +#### [block](https://scroll-into-view-if-needed.netlify.com/#scroll-alignment) |
| 68 | + |
| 69 | +Type: `'start' | 'center' | 'end' | 'nearest'`<br> Default: `'center'` |
| 70 | + |
| 71 | +Control the logical scroll position on the y-axis. The spec states that the `block` direction is related to the [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode), but this is not implemented yet in this library. |
| 72 | +This means that `block: 'start'` aligns to the top edge and `block: 'end'` to the bottom. |
| 73 | + |
| 74 | +#### [inline](https://scroll-into-view-if-needed.netlify.com/#scroll-alignment) |
| 75 | + |
| 76 | +Type: `'start' | 'center' | 'end' | 'nearest'`<br> Default: `'nearest'` |
| 77 | + |
| 78 | +Like `block` this is affected by the [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode). In left-to-right pages `inline: 'start'` will align to the left edge. In right-to-left it should be flipped. This will be supported in a future release. |
| 79 | + |
| 80 | +#### [scrollMode](https://scroll-into-view-if-needed.netlify.com/#scrolling-if-needed) |
| 81 | + |
| 82 | +Type: `'always' | 'if-needed'`<br> Default: `'always'` |
| 83 | + |
| 84 | +This is a proposed addition to the spec that you can track here: https://github.com/w3c/csswg-drafts/pull/1805 |
| 85 | + |
| 86 | +This library will be updated to reflect any changes to the spec and will provide a migration path. |
| 87 | +To be backwards compatible with `Element.scrollIntoViewIfNeeded` if something is not 100% visible it will count as "needs scrolling". If you need a different visibility ratio your best option would be to implement an [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API). |
| 88 | + |
| 89 | +#### [boundary](https://scroll-into-view-if-needed.netlify.com/#limit-propagation) |
| 90 | + |
| 91 | +Type: `Element | Function` |
| 92 | + |
| 93 | +By default there is no boundary. All the parent elements of your target is checked until it reaches the viewport ([`document.scrollingElement`](https://developer.mozilla.org/en-US/docs/Web/API/document/scrollingElement)) when calculating layout and what to scroll. |
| 94 | +By passing a boundary you can short-circuit this loop depending on your needs: |
| 95 | + |
| 96 | +- Prevent the browser window from scrolling. |
| 97 | +- Scroll elements into view in a list, without scrolling container elements. |
| 98 | + |
| 99 | +You can also pass a function to do more dynamic checks to override the scroll scoping: |
| 100 | + |
| 101 | +```js |
| 102 | +const actions = computeScrollIntoView(target, { |
| 103 | + boundary: parent => { |
| 104 | + // By default `overflow: hidden` elements are allowed, only `overflow: visible | clip` is skipped as |
| 105 | + // this is required by the CSSOM spec |
| 106 | + if (getComputedStyle(parent)['overflow'] === 'hidden') { |
| 107 | + return false |
| 108 | + } |
| 109 | + |
| 110 | + return true |
| 111 | + }, |
| 112 | +}) |
| 113 | +``` |
| 114 | + |
| 115 | +#### skipOverflowHiddenElements |
| 116 | + |
| 117 | +Type: `Boolean`<br> Default: `false` |
| 118 | + |
| 119 | +By default the [spec](https://drafts.csswg.org/cssom-view/#scrolling-box) states that `overflow: hidden` elements should be scrollable because it has [been used to allow programatic scrolling](https://drafts.csswg.org/css-overflow-3/#valdef-overflow-hidden). This behavior can sometimes lead to [scrolling issues](https://github.com/stipsan/scroll-into-view-if-needed/pull/225#issue-186419520) when you have a node that is a child of an `overflow: hidden` node. |
| 120 | + |
| 121 | +This package follows the convention [adopted by Firefox](https://hg.mozilla.org/integration/fx-team/rev/c48c3ec05012#l7.18) of setting a boolean option to _not_ scroll all nodes with `overflow: hidden` set. |
| 122 | + |
| 123 | +# TypeScript support |
| 124 | + |
| 125 | +This library ships with library definitions for TypeScript. |
| 126 | + |
| 127 | +> FlowType definitions is coming soon. |
| 128 | +
|
57 | 129 | [gzip-badge]: http://img.badgesize.io/https://unpkg.com/compute-scroll-into-view/umd/compute-scroll-into-view.min.js?compression=gzip&label=gzip%20size&style=flat-square
|
58 | 130 | [size-badge]: http://img.badgesize.io/https://unpkg.com/compute-scroll-into-view/umd/compute-scroll-into-view.min.js?label=size&style=flat-square
|
59 | 131 | [unpkg-dist]: https://unpkg.com/compute-scroll-into-view/umd/
|
|
0 commit comments