@sohanemon/utils 2.0.0 → 2.1.0

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.
@@ -0,0 +1 @@
1
+ export { Icon as Iconify } from '@iconify/react';
@@ -0,0 +1 @@
1
+ export { Icon as Iconify } from '@iconify/react';
@@ -0,0 +1,4 @@
1
+ import { type ClassValue } from 'clsx';
2
+ export declare function cn(...inputs: ClassValue[]): string;
3
+ export declare function isNavActive(href: string, path: string): boolean;
4
+ export declare function cleanSrc(src: string): string;
@@ -0,0 +1,13 @@
1
+ import { clsx } from 'clsx';
2
+ import { twMerge } from 'tailwind-merge';
3
+ export function cn(...inputs) {
4
+ return twMerge(clsx(inputs));
5
+ }
6
+ export function isNavActive(href, path) {
7
+ return href === '/' ? path === '/' : path?.includes(href);
8
+ }
9
+ export function cleanSrc(src) {
10
+ if (src.includes('/public/'))
11
+ return src.replace('/public/', '/');
12
+ return src;
13
+ }
@@ -0,0 +1,9 @@
1
+ import { EffectCallback } from 'react';
2
+ export declare const useClickOutside: (callback?: () => void) => any;
3
+ export declare function useMediaQuery(tailwindBreakpoint: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | `(${string})`): any;
4
+ export declare const useSwiperRef: () => any[];
5
+ export declare function useEffectOnce(effect: EffectCallback): void;
6
+ export declare function useUpdateEffect(effect: EffectCallback, deps: any[]): void;
7
+ export declare const useIsomorphicEffect: any;
8
+ export declare function useTimeout(callback: () => void, delay?: number | null): void;
9
+ export declare function useWindowEvent<K extends string = keyof WindowEventMap>(type: K, listener: K extends keyof WindowEventMap ? (this: Window, ev: WindowEventMap[K]) => void : (this: Window, ev: CustomEvent) => void, options?: boolean | AddEventListenerOptions): void;
@@ -0,0 +1,115 @@
1
+ import { useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
2
+ export const useClickOutside = (callback = () => alert('clicked outside')) => {
3
+ const ref = useRef(null);
4
+ const listener = (e) => {
5
+ if (ref.current && !ref.current.contains(e.target)) {
6
+ callback();
7
+ }
8
+ };
9
+ useEffect(() => {
10
+ document.addEventListener('mousedown', listener);
11
+ document.addEventListener('touchstart', listener);
12
+ return () => {
13
+ document.removeEventListener('mousedown', listener);
14
+ document.removeEventListener('touchstart', listener);
15
+ };
16
+ });
17
+ return ref;
18
+ };
19
+ export function useMediaQuery(tailwindBreakpoint) {
20
+ const parsedQuery = useMemo(() => {
21
+ switch (tailwindBreakpoint) {
22
+ case 'sm':
23
+ return '(min-width: 640px)';
24
+ case 'md':
25
+ return '(min-width: 768px)';
26
+ case 'lg':
27
+ return '(min-width: 1024px)';
28
+ case 'xl':
29
+ return '(min-width: 1280px)';
30
+ case '2xl':
31
+ return '(min-width: 1536px)';
32
+ default:
33
+ return tailwindBreakpoint;
34
+ }
35
+ }, [tailwindBreakpoint]);
36
+ const getMatches = (parsedQuery) => {
37
+ if (typeof window !== 'undefined') {
38
+ return window.matchMedia(parsedQuery).matches;
39
+ }
40
+ return false;
41
+ };
42
+ const [matches, setMatches] = useState(getMatches(parsedQuery));
43
+ function handleChange() {
44
+ setMatches(getMatches(parsedQuery));
45
+ }
46
+ useEffect(() => {
47
+ const matchMedia = window.matchMedia(parsedQuery);
48
+ handleChange();
49
+ matchMedia.addEventListener('change', handleChange);
50
+ return () => {
51
+ matchMedia.removeEventListener('change', handleChange);
52
+ };
53
+ // eslint-disable-next-line react-hooks/exhaustive-deps
54
+ }, [parsedQuery]);
55
+ return matches;
56
+ }
57
+ export const useSwiperRef = () => {
58
+ const [navigationElement, setNavigationElement] = useState(null);
59
+ const ref = useRef(null);
60
+ useEffect(() => {
61
+ setNavigationElement(ref.current);
62
+ }, []);
63
+ return [navigationElement, ref];
64
+ };
65
+ // call the hook
66
+ // const [nextEl, nextRef] = useSwiperRef();
67
+ // const [prevEl, prevRef] = useSwiperRef();
68
+ // add to navigation module
69
+ // <Swiper
70
+ // modules={[Navigation]}
71
+ // navigation={{
72
+ // prevEl,
73
+ // nextEl,
74
+ // }}
75
+ // >
76
+ // ...
77
+ // </Swiper>;
78
+ // add ref to any element which may trigger
79
+ // <button ref={nextRef}>...</button>;
80
+ export function useEffectOnce(effect) {
81
+ // eslint-disable-next-line react-hooks/exhaustive-deps
82
+ useEffect(effect, []);
83
+ }
84
+ export function useUpdateEffect(effect, deps) {
85
+ const isInitialMount = useRef(true);
86
+ useEffect(() => {
87
+ if (isInitialMount.current) {
88
+ isInitialMount.current = false;
89
+ }
90
+ else {
91
+ return effect();
92
+ }
93
+ // eslint-disable-next-line react-hooks/exhaustive-deps
94
+ }, deps);
95
+ }
96
+ export const useIsomorphicEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
97
+ export function useTimeout(callback, delay = 1000) {
98
+ const savedCallback = useRef(callback);
99
+ useIsomorphicEffect(() => {
100
+ savedCallback.current = callback;
101
+ }, [callback]);
102
+ useEffect(() => {
103
+ if (!delay && delay !== 0) {
104
+ return;
105
+ }
106
+ const id = setTimeout(() => savedCallback.current(), delay);
107
+ return () => clearTimeout(id);
108
+ }, [delay]);
109
+ }
110
+ export function useWindowEvent(type, listener, options) {
111
+ useEffect(() => {
112
+ window.addEventListener(type, listener, options);
113
+ return () => window.removeEventListener(type, listener, options);
114
+ }, [type, listener]);
115
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- import { type ClassValue } from 'clsx';
2
- export declare function cn(...inputs: ClassValue[]): string;
3
- export declare function isNavActive(href: string, path: string): boolean;
4
- export declare function cleanSrc(src: string): string;
1
+ export { Iconify } from './components';
2
+ export { cleanSrc, cn, isNavActive } from './functions';
package/dist/index.js CHANGED
@@ -1,13 +1,3 @@
1
- import { clsx } from 'clsx';
2
- import { twMerge } from 'tailwind-merge';
3
- export function cn(...inputs) {
4
- return twMerge(clsx(inputs));
5
- }
6
- export function isNavActive(href, path) {
7
- return href === '/' ? path === '/' : path?.includes(href);
8
- }
9
- export function cleanSrc(src) {
10
- if (src.includes('/public/'))
11
- return src.replace('/public/', '/');
12
- return src;
13
- }
1
+ 'use client';
2
+ export { Iconify } from './components';
3
+ export { cleanSrc, cn, isNavActive } from './functions';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./dist/index.js",
@@ -17,10 +17,10 @@
17
17
  "author": "sohanemon",
18
18
  "license": "ISC",
19
19
  "devDependencies": {
20
- "@types/node": "^20.5.7",
21
20
  "typescript": "^5.2.2"
22
21
  },
23
22
  "dependencies": {
23
+ "@iconify/react": "^4.1.1",
24
24
  "clsx": "^2.0.0",
25
25
  "react": "^18.2.0",
26
26
  "tailwind-merge": "^1.14.0"