Skip to content

Commit

Permalink
feat(prevent-scroll): allow disabling scroll back
Browse files Browse the repository at this point in the history
  • Loading branch information
jer3m01 committed Feb 9, 2024
1 parent 93d33ae commit 9e8b538
Showing 1 changed file with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ function isActive(id: string) {
* @param props.element - Prevent scroll outside of this element. If the element is `null`, scroll will be prevented on the whole page. *Default = `null`*
* @param props.enabled - Whether scroll should be prevented. *Default = `true`*
* @param props.allowPinchZoom - Whether pinch zoom should be allowed. *Default = `true`*
* @param props.scrollBack - Whether to scroll back to initial position on cleanup. *Default = `true`*
*/
export function createPreventScroll(props: {
element?: MaybeAccessor<HTMLElement | null>;
enabled?: MaybeAccessor<boolean>;
allowPinchZoom?: MaybeAccessor<boolean>;
scrollBack?: boolean;
}) {
const defaultedProps = mergeProps(
{
element: null,
enabled: true,
allowPinchZoom: true,
scrollBack: true,
},
props,
);
Expand All @@ -77,8 +80,6 @@ export function createPreventScroll(props: {

if (!access(defaultedProps.enabled)) return;

console.log("preventscroll mount");

setPreventScrollStack((stack) => [...stack, preventScrollId]);

onCleanup(() => {
Expand All @@ -97,8 +98,6 @@ export function createPreventScroll(props: {
)
return;

console.log("preventscroll run");

if (!preventScrollStack().includes(preventScrollId)) {
untrack(() => {
setPreventScrollStack((stack) => [...stack, preventScrollId]);
Expand Down Expand Up @@ -138,7 +137,6 @@ export function createPreventScroll(props: {
const offsetLeft = window.scrollX;

if (preventScrollStack().length === 1) {
console.log("setstyle");
const createStyleCleanup = createStyle({
element: body,
style,
Expand All @@ -147,7 +145,7 @@ export function createPreventScroll(props: {

setResetCallback(() => () => {
createStyleCleanup();
if (scrollbarWidth > 0) {
if (props.scrollBack && scrollbarWidth > 0) {
window.scrollTo(offsetLeft, offsetTop);
}
});
Expand Down

0 comments on commit 9e8b538

Please sign in to comment.