mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 23:15:36 +00:00
refactor: use tabs instead of spaces
This commit is contained in:
@@ -6,17 +6,17 @@ import { RefObject, useEffect } from 'react';
|
||||
* This Hook can be used for detecting clicks outside the Opened Menu
|
||||
*/
|
||||
export default function useClickOutside(
|
||||
ref: RefObject<HTMLElement>,
|
||||
onClickOutside: () => void
|
||||
ref: RefObject<HTMLElement>,
|
||||
onClickOutside: () => void
|
||||
) {
|
||||
useEffect(() => {
|
||||
function handleClickOutside(event: MouseEvent) {
|
||||
if (ref?.current && !ref.current?.contains(event.target as any)) {
|
||||
onClickOutside();
|
||||
}
|
||||
}
|
||||
useEffect(() => {
|
||||
function handleClickOutside(event: MouseEvent) {
|
||||
if (ref?.current && !ref.current?.contains(event.target as any)) {
|
||||
onClickOutside();
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, [ref, onClickOutside]);
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, [ref, onClickOutside]);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const MOBILE_SCREEN_SIZE = 768;
|
||||
|
||||
export default function useIsMobile() {
|
||||
return (
|
||||
typeof window !== 'undefined' &&
|
||||
window.matchMedia(`screen and (max-width: ${MOBILE_SCREEN_SIZE}px)`).matches
|
||||
);
|
||||
return (
|
||||
typeof window !== 'undefined' &&
|
||||
window.matchMedia(`screen and (max-width: ${MOBILE_SCREEN_SIZE}px)`).matches
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
export function useLocalStorage(key: string, initialValue: any) {
|
||||
const [storedValue, setStoredValue] = useState(() => {
|
||||
if (typeof window === 'undefined') {
|
||||
return initialValue;
|
||||
}
|
||||
try {
|
||||
const item = window.localStorage.getItem(key);
|
||||
return item ? JSON.parse(item) : initialValue;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
const [storedValue, setStoredValue] = useState(() => {
|
||||
if (typeof window === 'undefined') {
|
||||
return initialValue;
|
||||
}
|
||||
try {
|
||||
const item = window.localStorage.getItem(key);
|
||||
return item ? JSON.parse(item) : initialValue;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
|
||||
const setValue = (value: any) => {
|
||||
try {
|
||||
const valueToStore =
|
||||
value instanceof Function ? value(storedValue) : value;
|
||||
setStoredValue(valueToStore);
|
||||
if (typeof window !== 'undefined') {
|
||||
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
return [storedValue, setValue];
|
||||
const setValue = (value: any) => {
|
||||
try {
|
||||
const valueToStore =
|
||||
value instanceof Function ? value(storedValue) : value;
|
||||
setStoredValue(valueToStore);
|
||||
if (typeof window !== 'undefined') {
|
||||
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
return [storedValue, setValue];
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
const useToggle = (defaultValue: boolean = false) => {
|
||||
const [isShowing, setIsShowing] = useState<boolean>(defaultValue);
|
||||
const [isShowing, setIsShowing] = useState<boolean>(defaultValue);
|
||||
|
||||
const toggle = () => setIsShowing((value) => !value);
|
||||
const open = () => setIsShowing(true);
|
||||
const close = () => setIsShowing(false);
|
||||
const toggle = () => setIsShowing((value) => !value);
|
||||
const open = () => setIsShowing(true);
|
||||
const close = () => setIsShowing(false);
|
||||
|
||||
return {
|
||||
isShowing,
|
||||
toggle,
|
||||
open,
|
||||
close,
|
||||
};
|
||||
return {
|
||||
isShowing,
|
||||
toggle,
|
||||
open,
|
||||
close,
|
||||
};
|
||||
};
|
||||
|
||||
export default useToggle;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { usePage } from '@inertiajs/react';
|
||||
|
||||
const useSearchParam = (urlParam: string) => {
|
||||
const { url } = usePage();
|
||||
const urlParams = url.split('?');
|
||||
urlParams.shift();
|
||||
const { url } = usePage();
|
||||
const urlParams = url.split('?');
|
||||
urlParams.shift();
|
||||
|
||||
const urlSearchParam = new URLSearchParams(urlParams.join(''));
|
||||
return urlSearchParam.get(urlParam);
|
||||
const urlSearchParam = new URLSearchParams(urlParams.join(''));
|
||||
return urlSearchParam.get(urlParam);
|
||||
};
|
||||
|
||||
export default useSearchParam;
|
||||
|
||||
@@ -3,28 +3,28 @@ import { useHotkeys } from 'react-hotkeys-hook';
|
||||
import useGlobalHotkeys from '~/hooks/use_global_hotkeys';
|
||||
|
||||
type ShortcutOptions = {
|
||||
enabled?: boolean;
|
||||
disableGlobalCheck?: boolean;
|
||||
enabled?: boolean;
|
||||
disableGlobalCheck?: boolean;
|
||||
};
|
||||
|
||||
export default function useShortcut(
|
||||
key: keyof typeof KEYS,
|
||||
cb: () => void,
|
||||
{ enabled, disableGlobalCheck }: ShortcutOptions = {
|
||||
enabled: true,
|
||||
disableGlobalCheck: false,
|
||||
}
|
||||
key: keyof typeof KEYS,
|
||||
cb: () => void,
|
||||
{ enabled, disableGlobalCheck }: ShortcutOptions = {
|
||||
enabled: true,
|
||||
disableGlobalCheck: false,
|
||||
}
|
||||
) {
|
||||
const { globalHotkeysEnabled } = useGlobalHotkeys();
|
||||
return useHotkeys(
|
||||
KEYS[key],
|
||||
(event) => {
|
||||
event.preventDefault();
|
||||
cb();
|
||||
},
|
||||
{
|
||||
enabled: disableGlobalCheck ? enabled : enabled && globalHotkeysEnabled,
|
||||
enableOnFormTags: ['INPUT'],
|
||||
}
|
||||
);
|
||||
const { globalHotkeysEnabled } = useGlobalHotkeys();
|
||||
return useHotkeys(
|
||||
KEYS[key],
|
||||
(event) => {
|
||||
event.preventDefault();
|
||||
cb();
|
||||
},
|
||||
{
|
||||
enabled: disableGlobalCheck ? enabled : enabled && globalHotkeysEnabled,
|
||||
enableOnFormTags: ['INPUT'],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export function useMediaQuery(query: string): boolean {
|
||||
const [matches, setMatches] = useState<boolean>(false);
|
||||
const [matches, setMatches] = useState<boolean>(false);
|
||||
|
||||
const handleMediaChange = () => setMatches(getMediaMatches(query));
|
||||
const handleMediaChange = () => setMatches(getMediaMatches(query));
|
||||
|
||||
useEffect(() => {
|
||||
const matchMedia = window.matchMedia(query);
|
||||
handleMediaChange();
|
||||
useEffect(() => {
|
||||
const matchMedia = window.matchMedia(query);
|
||||
handleMediaChange();
|
||||
|
||||
matchMedia.addEventListener('change', handleMediaChange);
|
||||
return () => matchMedia.removeEventListener('change', handleMediaChange);
|
||||
}, [query]);
|
||||
matchMedia.addEventListener('change', handleMediaChange);
|
||||
return () => matchMedia.removeEventListener('change', handleMediaChange);
|
||||
}, [query]);
|
||||
|
||||
return matches;
|
||||
return matches;
|
||||
}
|
||||
|
||||
function getMediaMatches(query: string): boolean {
|
||||
if (typeof window !== 'undefined') {
|
||||
return window.matchMedia(query).matches;
|
||||
}
|
||||
return false;
|
||||
if (typeof window !== 'undefined') {
|
||||
return window.matchMedia(query).matches;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ import { media } from '~/styles/media_queries';
|
||||
|
||||
type ScreenTypes = keyof Theme['media'];
|
||||
const useScreenType = (screen: ScreenTypes) =>
|
||||
useMediaQuery(`(max-width: ${media[screen]})`);
|
||||
useMediaQuery(`(max-width: ${media[screen]})`);
|
||||
|
||||
export default useScreenType;
|
||||
|
||||
Reference in New Issue
Block a user