Skip to content

Commit 2bb4988

Browse files
authored
chore(docs): add API section to readme (#35)
* add note on scrollMode * add reminder note * add API section to readme * some extra info * add info on TS and FlowType
1 parent ba3313c commit 2bb4988

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

README.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
![compute-scroll-into-view](https://user-images.githubusercontent.com/81981/43024153-a2cc212c-8c6d-11e8-913b-b4d62efcf105.png)
1010

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).
1212
Use this if you want the smallest possible bundlesize and is ok with implementing the actual scrolling yourself.
1313

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+
1416
## Install
1517

1618
```bash
@@ -54,6 +56,76 @@ actions.forEach(({ el, top, left }) => {
5456
})
5557
```
5658

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+
57129
[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
58130
[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
59131
[unpkg-dist]: https://unpkg.com/compute-scroll-into-view/umd/

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ export default (target: Element, options: Options): CustomScrollAction[] => {
319319
// In chrome there's no longer a difference between caching the `frames.length` to a var or not, so we don't in this case (size > speed anyways)
320320
for (let index = 0; index < frames.length; index++) {
321321
const frame = frames[index]
322+
323+
// @TODO add a shouldScroll hook here that allows userland code to take control
324+
322325
const {
323326
height,
324327
width,

0 commit comments

Comments
 (0)