diff --git a/src/hooks/useCopyToClipboard.ts b/src/hooks/useCopyToClipboard.ts new file mode 100644 index 0000000..98c9bb3 --- /dev/null +++ b/src/hooks/useCopyToClipboard.ts @@ -0,0 +1,18 @@ +function copyToClipboard(text: string | number) { + let input = document.createElement('input'); + input.setAttribute('value', text.toString()); + document.body.appendChild(input); + input.select(); + let result = document.execCommand('copy'); + document.body.removeChild(input); + return result; +} + +export const useCopyToClipboard = () => { + return (text: string | number) => { + if (typeof text === "string" || typeof text == "number") { + return copyToClipboard(text); + } + return false; + } +} \ No newline at end of file diff --git a/src/hooks/useNetworkStatus.ts b/src/hooks/useNetworkStatus.ts new file mode 100644 index 0000000..7db6366 --- /dev/null +++ b/src/hooks/useNetworkStatus.ts @@ -0,0 +1,23 @@ +import { onMounted, onUnmounted } from 'vue'; + +export enum NetWorkStatus { + ONLINE = "online", + OFFLINE = "offline", +} + +export const useNetworkStatus = (callback = (_status: NetWorkStatus) => { }) => { + const updateOnlineStatus = () => { + const status: NetWorkStatus = navigator.onLine ? NetWorkStatus.ONLINE : NetWorkStatus.OFFLINE; + callback(status); + } + + onMounted(() => { + window.addEventListener('online', updateOnlineStatus); + window.addEventListener('offline', updateOnlineStatus); + }); + + onUnmounted(() => { + window.removeEventListener('online', updateOnlineStatus); + window.removeEventListener('offline', updateOnlineStatus); + }) +} \ No newline at end of file diff --git a/src/hooks/useScrollToBottom.ts b/src/hooks/useScrollToBottom.ts new file mode 100644 index 0000000..b79158c --- /dev/null +++ b/src/hooks/useScrollToBottom.ts @@ -0,0 +1,17 @@ +import { onMounted, onUnmounted } from 'vue'; + +export const useScrollToBottom = (callback = () => { }) => { + const handleScrolling = () => { + if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) { + callback(); + } + } + + onMounted(() => { + window.addEventListener('scroll', handleScrolling); + }); + + onUnmounted(() => { + window.removeEventListener('scroll', handleScrolling); + }); +} \ No newline at end of file diff --git a/src/hooks/useStorage.ts b/src/hooks/useStorage.ts new file mode 100644 index 0000000..9e421ba --- /dev/null +++ b/src/hooks/useStorage.ts @@ -0,0 +1,38 @@ +import { ref } from 'vue'; + +const getItem = (key: string, storage: Storage) => { + let value = storage.getItem(key); + if (!value) { + return null; + } + try { + return JSON.parse(value) + } catch (error) { + return value; + } +} + +export const useStorage = (key: string, type = 'session') => { + let storage = null; + switch (type) { + case 'session': + storage = sessionStorage; + break; + case 'local': + storage = localStorage; + break; + default: + return null; + } + const value = ref(getItem(key, storage)); + const setItem = (storage: Storage) => { + return (newValue: any) => { + value.value = newValue; + storage.setItem(key, JSON.stringify(newValue)); + } + } + return [ + value, + setItem(storage) + ] +} \ No newline at end of file diff --git a/src/hooks/useTimer.ts b/src/hooks/useTimer.ts new file mode 100644 index 0000000..8db8c05 --- /dev/null +++ b/src/hooks/useTimer.ts @@ -0,0 +1,51 @@ +import { ref, onUnmounted } from 'vue'; + +export const useTimer = (callback = () => { }, step = 1000) => { + let timerVariableId: ReturnType | null = null; + let times = 0; + const isPaused = ref(false); + + const stop = () => { + if (timerVariableId) { + clearInterval(timerVariableId); + timerVariableId = null; + resume(); + } + } + + const start = () => { + stop(); + if (!timerVariableId) { + times = 0; + timerVariableId = setInterval(() => { + if (!isPaused.value) { + times++; + //callback(times, step * times); + callback(); + } + }, step) + } + } + + const pause = () => { + isPaused.value = true; + } + + const resume = () => { + isPaused.value = false; + } + + onUnmounted(() => { + if (timerVariableId) { + clearInterval(timerVariableId); + } + }) + + return { + start, + stop, + pause, + resume, + isPaused + } +} \ No newline at end of file diff --git a/src/hooks/useViewport.ts b/src/hooks/useViewport.ts new file mode 100644 index 0000000..fdad1d2 --- /dev/null +++ b/src/hooks/useViewport.ts @@ -0,0 +1,43 @@ +import { ref, onMounted, onUnmounted } from 'vue'; + +export enum DEVICE{ + MOBILE = 'MOBILE', + TABLET = 'TABLET', + DESKTOP = 'DESKTOP' +} + +export interface DeviceSize { + mobile: number | null, + tablet: number | null, +} + +export const useViewport = (config: DeviceSize) => { + const { mobile = null, tablet = null } = config; + let mobileWidth = mobile ? mobile : 768; + let tabletWidth = tablet ? tablet : 922; + let device = ref(getDevice(window.innerWidth)); + function getDevice(width: number) { + if (width < mobileWidth) { + return DEVICE.MOBILE; + } else if (width < tabletWidth) { + return DEVICE.TABLET; + } + return DEVICE.DESKTOP; + } + + const handleResize = () => { + device.value = getDevice(window.innerWidth); + } + + onMounted(() => { + window.addEventListener('resize', handleResize); + }); + + onUnmounted(() => { + window.removeEventListener('resize', handleResize); + }); + + return { + device + } +} \ No newline at end of file diff --git a/src/hooks/useWindowResize.ts b/src/hooks/useWindowResize.ts new file mode 100644 index 0000000..426cc94 --- /dev/null +++ b/src/hooks/useWindowResize.ts @@ -0,0 +1,24 @@ +import { ref, onMounted, onUnmounted } from 'vue'; + +export function useWindowResize() { + const width = ref(window.innerWidth); + const height = ref(window.innerHeight); + + const handleResize = () => { + width.value = window.innerWidth; + height.value = window.innerHeight; + } + + onMounted(() => { + window.addEventListener('resize', handleResize) + }); + + onUnmounted(() => { + window.removeEventListener('resize', handleResize) + }) + + return { + width, + height + } +} \ No newline at end of file diff --git a/src/router/index.ts b/src/router/index.ts index a2749a9..40d2194 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -27,6 +27,9 @@ const routes = [ const router = createRouter({ history: createWebHistory("quick/"), routes, + scrollBehavior() { + document.getElementById('app')?.scrollIntoView({ behavior: 'smooth' }); + } }) export default router; \ No newline at end of file