Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions packages/core/src/lenis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class Lenis {
private _isScrolling: Scrolling = false // true when scroll is animating
private _isStopped = false // true if user should not be able to scroll - enable/disable programmatically
private _isLocked = false // same as isStopped but enabled/disabled when scroll reaches target
private _forceLocked = false // manual override for lock()/unlock()
private _preventNextNativeScrollEvent = false
private _resetVelocityTimeout: ReturnType<typeof setTimeout> | null = null
private _rafId: number | null = null
Expand Down Expand Up @@ -565,6 +566,12 @@ export class Lenis {
return
}

if (this.isLocked && this.actualScroll !== this.scroll) {
this.preventNextNativeScrollEvent()
this.setScroll(this.scroll)
return
}

if (this.isScrolling === false || this.isScrolling === 'native') {
const lastScroll = this.animatedScroll
this.animatedScroll = this.targetScroll = this.actualScroll
Expand Down Expand Up @@ -643,6 +650,20 @@ export class Lenis {
this.emit()
}

/**
* Lock lenis scroll
*/
lock() {
this.forceLocked = true
}

/**
* Unlock lenis scroll
*/
unlock() {
this.forceLocked = false
}

/**
* RequestAnimationFrame for lenis
*
Expand Down Expand Up @@ -1082,13 +1103,30 @@ export class Lenis {
* Check if lenis is locked
*/
get isLocked() {
return this._isLocked
return this._forceLocked || this._isLocked
}

private set isLocked(value: boolean) {
const isLocked = this.isLocked

if (this._isLocked !== value) {
this._isLocked = value
this.updateClassName()

if (isLocked !== this.isLocked) {
this.updateClassName()
}
}
}

private set forceLocked(value: boolean) {
const isLocked = this.isLocked

if (this._forceLocked !== value) {
this._forceLocked = value

if (isLocked !== this.isLocked) {
this.updateClassName()
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion playground/core/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const debugEl = document.createElement('div')
debugEl.style.cssText = `
position: fixed;
top: env(safe-area-inset-top, 8px);
left: 8px;
right: 8px;
z-index: 9999;
padding: 8px 10px;
font: 12px/1.3 ui-monospace, Menlo, monospace;
Expand All @@ -124,6 +124,7 @@ const renderDebug = (e: Lenis) => {
`progress ${(e.progress * 100).toFixed(1)}%`,
`direction ${e.direction}`,
`isScrolling ${e.isScrolling}`,
`isLocked ${e.isLocked}`,
].join('\n')
}

Expand Down Expand Up @@ -216,6 +217,14 @@ document.getElementById('start')?.addEventListener('click', () => {
// document.documentElement.style.overflow = 'auto'
lenis.start()
})
document.getElementById('lock')?.addEventListener('click', () => {
// document.documentElement.style.overflow = 'auto'
lenis.lock()
})
document.getElementById('unlock')?.addEventListener('click', () => {
// document.documentElement.style.overflow = 'auto'
lenis.unlock()
})

document.getElementById('scroll-start')?.addEventListener('click', () => {
lenis.scrollTo(100, {
Expand Down
1 change: 1 addition & 0 deletions playground/react/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ html:not(.lenis) {
background: #222;
color: #fff;
border-radius: 4px;
margin-right: 4px;
}

.debug-panel button:hover {
Expand Down
2 changes: 2 additions & 0 deletions playground/www/pages/core.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Layout from '../layouts/Layout.astro'
<button id="scroll-end">Scroll end</button>
<button id="stop">Stop</button>
<button id="start">Start</button>
<button id="lock">Lock</button>
<button id="unlock">Unlock</button>
</div>
<div id="app">
<div id="about">
Expand Down