Skip to content

Commit

Permalink
feat: calm down I store some useful hook and how to scroll to top whe…
Browse files Browse the repository at this point in the history
…n change page url
  • Loading branch information
nxhawk committed Jun 20, 2024
1 parent d3298e4 commit e97fffa
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/hooks/useCopyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions src/hooks/useNetworkStatus.ts
Original file line number Diff line number Diff line change
@@ -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);
})
}
17 changes: 17 additions & 0 deletions src/hooks/useScrollToBottom.ts
Original file line number Diff line number Diff line change
@@ -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);
});
}
38 changes: 38 additions & 0 deletions src/hooks/useStorage.ts
Original file line number Diff line number Diff line change
@@ -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)
]
}
51 changes: 51 additions & 0 deletions src/hooks/useTimer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ref, onUnmounted } from 'vue';

export const useTimer = (callback = () => { }, step = 1000) => {
let timerVariableId: ReturnType<typeof setInterval> | 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
}
}
43 changes: 43 additions & 0 deletions src/hooks/useViewport.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
24 changes: 24 additions & 0 deletions src/hooks/useWindowResize.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const routes = [
const router = createRouter({
history: createWebHistory("quick/"),
routes,
scrollBehavior() {
document.getElementById('app')?.scrollIntoView({ behavior: 'smooth' });
}
})

export default router;

0 comments on commit e97fffa

Please sign in to comment.