nuxt-hs-ui 2.1.6 → 2.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const useHsScrollLockPinia: import("pinia").StoreDefinition<"HsScrollLockPinia", Pick<{
|
|
2
|
+
active: import("vue").Ref<boolean, boolean>;
|
|
3
|
+
key: import("vue").Ref<string, string>;
|
|
4
|
+
}, "key" | "active">, Pick<{
|
|
5
|
+
active: import("vue").Ref<boolean, boolean>;
|
|
6
|
+
key: import("vue").Ref<string, string>;
|
|
7
|
+
}, never>, Pick<{
|
|
8
|
+
active: import("vue").Ref<boolean, boolean>;
|
|
9
|
+
key: import("vue").Ref<string, string>;
|
|
10
|
+
}, never>>;
|
|
11
|
+
export declare const useHsScrollLock: () => {
|
|
12
|
+
unlock: () => void;
|
|
13
|
+
lock: () => void;
|
|
14
|
+
init: (element: HTMLElement | null) => void;
|
|
15
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { defineStore } from "pinia";
|
|
2
|
+
import { GenerateUniqeKey } from "../utils/com.js";
|
|
3
|
+
import { disableBodyScroll, enableBodyScroll } from "body-scroll-lock";
|
|
4
|
+
import { ref } from "vue";
|
|
5
|
+
export const useHsScrollLockPinia = defineStore("HsScrollLockPinia", () => {
|
|
6
|
+
const active = ref(false);
|
|
7
|
+
const key = ref("");
|
|
8
|
+
return { active, key };
|
|
9
|
+
});
|
|
10
|
+
export const useHsScrollLock = () => {
|
|
11
|
+
const scrollY = ref(0);
|
|
12
|
+
const isIOS = ref(false);
|
|
13
|
+
const elm = ref(null);
|
|
14
|
+
const key = GenerateUniqeKey();
|
|
15
|
+
const hsScrollLockPinia = useHsScrollLockPinia();
|
|
16
|
+
const checkSafariMobile = () => {
|
|
17
|
+
if (typeof navigator === "undefined") return false;
|
|
18
|
+
const ua = navigator.userAgent || "";
|
|
19
|
+
const isOldIPad = /\(iPad.*OS/.test(ua);
|
|
20
|
+
const isIpad = /Macintosh/.test(ua) && navigator.maxTouchPoints > 1;
|
|
21
|
+
const isiOS = /iP(?:ad|hone|od)/.test(ua);
|
|
22
|
+
console.log({ ua, isOldIPad, isIpad, isiOS });
|
|
23
|
+
return isOldIPad || isIpad || isiOS;
|
|
24
|
+
};
|
|
25
|
+
const init = (element) => {
|
|
26
|
+
if (element === null) {
|
|
27
|
+
console.error(
|
|
28
|
+
"[useScrollLock] Expected a valid HTMLElement in init(), but got null. Make sure to pass the correct ref."
|
|
29
|
+
);
|
|
30
|
+
if (import.meta.dev) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
"[useScrollLock] Expected a valid HTMLElement in init(), but got null. Make sure to pass the correct ref."
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
elm.value = element;
|
|
37
|
+
isIOS.value = checkSafariMobile();
|
|
38
|
+
};
|
|
39
|
+
const lock = () => {
|
|
40
|
+
if (hsScrollLockPinia.active) return;
|
|
41
|
+
hsScrollLockPinia.key = key;
|
|
42
|
+
hsScrollLockPinia.active = true;
|
|
43
|
+
if (isIOS.value) {
|
|
44
|
+
scrollY.value = window.scrollY;
|
|
45
|
+
document.body.style.position = "fixed";
|
|
46
|
+
document.body.style.top = `-${scrollY.value}px`;
|
|
47
|
+
document.body.style.left = "0";
|
|
48
|
+
document.body.style.right = "0";
|
|
49
|
+
document.body.style.width = "100%";
|
|
50
|
+
} else {
|
|
51
|
+
if (!elm.value) return;
|
|
52
|
+
const options = {
|
|
53
|
+
// bodyにスクロールバー分のpadding-rightを追加するかどうか(デフォルト値:false)
|
|
54
|
+
reserveScrollBarGap: true
|
|
55
|
+
};
|
|
56
|
+
disableBodyScroll(elm.value, options);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const unlock = () => {
|
|
60
|
+
if (hsScrollLockPinia.key !== key) return;
|
|
61
|
+
hsScrollLockPinia.key = "";
|
|
62
|
+
hsScrollLockPinia.active = false;
|
|
63
|
+
if (isIOS.value) {
|
|
64
|
+
document.body.style.position = "";
|
|
65
|
+
document.body.style.top = "";
|
|
66
|
+
document.body.style.left = "";
|
|
67
|
+
document.body.style.right = "";
|
|
68
|
+
document.body.style.width = "";
|
|
69
|
+
window.scrollTo(0, scrollY.value);
|
|
70
|
+
} else {
|
|
71
|
+
if (!elm.value) return;
|
|
72
|
+
enableBodyScroll(elm.value);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
return { unlock, lock, init };
|
|
76
|
+
};
|