minimal-shared 0.0.2
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/README.md +3 -0
- package/dist/hooks/index.d.ts +20 -0
- package/dist/hooks/index.js +19 -0
- package/dist/hooks/use-back-to-top/index.d.ts +2 -0
- package/dist/hooks/use-back-to-top/index.js +1 -0
- package/dist/hooks/use-back-to-top/use-back-to-top.d.ts +33 -0
- package/dist/hooks/use-back-to-top/use-back-to-top.js +64 -0
- package/dist/hooks/use-boolean/index.d.ts +2 -0
- package/dist/hooks/use-boolean/index.js +1 -0
- package/dist/hooks/use-boolean/use-boolean.d.ts +29 -0
- package/dist/hooks/use-boolean/use-boolean.js +24 -0
- package/dist/hooks/use-client-rect/index.d.ts +2 -0
- package/dist/hooks/use-client-rect/index.js +1 -0
- package/dist/hooks/use-client-rect/use-client-rect.d.ts +29 -0
- package/dist/hooks/use-client-rect/use-client-rect.js +47 -0
- package/dist/hooks/use-cookies/index.d.ts +1 -0
- package/dist/hooks/use-cookies/index.js +1 -0
- package/dist/hooks/use-cookies/use-cookies.d.ts +39 -0
- package/dist/hooks/use-cookies/use-cookies.js +110 -0
- package/dist/hooks/use-copy-to-clipboard/index.d.ts +1 -0
- package/dist/hooks/use-copy-to-clipboard/index.js +1 -0
- package/dist/hooks/use-copy-to-clipboard/use-copy-to-clipboard.d.ts +26 -0
- package/dist/hooks/use-copy-to-clipboard/use-copy-to-clipboard.js +27 -0
- package/dist/hooks/use-countdown-date/index.d.ts +1 -0
- package/dist/hooks/use-countdown-date/index.js +1 -0
- package/dist/hooks/use-countdown-date/use-countdown-date.d.ts +29 -0
- package/dist/hooks/use-countdown-date/use-countdown-date.js +41 -0
- package/dist/hooks/use-countdown-seconds/index.d.ts +2 -0
- package/dist/hooks/use-countdown-seconds/index.js +1 -0
- package/dist/hooks/use-countdown-seconds/use-countdown-seconds.d.ts +35 -0
- package/dist/hooks/use-countdown-seconds/use-countdown-seconds.js +36 -0
- package/dist/hooks/use-debounce/index.d.ts +1 -0
- package/dist/hooks/use-debounce/index.js +1 -0
- package/dist/hooks/use-debounce/use-debounce.d.ts +21 -0
- package/dist/hooks/use-debounce/use-debounce.js +17 -0
- package/dist/hooks/use-double-click/index.d.ts +2 -0
- package/dist/hooks/use-double-click/index.js +1 -0
- package/dist/hooks/use-double-click/use-double-click.d.ts +28 -0
- package/dist/hooks/use-double-click/use-double-click.js +33 -0
- package/dist/hooks/use-event-listener/index.d.ts +2 -0
- package/dist/hooks/use-event-listener/index.js +1 -0
- package/dist/hooks/use-event-listener/use-event-listener.d.ts +7 -0
- package/dist/hooks/use-event-listener/use-event-listener.js +23 -0
- package/dist/hooks/use-is-client/index.d.ts +1 -0
- package/dist/hooks/use-is-client/index.js +1 -0
- package/dist/hooks/use-is-client/use-is-client.d.ts +18 -0
- package/dist/hooks/use-is-client/use-is-client.js +12 -0
- package/dist/hooks/use-local-storage/index.d.ts +1 -0
- package/dist/hooks/use-local-storage/index.js +1 -0
- package/dist/hooks/use-local-storage/use-local-storage.d.ts +37 -0
- package/dist/hooks/use-local-storage/use-local-storage.js +113 -0
- package/dist/hooks/use-multi-select/index.d.ts +2 -0
- package/dist/hooks/use-multi-select/index.js +1 -0
- package/dist/hooks/use-multi-select/use-multi-select.d.ts +55 -0
- package/dist/hooks/use-multi-select/use-multi-select.js +36 -0
- package/dist/hooks/use-popover/index.d.ts +2 -0
- package/dist/hooks/use-popover/index.js +1 -0
- package/dist/hooks/use-popover/use-popover.d.ts +35 -0
- package/dist/hooks/use-popover/use-popover.js +21 -0
- package/dist/hooks/use-popover-hover/index.d.ts +2 -0
- package/dist/hooks/use-popover-hover/index.js +1 -0
- package/dist/hooks/use-popover-hover/use-popover-hover.d.ts +41 -0
- package/dist/hooks/use-popover-hover/use-popover-hover.js +24 -0
- package/dist/hooks/use-scroll-offset-top/index.d.ts +2 -0
- package/dist/hooks/use-scroll-offset-top/index.js +1 -0
- package/dist/hooks/use-scroll-offset-top/use-scroll-offset-top.d.ts +28 -0
- package/dist/hooks/use-scroll-offset-top/use-scroll-offset-top.js +29 -0
- package/dist/hooks/use-set-state/index.d.ts +1 -0
- package/dist/hooks/use-set-state/index.js +1 -0
- package/dist/hooks/use-set-state/use-set-state.d.ts +32 -0
- package/dist/hooks/use-set-state/use-set-state.js +26 -0
- package/dist/hooks/use-tabs/index.d.ts +2 -0
- package/dist/hooks/use-tabs/index.js +1 -0
- package/dist/hooks/use-tabs/use-tabs.d.ts +30 -0
- package/dist/hooks/use-tabs/use-tabs.js +16 -0
- package/dist/hooks/use-text-input/index.d.ts +2 -0
- package/dist/hooks/use-text-input/index.js +1 -0
- package/dist/hooks/use-text-input/use-text-input.d.ts +16 -0
- package/dist/hooks/use-text-input/use-text-input.js +16 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +2 -0
- package/dist/utils/active-link/active-link.d.ts +16 -0
- package/dist/utils/active-link/active-link.js +43 -0
- package/dist/utils/active-link/index.d.ts +1 -0
- package/dist/utils/active-link/index.js +1 -0
- package/dist/utils/classes/classes.d.ts +25 -0
- package/dist/utils/classes/classes.js +14 -0
- package/dist/utils/classes/index.d.ts +1 -0
- package/dist/utils/classes/index.js +1 -0
- package/dist/utils/color/color.d.ts +67 -0
- package/dist/utils/color/color.js +47 -0
- package/dist/utils/color/index.d.ts +1 -0
- package/dist/utils/color/index.js +1 -0
- package/dist/utils/cookies/cookies.d.ts +34 -0
- package/dist/utils/cookies/cookies.js +46 -0
- package/dist/utils/cookies/index.d.ts +1 -0
- package/dist/utils/cookies/index.js +1 -0
- package/dist/utils/font/font.d.ts +37 -0
- package/dist/utils/font/font.js +20 -0
- package/dist/utils/font/index.d.ts +1 -0
- package/dist/utils/font/index.js +1 -0
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/local-storage/index.d.ts +1 -0
- package/dist/utils/local-storage/index.js +1 -0
- package/dist/utils/local-storage/local-storage.d.ts +55 -0
- package/dist/utils/local-storage/local-storage.js +51 -0
- package/dist/utils/object/index.d.ts +1 -0
- package/dist/utils/object/index.js +1 -0
- package/dist/utils/object/object.d.ts +26 -0
- package/dist/utils/object/object.js +10 -0
- package/dist/utils/url/index.d.ts +1 -0
- package/dist/utils/url/index.js +1 -0
- package/dist/utils/url/url.d.ts +46 -0
- package/dist/utils/url/url.js +28 -0
- package/dist/utils/uuidv4/index.d.ts +1 -0
- package/dist/utils/uuidv4/index.js +1 -0
- package/dist/utils/uuidv4/uuidv4.d.ts +12 -0
- package/dist/utils/uuidv4/uuidv4.js +11 -0
- package/package.json +87 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/hooks/use-scroll-offset-top/use-scroll-offset-top.ts
|
|
2
|
+
import { useRef, useState, useEffect, useCallback } from "react";
|
|
3
|
+
function useScrollOffsetTop(defaultValue = 0) {
|
|
4
|
+
const elementRef = useRef(null);
|
|
5
|
+
const [offsetTop, setOffsetTop] = useState(false);
|
|
6
|
+
const handleScroll = useCallback(() => {
|
|
7
|
+
const windowScrollY = window.scrollY;
|
|
8
|
+
if (elementRef.current) {
|
|
9
|
+
const elementOffsetTop = elementRef.current.offsetTop;
|
|
10
|
+
setOffsetTop(windowScrollY > elementOffsetTop - defaultValue);
|
|
11
|
+
} else {
|
|
12
|
+
setOffsetTop(windowScrollY > defaultValue);
|
|
13
|
+
}
|
|
14
|
+
}, [defaultValue]);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
handleScroll();
|
|
17
|
+
window.addEventListener("scroll", handleScroll);
|
|
18
|
+
return () => {
|
|
19
|
+
window.removeEventListener("scroll", handleScroll);
|
|
20
|
+
};
|
|
21
|
+
}, [handleScroll]);
|
|
22
|
+
return {
|
|
23
|
+
elementRef,
|
|
24
|
+
offsetTop
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
useScrollOffsetTop
|
|
29
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { UseSetStateReturn, useSetState } from './use-set-state.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './use-set-state';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom hook to manage state with utility functions to set state, set a specific field, and reset state.
|
|
3
|
+
*
|
|
4
|
+
* @param {T} initialState - The initial state value.
|
|
5
|
+
*
|
|
6
|
+
* @returns {UseSetStateReturn<T>} - An object containing:
|
|
7
|
+
* - `state`: The current state.
|
|
8
|
+
* - `onReset`: A function to reset the state to the initial value.
|
|
9
|
+
* - `setState`: A function to update the state.
|
|
10
|
+
* - `setField`: A function to update a specific field in the state.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const { state, setState, setField, onReset } = useSetState({ name: '', age: 0 });
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* <div>
|
|
17
|
+
* <p>Name: {state.name}</p>
|
|
18
|
+
* <p>Age: {state.age}</p>
|
|
19
|
+
* <button onClick={() => setField('name', 'John')}>Set Name</button>
|
|
20
|
+
* <button onClick={onReset}>Reset</button>
|
|
21
|
+
* </div>
|
|
22
|
+
* );
|
|
23
|
+
*/
|
|
24
|
+
type UseSetStateReturn<T> = {
|
|
25
|
+
state: T;
|
|
26
|
+
onReset: () => void;
|
|
27
|
+
setState: (updateState: T | Partial<T>) => void;
|
|
28
|
+
setField: (name: keyof T, updateValue: T[keyof T]) => void;
|
|
29
|
+
};
|
|
30
|
+
declare function useSetState<T>(initialState: T): UseSetStateReturn<T>;
|
|
31
|
+
|
|
32
|
+
export { type UseSetStateReturn, useSetState };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// src/hooks/use-set-state/use-set-state.ts
|
|
2
|
+
import { useState, useCallback } from "react";
|
|
3
|
+
function useSetState(initialState) {
|
|
4
|
+
const [state, set] = useState(initialState);
|
|
5
|
+
const setState = useCallback((updateState) => {
|
|
6
|
+
set((prevValue) => ({ ...prevValue, ...updateState }));
|
|
7
|
+
}, []);
|
|
8
|
+
const setField = useCallback(
|
|
9
|
+
(name, updateValue) => {
|
|
10
|
+
setState({ [name]: updateValue });
|
|
11
|
+
},
|
|
12
|
+
[setState]
|
|
13
|
+
);
|
|
14
|
+
const onReset = useCallback(() => {
|
|
15
|
+
set(initialState);
|
|
16
|
+
}, [initialState]);
|
|
17
|
+
return {
|
|
18
|
+
state,
|
|
19
|
+
setState,
|
|
20
|
+
setField,
|
|
21
|
+
onReset
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export {
|
|
25
|
+
useSetState
|
|
26
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './use-tabs';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction, SyntheticEvent } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook to manage the state of tabs.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} defaultValue - The initial value of the tab.
|
|
7
|
+
*
|
|
8
|
+
* @returns {UseTabsReturn} - An object containing:
|
|
9
|
+
* - `value`: The current value of the tab.
|
|
10
|
+
* - `setValue`: A function to manually set the value of the tab.
|
|
11
|
+
* - `onChange`: A function to handle the change event when a new tab is selected.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const { value, onChange } = useTabs('tab1');
|
|
15
|
+
*
|
|
16
|
+
* return (
|
|
17
|
+
* <Tabs value={value} onChange={onChange}>
|
|
18
|
+
* <Tab label="Tab 1" value="tab1" />
|
|
19
|
+
* <Tab label="Tab 2" value="tab2" />
|
|
20
|
+
* </Tabs>
|
|
21
|
+
* );
|
|
22
|
+
*/
|
|
23
|
+
type UseTabsReturn = {
|
|
24
|
+
value: string;
|
|
25
|
+
setValue: Dispatch<SetStateAction<string>>;
|
|
26
|
+
onChange: (event: SyntheticEvent, newValue: string) => void;
|
|
27
|
+
};
|
|
28
|
+
declare function useTabs(defaultValue: string): UseTabsReturn;
|
|
29
|
+
|
|
30
|
+
export { type UseTabsReturn, useTabs };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/hooks/use-tabs/use-tabs.ts
|
|
2
|
+
import { useState, useCallback } from "react";
|
|
3
|
+
function useTabs(defaultValue) {
|
|
4
|
+
const [value, setValue] = useState(defaultValue);
|
|
5
|
+
const onChange = useCallback((event, newValue) => {
|
|
6
|
+
setValue(newValue);
|
|
7
|
+
}, []);
|
|
8
|
+
return {
|
|
9
|
+
value,
|
|
10
|
+
setValue,
|
|
11
|
+
onChange
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
useTabs
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './use-text-input';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook to manage text input state.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} [defaultValue=''] - The default value for the input.
|
|
7
|
+
* @returns {UseTextInputReturn} The current value, change handler, and setValue function.
|
|
8
|
+
*/
|
|
9
|
+
type UseTextInputReturn = {
|
|
10
|
+
value: string;
|
|
11
|
+
setValue: Dispatch<SetStateAction<string>>;
|
|
12
|
+
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
|
13
|
+
};
|
|
14
|
+
declare function useTextInput(defaultValue?: string): UseTextInputReturn;
|
|
15
|
+
|
|
16
|
+
export { type UseTextInputReturn, useTextInput };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/hooks/use-text-input/use-text-input.ts
|
|
2
|
+
import { useState, useCallback } from "react";
|
|
3
|
+
function useTextInput(defaultValue = "") {
|
|
4
|
+
const [value, setValue] = useState(defaultValue);
|
|
5
|
+
const onChange = useCallback((event) => {
|
|
6
|
+
setValue(event.target.value);
|
|
7
|
+
}, []);
|
|
8
|
+
return {
|
|
9
|
+
value,
|
|
10
|
+
setValue,
|
|
11
|
+
onChange
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
useTextInput
|
|
16
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export { hasParams, isExternalLink, removeLastSlash, removeParams } from './utils/url/url.js';
|
|
2
|
+
export { pxToRem, remToPx, setFont } from './utils/font/font.js';
|
|
3
|
+
export { ChannelPalette, InputPalette, createPaletteChannel, hexToRgbChannel, varAlpha } from './utils/color/color.js';
|
|
4
|
+
export { hasKeys } from './utils/object/object.js';
|
|
5
|
+
export { uuidv4 } from './utils/uuidv4/uuidv4.js';
|
|
6
|
+
export { StateProps, mergeClasses } from './utils/classes/classes.js';
|
|
7
|
+
export { getCookie, removeCookie, setCookie } from './utils/cookies/cookies.js';
|
|
8
|
+
export { isActiveLink } from './utils/active-link/active-link.js';
|
|
9
|
+
export { getStorage, localStorageAvailable, localStorageGetItem, removeStorage, setStorage } from './utils/local-storage/local-storage.js';
|
|
10
|
+
export { UseTabsReturn, useTabs } from './hooks/use-tabs/use-tabs.js';
|
|
11
|
+
export { UseBooleanReturn, useBoolean } from './hooks/use-boolean/use-boolean.js';
|
|
12
|
+
export { UsePopoverReturn, usePopover } from './hooks/use-popover/use-popover.js';
|
|
13
|
+
export { UseCookiesReturn, useCookies } from './hooks/use-cookies/use-cookies.js';
|
|
14
|
+
export { UseDebounceReturn, useDebounce } from './hooks/use-debounce/use-debounce.js';
|
|
15
|
+
export { UseSetStateReturn, useSetState } from './hooks/use-set-state/use-set-state.js';
|
|
16
|
+
export { UseIsClientReturn, useIsClient } from './hooks/use-is-client/use-is-client.js';
|
|
17
|
+
export { UseTextInputReturn, useTextInput } from './hooks/use-text-input/use-text-input.js';
|
|
18
|
+
export { UseBackToTopReturn, useBackToTop } from './hooks/use-back-to-top/use-back-to-top.js';
|
|
19
|
+
export { UseClientRectReturn, useClientRect } from './hooks/use-client-rect/use-client-rect.js';
|
|
20
|
+
export { UseMultiSelectReturn, updateSelectedItems, useMultiSelect } from './hooks/use-multi-select/use-multi-select.js';
|
|
21
|
+
export { UseDoubleClickReturn, useDoubleClick } from './hooks/use-double-click/use-double-click.js';
|
|
22
|
+
export { UseLocalStorageReturn, useLocalStorage } from './hooks/use-local-storage/use-local-storage.js';
|
|
23
|
+
export { usePopoverHover } from './hooks/use-popover-hover/use-popover-hover.js';
|
|
24
|
+
export { UseCountdownDateReturn, useCountdownDate } from './hooks/use-countdown-date/use-countdown-date.js';
|
|
25
|
+
export { useEventListener } from './hooks/use-event-listener/use-event-listener.js';
|
|
26
|
+
export { UseScrollOffsetTopReturn, useScrollOffsetTop } from './hooks/use-scroll-offset-top/use-scroll-offset-top.js';
|
|
27
|
+
export { UseCountdownSecondsReturn, useCountdownSeconds } from './hooks/use-countdown-seconds/use-countdown-seconds.js';
|
|
28
|
+
export { CopiedValue, CopyFn, UseCopyToClipboardReturn, useCopyToClipboard } from './hooks/use-copy-to-clipboard/use-copy-to-clipboard.js';
|
|
29
|
+
import 'react';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if a given link is active based on the current pathname.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} pathnameProps - The current pathname.
|
|
5
|
+
* @param {string} itemPath - The path of the item to check.
|
|
6
|
+
* @param {boolean} [deep=true] - Whether to perform a deep check, including child paths and parameters.
|
|
7
|
+
*
|
|
8
|
+
* @returns {boolean} - True if the link is active, false otherwise.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const isActive = isActiveLink('/dashboard/user', '/dashboard/user', true);
|
|
12
|
+
* console.log(isActive); // true
|
|
13
|
+
*/
|
|
14
|
+
declare function isActiveLink(pathnameProps: string, itemPath: string, deep?: boolean): boolean;
|
|
15
|
+
|
|
16
|
+
export { isActiveLink };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/utils/url/url.ts
|
|
2
|
+
var hasParams = (url) => {
|
|
3
|
+
const queryString = url.split("?")[1];
|
|
4
|
+
return queryString ? new URLSearchParams(queryString).toString().length > 0 : false;
|
|
5
|
+
};
|
|
6
|
+
function removeLastSlash(pathname) {
|
|
7
|
+
if (pathname !== "/" && pathname.endsWith("/")) {
|
|
8
|
+
return pathname.slice(0, -1);
|
|
9
|
+
}
|
|
10
|
+
return pathname;
|
|
11
|
+
}
|
|
12
|
+
function removeParams(url) {
|
|
13
|
+
try {
|
|
14
|
+
const urlObj = new URL(url, window.location.origin);
|
|
15
|
+
return removeLastSlash(urlObj.pathname);
|
|
16
|
+
} catch {
|
|
17
|
+
return url;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function isExternalLink(url) {
|
|
21
|
+
return url.startsWith("http");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/utils/active-link/active-link.ts
|
|
25
|
+
function isActiveLink(pathnameProps, itemPath, deep = true) {
|
|
26
|
+
const pathname = removeLastSlash(pathnameProps);
|
|
27
|
+
const pathHasParams = hasParams(itemPath);
|
|
28
|
+
const notValid = itemPath.startsWith("#") || isExternalLink(itemPath);
|
|
29
|
+
if (notValid) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const isDeep = deep || pathHasParams;
|
|
33
|
+
if (isDeep) {
|
|
34
|
+
const defaultActive = pathname.includes(itemPath);
|
|
35
|
+
const originItemPath = removeParams(itemPath);
|
|
36
|
+
const hasParamsActive = pathHasParams && originItemPath === pathname;
|
|
37
|
+
return defaultActive || hasParamsActive;
|
|
38
|
+
}
|
|
39
|
+
return pathname === itemPath;
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
isActiveLink
|
|
43
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isActiveLink } from './active-link.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './active-link';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
type StateProps = {
|
|
2
|
+
[key: string]: boolean | undefined | [boolean, string];
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* Merges class names with state-based class names.
|
|
6
|
+
*
|
|
7
|
+
* @param {string | string[] | null} className - The base class name(s).
|
|
8
|
+
* @param {StateProps} state - The state object containing boolean or [boolean, string] pairs.
|
|
9
|
+
* @returns {string} - The merged class names.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* const classNames = mergeClasses('item__base', {
|
|
14
|
+
* ['active__class']: true,
|
|
15
|
+
* ['open__class']: true,
|
|
16
|
+
* ['disabled__class']: false,
|
|
17
|
+
* ['hover__class']: undefined,
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* console.log(classNames);
|
|
21
|
+
* Output: 'item__base active__class open__class'
|
|
22
|
+
*/
|
|
23
|
+
declare function mergeClasses(className?: string | (string | undefined)[] | null, state?: StateProps): string;
|
|
24
|
+
|
|
25
|
+
export { type StateProps, mergeClasses };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/utils/classes/classes.ts
|
|
2
|
+
function mergeClasses(className, state) {
|
|
3
|
+
const classList = className ? Array.isArray(className) ? className : [className] : [];
|
|
4
|
+
const dynamicStateClassesArray = Object.entries(state || {}).filter(([key, value]) => value !== void 0 && value !== false).map(([key, value]) => {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
return value[0] ? value[1] : "";
|
|
7
|
+
}
|
|
8
|
+
return value ? key : "";
|
|
9
|
+
}).filter(Boolean);
|
|
10
|
+
return [...classList.filter(Boolean), ...dynamicStateClassesArray].join(" ");
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
mergeClasses
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { StateProps, mergeClasses } from './classes.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './classes';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a hex color to RGB channels.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} hexColor - The hex color string.
|
|
5
|
+
* @returns {string} - The RGB channels string.
|
|
6
|
+
* @throws {Error} - Throws an error if the hex color is invalid.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const rgbChannel = hexToRgbChannel("#C8FAD6");
|
|
10
|
+
* console.log(rgbChannel); // "200 250 214"
|
|
11
|
+
*/
|
|
12
|
+
declare function hexToRgbChannel(hexColor: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a hex palette color to RGB channels palette.
|
|
15
|
+
*
|
|
16
|
+
* @typedef {Object} InputPalette - The input palette object with hex color strings.
|
|
17
|
+
* @property {string} lighter - The lighter hex color.
|
|
18
|
+
* @property {string} light - The light hex color.
|
|
19
|
+
* @property {string} main - The main hex color.
|
|
20
|
+
*
|
|
21
|
+
* @typedef {Object} ChannelPalette - The output palette object with RGB channels.
|
|
22
|
+
* @property {string} lighterChannel - The lighter RGB channels.
|
|
23
|
+
* @property {string} lightChannel - The light RGB channels.
|
|
24
|
+
* @property {string} mainChannel - The main RGB channels.
|
|
25
|
+
*
|
|
26
|
+
* @param {InputPalette} hexPalette - The input palette object.
|
|
27
|
+
* @returns {ChannelPalette} - The output palette object with RGB channels.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const palette = createPaletteChannel({
|
|
31
|
+
* lighter: "#C8FAD6",
|
|
32
|
+
* light: "#5BE49B",
|
|
33
|
+
* main: "#00A76F",
|
|
34
|
+
* });
|
|
35
|
+
* console.log(palette);
|
|
36
|
+
* // {
|
|
37
|
+
* // lighter: "#C8FAD6",
|
|
38
|
+
* // light: "#5BE49B",
|
|
39
|
+
* // main: "#00A76F",
|
|
40
|
+
* // lighterChannel: "200 250 214",
|
|
41
|
+
* // lightChannel: "91 228 155",
|
|
42
|
+
* // mainChannel: "0 167 111",
|
|
43
|
+
* // }
|
|
44
|
+
*/
|
|
45
|
+
type InputPalette = Record<string, string | undefined>;
|
|
46
|
+
type ChannelPalette<T extends InputPalette> = T & {
|
|
47
|
+
[K in keyof T as `${string & K}Channel`]: string;
|
|
48
|
+
};
|
|
49
|
+
declare function createPaletteChannel<T extends InputPalette>(hexPalette: T): ChannelPalette<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Adds an alpha channel to a color.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} color - The color string in RGB channels or CSS variable format.
|
|
54
|
+
* @param {number} [opacity=1] - The opacity value.
|
|
55
|
+
* @returns {string} - The color string with alpha channel.
|
|
56
|
+
* @throws {Error} - Throws an error if the color format is unsupported.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const rgbaColor = varAlpha('200 250 214', 0.8);
|
|
60
|
+
* console.log(rgbaColor); // "rgba(200 250 214 / 0.8)"
|
|
61
|
+
*
|
|
62
|
+
* const rgbaVarColor = varAlpha('var(--palette-primary-lighterChannel)', 0.8);
|
|
63
|
+
* console.log(rgbaVarColor); // "rgba(var(--palette-primary-lighterChannel) / 0.8)"
|
|
64
|
+
*/
|
|
65
|
+
declare function varAlpha(color: string, opacity?: number): string;
|
|
66
|
+
|
|
67
|
+
export { type ChannelPalette, type InputPalette, createPaletteChannel, hexToRgbChannel, varAlpha };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/utils/color/color.ts
|
|
2
|
+
function hexToRgbChannel(hexColor) {
|
|
3
|
+
if (!hexColor) {
|
|
4
|
+
throw new Error("Hex color is undefined!");
|
|
5
|
+
}
|
|
6
|
+
if (!/^#[0-9A-F]{6}$/i.test(hexColor)) {
|
|
7
|
+
throw new Error(`Invalid hex color: ${hexColor}`);
|
|
8
|
+
}
|
|
9
|
+
const r = parseInt(hexColor.substring(1, 3), 16);
|
|
10
|
+
const g = parseInt(hexColor.substring(3, 5), 16);
|
|
11
|
+
const b = parseInt(hexColor.substring(5, 7), 16);
|
|
12
|
+
return `${r} ${g} ${b}`;
|
|
13
|
+
}
|
|
14
|
+
function createPaletteChannel(hexPalette) {
|
|
15
|
+
const channelPalette = {};
|
|
16
|
+
Object.entries(hexPalette).forEach(([key, value]) => {
|
|
17
|
+
if (value) {
|
|
18
|
+
channelPalette[`${key}Channel`] = hexToRgbChannel(value);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return { ...hexPalette, ...channelPalette };
|
|
22
|
+
}
|
|
23
|
+
function varAlpha(color, opacity = 1) {
|
|
24
|
+
if (!color) {
|
|
25
|
+
throw new Error("[Alpha]: Color is undefined!");
|
|
26
|
+
}
|
|
27
|
+
const unsupported = color.startsWith("#") || color.startsWith("rgb") || color.startsWith("rgba") || !color.includes("var") && color.includes("Channel");
|
|
28
|
+
if (unsupported) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`[Alpha]: Unsupported color format "${color}".
|
|
31
|
+
Supported formats are:
|
|
32
|
+
- RGB channels: "0 184 217".
|
|
33
|
+
- CSS variables with "Channel" prefix: "var(--palette-common-blackChannel, #000000)".
|
|
34
|
+
Unsupported formats are:
|
|
35
|
+
- Hex: "#00B8D9".
|
|
36
|
+
- RGB: "rgb(0, 184, 217)".
|
|
37
|
+
- RGBA: "rgba(0, 184, 217, 1)".
|
|
38
|
+
`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return `rgba(${color} / ${opacity})`;
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
createPaletteChannel,
|
|
45
|
+
hexToRgbChannel,
|
|
46
|
+
varAlpha
|
|
47
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ChannelPalette, InputPalette, createPaletteChannel, hexToRgbChannel, varAlpha } from './color.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './color';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieves a cookie value by key.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} key - The key of the cookie to retrieve.
|
|
5
|
+
* @returns {any | null} - The parsed value of the cookie, or null if not found or an error occurs.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const user = getCookie('user');
|
|
9
|
+
* console.log(user); // { name: 'John', age: 30 }
|
|
10
|
+
*/
|
|
11
|
+
declare function getCookie(key: string): any | null;
|
|
12
|
+
/**
|
|
13
|
+
* Sets a cookie with a specified key, value, and expiration time.
|
|
14
|
+
*
|
|
15
|
+
* @template T
|
|
16
|
+
* @param {string} key - The key of the cookie to set.
|
|
17
|
+
* @param {T} value - The value of the cookie to set.
|
|
18
|
+
* @param {number} [daysUntilExpiration=0] - The number of days until the cookie expires. Defaults to session cookie if not set.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* setCookie('user', { name: 'John', age: 30 }, 7);
|
|
22
|
+
*/
|
|
23
|
+
declare function setCookie<T>(key: string, value: T, daysUntilExpiration?: number): void;
|
|
24
|
+
/**
|
|
25
|
+
* Removes a cookie by key.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} key - The key of the cookie to remove.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* removeCookie('user');
|
|
31
|
+
*/
|
|
32
|
+
declare function removeCookie(key: string): void;
|
|
33
|
+
|
|
34
|
+
export { getCookie, removeCookie, setCookie };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/utils/cookies/cookies.ts
|
|
2
|
+
function getCookie(key) {
|
|
3
|
+
try {
|
|
4
|
+
const keyName = `${key}=`;
|
|
5
|
+
const cDecoded = decodeURIComponent(document.cookie);
|
|
6
|
+
const cArr = cDecoded.split("; ");
|
|
7
|
+
for (const val of cArr) {
|
|
8
|
+
if (val.startsWith(keyName)) {
|
|
9
|
+
const cookieValue = val.substring(keyName.length);
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(cookieValue);
|
|
12
|
+
} catch {
|
|
13
|
+
return cookieValue;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
} catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
function setCookie(key, value, daysUntilExpiration = 0) {
|
|
23
|
+
try {
|
|
24
|
+
const serializedValue = encodeURIComponent(JSON.stringify(value));
|
|
25
|
+
let cookieOptions = `${key}=${serializedValue}; path=/`;
|
|
26
|
+
if (daysUntilExpiration > 0) {
|
|
27
|
+
const expirationDate = new Date(Date.now() + daysUntilExpiration * 24 * 60 * 60 * 1e3);
|
|
28
|
+
cookieOptions += `; expires=${expirationDate.toUTCString()}`;
|
|
29
|
+
}
|
|
30
|
+
document.cookie = cookieOptions;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error("Error while setting cookie:", error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function removeCookie(key) {
|
|
36
|
+
try {
|
|
37
|
+
document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error("Error while removing cookie:", error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
getCookie,
|
|
44
|
+
removeCookie,
|
|
45
|
+
setCookie
|
|
46
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getCookie, removeCookie, setCookie } from './cookies.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './cookies';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets the font family.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} [fontName] - The name of the font to set.
|
|
5
|
+
* @returns {string} - The complete font family string.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const fontFamily = setFont('CustomFont');
|
|
9
|
+
* console.log(fontFamily); // "CustomFont, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol""
|
|
10
|
+
*/
|
|
11
|
+
declare function setFont(fontName?: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Converts rem to px.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} value - The rem value to convert.
|
|
16
|
+
* @returns {number} - The equivalent value in pixels.
|
|
17
|
+
* @throws {Error} - Throws an error if the rem value is invalid.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const pixels = remToPx('1.5rem');
|
|
21
|
+
* console.log(pixels); // 24
|
|
22
|
+
*/
|
|
23
|
+
declare function remToPx(value: string): number;
|
|
24
|
+
/**
|
|
25
|
+
* Converts px to rem.
|
|
26
|
+
*
|
|
27
|
+
* @param {number} value - The pixel value to convert.
|
|
28
|
+
* @returns {string} - The equivalent value in rem.
|
|
29
|
+
* @throws {Error} - Throws an error if the pixel value is invalid.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const remValue = pxToRem(24);
|
|
33
|
+
* console.log(remValue); // "1.5rem"
|
|
34
|
+
*/
|
|
35
|
+
declare function pxToRem(value: number): string;
|
|
36
|
+
|
|
37
|
+
export { pxToRem, remToPx, setFont };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/utils/font/font.ts
|
|
2
|
+
var DEFAULT_FONT_FAMILY = `-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"`;
|
|
3
|
+
function setFont(fontName) {
|
|
4
|
+
return fontName ? `"${fontName}", ${DEFAULT_FONT_FAMILY}` : DEFAULT_FONT_FAMILY;
|
|
5
|
+
}
|
|
6
|
+
function remToPx(value) {
|
|
7
|
+
const remValue = parseFloat(value);
|
|
8
|
+
return Math.round(remValue * 16);
|
|
9
|
+
}
|
|
10
|
+
function pxToRem(value) {
|
|
11
|
+
if (typeof value !== "number" || isNaN(value)) {
|
|
12
|
+
throw new Error(`Invalid pixel value: ${value}`);
|
|
13
|
+
}
|
|
14
|
+
return `${value / 16}rem`;
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
pxToRem,
|
|
18
|
+
remToPx,
|
|
19
|
+
setFont
|
|
20
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { pxToRem, remToPx, setFont } from './font.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './font';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { hasParams, isExternalLink, removeLastSlash, removeParams } from './url/url.js';
|
|
2
|
+
export { pxToRem, remToPx, setFont } from './font/font.js';
|
|
3
|
+
export { ChannelPalette, InputPalette, createPaletteChannel, hexToRgbChannel, varAlpha } from './color/color.js';
|
|
4
|
+
export { hasKeys } from './object/object.js';
|
|
5
|
+
export { uuidv4 } from './uuidv4/uuidv4.js';
|
|
6
|
+
export { StateProps, mergeClasses } from './classes/classes.js';
|
|
7
|
+
export { getCookie, removeCookie, setCookie } from './cookies/cookies.js';
|
|
8
|
+
export { isActiveLink } from './active-link/active-link.js';
|
|
9
|
+
export { getStorage, localStorageAvailable, localStorageGetItem, removeStorage, setStorage } from './local-storage/local-storage.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from './url';
|
|
2
|
+
|
|
3
|
+
export * from './font';
|
|
4
|
+
|
|
5
|
+
export * from './color';
|
|
6
|
+
|
|
7
|
+
export * from './object';
|
|
8
|
+
|
|
9
|
+
export * from './uuidv4';
|
|
10
|
+
|
|
11
|
+
export * from './classes';
|
|
12
|
+
|
|
13
|
+
export * from './cookies';
|
|
14
|
+
|
|
15
|
+
export * from './active-link';
|
|
16
|
+
|
|
17
|
+
export * from './local-storage';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getStorage, localStorageAvailable, localStorageGetItem, removeStorage, setStorage } from './local-storage.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './local-storage';
|