@websolutespa/bom-mixer-hooks 0.2.1 → 1.0.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.
- package/CHANGELOG.md +12 -0
- package/dist/index.d.ts +10 -8
- package/dist/index.js +53 -31
- package/dist/index.mjs +31 -10
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/useApi/useApi.test.tsx +4 -3
- package/src/useBreakpoint/useBreakpoint.ts +1 -1
- package/src/useCurrency/useCurrency.test.tsx +2 -1
- package/src/useCurrency/useCurrency.ts +1 -1
- package/src/useDateTimeFormat/useDateTimeFormat.test.tsx +2 -1
- package/src/useHttp/useHttp.test.tsx +1 -1
- package/src/useLabel/useLabel.test.tsx +2 -1
- package/src/useLayout/useLayout.test.tsx +2 -1
- package/src/useNumber/useNumber.test.tsx +2 -1
- package/src/useScroll/useScroll.test.tsx +1 -1
- package/src/useScroll/useScroll.ts +2 -2
- package/src/useScrollTo/useScrollTo.test.tsx +4 -4
- package/src/useScrollTo/useScrollTo.ts +2 -2
- package/src/useScrolled/useScrolled.test.tsx +34 -0
- package/src/useScrolled/useScrolled.ts +28 -0
- package/src/useUnit/useUnit.test.tsx +2 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -436,6 +436,13 @@ declare function getRealSize(element: HTMLElement | null): SizeType;
|
|
|
436
436
|
|
|
437
437
|
declare function useResize(callback: () => unknown, immediate?: boolean): void;
|
|
438
438
|
|
|
439
|
+
type SSRState = {
|
|
440
|
+
isBrowser: boolean;
|
|
441
|
+
isServer: boolean;
|
|
442
|
+
};
|
|
443
|
+
declare const detectIsBrowser: () => boolean;
|
|
444
|
+
declare function useSSR(): SSRState;
|
|
445
|
+
|
|
439
446
|
type Scroll = {
|
|
440
447
|
left: number;
|
|
441
448
|
top: number;
|
|
@@ -447,6 +454,8 @@ declare function scrollToSelector(selector?: string, offset?: number): void;
|
|
|
447
454
|
declare function scrollToRelativeY(y?: number, offset?: number): void;
|
|
448
455
|
declare function scrollToY(y?: number): void;
|
|
449
456
|
|
|
457
|
+
declare function useScrolled(callback?: (top: number, left: number) => boolean): boolean;
|
|
458
|
+
|
|
450
459
|
declare const isBrowser: boolean;
|
|
451
460
|
declare function getSearchParams(url: string, key?: string): IParams | string | null;
|
|
452
461
|
declare function updateSearchParams(currentPath: string, params: IParams): {
|
|
@@ -504,13 +513,6 @@ declare function useSorting<T>(items_: T[], callback_: (items: T[], sort?: IEqua
|
|
|
504
513
|
setSort: (sort?: IEquatable) => void;
|
|
505
514
|
};
|
|
506
515
|
|
|
507
|
-
type SSRState = {
|
|
508
|
-
isBrowser: boolean;
|
|
509
|
-
isServer: boolean;
|
|
510
|
-
};
|
|
511
|
-
declare const detectIsBrowser: () => boolean;
|
|
512
|
-
declare function useSSR(): SSRState;
|
|
513
|
-
|
|
514
516
|
declare function useSwiperIndex(): [number, (index: number) => void];
|
|
515
517
|
|
|
516
518
|
/**
|
|
@@ -548,4 +550,4 @@ type WindowSize = {
|
|
|
548
550
|
};
|
|
549
551
|
declare function useWindowSize(): WindowSize;
|
|
550
552
|
|
|
551
|
-
export { BodyScrollOptions, CartContext, CartProvider, CheckoutContext, CheckoutProvider, ClickOutGetContainer, CurrentStateType, ElementStackItem, Filter, FilterMode, FilterParams, FilterValues, GeoLocationSensorState, IFilter, IFilterOption, IGeolocationPositionError, ILabelContext, ILayoutContext, IPageContext, IPaginationInfo, IParams, IParamsEncoded, IUseSearchParamsEncodedResult, IUseSearchParamsResult, IUseSearchParamsValueResult, KeyCode, KeyMod, KeyboardOptions, KeyboardResult, LabelProvider, LayoutProvider, PageProvider, SSRState, SizeResult, SizeType, Sorting, UIReducer, UIValue, UseFiltersResultType, UseKeyboard, UseKeyboardHandler, createElementWithId, createGenericContext, decode, detectIsBrowser, filtersToParams, getCurrentPosition, getFilteredItems, getFilters, getOwnerPaddingRight, getOwnerScrollbarWidth, getPagedItems, getRealSize, getSearchParams, getSearchParamsEncoded, getUID, hasGeolocation, isBrowser, parseParams, pushSearchParams, pushSearchParamsEncoded, replaceSearchParams, replaceSearchParamsEncoded, replaceSearchParamsEncodedSilently, replaceSearchParamsSilently, scrollToRelativeY, scrollToSelector, scrollToY, setFilters, updateSearchParams, updateSearchParamsEncoded, useApi, useApiDelete, useApiFetch, useApiGet, useApiPatch, useApiPost, useApiPut, useBodyScroll, useBreakpoint, useCart, useCheckout, useClickAnyWhere, useClickOut, useCurrency, useCurrentState, useDateTimeFormat, useDebounce, useDebounceCallback, useDebugChangedProps, useDomObserver, useDrawer, useElementSize, useEventListener, useFilters, useGeolocation, useIdle, useInfiniteLoader, useIsomorphicLayoutEffect, useKeyboard, useLabel, useLayout, useModal, useMounted, useNumber, usePage, usePagination, usePortal, usePrevious, useRealSize, useResize, useSSR, useScroll, useScrollTo, useSearchParams, useSearchParamsEncoded, useSearchParamsValue, useSorting, useSwiperIndex, useSyncedRef, useUI, useUnit, useUnmountEffect, useUser, useWindowSize };
|
|
553
|
+
export { BodyScrollOptions, CartContext, CartProvider, CheckoutContext, CheckoutProvider, ClickOutGetContainer, CurrentStateType, ElementStackItem, Filter, FilterMode, FilterParams, FilterValues, GeoLocationSensorState, IFilter, IFilterOption, IGeolocationPositionError, ILabelContext, ILayoutContext, IPageContext, IPaginationInfo, IParams, IParamsEncoded, IUseSearchParamsEncodedResult, IUseSearchParamsResult, IUseSearchParamsValueResult, KeyCode, KeyMod, KeyboardOptions, KeyboardResult, LabelProvider, LayoutProvider, PageProvider, SSRState, SizeResult, SizeType, Sorting, UIReducer, UIValue, UseFiltersResultType, UseKeyboard, UseKeyboardHandler, createElementWithId, createGenericContext, decode, detectIsBrowser, filtersToParams, getCurrentPosition, getFilteredItems, getFilters, getOwnerPaddingRight, getOwnerScrollbarWidth, getPagedItems, getRealSize, getSearchParams, getSearchParamsEncoded, getUID, hasGeolocation, isBrowser, parseParams, pushSearchParams, pushSearchParamsEncoded, replaceSearchParams, replaceSearchParamsEncoded, replaceSearchParamsEncodedSilently, replaceSearchParamsSilently, scrollToRelativeY, scrollToSelector, scrollToY, setFilters, updateSearchParams, updateSearchParamsEncoded, useApi, useApiDelete, useApiFetch, useApiGet, useApiPatch, useApiPost, useApiPut, useBodyScroll, useBreakpoint, useCart, useCheckout, useClickAnyWhere, useClickOut, useCurrency, useCurrentState, useDateTimeFormat, useDebounce, useDebounceCallback, useDebugChangedProps, useDomObserver, useDrawer, useElementSize, useEventListener, useFilters, useGeolocation, useIdle, useInfiniteLoader, useIsomorphicLayoutEffect, useKeyboard, useLabel, useLayout, useModal, useMounted, useNumber, usePage, usePagination, usePortal, usePrevious, useRealSize, useResize, useSSR, useScroll, useScrollTo, useScrolled, useSearchParams, useSearchParamsEncoded, useSearchParamsValue, useSorting, useSwiperIndex, useSyncedRef, useUI, useUnit, useUnmountEffect, useUser, useWindowSize };
|
package/dist/index.js
CHANGED
|
@@ -153,6 +153,7 @@ __export(src_exports, {
|
|
|
153
153
|
useSSR: () => useSSR,
|
|
154
154
|
useScroll: () => useScroll,
|
|
155
155
|
useScrollTo: () => useScrollTo,
|
|
156
|
+
useScrolled: () => useScrolled,
|
|
156
157
|
useSearchParams: () => useSearchParams,
|
|
157
158
|
useSearchParamsEncoded: () => useSearchParamsEncoded,
|
|
158
159
|
useSearchParamsValue: () => useSearchParamsValue,
|
|
@@ -380,7 +381,7 @@ function useBreakpoint() {
|
|
|
380
381
|
throw new Error("useBreakpoint must be used within ThemeProvider");
|
|
381
382
|
}
|
|
382
383
|
const breakpoints = Object.keys(theme.mediaQuery);
|
|
383
|
-
const values = Object.values(theme.mediaQuery);
|
|
384
|
+
const values = Object.values(theme.mediaQuery).map((x) => parseInt(String(x)));
|
|
384
385
|
const breakpoint = (0, import_react7.useMemo)(() => {
|
|
385
386
|
const breakpoint2 = values.reduce((p, c, i) => {
|
|
386
387
|
if (c < windowSize.width) {
|
|
@@ -1523,8 +1524,8 @@ var import_react30 = require("react");
|
|
|
1523
1524
|
function useScroll() {
|
|
1524
1525
|
const [scroll, setScroll] = (0, import_react30.useState)({ left: 0, top: 0 });
|
|
1525
1526
|
const onScroll = () => {
|
|
1526
|
-
const left = window.
|
|
1527
|
-
const top = window.
|
|
1527
|
+
const left = window.scrollX;
|
|
1528
|
+
const top = window.scrollY;
|
|
1528
1529
|
if (scroll.left !== left || scroll.top !== top) {
|
|
1529
1530
|
setScroll({ left, top });
|
|
1530
1531
|
}
|
|
@@ -1558,11 +1559,11 @@ function scrollToSelector(selector, offset = 0) {
|
|
|
1558
1559
|
}
|
|
1559
1560
|
}
|
|
1560
1561
|
function scrollToRelativeY(y = 0, offset = 0) {
|
|
1561
|
-
const to = window.
|
|
1562
|
+
const to = window.scrollY + y - 60 + offset;
|
|
1562
1563
|
return scrollToY(to);
|
|
1563
1564
|
}
|
|
1564
1565
|
function scrollToY(y = 0) {
|
|
1565
|
-
const from = window.
|
|
1566
|
+
const from = window.scrollY;
|
|
1566
1567
|
const to = y;
|
|
1567
1568
|
const duration = Math.sqrt(Math.abs(to - from) / 4e3);
|
|
1568
1569
|
(0, import_framer_motion.animate)(from, to, {
|
|
@@ -1571,9 +1572,29 @@ function scrollToY(y = 0) {
|
|
|
1571
1572
|
});
|
|
1572
1573
|
}
|
|
1573
1574
|
|
|
1575
|
+
// src/useScrolled/useScrolled.ts
|
|
1576
|
+
var import_react31 = require("react");
|
|
1577
|
+
var DEFAULT_CALLBACK = (top, left) => {
|
|
1578
|
+
return top > 0;
|
|
1579
|
+
};
|
|
1580
|
+
function useScrolled(callback = DEFAULT_CALLBACK) {
|
|
1581
|
+
const [scrolled, setScrolled] = (0, import_react31.useState)(false);
|
|
1582
|
+
const onScrolled = () => {
|
|
1583
|
+
const scrolled_ = callback(window.scrollY, window.scrollX);
|
|
1584
|
+
if (scrolled !== scrolled_) {
|
|
1585
|
+
setScrolled(scrolled_);
|
|
1586
|
+
}
|
|
1587
|
+
};
|
|
1588
|
+
useEventListener("scroll", onScrolled);
|
|
1589
|
+
useIsomorphicLayoutEffect(() => {
|
|
1590
|
+
onScrolled();
|
|
1591
|
+
}, []);
|
|
1592
|
+
return scrolled;
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1574
1595
|
// src/useSearchParams/useSearchParams.ts
|
|
1575
1596
|
var import_router = require("next/router");
|
|
1576
|
-
var
|
|
1597
|
+
var import_react32 = require("react");
|
|
1577
1598
|
var isBrowser = typeof window !== "undefined";
|
|
1578
1599
|
function replace_(params) {
|
|
1579
1600
|
if (window.history) {
|
|
@@ -1632,29 +1653,29 @@ function replaceSearchParamsSilently(params) {
|
|
|
1632
1653
|
}
|
|
1633
1654
|
function useSearchParams() {
|
|
1634
1655
|
const router = (0, import_router.useRouter)();
|
|
1635
|
-
const initialValue = (0,
|
|
1656
|
+
const initialValue = (0, import_react32.useMemo)(() => {
|
|
1636
1657
|
const params2 = deserialize_(router.asPath);
|
|
1637
1658
|
const value = Object.fromEntries(params2);
|
|
1638
1659
|
return value;
|
|
1639
1660
|
}, [router.asPath]);
|
|
1640
1661
|
const params = initialValue;
|
|
1641
|
-
const replaceParamsSilently = (0,
|
|
1662
|
+
const replaceParamsSilently = (0, import_react32.useCallback)((params2) => {
|
|
1642
1663
|
replaceSearchParamsSilently(params2);
|
|
1643
1664
|
}, []);
|
|
1644
|
-
const replaceParams = (0,
|
|
1665
|
+
const replaceParams = (0, import_react32.useCallback)((params2) => {
|
|
1645
1666
|
replaceSearchParams(router, params2);
|
|
1646
1667
|
}, [router]);
|
|
1647
1668
|
return { params, replaceParamsSilently, replaceParams };
|
|
1648
1669
|
}
|
|
1649
1670
|
function useSearchParamsValue(key) {
|
|
1650
1671
|
const router = (0, import_router.useRouter)();
|
|
1651
|
-
const initialValue = (0,
|
|
1672
|
+
const initialValue = (0, import_react32.useMemo)(() => {
|
|
1652
1673
|
const params2 = deserialize_(router.asPath);
|
|
1653
1674
|
const value = params2.get(key);
|
|
1654
1675
|
return value;
|
|
1655
1676
|
}, [key, router.asPath]);
|
|
1656
1677
|
const params = initialValue;
|
|
1657
|
-
const replaceParamsSilently = (0,
|
|
1678
|
+
const replaceParamsSilently = (0, import_react32.useCallback)((params2) => {
|
|
1658
1679
|
replaceSearchParamsSilently(params2);
|
|
1659
1680
|
}, []);
|
|
1660
1681
|
return { params, replaceParamsSilently };
|
|
@@ -1662,7 +1683,7 @@ function useSearchParamsValue(key) {
|
|
|
1662
1683
|
|
|
1663
1684
|
// src/useSearchParamsEncoded/useSearchParamsEncoded.ts
|
|
1664
1685
|
var import_router2 = require("next/router");
|
|
1665
|
-
var
|
|
1686
|
+
var import_react33 = require("react");
|
|
1666
1687
|
var USE_ENCRYPTION = true;
|
|
1667
1688
|
function base64ToString(base64) {
|
|
1668
1689
|
if (!USE_ENCRYPTION)
|
|
@@ -1768,27 +1789,27 @@ function replaceSearchParamsEncodedSilently(value, key) {
|
|
|
1768
1789
|
}
|
|
1769
1790
|
function useSearchParamsEncoded(key) {
|
|
1770
1791
|
const router = (0, import_router2.useRouter)();
|
|
1771
|
-
const initialValue = (0,
|
|
1792
|
+
const initialValue = (0, import_react33.useMemo)(() => {
|
|
1772
1793
|
const value = getSearchParamsEncoded(router.asPath, key);
|
|
1773
1794
|
return value;
|
|
1774
1795
|
}, [key, router.asPath]);
|
|
1775
1796
|
const params = initialValue;
|
|
1776
|
-
const replaceParamsSilently = (0,
|
|
1797
|
+
const replaceParamsSilently = (0, import_react33.useCallback)((params2) => {
|
|
1777
1798
|
replaceSearchParamsEncodedSilently(params2, key);
|
|
1778
1799
|
}, [key]);
|
|
1779
|
-
const replaceParams = (0,
|
|
1800
|
+
const replaceParams = (0, import_react33.useCallback)((params2) => {
|
|
1780
1801
|
replaceSearchParamsEncoded(router, params2, key);
|
|
1781
1802
|
}, [key, router]);
|
|
1782
1803
|
return { params, replaceParamsSilently, replaceParams };
|
|
1783
1804
|
}
|
|
1784
1805
|
|
|
1785
1806
|
// src/useSorting/useSorting.ts
|
|
1786
|
-
var
|
|
1807
|
+
var import_react34 = require("react");
|
|
1787
1808
|
function useSorting(items_, callback_, sort_) {
|
|
1788
|
-
const [sort, setSort] = (0,
|
|
1789
|
-
const [items, setItems] = (0,
|
|
1790
|
-
const isInitialMount = (0,
|
|
1791
|
-
(0,
|
|
1809
|
+
const [sort, setSort] = (0, import_react34.useState)(sort_);
|
|
1810
|
+
const [items, setItems] = (0, import_react34.useState)(callback_(items_.slice(), sort));
|
|
1811
|
+
const isInitialMount = (0, import_react34.useRef)(true);
|
|
1812
|
+
(0, import_react34.useEffect)(() => {
|
|
1792
1813
|
if (isInitialMount.current) {
|
|
1793
1814
|
isInitialMount.current = false;
|
|
1794
1815
|
} else {
|
|
@@ -1804,18 +1825,18 @@ function useSorting(items_, callback_, sort_) {
|
|
|
1804
1825
|
}
|
|
1805
1826
|
|
|
1806
1827
|
// src/useSwiperIndex/useSwiperIndex.ts
|
|
1807
|
-
var
|
|
1808
|
-
var
|
|
1828
|
+
var import_react35 = require("react");
|
|
1829
|
+
var import_react36 = require("swiper/react");
|
|
1809
1830
|
function useSwiperIndex() {
|
|
1810
|
-
const swiper = (0,
|
|
1811
|
-
const [index, setIndex] = (0,
|
|
1812
|
-
const slideTo = (0,
|
|
1831
|
+
const swiper = (0, import_react36.useSwiper)();
|
|
1832
|
+
const [index, setIndex] = (0, import_react35.useState)(swiper.realIndex);
|
|
1833
|
+
const slideTo = (0, import_react35.useCallback)((index2) => {
|
|
1813
1834
|
swiper.slideTo(index2);
|
|
1814
1835
|
}, [swiper]);
|
|
1815
1836
|
const onSlideChange = (swiper2) => {
|
|
1816
1837
|
setIndex(swiper2.realIndex);
|
|
1817
1838
|
};
|
|
1818
|
-
(0,
|
|
1839
|
+
(0, import_react35.useEffect)(() => {
|
|
1819
1840
|
if (swiper) {
|
|
1820
1841
|
swiper.on("slideChange", onSlideChange);
|
|
1821
1842
|
}
|
|
@@ -1829,11 +1850,11 @@ function useSwiperIndex() {
|
|
|
1829
1850
|
}
|
|
1830
1851
|
|
|
1831
1852
|
// src/useSyncedRef/useSyncedRef.ts
|
|
1832
|
-
var
|
|
1853
|
+
var import_react37 = require("react");
|
|
1833
1854
|
function useSyncedRef(value) {
|
|
1834
|
-
const ref = (0,
|
|
1855
|
+
const ref = (0, import_react37.useRef)(value);
|
|
1835
1856
|
ref.current = value;
|
|
1836
|
-
return (0,
|
|
1857
|
+
return (0, import_react37.useMemo)(() => Object.freeze({
|
|
1837
1858
|
get current() {
|
|
1838
1859
|
return ref.current;
|
|
1839
1860
|
}
|
|
@@ -1846,10 +1867,10 @@ function useUnit(unit = "kilometer", locale) {
|
|
|
1846
1867
|
}
|
|
1847
1868
|
|
|
1848
1869
|
// src/useUnmountEffect/useUnmountEffect.ts
|
|
1849
|
-
var
|
|
1870
|
+
var import_react38 = require("react");
|
|
1850
1871
|
function useUnmountEffect(effect) {
|
|
1851
1872
|
const effectRef = useSyncedRef(effect);
|
|
1852
|
-
(0,
|
|
1873
|
+
(0, import_react38.useEffect)(() => () => {
|
|
1853
1874
|
effectRef.current();
|
|
1854
1875
|
}, []);
|
|
1855
1876
|
}
|
|
@@ -1947,6 +1968,7 @@ var useUser = (0, import_zustand4.create)((set) => ({
|
|
|
1947
1968
|
useSSR,
|
|
1948
1969
|
useScroll,
|
|
1949
1970
|
useScrollTo,
|
|
1971
|
+
useScrolled,
|
|
1950
1972
|
useSearchParams,
|
|
1951
1973
|
useSearchParamsEncoded,
|
|
1952
1974
|
useSearchParamsValue,
|
package/dist/index.mjs
CHANGED
|
@@ -263,7 +263,7 @@ function useBreakpoint() {
|
|
|
263
263
|
throw new Error("useBreakpoint must be used within ThemeProvider");
|
|
264
264
|
}
|
|
265
265
|
const breakpoints = Object.keys(theme.mediaQuery);
|
|
266
|
-
const values = Object.values(theme.mediaQuery);
|
|
266
|
+
const values = Object.values(theme.mediaQuery).map((x) => parseInt(String(x)));
|
|
267
267
|
const breakpoint = useMemo(() => {
|
|
268
268
|
const breakpoint2 = values.reduce((p, c, i) => {
|
|
269
269
|
if (c < windowSize.width) {
|
|
@@ -1406,8 +1406,8 @@ import { useState as useState16 } from "react";
|
|
|
1406
1406
|
function useScroll() {
|
|
1407
1407
|
const [scroll, setScroll] = useState16({ left: 0, top: 0 });
|
|
1408
1408
|
const onScroll = () => {
|
|
1409
|
-
const left = window.
|
|
1410
|
-
const top = window.
|
|
1409
|
+
const left = window.scrollX;
|
|
1410
|
+
const top = window.scrollY;
|
|
1411
1411
|
if (scroll.left !== left || scroll.top !== top) {
|
|
1412
1412
|
setScroll({ left, top });
|
|
1413
1413
|
}
|
|
@@ -1441,11 +1441,11 @@ function scrollToSelector(selector, offset = 0) {
|
|
|
1441
1441
|
}
|
|
1442
1442
|
}
|
|
1443
1443
|
function scrollToRelativeY(y = 0, offset = 0) {
|
|
1444
|
-
const to = window.
|
|
1444
|
+
const to = window.scrollY + y - 60 + offset;
|
|
1445
1445
|
return scrollToY(to);
|
|
1446
1446
|
}
|
|
1447
1447
|
function scrollToY(y = 0) {
|
|
1448
|
-
const from = window.
|
|
1448
|
+
const from = window.scrollY;
|
|
1449
1449
|
const to = y;
|
|
1450
1450
|
const duration = Math.sqrt(Math.abs(to - from) / 4e3);
|
|
1451
1451
|
animate(from, to, {
|
|
@@ -1454,6 +1454,26 @@ function scrollToY(y = 0) {
|
|
|
1454
1454
|
});
|
|
1455
1455
|
}
|
|
1456
1456
|
|
|
1457
|
+
// src/useScrolled/useScrolled.ts
|
|
1458
|
+
import { useState as useState17 } from "react";
|
|
1459
|
+
var DEFAULT_CALLBACK = (top, left) => {
|
|
1460
|
+
return top > 0;
|
|
1461
|
+
};
|
|
1462
|
+
function useScrolled(callback = DEFAULT_CALLBACK) {
|
|
1463
|
+
const [scrolled, setScrolled] = useState17(false);
|
|
1464
|
+
const onScrolled = () => {
|
|
1465
|
+
const scrolled_ = callback(window.scrollY, window.scrollX);
|
|
1466
|
+
if (scrolled !== scrolled_) {
|
|
1467
|
+
setScrolled(scrolled_);
|
|
1468
|
+
}
|
|
1469
|
+
};
|
|
1470
|
+
useEventListener("scroll", onScrolled);
|
|
1471
|
+
useIsomorphicLayoutEffect(() => {
|
|
1472
|
+
onScrolled();
|
|
1473
|
+
}, []);
|
|
1474
|
+
return scrolled;
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1457
1477
|
// src/useSearchParams/useSearchParams.ts
|
|
1458
1478
|
import { useRouter } from "next/router";
|
|
1459
1479
|
import { useCallback as useCallback4, useMemo as useMemo5 } from "react";
|
|
@@ -1666,10 +1686,10 @@ function useSearchParamsEncoded(key) {
|
|
|
1666
1686
|
}
|
|
1667
1687
|
|
|
1668
1688
|
// src/useSorting/useSorting.ts
|
|
1669
|
-
import { useEffect as useEffect22, useRef as useRef11, useState as
|
|
1689
|
+
import { useEffect as useEffect22, useRef as useRef11, useState as useState18 } from "react";
|
|
1670
1690
|
function useSorting(items_, callback_, sort_) {
|
|
1671
|
-
const [sort, setSort] =
|
|
1672
|
-
const [items, setItems] =
|
|
1691
|
+
const [sort, setSort] = useState18(sort_);
|
|
1692
|
+
const [items, setItems] = useState18(callback_(items_.slice(), sort));
|
|
1673
1693
|
const isInitialMount = useRef11(true);
|
|
1674
1694
|
useEffect22(() => {
|
|
1675
1695
|
if (isInitialMount.current) {
|
|
@@ -1687,11 +1707,11 @@ function useSorting(items_, callback_, sort_) {
|
|
|
1687
1707
|
}
|
|
1688
1708
|
|
|
1689
1709
|
// src/useSwiperIndex/useSwiperIndex.ts
|
|
1690
|
-
import { useCallback as useCallback6, useEffect as useEffect23, useState as
|
|
1710
|
+
import { useCallback as useCallback6, useEffect as useEffect23, useState as useState19 } from "react";
|
|
1691
1711
|
import { useSwiper } from "swiper/react";
|
|
1692
1712
|
function useSwiperIndex() {
|
|
1693
1713
|
const swiper = useSwiper();
|
|
1694
|
-
const [index, setIndex] =
|
|
1714
|
+
const [index, setIndex] = useState19(swiper.realIndex);
|
|
1695
1715
|
const slideTo = useCallback6((index2) => {
|
|
1696
1716
|
swiper.slideTo(index2);
|
|
1697
1717
|
}, [swiper]);
|
|
@@ -1829,6 +1849,7 @@ export {
|
|
|
1829
1849
|
useSSR,
|
|
1830
1850
|
useScroll,
|
|
1831
1851
|
useScrollTo,
|
|
1852
|
+
useScrolled,
|
|
1832
1853
|
useSearchParams,
|
|
1833
1854
|
useSearchParamsEncoded,
|
|
1834
1855
|
useSearchParamsValue,
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -37,12 +37,13 @@ export * from './usePortal/usePortal';
|
|
|
37
37
|
export * from './usePrevious/usePrevious';
|
|
38
38
|
export * from './useRealSize/useRealSize';
|
|
39
39
|
export * from './useResize/useResize';
|
|
40
|
+
export * from './useSSR/useSSR';
|
|
40
41
|
export * from './useScroll/useScroll';
|
|
41
42
|
export * from './useScrollTo/useScrollTo';
|
|
43
|
+
export * from './useScrolled/useScrolled';
|
|
42
44
|
export * from './useSearchParams/useSearchParams';
|
|
43
45
|
export * from './useSearchParamsEncoded/useSearchParamsEncoded';
|
|
44
46
|
export * from './useSorting/useSorting';
|
|
45
|
-
export * from './useSSR/useSSR';
|
|
46
47
|
export * from './useSwiperIndex/useSwiperIndex';
|
|
47
48
|
export * from './useSyncedRef/useSyncedRef';
|
|
48
49
|
export * from './useUI/useUI';
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import '@testing-library/jest-dom';
|
|
2
2
|
import { render, renderHook, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
|
|
3
|
+
import { ILayout } from '@websolutespa/bom-core';
|
|
3
4
|
import { LayoutDefaults } from '@websolutespa/bom-mixer-mock';
|
|
4
5
|
import { getApiUrl } from '@websolutespa/bom-mixer-store';
|
|
5
6
|
import { LayoutProvider } from '../useLayout/useLayout';
|
|
6
7
|
import { useApi, useApiDelete, useApiFetch, useApiGet, useApiPatch, useApiPost, useApiPut } from './useApi';
|
|
7
8
|
|
|
8
9
|
const wrapper = (props: { children: React.ReactNode }) => (
|
|
9
|
-
<LayoutProvider layout={LayoutDefaults}>{props.children}</LayoutProvider>
|
|
10
|
+
<LayoutProvider layout={LayoutDefaults as ILayout}>{props.children}</LayoutProvider>
|
|
10
11
|
);
|
|
11
12
|
|
|
12
13
|
const defaultResponse = {
|
|
@@ -62,7 +63,7 @@ describe('useApi', () => {
|
|
|
62
63
|
NEXT_PUBLIC_API: '/api',
|
|
63
64
|
NEXT_PUBLIC_GOOGLE_MAP_API_KEY: 'AIzaSyDvGw6iAoKdRv8mmaC9GeT-LWLPQtA8p60',
|
|
64
65
|
STORE_URL: 'http://localhost:4000',
|
|
65
|
-
STORE_API: '/api',
|
|
66
|
+
STORE_API: '/bowl/api',
|
|
66
67
|
STORE_BEARER: '7584f15d12972fb1e7695e998dd5e4c754f46c74d06a08e8d76f556adcd045e48cd52bed6faac098784471ac273ee40243194b9cff7eba3c4a768f8f41d2d51959a7f767b943c7f0170f6e5f632db523803c357083bf7a7bf03ee8e0df2d8ce5cd52e0211283b34d7781313da775018a9e950433d2b6faf711c20e5a63b25243',
|
|
67
68
|
STORE_STRATEGY: 'mock',
|
|
68
69
|
SECRET_COOKIE_PASSWORD: '2gyZ3GDw3LHZQKDhPmPDL3sjREVRXPr8',
|
|
@@ -178,7 +179,7 @@ describe('useApi', () => {
|
|
|
178
179
|
);
|
|
179
180
|
};
|
|
180
181
|
render(
|
|
181
|
-
<LayoutProvider layout={LayoutDefaults}><Wrapper /></LayoutProvider>
|
|
182
|
+
<LayoutProvider layout={LayoutDefaults as ILayout}><Wrapper /></LayoutProvider>
|
|
182
183
|
);
|
|
183
184
|
expect(screen.getByTestId('loading')).toHaveTextContent('loading');
|
|
184
185
|
await waitForElementToBeRemoved(() => screen.getByTestId('loading'));
|
|
@@ -22,7 +22,7 @@ export function useBreakpoint(): Breakpoint {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
const breakpoints: BreakpointType[] = Object.keys(theme.mediaQuery) as BreakpointType[];
|
|
25
|
-
const values: number[] = Object.values(theme.mediaQuery);
|
|
25
|
+
const values: number[] = Object.values(theme.mediaQuery).map(x => parseInt(String(x)));
|
|
26
26
|
|
|
27
27
|
const breakpoint = useMemo(() => {
|
|
28
28
|
const breakpoint = values.reduce((p, c, i) => {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { renderHook } from '@testing-library/react';
|
|
2
|
+
import { ILayout } from '@websolutespa/bom-core';
|
|
2
3
|
import { LayoutDefaults } from '@websolutespa/bom-mixer-mock';
|
|
3
4
|
import { LayoutProvider } from '../useLayout/useLayout';
|
|
4
5
|
import { useCurrency } from './useCurrency';
|
|
5
6
|
|
|
6
7
|
const wrapper = (props: { children: React.ReactNode }) => (
|
|
7
|
-
<LayoutProvider layout={LayoutDefaults}>{props.children}</LayoutProvider>
|
|
8
|
+
<LayoutProvider layout={LayoutDefaults as ILayout}>{props.children}</LayoutProvider>
|
|
8
9
|
);
|
|
9
10
|
|
|
10
11
|
describe('useCurrency', () => {
|
|
@@ -6,7 +6,7 @@ export function useCurrency(currency: string = 'EUR', locale?: string): (value:
|
|
|
6
6
|
|
|
7
7
|
/*
|
|
8
8
|
import { useMemo } from 'react';
|
|
9
|
-
import { useNumber } from '
|
|
9
|
+
import { useNumber } from '../useNumber/useNumber';
|
|
10
10
|
import { useLayout } from '../useLayout/useLayout';
|
|
11
11
|
|
|
12
12
|
export function useCurrency(currency: string = 'EUR', locale?: string): (value: number) => string {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { renderHook } from '@testing-library/react';
|
|
2
|
+
import { ILayout } from '@websolutespa/bom-core';
|
|
2
3
|
import { LayoutDefaults } from '@websolutespa/bom-mixer-mock';
|
|
3
4
|
import { LayoutProvider } from '../useLayout/useLayout';
|
|
4
5
|
import { useDateTimeFormat } from './useDateTimeFormat';
|
|
5
6
|
|
|
6
7
|
const wrapper = (props: { children: React.ReactNode }) => (
|
|
7
|
-
<LayoutProvider layout={LayoutDefaults}>{props.children}</LayoutProvider>
|
|
8
|
+
<LayoutProvider layout={LayoutDefaults as ILayout}>{props.children}</LayoutProvider>
|
|
8
9
|
);
|
|
9
10
|
|
|
10
11
|
describe('useDateTimeFormat', () => {
|
|
@@ -56,7 +56,7 @@ describe('useHttp', () => {
|
|
|
56
56
|
NEXT_PUBLIC_API: '/api',
|
|
57
57
|
NEXT_PUBLIC_GOOGLE_MAP_API_KEY: 'AIzaSyDvGw6iAoKdRv8mmaC9GeT-LWLPQtA8p60',
|
|
58
58
|
STORE_URL: 'http://localhost:4000',
|
|
59
|
-
STORE_API: '/api',
|
|
59
|
+
STORE_API: '/bowl/api',
|
|
60
60
|
STORE_BEARER: '7584f15d12972fb1e7695e998dd5e4c754f46c74d06a08e8d76f556adcd045e48cd52bed6faac098784471ac273ee40243194b9cff7eba3c4a768f8f41d2d51959a7f767b943c7f0170f6e5f632db523803c357083bf7a7bf03ee8e0df2d8ce5cd52e0211283b34d7781313da775018a9e950433d2b6faf711c20e5a63b25243',
|
|
61
61
|
STORE_STRATEGY: 'mock',
|
|
62
62
|
SECRET_COOKIE_PASSWORD: '2gyZ3GDw3LHZQKDhPmPDL3sjREVRXPr8',
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { renderHook } from '@testing-library/react';
|
|
2
|
+
import { ILayout } from '@websolutespa/bom-core';
|
|
2
3
|
import { LayoutDefaults } from '@websolutespa/bom-mixer-mock';
|
|
3
4
|
import { LayoutProvider } from '../useLayout/useLayout';
|
|
4
5
|
import { LabelProvider, parseParams, useLabel } from './useLabel';
|
|
5
6
|
|
|
6
7
|
const wrapper = (props: { children: React.ReactNode }) => (
|
|
7
|
-
<LayoutProvider layout={LayoutDefaults}>
|
|
8
|
+
<LayoutProvider layout={LayoutDefaults as ILayout}>
|
|
8
9
|
<LabelProvider>{props.children}</LabelProvider>
|
|
9
10
|
</LayoutProvider>
|
|
10
11
|
);
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// readme https://testing-library.com/
|
|
2
2
|
import { renderHook } from '@testing-library/react';
|
|
3
|
+
import { ILayout } from '@websolutespa/bom-core';
|
|
3
4
|
import { LayoutDefaults } from '@websolutespa/bom-mixer-mock';
|
|
4
5
|
import { LayoutProvider, useLayout } from './useLayout';
|
|
5
6
|
|
|
6
7
|
const wrapper = (props: { children: React.ReactNode }) => (
|
|
7
|
-
<LayoutProvider layout={LayoutDefaults}>{props.children}</LayoutProvider>
|
|
8
|
+
<LayoutProvider layout={LayoutDefaults as ILayout}>{props.children}</LayoutProvider>
|
|
8
9
|
);
|
|
9
10
|
|
|
10
11
|
describe('useLayout', () => {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { renderHook } from '@testing-library/react';
|
|
2
|
+
import { ILayout } from '@websolutespa/bom-core';
|
|
2
3
|
import { LayoutDefaults } from '@websolutespa/bom-mixer-mock';
|
|
3
4
|
import { LayoutProvider } from '../useLayout/useLayout';
|
|
4
5
|
import { useNumber } from './useNumber';
|
|
5
6
|
|
|
6
7
|
const wrapper = (props: { children: React.ReactNode }) => (
|
|
7
|
-
<LayoutProvider layout={LayoutDefaults}>{props.children}</LayoutProvider>
|
|
8
|
+
<LayoutProvider layout={LayoutDefaults as ILayout}>{props.children}</LayoutProvider>
|
|
8
9
|
);
|
|
9
10
|
|
|
10
11
|
describe('useNumber', () => {
|
|
@@ -3,7 +3,7 @@ import { act, renderHook } from '@testing-library/react';
|
|
|
3
3
|
import { useScroll } from './useScroll';
|
|
4
4
|
|
|
5
5
|
const windowScroll = (scrollTop: number): void => {
|
|
6
|
-
window.
|
|
6
|
+
window.scrollY = scrollTop;
|
|
7
7
|
window.dispatchEvent(new Event('scroll'));
|
|
8
8
|
};
|
|
9
9
|
|
|
@@ -11,8 +11,8 @@ export function useScroll(): Scroll {
|
|
|
11
11
|
const [scroll, setScroll] = useState<Scroll>({ left: 0, top: 0 });
|
|
12
12
|
|
|
13
13
|
const onScroll = () => {
|
|
14
|
-
const left = window.
|
|
15
|
-
const top = window.
|
|
14
|
+
const left = window.scrollX;
|
|
15
|
+
const top = window.scrollY;
|
|
16
16
|
if (scroll.left !== left || scroll.top !== top) {
|
|
17
17
|
setScroll({ left, top });
|
|
18
18
|
}
|
|
@@ -77,8 +77,8 @@ describe('useScrollTo', () => {
|
|
|
77
77
|
left: 0,
|
|
78
78
|
}) as any);
|
|
79
79
|
scrollTo = jest.spyOn(window, 'scrollTo').mockImplementation((left: number, top: number) => {
|
|
80
|
-
window.
|
|
81
|
-
window.
|
|
80
|
+
window.scrollX = left;
|
|
81
|
+
window.scrollY = top;
|
|
82
82
|
});
|
|
83
83
|
});
|
|
84
84
|
|
|
@@ -93,13 +93,13 @@ describe('useScrollTo', () => {
|
|
|
93
93
|
|
|
94
94
|
it('should work correctly', async () => {
|
|
95
95
|
render(<Wrapper />);
|
|
96
|
-
expect(window.
|
|
96
|
+
expect(window.scrollY).toBe(0);
|
|
97
97
|
const trigger = screen.getByTestId('trigger');
|
|
98
98
|
await userEvent.click(trigger);
|
|
99
99
|
expect(scrollTo).toBeCalled();
|
|
100
100
|
expect(scrollTo).toHaveBeenCalledTimes(2);
|
|
101
101
|
expect(scrollTo).toHaveBeenCalledWith(0, 940);
|
|
102
|
-
expect(window.
|
|
102
|
+
expect(window.scrollY).toBe(940);
|
|
103
103
|
});
|
|
104
104
|
|
|
105
105
|
});
|
|
@@ -26,12 +26,12 @@ export function scrollToSelector(selector?: string, offset: number = 0) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export function scrollToRelativeY(y: number = 0, offset: number = 0) {
|
|
29
|
-
const to = window.
|
|
29
|
+
const to = window.scrollY + y - 60 + offset;
|
|
30
30
|
return scrollToY(to);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export function scrollToY(y: number = 0) {
|
|
34
|
-
const from = window.
|
|
34
|
+
const from = window.scrollY;
|
|
35
35
|
const to = y;
|
|
36
36
|
const duration = Math.sqrt(Math.abs(to - from) / 4000);
|
|
37
37
|
animate(from, to, {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// readme https://testing-library.com/
|
|
2
|
+
import { act, renderHook } from '@testing-library/react';
|
|
3
|
+
import { useScrolled } from './useScrolled';
|
|
4
|
+
|
|
5
|
+
const windowScroll = (scrollTop: number): void => {
|
|
6
|
+
window.scrollY = scrollTop;
|
|
7
|
+
window.dispatchEvent(new Event('scroll'));
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const wrapper = (props: { children: React.ReactNode }) => (
|
|
11
|
+
<div data-testid="test" style={{ height: '2000px' }}>{props.children}</div>
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
describe('useScrolled', () => {
|
|
15
|
+
|
|
16
|
+
it('should work correctly', () => {
|
|
17
|
+
const { result } = renderHook(() => useScrolled(), { wrapper });
|
|
18
|
+
expect(result.current).toStrictEqual(false);
|
|
19
|
+
act(() => {
|
|
20
|
+
windowScroll(100);
|
|
21
|
+
});
|
|
22
|
+
expect(result.current).toStrictEqual(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return false', () => {
|
|
26
|
+
const { result } = renderHook(() => useScrolled((top, left) => top > 200), { wrapper });
|
|
27
|
+
expect(result.current).toStrictEqual(false);
|
|
28
|
+
act(() => {
|
|
29
|
+
windowScroll(100);
|
|
30
|
+
});
|
|
31
|
+
expect(result.current).toStrictEqual(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { useEventListener } from '../useEventListener/useEventListener';
|
|
3
|
+
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect/useIsomorphicLayoutEffect';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_CALLBACK = (top: number, left: number) => {
|
|
6
|
+
return top > 0;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function useScrolled(callback: (top: number, left: number) => boolean = DEFAULT_CALLBACK): boolean {
|
|
10
|
+
const [scrolled, setScrolled] = useState<boolean>(false);
|
|
11
|
+
|
|
12
|
+
const onScrolled = () => {
|
|
13
|
+
const scrolled_ = callback(window.scrollY, window.scrollX);
|
|
14
|
+
if (scrolled !== scrolled_) {
|
|
15
|
+
setScrolled(scrolled_);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
useEventListener('scroll', onScrolled);
|
|
20
|
+
|
|
21
|
+
// Set size at the first client-side load
|
|
22
|
+
useIsomorphicLayoutEffect(() => {
|
|
23
|
+
onScrolled();
|
|
24
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
25
|
+
}, []);
|
|
26
|
+
|
|
27
|
+
return scrolled;
|
|
28
|
+
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { renderHook } from '@testing-library/react';
|
|
2
|
+
import { ILayout } from '@websolutespa/bom-core';
|
|
2
3
|
import { LayoutDefaults } from '@websolutespa/bom-mixer-mock';
|
|
3
4
|
import { LayoutProvider } from '../useLayout/useLayout';
|
|
4
5
|
import { useUnit } from './useUnit';
|
|
5
6
|
|
|
6
7
|
const wrapper = (props: { children: React.ReactNode }) => (
|
|
7
|
-
<LayoutProvider layout={LayoutDefaults}>{props.children}</LayoutProvider>
|
|
8
|
+
<LayoutProvider layout={LayoutDefaults as ILayout}>{props.children}</LayoutProvider>
|
|
8
9
|
);
|
|
9
10
|
|
|
10
11
|
describe('useUnit', () => {
|