@folklore/hooks 0.0.82 → 0.0.85

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.
@@ -2,7 +2,7 @@ import raf from 'raf';
2
2
  import { useState, useEffect, useMemo, useRef, useCallback } from 'react';
3
3
  import { loadDailymotion, loadVimeo, loadYouTube } from '@folklore/services';
4
4
  import createDebug from 'debug';
5
- import EventsManager from '@folklore/events';
5
+ import { EventsManager } from '@folklore/events';
6
6
  import { cancelable } from 'cancelable-promise';
7
7
  import isNumber from 'lodash/isNumber';
8
8
 
@@ -57,7 +57,7 @@ function useCounter(desiredValue, {
57
57
  }
58
58
 
59
59
  const NO_PLAYER_ERROR$3 = new Error('No player');
60
- function useDailymotionPlayer(id = null, params = {}) {
60
+ function useDailymotionPlayer(idOrUrl, opts = {}) {
61
61
  const {
62
62
  width = 0,
63
63
  height = 0,
@@ -81,14 +81,14 @@ function useDailymotionPlayer(id = null, params = {}) {
81
81
  }
82
82
  return null;
83
83
  }
84
- } = params;
84
+ } = opts;
85
85
  const debug = useMemo(() => createDebug('folklore:video:dailymotion'), []);
86
86
  const [apiLoaded, setApiLoaded] = useState(typeof window !== 'undefined' && typeof window.dailymotion !== 'undefined');
87
87
  const [playerReady, setPlayerReady] = useState(false);
88
88
  const [loaded, setLoaded] = useState(false);
89
89
  const apiRef = useRef(typeof window !== 'undefined' && typeof window.dailymotion !== 'undefined' ? window.dailymotion : null);
90
90
  const ready = apiLoaded && playerReady;
91
- const videoId = useMemo(() => getVideoId(id), [id]);
91
+ const videoId = useMemo(() => getVideoId(idOrUrl), [idOrUrl]);
92
92
  const elementRef = useRef(null);
93
93
  const playerRef = useRef(null);
94
94
  const playerElementRef = useRef(elementRef.current);
@@ -400,7 +400,7 @@ function getOptionsKey({
400
400
  }) {
401
401
  return `root_${root}_rootMargin_${rootMargin || null}_threshold_${threshold}`;
402
402
  }
403
- function createObserver(Observer, options = {}) {
403
+ function createObserver(Observer, options = null) {
404
404
  let subscribers = [];
405
405
  const addSubscriber = (element, callback) => {
406
406
  const currentSubscriber = subscribers.find(it => it.element === element) || null;
@@ -433,7 +433,7 @@ function createObserver(Observer, options = {}) {
433
433
  });
434
434
  });
435
435
  };
