Skip to content

Commit

Permalink
snap demo
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Jun 5, 2024
1 parent 4b0a97b commit a6148fd
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ If you like Lenis, please consider [sponsoring us](https://github.com/sponsors/d

## Packages

- [lenis](https://github.com/darkroomengineering/lenis)
- [lenis/react](https://github.com/darkroomengineering/lenis/tree/main/packages/react)
- [lenis/snap](https://github.com/darkroomengineering/lenis/tree/main/packages/snap)
- [lenis](https://github.com/darkroomengineering/lenis/blob/main/README.md)
- [lenis/react](https://github.com/darkroomengineering/lenis/blob/main/packages/react/README.md)
- [lenis/snap](https://github.com/darkroomengineering/lenis/tree/main/packages/snap/README.md)


<br>
Expand Down Expand Up @@ -229,7 +229,7 @@ See documentation for [lenis/react](https://github.com/darkroomengineering/lenis

## Limitations

- no support for CSS scroll-snap ([lenis/snap](https://github.com/darkroomengineering/lenis/tree/main/packages/snap))
- no support for CSS scroll-snap ([lenis/snap](https://github.com/darkroomengineering/lenis/tree/main/packages/snap/README.md))
- capped to 60fps on Safari ([source](https://bugs.webkit.org/show_bug.cgi?id=173434)) and 30fps on low power mode
- smooth scroll will stop working over iframe since they don't forward wheel events
- position fixed seems to lag on MacOS Safari pre-M1 ([source](https://github.com/darkroomengineering/lenis/issues/103))
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# lenis/snap

## Introduction
lenis/snap provides a partial support for CSS scroll snap with [Lenis](https://github.com/darkroomengineering/lenis)
lenis/snap provides a partial support for CSS scroll snap with [Lenis](https://github.com/darkroomengineering/lenis), see [Demo](https://lenis.darkroom.engineering/snap)

## Installation

Expand Down
2 changes: 1 addition & 1 deletion packages/snap/dist/lenis-snap.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/snap/dist/lenis-snap.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/snap/dist/lenis-snap.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/snap/playground/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (true) {
const section2 = document.querySelector('.section-2')
const section3 = document.querySelector('.section-3')

snap.add(500)
// snap.add(500)

snap.addElement(section2, {
align: ['start', 'end'], // 'start', 'center', 'end'
Expand Down
1 change: 1 addition & 0 deletions packages/snap/src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Rect = {
export class SnapElement {
element: HTMLElement
options: SnapElementOptions
align: string[]
// @ts-ignore
rect: Rect = {}
wrapperResizeObserver: ResizeObserver
Expand Down
15 changes: 10 additions & 5 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Lenis from 'lenis'
import { SnapElement, SnapElementOptions } from './element'
import { uid, UID } from './uid'
import { UID, uid } from './uid'

// TODO:
// - horizontal
// - fix trackpad snapping too soon due to velocity (fuck Apple)
// - fix wheel scrolling after limits (see console scroll to)
// - fix touch scroll, do not snap when not released
// - arrow, spacebar

type Viewport = {
width: number
Expand All @@ -18,7 +19,7 @@ export type SnapOptions = {
lerp?: number
easing?: (t: number) => number
duration?: number
velocityThreshold?: number
velocityThreshold?: number
onSnapStart?: (t: number) => number
onSnapComplete?: (t: number) => number
}
Expand All @@ -41,7 +42,7 @@ export default class Snap {
velocityThreshold = 1,
onSnapStart,
onSnapComplete,
}: SnapOptions = {},
}: SnapOptions = {}
) {
this.lenis = lenis

Expand Down Expand Up @@ -164,9 +165,13 @@ export default class Snap {
if (align === 'start') {
snap = rect.top
} else if (align === 'center') {
snap = rect.top + rect.height / 2 - this.viewport.height / 2
snap = isHorizontal
? rect.left + rect.width / 2 - this.viewport.width / 2
: rect.top + rect.height / 2 - this.viewport.height / 2
} else if (align === 'end') {
snap = rect.top + rect.height - this.viewport.height
snap = isHorizontal
? rect.left + rect.width - this.viewport.width
: rect.top + rect.height - this.viewport.height
}

if (snap !== undefined) {
Expand Down
66 changes: 66 additions & 0 deletions website/pages/snap/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import cn from 'clsx'
import { useEffect, useRef } from 'react'
import Snap from '../../../dist/lenis-snap.mjs'
import Lenis from '../../../dist/lenis.mjs'
import s from './snap.module.scss'

export default function Page() {
const sectionRefs = useRef([])

useEffect(() => {
const lenis = new Lenis({
lerp: 0.1,
})
window.lenis = lenis

const snap = new Snap(lenis, {
type: 'mandatory', // 'mandatory', 'proximity'
velocityThreshold: 1,
// duration: 2,
// easing: (t) => t,
onSnapStart: (snap) => {
console.log('onSnapStart', snap)
},
onSnapComplete: (snap) => {
console.log('onSnapComplete', snap)
},
})
window.snap = snap

const section2 = sectionRefs.current[1]
const section3 = sectionRefs.current[2]

// snap.add(500)

snap.addElement(section2, {
align: ['start', 'end'], // 'start', 'center', 'end'
})

snap.addElement(section3, {
align: 'center', // 'start', 'center', 'end'
})

function raf(time) {
lenis.raf(time)
requestAnimationFrame(raf)
}

requestAnimationFrame(raf)
}, [])

return (
<div className={s.page}>
{Array.from({ length: 4 }).map((_, index) => (
<section
key={index}
className={cn(s.section, s[`section-${index + 1}`])}
ref={(node) => {
sectionRefs.current[index] = node
}}
>
<div className={s.inner}></div>
</section>
))}
</div>
)
}
49 changes: 49 additions & 0 deletions website/pages/snap/snap.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.page {
background-color: #000;
}

.section {
padding: 24px;

.inner {
height: 100%;
width: 100%;
border-radius: 16px;
}

&-1 {
height: 150vh;

.inner {
background-color: #ff98a2;
}
}

&-2 {
height: 50vh;

.inner {
background-color: #d585a9;
}
}

&-3 {
height: 80vh;

.inner {
background-color: #a378a4;
}
}

&-4 {
height: 150vh;

.inner {
background-color: #726a93;
}
}

&:not(:last-child) {
margin-bottom: -24px;
}
}

0 comments on commit a6148fd

Please sign in to comment.