@folklore/hooks 0.0.80 → 0.0.82

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.
Files changed (3) hide show
  1. package/dist/cjs.js +61 -13
  2. package/dist/es.js +61 -14
  3. package/package.json +2 -2
package/dist/cjs.js CHANGED
@@ -1081,15 +1081,23 @@ const getWindowSize = () => ({
1081
1081
  width: typeof window !== 'undefined' ? window.innerWidth || 0 : 0,
1082
1082
  height: typeof window !== 'undefined' ? window.innerHeight || 0 : 0
1083
1083
  });
1084
- let currentSize = getWindowSize();
1084
+ let currentSize = null;
1085
1085
  function useWindowSize({
1086
- onChange = null
1086
+ onChange = null,
1087
+ onMount = false,
1088
+ memo = false
1087
1089
  } = {}) {
1088
- const [size, setSize] = react.useState(currentSize);
1090
+ const [size, setSize] = react.useState(onMount ? getWindowSize() : {
1091
+ width: 0,
1092
+ height: 0
1093
+ });
1089
1094
  const sizeRef = react.useRef(size);
1095
+ if (currentSize === null && memo) {
1096
+ currentSize = size;
1097
+ }
1090
1098
  const updateSize = react.useCallback(() => {
1091
1099
  const newSize = getWindowSize();
1092
- if (currentSize.width !== newSize.width || currentSize.height !== newSize.height) {
1100
+ if (memo && (currentSize.width !== newSize.width || currentSize.height !== newSize.height)) {
1093
1101
  currentSize = newSize;
1094
1102
  }
1095
1103
  if (sizeRef.current.width !== newSize.width || sizeRef.current.height !== newSize.height) {
@@ -1098,7 +1106,7 @@ function useWindowSize({
1098
1106
  return newSize;
1099
1107
  }
1100
1108
  return null;
1101
- }, [setSize]);
1109
+ }, [setSize, memo]);
1102
1110
  const onResize = react.useCallback(() => {
1103
1111
  const newSize = updateSize();
1104
1112
  if (newSize !== null && onChange !== null) {
@@ -1179,6 +1187,34 @@ function useScrollTrigger({
1179
1187
  };
1180
1188
  }
1181
1189
 
1190
+ function checkWebpSupport() {
1191
+ return new Promise(resolve => {
1192
+ const img = document.createElement('img');
1193
+ img.onload = () => {
1194
+ resolve(img.width > 0 && img.height > 0);
1195
+ };
1196
+ img.onerror = () => {
1197
+ resolve(false);
1198
+ };
1199
+ img.src = 'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoCAAEAAQAcJaQAA3AA/v3AgAA=';
1200
+ });
1201
+ }
1202
+ function useSupportsWebp(defaultValue = true) {
1203
+ const [supportsWebp, setSupportsWebp] = react.useState(defaultValue);
1204
+ react.useEffect(() => {
1205
+ let canceled = false;
1206
+ checkWebpSupport().then(newSupport => {
1207
+ if (!canceled && newSupport !== supportsWebp) {
1208
+ setSupportsWebp(newSupport);
1209
+ }
1210
+ });
1211
+ return () => {
1212
+ canceled = true;
1213
+ };
1214
+ }, []);
1215
+ return supportsWebp;
1216
+ }
1217
+
1182
1218
  const NO_PLAYER_ERROR$1 = new Error('No player');
1183
1219
  function useVimeoPlayer(id, {
1184
1220
  width = 0,
@@ -1855,16 +1891,27 @@ const getWindowScroll = () => ({
1855
1891
  x: typeof window !== 'undefined' ? window.scrollX || 0 : 0,
1856
1892
  y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
1857
1893
  });
1858
- let currentScroll = getWindowScroll();
1859
- function useWindowScroll(opts = {}) {
1860
- const {
1861
- onChange = null
1862
- } = opts;
1863
- const [scroll, setScroll] = react.useState(currentScroll);
1894
+ let currentScroll = null;
1895
+ function useWindowScroll({
1896
+ onChange = null,
1897
+ onMount = false,
1898
+ memo = false
1899
+ } = {}) {
1900
+ const [scroll, setScroll] = react.useState(onMount ? getWindowScroll() : {
1901
+ x: 0,
1902
+ y: 0
1903
+ });
1864
1904
  const scrollRef = react.useRef(scroll);
1905
+ if (currentScroll === null && memo) {
1906
+ currentScroll = scroll;
1907
+ }
1865
1908
  const updateScroll = react.useCallback(() => {
1866
1909
  const newScroll = getWindowScroll();
1867
- if (currentScroll.x !== newScroll.x || currentScroll.y !== newScroll.y) {
1910
+ const {
1911
+ x: currentX,
1912
+ y: currentY
1913
+ } = currentScroll || {};
1914
+ if (memo && (currentX !== newScroll.x || currentY !== newScroll.y)) {
1868
1915
  currentScroll = newScroll;
1869
1916
  }
1870
1917
  if (scrollRef.current.x !== newScroll.x || scrollRef.current.y !== newScroll.y) {
@@ -1873,7 +1920,7 @@ function useWindowScroll(opts = {}) {
1873
1920
  return newScroll;
1874
1921
  }
1875
1922
  return null;
1876
- }, [setScroll]);
1923
+ }, [setScroll, memo]);
1877
1924
  const onScroll = react.useCallback(() => {
1878
1925
  const newScroll = updateScroll();
1879
1926
  if (newScroll !== null && onChange !== null) {
@@ -1901,6 +1948,7 @@ exports.useObserver = useObserver;
1901
1948
  exports.usePlayerCurrentTime = usePlayerCurrentTime;
1902
1949
  exports.useResizeObserver = useResizeObserver;
1903
1950
  exports.useScrollTrigger = useScrollTrigger;
1951
+ exports.useSupportsWebp = useSupportsWebp;
1904
1952
  exports.useVideoPlayer = useVideoPlayer;
1905
1953
  exports.useVimeoPlayer = useVimeoPlayer;
1906
1954
  exports.useVisualViewport = useVisualViewport;
package/dist/es.js CHANGED
@@ -1079,15 +1079,23 @@ const getWindowSize = () => ({
1079
1079
  width: typeof window !== 'undefined' ? window.innerWidth || 0 : 0,
1080
1080
  height: typeof window !== 'undefined' ? window.innerHeight || 0 : 0
1081
1081
  });
1082
- let currentSize = getWindowSize();
1082
+ let currentSize = null;
1083
1083
  function useWindowSize({
1084
- onChange = null
1084
+ onChange = null,
1085
+ onMount = false,
1086
+ memo = false
1085
1087
  } = {}) {
1086
- const [size, setSize] = useState(currentSize);
1088
+ const [size, setSize] = useState(onMount ? getWindowSize() : {
1089
+ width: 0,
1090
+ height: 0
1091
+ });
1087
1092
  const sizeRef = useRef(size);
1093
+ if (currentSize === null && memo) {
1094
+ currentSize = size;
1095
+ }
1088
1096
  const updateSize = useCallback(() => {
1089
1097
  const newSize = getWindowSize();
1090
- if (currentSize.width !== newSize.width || currentSize.height !== newSize.height) {
1098
+ if (memo && (currentSize.width !== newSize.width || currentSize.height !== newSize.height)) {
1091
1099
  currentSize = newSize;
1092
1100
  }
1093
1101
  if (sizeRef.current.width !== newSize.width || sizeRef.current.height !== newSize.height) {
@@ -1096,7 +1104,7 @@ function useWindowSize({
1096
1104
  return newSize;
1097
1105
  }
1098
1106
  return null;
1099
- }, [setSize]);
1107
+ }, [setSize, memo]);
1100
1108
  const onResize = useCallback(() => {
1101
1109
  const newSize = updateSize();
1102
1110
  if (newSize !== null && onChange !== null) {
@@ -1177,6 +1185,34 @@ function useScrollTrigger({
1177
1185
  };
1178
1186
  }
1179
1187
 
1188
+ function checkWebpSupport() {
1189
+ return new Promise(resolve => {
1190
+ const img = document.createElement('img');
1191
+ img.onload = () => {
1192
+ resolve(img.width > 0 && img.height > 0);
1193
+ };
1194
+ img.onerror = () => {
1195
+ resolve(false);
1196
+ };
1197
+ img.src = 'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoCAAEAAQAcJaQAA3AA/v3AgAA=';
1198
+ });
1199
+ }
1200
+ function useSupportsWebp(defaultValue = true) {
1201
+ const [supportsWebp, setSupportsWebp] = useState(defaultValue);
1202
+ useEffect(() => {
1203
+ let canceled = false;
1204
+ checkWebpSupport().then(newSupport => {
1205
+ if (!canceled && newSupport !== supportsWebp) {
1206
+ setSupportsWebp(newSupport);
1207
+ }
1208
+ });
1209
+ return () => {
1210
+ canceled = true;
1211
+ };
1212
+ }, []);
1213
+ return supportsWebp;
1214
+ }
1215
+
1180
1216
  const NO_PLAYER_ERROR$1 = new Error('No player');
1181
1217
  function useVimeoPlayer(id, {
1182
1218
  width = 0,
@@ -1853,16 +1889,27 @@ const getWindowScroll = () => ({
1853
1889
  x: typeof window !== 'undefined' ? window.scrollX || 0 : 0,
1854
1890
  y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
1855
1891
  });
1856
- let currentScroll = getWindowScroll();
1857
- function useWindowScroll(opts = {}) {
1858
- const {
1859
- onChange = null
1860
- } = opts;
1861
- const [scroll, setScroll] = useState(currentScroll);
1892
+ let currentScroll = null;
1893
+ function useWindowScroll({
1894
+ onChange = null,
1895
+ onMount = false,
1896
+ memo = false
1897
+ } = {}) {
1898
+ const [scroll, setScroll] = useState(onMount ? getWindowScroll() : {
1899
+ x: 0,
1900
+ y: 0
1901
+ });
1862
1902
  const scrollRef = useRef(scroll);
1903
+ if (currentScroll === null && memo) {
1904
+ currentScroll = scroll;
1905
+ }
1863
1906
  const updateScroll = useCallback(() => {
1864
1907
  const newScroll = getWindowScroll();
1865
- if (currentScroll.x !== newScroll.x || currentScroll.y !== newScroll.y) {
1908
+ const {
1909
+ x: currentX,
1910
+ y: currentY
1911
+ } = currentScroll || {};
1912
+ if (memo && (currentX !== newScroll.x || currentY !== newScroll.y)) {
1866
1913
  currentScroll = newScroll;
1867
1914
  }
1868
1915
  if (scrollRef.current.x !== newScroll.x || scrollRef.current.y !== newScroll.y) {
@@ -1871,7 +1918,7 @@ function useWindowScroll(opts = {}) {
1871
1918
  return newScroll;
1872
1919
  }
1873
1920
  return null;
1874
- }, [setScroll]);
1921
+ }, [setScroll, memo]);
1875
1922
  const onScroll = useCallback(() => {
1876
1923
  const newScroll = updateScroll();
1877
1924
  if (newScroll !== null && onChange !== null) {
@@ -1885,4 +1932,4 @@ function useWindowScroll(opts = {}) {
1885
1932
  return scroll;
1886
1933
  }
1887
1934
 
1888
- export { eventsManager$1 as documentEventsManager, getObserver, useCounter, useDailymotionPlayer, useDocumentEvent, useIntersectionObserver, useIsVisible, useItemsPaginated, useKeyboard, useNativeVideoPlayer, useObserver, usePlayerCurrentTime, useResizeObserver, useScrollTrigger, useVideoPlayer, useVimeoPlayer, useVisualViewport, useVisualViewport as useVisualViewportSize, useWindowEvent, useWindowScroll, useWindowSize, useYouTubePlayer, eventsManager as windowEventsManager };
1935
+ export { eventsManager$1 as documentEventsManager, getObserver, useCounter, useDailymotionPlayer, useDocumentEvent, useIntersectionObserver, useIsVisible, useItemsPaginated, useKeyboard, useNativeVideoPlayer, useObserver, usePlayerCurrentTime, useResizeObserver, useScrollTrigger, useSupportsWebp, useVideoPlayer, useVimeoPlayer, useVisualViewport, useVisualViewport as useVisualViewportSize, useWindowEvent, useWindowScroll, useWindowSize, useYouTubePlayer, eventsManager as windowEventsManager };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@folklore/hooks",
3
- "version": "0.0.80",
3
+ "version": "0.0.82",
4
4
  "description": "React hooks",
5
5
  "keywords": [
6
6
  "javascript",
@@ -49,7 +49,7 @@
49
49
  "publishConfig": {
50
50
  "access": "public"
51
51
  },
52
- "gitHead": "6d9d9e7770eaabf06aff6864a4d33d0b6ad99eb2",
52
+ "gitHead": "565254f67e63bf9f1ccb4ef925633af37682e44d",
53
53
  "dependencies": {
54
54
  "@folklore/events": "^0.0.10",
55
55
  "@folklore/services": "^0.1.44",