Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useLockScroll): new function (#958)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
4e40c6f
commit 930890a
Showing
6 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue-demi' | ||
import { useToggle } from '@vueuse/shared' | ||
import { useScroll } from '../useScroll' | ||
import { useScrollLock } from '.' | ||
const el = ref<HTMLElement | null>(null) | ||
useScroll(el) | ||
const isLocked = useScrollLock(el) | ||
const toggleLock = useToggle(isLocked) | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-wrap"> | ||
<div ref="el" class="w-300px h-300px m-auto overflow-scroll bg-gray-500/5 rounded"> | ||
<div class="w-500px h-400px relative"> | ||
<div position="absolute left-0 top-0" bg="gray-500/5" p="x-2 y-1"> | ||
TopLeft | ||
</div> | ||
<div position="absolute left-0 bottom-0" bg="gray-500/5" p="x-2 y-1"> | ||
BottomLeft | ||
</div> | ||
<div position="absolute right-0 top-0" bg="gray-500/5" p="x-2 y-1"> | ||
TopRight | ||
</div> | ||
<div position="absolute right-0 bottom-0" bg="gray-500/5" p="x-2 y-1"> | ||
BottomRight | ||
</div> | ||
<div position="absolute left-1/3 top-1/3" bg="gray-500/5" p="x-2 y-1"> | ||
Scroll Me | ||
</div> | ||
</div> | ||
</div> | ||
<div class="m-auto px-6 py-4 rounded flex flex-col w-60 gap-2 bg-gray-500/5"> | ||
<div> | ||
<span opacity="75"> | ||
isLocked | ||
</span> | ||
<BooleanDisplay :value="isLocked" /> | ||
</div> | ||
<button opacity="75" @click="toggleLock()"> | ||
{{ isLocked ? 'Unlock' : 'Lock' }} | ||
</button> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
category: Sensors | ||
--- | ||
|
||
# useScrollLock | ||
|
||
Lock scrolling of the element. | ||
|
||
## Usage | ||
|
||
```html | ||
<script setup lang="ts"> | ||
import { useScrollLock } from '@vueuse/core' | ||
const el = ref<HTMLElement | null>(null) | ||
const isLocked = useScrollLock(el) | ||
isLocked.value = true // lock | ||
isLocked.value = false // unlock | ||
</script> | ||
|
||
<template> | ||
<div ref="el"></div> | ||
</template> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { unref, ref, computed } from 'vue-demi' | ||
import { MaybeRef, isClient, Fn } from '@vueuse/shared' | ||
import { useEventListener } from '../useEventListener' | ||
|
||
function preventDefault(rawEvent: TouchEvent): boolean { | ||
const e = rawEvent || window.event | ||
// Do not prevent if the event has more than one touch (usually meaning this is a multi touch gesture like pinch to zoom). | ||
if (e.touches.length > 1) return true | ||
|
||
if (e.preventDefault) e.preventDefault() | ||
|
||
return false | ||
} | ||
|
||
// TODO: move to @vueuse/share | ||
const isIOS = /* #__PURE__ */ isClient && window?.navigator && window?.navigator?.platform && /iP(ad|hone|od)/.test(window?.navigator?.platform) | ||
|
||
/** | ||
* Lock scrolling of the element. | ||
* | ||
* @see https://vueuse.org/useScrollLock | ||
* @param element | ||
*/ | ||
export function useScrollLock( | ||
element: MaybeRef<HTMLElement | SVGElement | Window | Document | null | undefined>, | ||
initialState = false, | ||
) { | ||
const isLocked = ref(initialState) | ||
let touchMoveListener: Fn | null = null | ||
let initialOverflow: CSSStyleDeclaration['overflow'] | ||
|
||
const lock = () => { | ||
const ele = (unref(element) as HTMLElement) | ||
if (!ele || isLocked.value) return | ||
initialOverflow = ele.style.overflow | ||
if (isIOS) { | ||
touchMoveListener = useEventListener( | ||
document, | ||
'touchmove', | ||
preventDefault, | ||
{ passive: false }, | ||
) | ||
} | ||
else { | ||
ele.style.overflow = 'hidden' | ||
} | ||
isLocked.value = true | ||
} | ||
|
||
const unlock = () => { | ||
const ele = (unref(element) as HTMLElement) | ||
if (!ele || !isLocked.value) return | ||
isIOS ? touchMoveListener?.() : ele.style.overflow = initialOverflow | ||
isLocked.value = false | ||
} | ||
|
||
return computed<boolean>({ | ||
get() { | ||
return isLocked.value | ||
}, | ||
set(v) { | ||
if (v) lock() | ||
else unlock() | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters