Skip to content

Commit c15f4ea

Browse files
authored
fix: move animation code to smooth package (#187)
* fix: move animation code to smooth package * Typo * prepare deploy * tweak * Sync with Prettier * make TS happy * fix: ci * duration * again * just build
1 parent 45b92de commit c15f4ea

File tree

23 files changed

+392
-935
lines changed

23 files changed

+392
-935
lines changed

.circleci/config.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,17 @@ jobs:
2929
- restore-cache: *restore-cache
3030
- *install
3131
- run:
32-
command: yarn typecheck
32+
name: scroll-into-view-if-needed
33+
command: yarn build:d.ts
34+
working_directory: packages/scroll-into-view-if-needed
35+
- run:
36+
name: scroll-into-view-if-needed
37+
command: yarn build
3338
working_directory: packages/scroll-into-view-if-needed
39+
- run:
40+
name: smooth-scroll-into-view-if-needed
41+
command: yarn typecheck
42+
working_directory: packages/smooth-scroll-into-view-if-needed
3443
- save-cache: *save-cache
3544

3645
Build:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
lerna-debug.log

lerna.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lerna": "2.10.2",
33
"npmClient": "yarn",
44
"useWorkspaces": true,
5-
"version": "independent"
5+
"version": "independent",
6+
"ignore": "scroll-into-view-if-needed-website"
67
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"predeploy:website": "yarn build",
1111
"deploy:website": "cd website && yarn deploy",
1212
"publish:canary": "lerna publish --npm-tag=next --canary",
13-
"tyecheck": "lerna exec --concurrency 1 -- yarn typecheck"
13+
"typecheck": "lerna exec --concurrency 1 -- yarn typecheck"
1414
},
1515
"devDependencies": {
1616
"husky": "0.14.3",

packages/scroll-into-view-if-needed/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@
1111
"version": "2.0.0",
1212
"main": "index.js",
1313
"files": [
14-
"auto.js",
1514
"compute.js",
1615
"es",
17-
"ponyfill.js",
1816
"umd"
1917
],
2018
"scripts": {
2119
"prebuild": "yarn clean",
22-
"build": "yarn build:cjs && yarn build:es && yarn build:umd && yarn build:umd.min",
20+
"build": "yarn build:d.ts && yarn build:cjs && yarn build:es && yarn build:umd && yarn build:umd.min",
2321
"build:cjs": "BABEL_ENV=cjs babel src -d . --extensions '.ts'",
22+
"build:d.ts": "tsc --emitDeclarationOnly",
2423
"build:es": "BABEL_ENV=es babel src -d es --extensions '.ts'",
2524
"build:umd": "BABEL_ENV=umd NODE_ENV=development rollup -c -f umd -o umd/scroll-into-view-if-needed.js",
2625
"build:umd.min": "BABEL_ENV=umd NODE_ENV=production rollup -c -f umd -o umd/scroll-into-view-if-needed.min.js",
27-
"clean": "rimraf 'umd' 'es'",
26+
"clean": "rimraf 'umd' 'es' 'typings'",
2827
"dev": "concurrently 'tsc --watch' 'yarn build:cjs --watch'",
2928
"prepublishOnly": "unset npm_config_cafile && yarn build",
30-
"typecheck": "tsc"
29+
"typecheck": "tsc --noEmit"
3130
},
3231
"dependencies": {
3332
"invariant": "^2.2.4",

packages/scroll-into-view-if-needed/src/auto.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/scroll-into-view-if-needed/src/compute.ts

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ export interface Options extends ScrollIntoViewOptions {
1414
}
1515

1616
const isElement = el => el != null && typeof el == 'object' && el.nodeType === 1
17-
18-
/**
19-
* indicates if an element has scrollable space in the provided axis
20-
*/
2117
function hasScrollableSpace(el, axis: 'Y' | 'X') {
2218
if (axis === 'Y') {
2319
return el.clientHeight < el.scrollHeight
@@ -30,13 +26,6 @@ function hasScrollableSpace(el, axis: 'Y' | 'X') {
3026
return false
3127
}
3228

33-
/**
34-
* indicates if an element can be scrolled in either axis
35-
* @method isScrollable
36-
* @param {Node} el
37-
* @param {String} axis
38-
* @returns {Boolean}
39-
*/
4029
function isScrollable(el) {
4130
var isScrollableY = hasScrollableSpace(el, 'Y')
4231
var isScrollableX = hasScrollableSpace(el, 'X')
@@ -48,7 +37,7 @@ function isScrollable(el) {
4837
const alignNearestBlock = (
4938
targetStart: number,
5039
targetSize: number,
51-
frame: HTMLElement,
40+
frame: Element,
5241
frameRect: ClientRect | DOMRect
5342
) => {
5443
// targetSize is either targetRect.height or targetRect.width depending on if it's `block` or `inline`
@@ -95,17 +84,6 @@ const alignNearestBlock = (
9584
return targetStart - frameRect.top
9685
}
9786

98-
console.log(
99-
'alignToNearest',
100-
'targetStart',
101-
targetStart,
102-
'should be 33 if clicking on 1',
103-
Math.min(
104-
targetStart,
105-
Math.max(targetEnd - frame.clientHeight, frame.scrollTop)
106-
)
107-
)
108-
10987
// start
11088
// Math.min(targetStart - frameRect.top, frame.scrollHeight - frame.clientHeight - frame.scrollTop)
11189

@@ -123,7 +101,7 @@ const alignNearestBlock = (
123101
const alignNearestInline = (
124102
targetStart: number,
125103
targetSize: number,
126-
frame: HTMLElement,
104+
frame: Element,
127105
frameRect: ClientRect | DOMRect
128106
) => {
129107
// targetSize is either targetRect.height or targetRect.width depending on if it's `block` or `inline`
@@ -170,17 +148,6 @@ const alignNearestInline = (
170148
return targetStart - frameRect.left
171149
}
172150

173-
console.log(
174-
'alignToNearest',
175-
'targetStart',
176-
targetStart,
177-
'should be 33 if clicking on 1',
178-
Math.min(
179-
targetStart,
180-
Math.max(targetEnd - frame.clientWidth, frame.scrollTop)
181-
)
182-
)
183-
184151
// start
185152
// Math.min(targetStart - frameRect.left, frame.scrollHeight - frame.clientWidth - frame.scrollTop)
186153

@@ -195,19 +162,21 @@ const alignNearestInline = (
195162
return 0
196163
}
197164

198-
export const compute = (maybeElement: Element, options: Options = {}) => {
165+
export const compute = (
166+
target: Element,
167+
options: Options = {}
168+
): [Element, number, number][] => {
199169
const {
200170
scrollMode = 'always',
201171
block = 'center',
202172
inline = 'nearest',
203173
boundary,
204174
} = options
205175

206-
if (!isElement(maybeElement)) {
176+
if (!isElement(target)) {
207177
throw new Error('Element is required in scrollIntoViewIfNeeded')
208178
}
209179

210-
let target = maybeElement
211180
let targetRect = target.getBoundingClientRect()
212181
console.error(
213182
'scrollMode',
@@ -222,7 +191,7 @@ export const compute = (maybeElement: Element, options: Options = {}) => {
222191
target
223192
)
224193
// Collect parents
225-
const frames: HTMLElement[] = []
194+
const frames: Element[] = []
226195
let parent
227196
while (isElement((parent = target.parentNode)) && target !== boundary) {
228197
if (isScrollable(parent)) {
@@ -238,7 +207,7 @@ export const compute = (maybeElement: Element, options: Options = {}) => {
238207
let targetInline
239208

240209
// Collect new scroll positions
241-
const computations = frames.map(frame => {
210+
const computations = frames.map((frame): [Element, number, number] => {
242211
const frameRect = frame.getBoundingClientRect()
243212
// @TODO fix hardcoding of block => top/Y
244213
/*
@@ -429,5 +398,5 @@ export const compute = (maybeElement: Element, options: Options = {}) => {
429398
return [frame, blockScroll, inlineScroll]
430399
})
431400

432-
return computations as [[Element, number, number]]
401+
return computations
433402
}
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
import { auto } from './auto'
1+
import { compute, Options as ComputeOptions } from './compute'
2+
3+
export interface Options {
4+
behavior?: 'auto' | 'smooth' | 'instant' | Function
5+
scrollMode?: ComputeOptions['scrollMode']
6+
boundary?: ComputeOptions['boundary']
7+
block?: ComputeOptions['block']
8+
inline?: ComputeOptions['inline']
9+
}
210

3-
// The "essentials-only" mode that is only as large as it needs to be, and supports native smooth scrolling which is potentially more performant
4-
export default auto
511
// Some people might use both "auto" and "ponyfill" modes in the same file, so we also provide a named export so
612
// that imports in userland code (like if they use native smooth scrolling on some browsers, and the ponyfill for everything else)
713
// the named export allows this `import {auto as autoScrollIntoView, ponyfill as smoothScrollIntoView} from ...`
8-
export { auto }
14+
export default (target: Element, options: Options = {}) => {
15+
const { behavior = 'auto', ...computeOptions } = options
16+
//return target.scrollIntoView(options)
17+
const instructions = compute(target, computeOptions)
918

10-
// The ponyfill variant that provide the same smooth scrolling experience for every browser
11-
export { ponyfill } from './ponyfill'
19+
if (typeof behavior == 'function') {
20+
return behavior(instructions)
21+
}
1222

13-
// This is for people who only need the code that computes scrolling instructions, but roll their own
14-
// code for doing the actual scrolling and want the smallest possible bundle size.
15-
export { compute } from './compute'
23+
instructions.forEach(([el, top, left]) => {
24+
// browser implements the new Element.prototype.scroll API that supports `behavior`
25+
if (el.scroll) {
26+
el.scroll({ top, left, behavior })
27+
} else {
28+
el.scrollTop = top
29+
el.scrollLeft = left
30+
}
31+
})
32+
}

packages/scroll-into-view-if-needed/src/ponyfill.ts

Lines changed: 0 additions & 92 deletions
This file was deleted.

packages/scroll-into-view-if-needed/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"module": "es2015",
55
"moduleResolution": "node",
66
"declaration": true,
7-
"emitDeclarationOnly": true,
87
"rootDir": "src",
98
"declarationDir": "typings",
109
"noImplicitReturns": true,

0 commit comments

Comments
 (0)