436
- const observer = new Observer(onUpdate, options);
436
+ const observer = new Observer(onUpdate, options || {});
437
437
  const unsubscribe = (element, callback = null) => {
438
438
  subscribers = removeSubscriber(element, callback);
439
439
  if (typeof observer.unobserve === 'undefined') {
@@ -480,18 +480,18 @@ function getObserver(Observer = null, options = {}) {
480
480
  }
481
481
  return observers[observerKey];
482
482
  }
483
- function useObserver(Observer, opts = {}, initialEntry = {}) {
483
+ function useObserver(Observer, opts, initialEntry) {
484
484
  const {
485
485
  root = null,
486
486
  rootMargin = null,
487
487
  threshold: defaultThreshold = null,
488
488
  disabled = false
489
- } = opts;
489
+ } = opts || {};
490
490
  const [entry, setEntry] = useState(initialEntry);
491
491
  const threshold = useMemo(() => defaultThreshold, [defaultThreshold]);
492
492
  const nodeRef = useRef(null);
493
- const currentElement = useRef(null);
494
- const elementChanged = nodeRef.current !== currentElement.current;
493
+ const currentElementRef = useRef(null);
494
+ const elementChanged = nodeRef.current !== currentElementRef.current;
495
495
  useEffect(() => {
496
496
  if (disabled) {
497
497
  return () => {};
@@ -519,7 +519,7 @@ function useObserver(Observer, opts = {}, initialEntry = {}) {
519
519
  unsubscribe = localUnsubscribe;
520
520
  subscribe(nodeElement, callback);
521
521
  }
522
- currentElement.current = nodeElement;
522
+ currentElementRef.current = nodeElement;
523
523
  return () => {
524
524
  if (unsubscribe !== null) {
525
525
  unsubscribe(nodeElement, callback);
@@ -535,6 +535,7 @@ function useObserver(Observer, opts = {}, initialEntry = {}) {
535
535
  /**
536
536
  * Intersection Observer
537
537
  */
538
+
538
539
  const defaultThreshold = [0, 1.0];
539
540
  const intersectionObserverInitialEntry = {
540
541
  target: null,
@@ -546,34 +547,31 @@ const intersectionObserverInitialEntry = {
546
547
  boundingClientRect: null,
547
548
  rootBounds: null
548
549
  };
549
- function useIntersectionObserver({
550
- root = null,
551
- rootMargin = '0px',
552
- threshold = defaultThreshold,
553
- disabled = false
554
- } = {}) {
550
+ function useIntersectionObserver(opts = {}) {
555
551
  return useObserver(typeof IntersectionObserver !== 'undefined' ? IntersectionObserver : null, {
556
- root,
557
- rootMargin,
558
- threshold,
559
- disabled
552
+ root: null,
553
+ rootMargin: '0px',
554
+ threshold: defaultThreshold,
555
+ disabled: false,
556
+ ...opts
560
557
  }, intersectionObserverInitialEntry);
561
558
  }
562
559
 
563
560
  /**
564
561
  * Resize Observer
565
562
  */
563
+
566
564
  const resizeObserverInitialEntry = {
567
565
  target: null,
568
566
  contentRect: null,
569
567
  contentBoxSize: null,
570
- borderBoxSize: null
568
+ borderBoxSize: null,
569
+ devicePixelContentBoxSize: null
571
570
  };
572
- function useResizeObserver({
573
- disabled = false
574
- } = {}) {
571
+ function useResizeObserver(opts = {}) {
575
572
  return useObserver(typeof ResizeObserver !== 'undefined' ? ResizeObserver : null, {
576
- disabled
573
+ disabled: false,
574
+ ...opts
577
575
  }, resizeObserverInitialEntry);
578
576
  }
579
577
 
@@ -668,7 +666,7 @@ const useItemsPaginated = (loader, {
668
666
  onError = null,
669
667
  query = null
670
668
  } = {}) => {
671
- const lastState = useRef(null);
669
+ const lastStateRef = useRef(null);
672
670
  const initialState = useMemo(() => {
673
671
  const finalInitialPages = initialPages !== null ? initialPages.map(it => getPageFromResponse(it)) : null;
674
672
  const finalLastPage = finalInitialPages !== null ? finalInitialPages.reduce((currentLastPage, {
@@ -783,7 +781,7 @@ const useItemsPaginated = (loader, {
783
781
 
784
782
  // Reset all on query change
785
783
  useEffect(() => {
786
- const hadState = lastState.current !== null;
784
+ const hadState = lastStateRef.current !== null;
787
785
  if (hadState) {
788
786
  currentPagesRef.current = null;
789
787
  updateState({
@@ -796,8 +794,8 @@ const useItemsPaginated = (loader, {
796
794
  }
797
795
  }, [query]);
798
796
  useEffect(() => {
799
- const hadState = lastState.current !== null;
800
- lastState.current = initialState;
797
+ const hadState = lastStateRef.current !== null;
798
+ lastStateRef.current = initialState;
801
799
  if (hadState) {
802
800
  currentPagesRef.current = initialState.pages;
803
801
  setState(initialState);
@@ -885,15 +883,16 @@ function usePlayerCurrentTime(player, {
885
883
  }
886
884
 
887
885
  const NO_PLAYER_ERROR$2 = new Error('No player');
888
- function useNativeVideoPlayer(url, {
889
- width = 0,
890
- height = 0,
891
- duration = 0,
892
- muted: providedMuted = false,
893
- initialMuted = false,
894
- timeUpdateInterval = 1000,
895
- onTimeUpdate: customOnTimeUpdate = null
896
- } = {}) {
886
+ function useNativeVideoPlayer(url, opts = null) {
887
+ const {
888
+ width = 0,
889
+ height = 0,
890
+ duration = 0,
891
+ muted: providedMuted = false,
892
+ initialMuted = false,
893
+ timeUpdateInterval = 1000,
894
+ onTimeUpdate: customOnTimeUpdate = null
895
+ } = opts || {};
897
896
  const debug = useMemo(() => createDebug('folklore:video:native'), []);
898
897
  const elementRef = useRef(null);
899
898
  const [loaded, setLoaded] = useState(false);
@@ -1075,17 +1074,19 @@ function useNativeVideoPlayer(url, {
1075
1074
  };
1076
1075
  }
1077
1076
 
1078
- const getWindowSize = () => ({
1079
- width: typeof window !== 'undefined' ? window.innerWidth || 0 : 0,
1080
- height: typeof window !== 'undefined' ? window.innerHeight || 0 : 0
1081
- });
1077
+ function getWindowSize() {
1078
+ return {
1079
+ width: typeof window !== 'undefined' ? window.innerWidth || 0 : 0,
1080
+ height: typeof window !== 'undefined' ? window.innerHeight || 0 : 0
1081
+ };
1082
+ }
1082
1083
  let currentSize = null;
1083
1084
  function useWindowSize({
1084
1085
  onChange = null,
1085
1086
  onMount = false,
1086
1087
  memo = false
1087
1088
  } = {}) {
1088
- const [size, setSize] = useState(onMount ? getWindowSize() : {
1089
+ const [size, setSize] = useState(() => onMount ? getWindowSize() : {
1089
1090
  width: 0,
1090
1091
  height: 0
1091
1092
  });
@@ -1118,9 +1119,10 @@ function useWindowSize({
1118
1119
  return size;
1119
1120
  }
1120
1121
 
1122
+ const defaultTriggers = [0.25, 0.5, 0.75, 1];
1121
1123
  function useScrollTrigger({
1122
1124
  disabled = false,
1123
- triggers = [0.1, 0.25, 0.5, 0.75, 0.9, 1.0],
1125
+ triggers = defaultTriggers,
1124
1126
  useElementScroll = false,
1125
1127
  onTrigger = null
1126
1128
  } = {}) {
@@ -1214,26 +1216,27 @@ function useSupportsWebp(defaultValue = true) {
1214
1216
  }
1215
1217
 
1216
1218
  const NO_PLAYER_ERROR$1 = new Error('No player');
1217
- function useVimeoPlayer(id, {
1218
- width = 0,
1219
- height = 0,
1220
- duration = 0,
1221
- autoplay = false,
1222
- autopause = true,
1223
- byline = false,
1224
- controls = false,
1225
- muted: providedMuted = false,
1226
- initialMuted = false,
1227
- timeUpdateInterval = 1000,
1228
- onTimeUpdate: customOnTimeUpdate = null,
1229
- getVideoId = url => {
1230
- if (url === null || url.match(/^[0-9]+$/) !== null) {
1231
- return url;
1232
- }
1233
- const match = url.match(/\/[0-9]+/);
1234
- return match !== null ? match[1] : null;
1235
- }
1236
- } = {}) {
1219
+ function useVimeoPlayer(idOrUrl, opts = {}) {
1220
+ const {
1221
+ width = 0,
1222
+ height = 0,
1223
+ duration = 0,
1224
+ autoplay = false,
1225
+ autopause = true,
1226
+ byline = false,
1227
+ controls = false,
1228
+ muted: providedMuted = false,
1229
+ initialMuted = false,
1230
+ timeUpdateInterval = 1000,
1231
+ onTimeUpdate: customOnTimeUpdate = null,
1232
+ getVideoId = url => {
1233
+ if (url === null || url.match(/^[0-9]+$/) !== null) {
1234
+ return url;
1235
+ }
1236
+ const match = url.match(/\/[0-9]+/);
1237
+ return match !== null ? match[1] : null;
1238
+ }
1239
+ } = opts || {};
1237
1240
  const debug = useMemo(() => createDebug('folklore:video:vimeo'), []);
1238
1241
  const [apiLoaded, setApiLoaded] = useState(false);
1239
1242
  const apiRef = useRef(null);
@@ -1241,7 +1244,7 @@ function useVimeoPlayer(id, {
1241
1244
  const playerRef = useRef(null);
1242
1245
  const playerElementRef = useRef(elementRef.current);
1243
1246
  const elementHasChanged = elementRef.current !== playerElementRef.current;
1244
- const videoId = useMemo(() => getVideoId(id), [id]);
1247
+ const videoId = useMemo(() => getVideoId(idOrUrl), [idOrUrl]);
1245
1248
  const [ready, setReady] = useState(false);
1246
1249
  const [loaded, setLoaded] = useState(false);
1247
1250
  const [volume, setVolumeState] = useState(initialMuted || providedMuted ? 0 : 1);
@@ -1260,7 +1263,7 @@ function useVimeoPlayer(id, {
1260
1263
  // Load SDK
1261
1264
  useEffect(() => {
1262
1265
  let canceled = false;
1263
- if (!apiLoaded && id !== null) {
1266
+ if (!apiLoaded && videoId !== null) {
1264
1267
  debug('Load API');
1265
1268
  loadVimeo().then(api => {
1266
1269
  if (!canceled) {
@@ -1273,7 +1276,7 @@ function useVimeoPlayer(id, {
1273
1276
  return () => {
1274
1277
  canceled = true;
1275
1278
  };
1276
- }, [id]);
1279
+ }, [videoId]);
1277
1280
  const play = useCallback(() => {
1278
1281
  const {
1279
1282
  current: player
@@ -1514,26 +1517,27 @@ function useVimeoPlayer(id, {
1514
1517
  }
1515
1518
 
1516
1519
  const NO_PLAYER_ERROR = new Error('No player');
1517
- function useYouTubePlayer(id, {
1518
- width = 0,
1519
- height = 0,
1520
- duration = 0,
1521
- autoplay = false,
1522
- controls = true,
1523
- timeUpdateInterval = 1000,
1524
- muted: providedMuted = false,
1525
- initialMuted = false,
1526
- onVolumeChange: customOnVolumeChange = null,
1527
- onTimeUpdate: customOnTimeUpdate = null,
1528
- getVideoId = url => {
1529
- if (url === null || url.match(/^https?:/) === null) {
1530
- return url;
1531
- }
1532
- const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
1533
- const match = url.match(regExp);
1534
- return match !== null ? match[7] : null;
1535
- }
1536
- } = {}) {
1520
+ function useYouTubePlayer(idOrUrl, opts = null) {
1521
+ const {
1522
+ width = 0,
1523
+ height = 0,
1524
+ duration = 0,
1525
+ autoplay = false,
1526
+ controls = true,
1527
+ timeUpdateInterval = 1000,
1528
+ muted: providedMuted = false,
1529
+ initialMuted = false,
1530
+ onVolumeChange: customOnVolumeChange = null,
1531
+ onTimeUpdate: customOnTimeUpdate = null,
1532
+ getVideoId = url => {
1533
+ if (url === null || url.match(/^https?:/) === null) {
1534
+ return url;
1535
+ }
1536
+ const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
1537
+ const match = url.match(regExp);
1538
+ return match !== null ? match[7] : null;
1539
+ }
1540
+ } = opts || {};
1537
1541
  const debug = useMemo(() => createDebug('folklore:video:youtube'), []);
1538
1542
  const [apiLoaded, setApiLoaded] = useState(typeof window !== 'undefined' && typeof window.YT !== 'undefined');
1539
1543
  const apiRef = useRef(typeof window !== 'undefined' && typeof window.YT !== 'undefined' ? window.YT : null);
@@ -1544,7 +1548,7 @@ function useYouTubePlayer(id, {
1544
1548
  playerElementRef.current = elementRef.current;
1545
1549
  }
1546
1550
  const elementHasChanged = elementRef.current !== playerElementRef.current;
1547
- const videoId = useMemo(() => getVideoId(id), [id]);
1551
+ const videoId = useMemo(() => getVideoId(idOrUrl), [idOrUrl]);
1548
1552
  const [ready, setReady] = useState(false);
1549
1553
  const [muted, setMuted] = useState(initialMuted || providedMuted);
1550
1554
  const [playState, setPlayState] = useState({
@@ -1754,23 +1758,23 @@ function useYouTubePlayer(id, {
1754
1758
  };
1755
1759
  }
1756
1760
 
1757
- function useVideoPlayer(params) {
1758
- const {
1759
- service = null,
1760
- videoId = null,
1761
- url = null,
1762
- onLoaded: customOnLoaded = null,
1763
- onPlay: customOnPlay = null,
1764
- onPause: customOnPause = null,
1765
- onEnd: customOnEnd = null,
1766
- onMetadataChange: customOnMetadataChange = null,
1767
- onBufferStart: customOnBufferStart = null,
1768
- onBufferEnded: customOnBufferEnded = null
1769
- } = params || {};
1770
- const dailymotionPlayer = useDailymotionPlayer(service === 'dailymotion' ? videoId || url : null, params);
1771
- const youtubePlayer = useYouTubePlayer(service === 'youtube' ? videoId || url : null, params);
1772
- const vimeoPlayer = useVimeoPlayer(service === 'vimeo' ? videoId || url : null, params);
1773
- const nativePlayer = useNativeVideoPlayer(url, params);
1761
+ function useVideoPlayer({
1762
+ service = null,
1763
+ videoId = null,
1764
+ url = null,
1765
+ onLoaded: customOnLoaded = null,
1766
+ onPlay: customOnPlay = null,
1767
+ onPause: customOnPause = null,
1768
+ onEnd: customOnEnd = null,
1769
+ onMetadataChange: customOnMetadataChange = null,
1770
+ onBufferStart: customOnBufferStart = null,
1771
+ onBufferEnded: customOnBufferEnded = null,
1772
+ ...opts
1773
+ } = {}) {
1774
+ const dailymotionPlayer = useDailymotionPlayer(service === 'dailymotion' ? videoId || url : null, opts);
1775
+ const youtubePlayer = useYouTubePlayer(service === 'youtube' ? videoId || url : null, opts);
1776
+ const vimeoPlayer = useVimeoPlayer(service === 'vimeo' ? videoId || url : null, opts);
1777
+ const nativePlayer = useNativeVideoPlayer(['dailymotion', 'youtube', 'vimeo'].indexOf(service) === -1 ? url : null, opts);
1774
1778
  let player = null;
1775
1779
  if (service === 'dailymotion') {
1776
1780
  player = dailymotionPlayer;
@@ -1836,15 +1840,11 @@ function useVisualViewport() {
1836
1840
  width: windowWidth,
1837
1841
  height: windowHeight
1838
1842
  } = useWindowSize();
1839
- const [{
1840
- width: viewportWidth,
1841
- height: viewportHeight,
1842
- ...viewport
1843
- }, setViewport] = useState({
1843
+ const [viewport, setViewport] = useState({
1844
1844
  width: windowWidth,
1845
1845
  height: windowHeight
1846
1846
  });
1847
- const updateViewport = useCallback((viewPort = null) => {
1847
+ const updateViewport = useCallback((newViewport = null) => {
1848
1848
  const {
1849
1849
  width: newWidth = 0,
1850
1850
  height: newHeight = 0,
@@ -1852,7 +1852,7 @@ function useVisualViewport() {
1852
1852
  offsetLeft: newOffsetLeft = 0,
1853
1853
  pageLeft: newPageLeft = 0,
1854
1854
  pageTop: newPageTop = 0
1855
- } = viewPort || window.visualViewport || {};
1855
+ } = newViewport || window.visualViewport || {};
1856
1856
  setViewport({
1857
1857
  width: newWidth,
1858
1858
  height: newHeight,
@@ -1877,25 +1877,32 @@ function useVisualViewport() {
1877
1877
  window.visualViewport.removeEventListener('scroll', onUpdate);
1878
1878
  };
1879
1879
  }, [updateViewport]);
1880
+ const {
1881
+ width: viewportWidth,
1882
+ height: viewportHeight,
1883
+ ...otherViewport
1884
+ } = viewport;
1880
1885
  return {
1881
1886
  width: viewportWidth || windowWidth,
1882
1887
  height: viewportHeight || windowHeight,
1883
- ...viewport,
1888
+ ...otherViewport,
1884
1889
  updateViewport
1885
1890
  };
1886
1891
  }
1887
1892
 
1888
- const getWindowScroll = () => ({
1889
- x: typeof window !== 'undefined' ? window.scrollX || 0 : 0,
1890
- y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
1891
- });
1893
+ function getWindowScroll() {
1894
+ return {
1895
+ x: typeof window !== 'undefined' ? window.scrollX || 0 : 0,
1896
+ y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
1897
+ };
1898
+ }
1892
1899
  let currentScroll = null;
1893
1900
  function useWindowScroll({
1894
1901
  onChange = null,
1895
1902
  onMount = false,
1896
1903
  memo = false
1897
1904
  } = {}) {
1898
- const [scroll, setScroll] = useState(onMount ? getWindowScroll() : {
1905
+ const [scroll, setScroll] = useState(() => onMount ? getWindowScroll() : {
1899
1906
  x: 0,
1900
1907
  y: 0
1901
1908
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@folklore/hooks",
3
- "version": "0.0.82",
3
+ "version": "0.0.85",
4
4
  "description": "React hooks",
5
5
  "keywords": [
6
6
  "javascript",
@@ -27,16 +27,22 @@
27
27
  "email": "nrb@folklore.email"
28
28
  }
29
29
  ],
30
- "main": "dist/cjs.js",
31
- "module": "dist/es.js",
30
+ "type": "module",
31
+ "module": "dist/index.js",
32
+ "types": "dist/index.d.ts",
33
+ "exports": {
34
+ ".": {
35
+ "default": "./dist/index.js",
36
+ "types": "./dist/index.d.ts"
37
+ }
38
+ },
32
39
  "files": [
33
40
  "dist"
34
41
  ],
35
42
  "sideEffects": false,
36
43
  "scripts": {
37
- "clean": "rm -rf dist",
38
- "build": "rollup --bundleConfigAsCjs --config ../../rollup.config.js",
39
- "prepublishOnly": "npm run clean && npm run build"
44
+ "build": "../../scripts/prepare-package.sh --types",
45
+ "prepublishOnly": "npm run build"
40
46
  },
41
47
  "devDependencies": {
42
48
  "react": "^17.0.0 || ^18.0.0 || ^19.0.0",
@@ -49,11 +55,11 @@
49
55
  "publishConfig": {
50
56
  "access": "public"
51
57
  },
52
- "gitHead": "565254f67e63bf9f1ccb4ef925633af37682e44d",
58
+ "gitHead": "d1ea3212d18c8203657536bc2b80caeb284403e2",
53
59
  "dependencies": {
54
- "@folklore/events": "^0.0.10",
55
- "@folklore/services": "^0.1.44",
56
- "@folklore/utils": "^0.1.4",
60
+ "@folklore/events": "^0.0.12",
61
+ "@folklore/services": "^0.1.46",
62
+ "@folklore/utils": "^0.1.6",
57
63
  "cancelable-promise": "^4.3.1",
58
64
  "debug": "^4.3.4",
59
65
  "lodash": "^4.17.21",