Skip to content

Commit

Permalink
chore(prettier): 🤖 ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Mar 9, 2023
1 parent 4e48695 commit c50f55e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .releaserc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "@sanity/semantic-release-preset",
"branches": ["main"]
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ Linear example:

```typescript
scrollIntoView(node, {
ease: t => t,
ease: (t) => t,
})
```

Acceleration until halfway, then deceleration:

```typescript
scrollIntoView(node, {
ease: t =>
ease: (t) =>
t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
})
```
Expand All @@ -122,7 +122,7 @@ Sine easing in and out:

```typescript
scrollIntoView(node, {
ease: t => (1 + Math.sin(Math.PI * t - Math.PI / 2)) / 2,
ease: (t) => (1 + Math.sin(Math.PI * t - Math.PI / 2)) / 2,
})
```

Expand Down
2 changes: 1 addition & 1 deletion package.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {defineConfig} from '@sanity/pkg-utils'
import { defineConfig } from '@sanity/pkg-utils'

export default defineConfig({
minify: true,
Expand Down
4 changes: 1 addition & 3 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"extends": [
"github>stipsan/renovate-presets:auto"
],
"extends": ["github>stipsan/renovate-presets:auto"],
"packageRules": [
{
"packageNames": ["scroll-into-view-if-needed"],
Expand Down
69 changes: 36 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ const now = () => {
}

type SmoothScrollAction = {
el: Element;
el: Element
// [start, end] tuples of the distance animated
left: [number, number];
top: [number, number];
left: [number, number]
top: [number, number]
}

type Context = {
Expand Down Expand Up @@ -77,15 +77,15 @@ function smoothScroll(
x: number,
y: number,
duration = 600,
ease: CustomEasing = t => 1 + --t * t * t * t * t,
ease: CustomEasing = (t) => 1 + --t * t * t * t * t,
cb: Function,
onScrollChange?: OnScrollChangeCallback
) {
// define scroll context
const scrollable = el
const startX = el.scrollLeft
const startY = el.scrollTop
const method = (x: number, y: number, elapsed: number, value: number, ) => {
const method = (x: number, y: number, elapsed: number, value: number) => {
// @TODO use Element.scroll if it exists, as it is potentially better performing
// use ceil to include the the fractional part of the number for the scrolling
const left = Math.ceil(x)
Expand Down Expand Up @@ -133,35 +133,38 @@ function scroll<T>(target: Element, options?: any) {
inline: overrides.inline,
scrollMode: overrides.scrollMode,
boundary: overrides.boundary,
behavior: actions =>
behavior: (actions) =>
Promise.all(
actions.reduce((results: Promise<SmoothScrollAction>[], { el, left, top }) => {
const startLeft = el.scrollLeft
const startTop = el.scrollTop
if (startLeft === left && startTop === top) {
return results
}

return [
...results,
new Promise(resolve => {
return smoothScroll(
el,
left,
top,
overrides.duration,
overrides.ease,
() =>
resolve({
el,
left: [startLeft, left],
top: [startTop, top],
}),
overrides.onScrollChange
)
}),
]
}, [])
actions.reduce(
(results: Promise<SmoothScrollAction>[], { el, left, top }) => {
const startLeft = el.scrollLeft
const startTop = el.scrollTop
if (startLeft === left && startTop === top) {
return results
}

return [
...results,
new Promise((resolve) => {
return smoothScroll(
el,
left,
top,
overrides.duration,
overrides.ease,
() =>
resolve({
el,
left: [startLeft, left],
top: [startTop, top],
}),
overrides.onScrollChange
)
}),
]
},
[]
)
),
})
}
Expand Down

0 comments on commit c50f55e

Please sign in to comment.