@folklore/hooks 0.0.32 → 0.0.34

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 +308 -308
  2. package/dist/es.js +309 -309
  3. package/package.json +5 -4
package/dist/cjs.js CHANGED
@@ -107,9 +107,8 @@ function usePlayerCurrentTime(player) {
107
107
  return realCurrentTime.current;
108
108
  }
109
109
 
110
- const noPlayerError$1 = new Error('No player');
111
- const debug$3 = createDebug__default["default"]('folklore:video:dailymotion');
112
- const useDailymotionPlayer = function () {
110
+ const NO_PLAYER_ERROR$3 = new Error('No player');
111
+ function useDailymotionPlayer() {
113
112
  let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
114
113
  let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
115
114
  const {
@@ -135,6 +134,7 @@ const useDailymotionPlayer = function () {
135
134
  return match !== null ? match[1] : null;
136
135
  }
137
136
  } = params;
137
+ const debug = react.useMemo(() => createDebug__default["default"]('folklore:video:dailymotion'), []);
138
138
  const [apiLoaded, setApiLoaded] = react.useState(typeof window !== 'undefined' && typeof window.DM !== 'undefined');
139
139
  const [playerReady, setPlayerReady] = react.useState(false);
140
140
  const [loaded, setLoaded] = react.useState(false);
@@ -164,12 +164,12 @@ const useDailymotionPlayer = function () {
164
164
  react.useEffect(() => {
165
165
  let canceled = false;
166
166
  if (!apiLoaded && videoId !== null) {
167
- debug$3('Load API');
167
+ debug('Load API');
168
168
  services.loadDailymotion().then(api => {
169
169
  if (!canceled) {
170
170
  apiRef.current = api;
171
171
  setApiLoaded(true);
172
- debug$3('API Loaded');
172
+ debug('API Loaded');
173
173
  }
174
174
  });
175
175
  }
@@ -208,7 +208,7 @@ const useDailymotionPlayer = function () {
208
208
  player.load(videoId, {
209
209
  params: playerParams
210
210
  });
211
- debug$3('Load video [ID: %s]', videoId);
211
+ debug('Load video [ID: %s]', videoId);
212
212
  } else {
213
213
  player = DM.player(element, {
214
214
  video: videoId,
@@ -216,7 +216,7 @@ const useDailymotionPlayer = function () {
216
216
  height,
217
217
  params: playerParams
218
218
  });
219
- debug$3('Create player [ID: %s]', videoId);
219
+ debug('Create player [ID: %s]', videoId);
220
220
  }
221
221
  if (!playerReady) {
222
222
  setPlayerReady(true);
@@ -235,7 +235,7 @@ const useDailymotionPlayer = function () {
235
235
  let currentMetadata = metadata;
236
236
  function onPlaybackReady() {
237
237
  setLoaded(true);
238
- debug$3('onPlaybackReady [ID: %s]', videoId);
238
+ debug('onPlaybackReady [ID: %s]', videoId);
239
239
  }
240
240
  function onLoadedMetadata() {
241
241
  currentMetadata = {
@@ -243,7 +243,7 @@ const useDailymotionPlayer = function () {
243
243
  duration: player.duration
244
244
  };
245
245
  setMetadata(currentMetadata);
246
- debug$3('onLoadedMetadata [ID: %s]', videoId);
246
+ debug('onLoadedMetadata [ID: %s]', videoId);
247
247
  }
248
248
  function onDurationChange() {
249
249
  currentMetadata = {
@@ -251,12 +251,12 @@ const useDailymotionPlayer = function () {
251
251
  duration: player.duration
252
252
  };
253
253
  setMetadata(currentMetadata);
254
- debug$3('onDurationChange [ID: %s]', videoId);
254
+ debug('onDurationChange [ID: %s]', videoId);
255
255
  }
256
256
  function onVolumeChange() {
257
257
  setMuted(player.muted);
258
258
  setVolumeState(player.volume);
259
- debug$3('onVolumeChange [ID: %s]', videoId);
259
+ debug('onVolumeChange [ID: %s]', videoId);
260
260
  }
261
261
  function onPlay() {
262
262
  currentPlayState = {
@@ -266,7 +266,7 @@ const useDailymotionPlayer = function () {
266
266
  ended: false
267
267
  };
268
268
  setPlayState(currentPlayState);
269
- debug$3('onPlay [ID: %s]', videoId);
269
+ debug('onPlay [ID: %s]', videoId);
270
270
  }
271
271
  function onPause() {
272
272
  currentPlayState = {
@@ -276,7 +276,7 @@ const useDailymotionPlayer = function () {
276
276
  ended: false
277
277
  };
278
278
  setPlayState(currentPlayState);
279
- debug$3('onPause [ID: %s]', videoId);
279
+ debug('onPause [ID: %s]', videoId);
280
280
  }
281
281
  function onEnd() {
282
282
  currentPlayState = {
@@ -286,7 +286,7 @@ const useDailymotionPlayer = function () {
286
286
  ended: true
287
287
  };
288
288
  setPlayState(currentPlayState);
289
- debug$3('onEnd [ID: %s]', videoId);
289
+ debug('onEnd [ID: %s]', videoId);
290
290
  }
291
291
  function onPlaying() {
292
292
  currentPlayState = {
@@ -294,7 +294,7 @@ const useDailymotionPlayer = function () {
294
294
  buffering: false
295
295
  };
296
296
  setPlayState(currentPlayState);
297
- debug$3('onPlaying [ID: %s]', videoId);
297
+ debug('onPlaying [ID: %s]', videoId);
298
298
  }
299
299
  function onWaiting() {
300
300
  currentPlayState = {
@@ -302,7 +302,7 @@ const useDailymotionPlayer = function () {
302
302
  buffering: true
303
303
  };
304
304
  setPlayState(currentPlayState);
305
- debug$3('onWaiting [ID: %s]', videoId);
305
+ debug('onWaiting [ID: %s]', videoId);
306
306
  }
307
307
  function onAdStart() {
308
308
  currentPlayState = {
@@ -310,7 +310,7 @@ const useDailymotionPlayer = function () {
310
310
  adPlaying: true
311
311
  };
312
312
  setPlayState(currentPlayState);
313
- debug$3('onAdStart [ID: %s]', videoId);
313
+ debug('onAdStart [ID: %s]', videoId);
314
314
  }
315
315
  function onAdEnd() {
316
316
  currentPlayState = {
@@ -318,7 +318,7 @@ const useDailymotionPlayer = function () {
318
318
  adPlaying: false
319
319
  };
320
320
  setPlayState(currentPlayState);
321
- debug$3('onAdEnd [ID: %s]', videoId);
321
+ debug('onAdEnd [ID: %s]', videoId);
322
322
  }
323
323
  player.addEventListener('playback_ready', onPlaybackReady);
324
324
  player.addEventListener('loadedmetadata', onLoadedMetadata);
@@ -349,37 +349,37 @@ const useDailymotionPlayer = function () {
349
349
  const {
350
350
  current: player
351
351
  } = playerRef;
352
- return player !== null ? player.play() : Promise.reject(noPlayerError$1);
352
+ return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR$3);
353
353
  }, []);
354
354
  const pause = react.useCallback(() => {
355
355
  const {
356
356
  current: player
357
357
  } = playerRef;
358
- return player !== null ? player.pause() : Promise.reject(noPlayerError$1);
358
+ return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR$3);
359
359
  }, []);
360
360
  const setVolume = react.useCallback(newVolume => {
361
361
  const {
362
362
  current: player
363
363
  } = playerRef;
364
- return player !== null ? player.setVolume(newVolume) : Promise.reject(noPlayerError$1);
364
+ return player !== null ? player.setVolume(newVolume) : Promise.reject(NO_PLAYER_ERROR$3);
365
365
  }, []);
366
366
  const mute = react.useCallback(() => {
367
367
  const {
368
368
  current: player
369
369
  } = playerRef;
370
- return player !== null ? player.setMute(true) : Promise.reject(noPlayerError$1);
370
+ return player !== null ? player.setMute(true) : Promise.reject(NO_PLAYER_ERROR$3);
371
371
  }, []);
372
372
  const unmute = react.useCallback(() => {
373
373
  const {
374
374
  current: player
375
375
  } = playerRef;
376
- return player !== null ? player.setMute(false) : Promise.reject(noPlayerError$1);
376
+ return player !== null ? player.setMute(false) : Promise.reject(NO_PLAYER_ERROR$3);
377
377
  }, []);
378
378
  const seek = react.useCallback(time => {
379
379
  const {
380
380
  current: player
381
381
  } = playerRef;
382
- return player !== null ? player.seek(time) : Promise.reject(noPlayerError$1);
382
+ return player !== null ? player.seek(time) : Promise.reject(NO_PLAYER_ERROR$3);
383
383
  }, []);
384
384
  const {
385
385
  playing
@@ -407,10 +407,10 @@ const useDailymotionPlayer = function () {
407
407
  ...metadata,
408
408
  ...playState
409
409
  };
410
- };
410
+ }
411
411
 
412
412
  const eventsManager$1 = typeof document !== 'undefined' ? new EventsManager__default["default"](document) : null;
413
- const useDocumentEvent = (event, callback) => {
413
+ function useDocumentEvent(event, callback) {
414
414
  react.useEffect(() => {
415
415
  if (eventsManager$1 !== null && callback !== null) {
416
416
  eventsManager$1.subscribe(event, callback);
@@ -421,10 +421,10 @@ const useDocumentEvent = (event, callback) => {
421
421
  }
422
422
  };
423
423
  }, [event, callback]);
424
- };
424
+ }
425
425
 
426
426
  const eventsManager = typeof window !== 'undefined' ? new EventsManager__default["default"](window) : null;
427
- const useWindowEvent = (event, callback) => {
427
+ function useWindowEvent(event, callback) {
428
428
  react.useEffect(() => {
429
429
  if (eventsManager !== null && callback !== null) {
430
430
  eventsManager.subscribe(event, callback);
@@ -435,7 +435,7 @@ const useWindowEvent = (event, callback) => {
435
435
  }
436
436
  };
437
437
  }, [event, callback]);
438
- };
438
+ }
439
439
 
440
440
  function useKeyboard() {
441
441
  let keyMap = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
@@ -675,9 +675,8 @@ const useItemsPaginated = function (loader) {
675
675
  };
676
676
  };
677
677
 
678
- const debug$2 = createDebug__default["default"]('folklore:video:native');
679
- const noPlayerError = new Error('No player');
680
- const useNativeVideoPlayer = function (url) {
678
+ const NO_PLAYER_ERROR$2 = new Error('No player');
679
+ function useNativeVideoPlayer(url) {
681
680
  let {
682
681
  width = 0,
683
682
  height = 0,
@@ -686,6 +685,7 @@ const useNativeVideoPlayer = function (url) {
686
685
  timeUpdateInterval = 1000,
687
686
  onTimeUpdate: customOnTimeUpdate = null
688
687
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
688
+ const debug = react.useMemo(() => createDebug__default["default"]('folklore:video:native'), []);
689
689
  const elementRef = react.useRef(null);
690
690
  const [loaded, setLoaded] = react.useState(false);
691
691
  const [volume, setVolumeState] = react.useState(initialMuted ? 0 : 1);
@@ -704,13 +704,13 @@ const useNativeVideoPlayer = function (url) {
704
704
  const {
705
705
  current: player
706
706
  } = elementRef;
707
- return player !== null ? player.play() : Promise.reject(noPlayerError);
707
+ return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR$2);
708
708
  }, []);
709
709
  const pause = react.useCallback(() => {
710
710
  const {
711
711
  current: player
712
712
  } = elementRef;
713
- return player !== null ? player.pause() : Promise.reject(noPlayerError);
713
+ return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR$2);
714
714
  }, []);
715
715
  const setVolume = react.useCallback(newVolume => {
716
716
  const {
@@ -720,7 +720,7 @@ const useNativeVideoPlayer = function (url) {
720
720
  player.volume = newVolume;
721
721
  return Promise.resolve(newVolume);
722
722
  }
723
- return Promise.reject(noPlayerError);
723
+ return Promise.reject(NO_PLAYER_ERROR$2);
724
724
  }, []);
725
725
  const mute = react.useCallback(() => {
726
726
  const {
@@ -730,7 +730,7 @@ const useNativeVideoPlayer = function (url) {
730
730
  player.muted = true;
731
731
  return Promise.resolve(true);
732
732
  }
733
- return Promise.reject(noPlayerError);
733
+ return Promise.reject(NO_PLAYER_ERROR$2);
734
734
  }, []);
735
735
  const unmute = react.useCallback(() => {
736
736
  const {
@@ -740,7 +740,7 @@ const useNativeVideoPlayer = function (url) {
740
740
  player.muted = false;
741
741
  return Promise.resolve(false);
742
742
  }
743
- return Promise.reject(noPlayerError);
743
+ return Promise.reject(NO_PLAYER_ERROR$2);
744
744
  }, []);
745
745
  const seek = react.useCallback(newTime => {
746
746
  const {
@@ -750,7 +750,7 @@ const useNativeVideoPlayer = function (url) {
750
750
  player.currentTime = newTime;
751
751
  return Promise.resolve(newTime);
752
752
  }
753
- return Promise.reject(noPlayerError);
753
+ return Promise.reject(NO_PLAYER_ERROR$2);
754
754
  }, []);
755
755
  const setLoop = react.useCallback(newLoop => {
756
756
  const {
@@ -760,7 +760,7 @@ const useNativeVideoPlayer = function (url) {
760
760
  player.loop = newLoop;
761
761
  return Promise.resolve(newLoop);
762
762
  }
763
- return Promise.reject(noPlayerError);
763
+ return Promise.reject(NO_PLAYER_ERROR$2);
764
764
  }, []);
765
765
 
766
766
  // Bind player events
@@ -773,7 +773,7 @@ const useNativeVideoPlayer = function (url) {
773
773
  }
774
774
  const onCanPlay = () => {
775
775
  setLoaded(true);
776
- debug$2('onCanPlay [URL: %s]', url);
776
+ debug('onCanPlay [URL: %s]', url);
777
777
  };
778
778
  const onMetadataLoaded = () => {
779
779
  const newMetadata = {
@@ -782,7 +782,7 @@ const useNativeVideoPlayer = function (url) {
782
782
  height: player.videoHeight
783
783
  };
784
784
  setMetadata(newMetadata);
785
- debug$2('onMetadataLoaded [URL: %s]', url);
785
+ debug('onMetadataLoaded [URL: %s]', url);
786
786
  };
787
787
  const onPlay = () => {
788
788
  setPlayState({
@@ -791,7 +791,7 @@ const useNativeVideoPlayer = function (url) {
791
791
  ended: false,
792
792
  buffering: false
793
793
  });
794
- debug$2('onPlay [URL: %s]', url);
794
+ debug('onPlay [URL: %s]', url);
795
795
  };
796
796
  const onPause = () => {
797
797
  setPlayState({
@@ -800,7 +800,7 @@ const useNativeVideoPlayer = function (url) {
800
800
  ended: false,
801
801
  buffering: false
802
802
  });
803
- debug$2('onPause [URL: %s]', url);
803
+ debug('onPause [URL: %s]', url);
804
804
  };
805
805
  const onEnded = () => {
806
806
  setPlayState({
@@ -809,16 +809,16 @@ const useNativeVideoPlayer = function (url) {
809
809
  ended: true,
810
810
  buffering: false
811
811
  });
812
- debug$2('onEnded [URL: %s]', url);
812
+ debug('onEnded [URL: %s]', url);
813
813
  };
814
- debug$2('Bind events [URL: %s]', url);
814
+ debug('Bind events [URL: %s]', url);
815
815
  player.addEventListener('canplay', onCanPlay);
816
816
  player.addEventListener('metadataloaded', onMetadataLoaded);
817
817
  player.addEventListener('play', onPlay);
818
818
  player.addEventListener('pause', onPause);
819
819
  player.addEventListener('ended', onEnded);
820
820
  return () => {
821
- debug$2('Unbind events [URL: %s]', url);
821
+ debug('Unbind events [URL: %s]', url);
822
822
  player.removeEventListener('canplay', onCanPlay);
823
823
  player.removeEventListener('metadataloaded', onMetadataLoaded);
824
824
  player.removeEventListener('play', onPlay);
@@ -853,18 +853,18 @@ const useNativeVideoPlayer = function (url) {
853
853
  ...metadata,
854
854
  ...playState
855
855
  };
856
- };
856
+ }
857
857
 
858
858
  const observersCache = new Map();
859
- const getOptionsKey = _ref => {
859
+ function getOptionsKey(_ref) {
860
860
  let {
861
861
  root = null,
862
862
  rootMargin,
863
863
  threshold = null
864
864
  } = _ref;
865
865
  return `root_${root}_rootMargin_${rootMargin || null}_threshold_${threshold}`;
866
- };
867
- const createObserver = function (Observer) {
866
+ }
867
+ function createObserver(Observer) {
868
868
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
869
869
  let subscribers = [];
870
870
  const addSubscriber = (element, callback) => {
@@ -927,8 +927,8 @@ const createObserver = function (Observer) {
927
927
  unsubscribe,
928
928
  observer
929
929
  };
930
- };
931
- const getObserver = function () {
930
+ }
931
+ function getObserver() {
932
932
  let Observer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
933
933
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
934
934
  if (Observer === null) {
@@ -948,8 +948,8 @@ const getObserver = function () {
948
948
  observersCache.set(Observer, observers);
949
949
  }
950
950
  return observers[observerKey];
951
- };
952
- const useObserver = function (Observer) {
951
+ }
952
+ function useObserver(Observer) {
953
953
  let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
954
954
  let initialEntry = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
955
955
  const {
@@ -998,7 +998,7 @@ const useObserver = function (Observer) {
998
998
  ref: nodeRef,
999
999
  entry
1000
1000
  };
1001
- };
1001
+ }
1002
1002
 
1003
1003
  /**
1004
1004
  * Intersection Observer
@@ -1014,7 +1014,7 @@ const intersectionObserverInitialEntry = {
1014
1014
  boundingClientRect: null,
1015
1015
  rootBounds: null
1016
1016
  };
1017
- const useIntersectionObserver = function () {
1017
+ function useIntersectionObserver() {
1018
1018
  let {
1019
1019
  root = null,
1020
1020
  rootMargin = '0px',
@@ -1027,7 +1027,7 @@ const useIntersectionObserver = function () {
1027
1027
  threshold,
1028
1028
  disabled
1029
1029
  }, intersectionObserverInitialEntry);
1030
- };
1030
+ }
1031
1031
 
1032
1032
  /**
1033
1033
  * Resize Observer
@@ -1038,255 +1038,17 @@ const resizeObserverInitialEntry = {
1038
1038
  contentBoxSize: null,
1039
1039
  borderBoxSize: null
1040
1040
  };
1041
- const useResizeObserver = function () {
1041
+ function useResizeObserver() {
1042
1042
  let {
1043
1043
  disabled = false
1044
1044
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1045
1045
  return useObserver(typeof ResizeObserver !== 'undefined' ? ResizeObserver : null, {
1046
1046
  disabled
1047
1047
  }, resizeObserverInitialEntry);
1048
- };
1049
-
1050
- const NO_PLAYER_ERROR$1 = new Error('No player');
1051
- const debug$1 = createDebug__default["default"]('folklore:video:youtube');
1052
- function useYouTubePlayer(id) {
1053
- let {
1054
- width = 0,
1055
- height = 0,
1056
- duration = 0,
1057
- autoplay = false,
1058
- controls = true,
1059
- timeUpdateInterval = 1000,
1060
- muted: initialMuted = false,
1061
- onVolumeChange: customOnVolumeChange = null,
1062
- onTimeUpdate: customOnTimeUpdate = null,
1063
- getVideoId = url => {
1064
- if (url === null || url.match(/^https?:/) === null) {
1065
- return url;
1066
- }
1067
- const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
1068
- const match = url.match(regExp);
1069
- return match && match[7].length === 11 ? match[7] : null;
1070
- }
1071
- } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1072
- const [apiLoaded, setApiLoaded] = react.useState(typeof window !== 'undefined' && typeof window.YT !== 'undefined');
1073
- const apiRef = react.useRef(typeof window !== 'undefined' && typeof window.YT !== 'undefined' ? window.YT : null);
1074
- const elementRef = react.useRef(null);
1075
- const playerRef = react.useRef(null);
1076
- const playerElementRef = react.useRef(elementRef.current);
1077
- const elementHasChanged = elementRef.current !== playerElementRef.current;
1078
- const videoId = react.useMemo(() => getVideoId(id), [id]);
1079
- const [ready, setReady] = react.useState(false);
1080
- const [muted, setMuted] = react.useState(initialMuted);
1081
- const [playState, setPlayState] = react.useState({
1082
- playing: false,
1083
- paused: false,
1084
- ended: false,
1085
- buffering: false
1086
- });
1087
- const [metadata, setMetadata] = react.useState({
1088
- width,
1089
- height,
1090
- duration
1091
- });
1092
- react.useEffect(() => {
1093
- let canceled = false;
1094
- if (!apiLoaded && videoId !== null) {
1095
- debug$1('Load API');
1096
- services.loadYouTube().then(api => {
1097
- if (!canceled) {
1098
- apiRef.current = api;
1099
- setApiLoaded(true);
1100
- debug$1('API Loaded');
1101
- }
1102
- });
1103
- }
1104
- return () => {
1105
- canceled = true;
1106
- };
1107
- }, [apiLoaded, videoId, setApiLoaded]);
1108
- const play = react.useCallback(() => {
1109
- const {
1110
- current: player
1111
- } = playerRef;
1112
- return player !== null && typeof player.playVideo !== 'undefined' ? Promise.resolve(player.playVideo()) : Promise.reject(NO_PLAYER_ERROR$1);
1113
- }, []);
1114
- const pause = react.useCallback(() => {
1115
- const {
1116
- current: player
1117
- } = playerRef;
1118
- return player !== null && typeof player.pauseVideo !== 'undefined' ? Promise.resolve(player.pauseVideo()) : Promise.reject(NO_PLAYER_ERROR$1);
1119
- }, []);
1120
- const setVolume = react.useCallback(volume => {
1121
- const {
1122
- current: player
1123
- } = playerRef;
1124
- const promise = player !== null && typeof player.setVolume !== 'undefined' ? Promise.resolve(player.setVolume(volume * 100)) : Promise.reject(NO_PLAYER_ERROR$1);
1125
- if (customOnVolumeChange) {
1126
- customOnVolumeChange(volume);
1127
- }
1128
- return promise;
1129
- }, [customOnVolumeChange]);
1130
- const mute = react.useCallback(() => {
1131
- const {
1132
- current: player
1133
- } = playerRef;
1134
- return (player !== null && typeof player.mute !== 'undefined' ? Promise.resolve(player.mute()) : Promise.reject(NO_PLAYER_ERROR$1)).then(() => setMuted(true));
1135
- }, [setMuted]);
1136
- const unmute = react.useCallback(() => {
1137
- const {
1138
- current: player
1139
- } = playerRef;
1140
- return (player !== null && typeof player.unMute !== 'undefined' ? Promise.resolve(player.unMute()) : Promise.reject(NO_PLAYER_ERROR$1)).then(() => setMuted(false));
1141
- }, []);
1142
- const seek = react.useCallback(time => {
1143
- const {
1144
- current: player
1145
- } = playerRef;
1146
- return player !== null && typeof player.seekTo !== 'undefined' ? Promise.resolve(player.seekTo(time)) : Promise.reject(NO_PLAYER_ERROR$1);
1147
- }, []);
1148
- const setLoop = react.useCallback(loop => {
1149
- const {
1150
- current: player
1151
- } = playerRef;
1152
- return player !== null && typeof player.setLoop !== 'undefined' ? Promise.resolve(player.setLoop(loop)) : Promise.reject(NO_PLAYER_ERROR$1);
1153
- }, []);
1154
- const destroyPlayer = react.useCallback(() => {
1155
- if (playerRef.current !== null) {
1156
- debug$1('Unset player');
1157
- playerRef.current = null;
1158
- }
1159
- }, []);
1160
-
1161
- // Detect iframe switch and destroy player
1162
-
1163
- react.useEffect(() => {
1164
- const {
1165
- current: currentPlayer
1166
- } = playerRef;
1167
- if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
1168
- debug$1('iFrame switched');
1169
- destroyPlayer();
1170
- }
1171
- }, [elementHasChanged]);
1172
-
1173
- // Create player
1174
- react.useEffect(() => {
1175
- const {
1176
- current: YT = null
1177
- } = apiRef;
1178
- const {
1179
- current: currentPlayer = null
1180
- } = playerRef;
1181
- const {
1182
- current: element = null
1183
- } = elementRef;
1184
- if (!apiLoaded || videoId === null || element === null) {
1185
- destroyPlayer();
1186
- return;
1187
- }
1188
- let player = currentPlayer;
1189
- if (player !== null && typeof player.loadVideoById !== 'undefined') {
1190
- debug$1('Switch video [ID: %s]', videoId);
1191
- player.loadVideoById(videoId);
1192
- } else {
1193
- debug$1('Create player [ID: %s]', videoId);
1194
- const onReady = _ref => {
1195
- let {
1196
- target
1197
- } = _ref;
1198
- player = target;
1199
- playerRef.current = target;
1200
- setReady(true);
1201
- const newDuration = target.getDuration();
1202
- if (newDuration !== metadata.duration) {
1203
- setMetadata({
1204
- ...metadata,
1205
- duration: newDuration
1206
- });
1207
- }
1208
- debug$1('onReady [ID: %s]', videoId);
1209
- };
1210
- const onStateChange = _ref2 => {
1211
- let {
1212
- data: state
1213
- } = _ref2;
1214
- const newState = {
1215
- playing: state === YT.PlayerState.PLAYING,
1216
- paused: state === YT.PlayerState.PAUSED,
1217
- ended: state === YT.PlayerState.ENDED,
1218
- buffering: state === YT.PlayerState.BUFFERING
1219
- };
1220
- setPlayState(newState);
1221
- let stateLabel = null;
1222
- if (newState.playing) {
1223
- stateLabel = 'playing';
1224
- } else if (newState.paused) {
1225
- stateLabel = 'paused';
1226
- } else if (newState.ended) {
1227
- stateLabel = 'ended';
1228
- } else if (newState.buffering) {
1229
- stateLabel = 'buffering';
1230
- } else if (state === -1) {
1231
- stateLabel = 'not started';
1232
- } else if (state === 0) {
1233
- stateLabel = 'stopped';
1234
- }
1235
- debug$1('onStateChange %s [ID: %s]', stateLabel, videoId);
1236
- };
1237
- player = new YT.Player(element, {
1238
- videoId,
1239
- playerVars: {
1240
- controls,
1241
- autoplay: autoplay ? 1 : 0,
1242
- mute: initialMuted,
1243
- playsinline: true,
1244
- rel: 0,
1245
- showinfo: 0,
1246
- modestbranding: 1
1247
- },
1248
- events: {
1249
- onReady,
1250
- onStateChange
1251
- }
1252
- });
1253
- }
1254
- playerRef.current = player;
1255
- playerElementRef.current = element;
1256
- }, [apiLoaded, videoId, elementHasChanged, setPlayState, setReady, setMetadata, destroyPlayer]);
1257
- const {
1258
- playing
1259
- } = playState;
1260
- const getCurrentTime = react.useCallback(p => p.getCurrentTime(), []);
1261
- const currentTime = usePlayerCurrentTime(playerRef.current, {
1262
- id: videoId,
1263
- disabled: !playing || timeUpdateInterval === null,
1264
- updateInterval: timeUpdateInterval,
1265
- onUpdate: customOnTimeUpdate,
1266
- getCurrentTime
1267
- });
1268
- return {
1269
- ref: elementRef,
1270
- player: playerRef.current,
1271
- play,
1272
- pause,
1273
- mute,
1274
- unmute,
1275
- setVolume,
1276
- seek,
1277
- setLoop,
1278
- ready,
1279
- currentTime,
1280
- muted,
1281
- loaded: ready,
1282
- ...metadata,
1283
- ...playState
1284
- };
1285
1048
  }
1286
1049
 
1287
- const debug = createDebug__default["default"]('folklore:video:vimeo');
1288
- const NO_PLAYER_ERROR = new Error('No player');
1289
- const useVimeoPlayer = function (id) {
1050
+ const NO_PLAYER_ERROR$1 = new Error('No player');
1051
+ function useVimeoPlayer(id) {
1290
1052
  let {
1291
1053
  width = 0,
1292
1054
  height = 0,
@@ -1306,6 +1068,7 @@ const useVimeoPlayer = function (id) {
1306
1068
  return match !== null ? match[1] : null;
1307
1069
  }
1308
1070
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1071
+ const debug = react.useMemo(() => createDebug__default["default"]('folklore:video:vimeo'), []);
1309
1072
  const [apiLoaded, setApiLoaded] = react.useState(false);
1310
1073
  const apiRef = react.useRef(null);
1311
1074
  const elementRef = react.useRef(null);
@@ -1349,43 +1112,43 @@ const useVimeoPlayer = function (id) {
1349
1112
  const {
1350
1113
  current: player
1351
1114
  } = playerRef;
1352
- return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR);
1115
+ return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR$1);
1353
1116
  }, []);
1354
1117
  const pause = react.useCallback(() => {
1355
1118
  const {
1356
1119
  current: player
1357
1120
  } = playerRef;
1358
- return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR);
1121
+ return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR$1);
1359
1122
  }, []);
1360
1123
  const setVolume = react.useCallback(newVolume => {
1361
1124
  const {
1362
1125
  current: player
1363
1126
  } = playerRef;
1364
- return player !== null ? player.setVolume(newVolume) : Promise.reject(NO_PLAYER_ERROR);
1127
+ return player !== null ? player.setVolume(newVolume) : Promise.reject(NO_PLAYER_ERROR$1);
1365
1128
  }, []);
1366
1129
  const mute = react.useCallback(() => {
1367
1130
  const {
1368
1131
  current: player
1369
1132
  } = playerRef;
1370
- return player !== null ? player.setVolume(0) : Promise.reject(NO_PLAYER_ERROR);
1133
+ return player !== null ? player.setVolume(0) : Promise.reject(NO_PLAYER_ERROR$1);
1371
1134
  }, []);
1372
1135
  const unmute = react.useCallback(() => {
1373
1136
  const {
1374
1137
  current: player
1375
1138
  } = playerRef;
1376
- return player !== null ? player.setVolume(1) : Promise.reject(NO_PLAYER_ERROR);
1139
+ return player !== null ? player.setVolume(1) : Promise.reject(NO_PLAYER_ERROR$1);
1377
1140
  }, []);
1378
1141
  const seek = react.useCallback(time => {
1379
1142
  const {
1380
1143
  current: player
1381
1144
  } = playerRef;
1382
- return player !== null ? player.setCurrentTime(time) : Promise.reject(NO_PLAYER_ERROR);
1145
+ return player !== null ? player.setCurrentTime(time) : Promise.reject(NO_PLAYER_ERROR$1);
1383
1146
  }, []);
1384
1147
  const setLoop = react.useCallback(loop => {
1385
1148
  const {
1386
1149
  current: player
1387
1150
  } = playerRef;
1388
- return player !== null ? player.setLoop(loop) : Promise.reject(NO_PLAYER_ERROR);
1151
+ return player !== null ? player.setLoop(loop) : Promise.reject(NO_PLAYER_ERROR$1);
1389
1152
  }, []);
1390
1153
  const destroyVideo = react.useCallback(() => {
1391
1154
  const {
@@ -1584,7 +1347,244 @@ const useVimeoPlayer = function (id) {
1584
1347
  ...metadata,
1585
1348
  ...playState
1586
1349
  };
1587
- };
1350
+ }
1351
+
1352
+ const NO_PLAYER_ERROR = new Error('No player');
1353
+ function useYouTubePlayer(id) {
1354
+ let {
1355
+ width = 0,
1356
+ height = 0,
1357
+ duration = 0,
1358
+ autoplay = false,
1359
+ controls = true,
1360
+ timeUpdateInterval = 1000,
1361
+ muted: initialMuted = false,
1362
+ onVolumeChange: customOnVolumeChange = null,
1363
+ onTimeUpdate: customOnTimeUpdate = null,
1364
+ getVideoId = url => {
1365
+ if (url === null || url.match(/^https?:/) === null) {
1366
+ return url;
1367
+ }
1368
+ const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
1369
+ const match = url.match(regExp);
1370
+ return match && match[7].length === 11 ? match[7] : null;
1371
+ }
1372
+ } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1373
+ const debug = react.useMemo(() => createDebug__default["default"]('folklore:video:youtube'), []);
1374
+ const [apiLoaded, setApiLoaded] = react.useState(typeof window !== 'undefined' && typeof window.YT !== 'undefined');
1375
+ const apiRef = react.useRef(typeof window !== 'undefined' && typeof window.YT !== 'undefined' ? window.YT : null);
1376
+ const elementRef = react.useRef(null);
1377
+ const playerRef = react.useRef(null);
1378
+ const playerElementRef = react.useRef(elementRef.current);
1379
+ const elementHasChanged = elementRef.current !== playerElementRef.current;
1380
+ const videoId = react.useMemo(() => getVideoId(id), [id]);
1381
+ const [ready, setReady] = react.useState(false);
1382
+ const [muted, setMuted] = react.useState(initialMuted);
1383
+ const [playState, setPlayState] = react.useState({
1384
+ playing: false,
1385
+ paused: false,
1386
+ ended: false,
1387
+ buffering: false
1388
+ });
1389
+ const [metadata, setMetadata] = react.useState({
1390
+ width,
1391
+ height,
1392
+ duration
1393
+ });
1394
+ react.useEffect(() => {
1395
+ let canceled = false;
1396
+ if (!apiLoaded && videoId !== null) {
1397
+ debug('Load API');
1398
+ services.loadYouTube().then(api => {
1399
+ if (!canceled) {
1400
+ apiRef.current = api;
1401
+ setApiLoaded(true);
1402
+ debug('API Loaded');
1403
+ }
1404
+ });
1405
+ }
1406
+ return () => {
1407
+ canceled = true;
1408
+ };
1409
+ }, [apiLoaded, videoId, setApiLoaded]);
1410
+ const play = react.useCallback(() => {
1411
+ const {
1412
+ current: player
1413
+ } = playerRef;
1414
+ return player !== null && typeof player.playVideo !== 'undefined' ? Promise.resolve(player.playVideo()) : Promise.reject(NO_PLAYER_ERROR);
1415
+ }, []);
1416
+ const pause = react.useCallback(() => {
1417
+ const {
1418
+ current: player
1419
+ } = playerRef;
1420
+ return player !== null && typeof player.pauseVideo !== 'undefined' ? Promise.resolve(player.pauseVideo()) : Promise.reject(NO_PLAYER_ERROR);
1421
+ }, []);
1422
+ const setVolume = react.useCallback(volume => {
1423
+ const {
1424
+ current: player
1425
+ } = playerRef;
1426
+ const promise = player !== null && typeof player.setVolume !== 'undefined' ? Promise.resolve(player.setVolume(volume * 100)) : Promise.reject(NO_PLAYER_ERROR);
1427
+ if (customOnVolumeChange) {
1428
+ customOnVolumeChange(volume);
1429
+ }
1430
+ return promise;
1431
+ }, [customOnVolumeChange]);
1432
+ const mute = react.useCallback(() => {
1433
+ const {
1434
+ current: player
1435
+ } = playerRef;
1436
+ return (player !== null && typeof player.mute !== 'undefined' ? Promise.resolve(player.mute()) : Promise.reject(NO_PLAYER_ERROR)).then(() => setMuted(true));
1437
+ }, [setMuted]);
1438
+ const unmute = react.useCallback(() => {
1439
+ const {
1440
+ current: player
1441
+ } = playerRef;
1442
+ return (player !== null && typeof player.unMute !== 'undefined' ? Promise.resolve(player.unMute()) : Promise.reject(NO_PLAYER_ERROR)).then(() => setMuted(false));
1443
+ }, []);
1444
+ const seek = react.useCallback(time => {
1445
+ const {
1446
+ current: player
1447
+ } = playerRef;
1448
+ return player !== null && typeof player.seekTo !== 'undefined' ? Promise.resolve(player.seekTo(time)) : Promise.reject(NO_PLAYER_ERROR);
1449
+ }, []);
1450
+ const setLoop = react.useCallback(loop => {
1451
+ const {
1452
+ current: player
1453
+ } = playerRef;
1454
+ return player !== null && typeof player.setLoop !== 'undefined' ? Promise.resolve(player.setLoop(loop)) : Promise.reject(NO_PLAYER_ERROR);
1455
+ }, []);
1456
+ const destroyPlayer = react.useCallback(() => {
1457
+ if (playerRef.current !== null) {
1458
+ debug('Unset player');
1459
+ playerRef.current = null;
1460
+ }
1461
+ }, []);
1462
+
1463
+ // Detect iframe switch and destroy player
1464
+
1465
+ react.useEffect(() => {
1466
+ const {
1467
+ current: currentPlayer
1468
+ } = playerRef;
1469
+ if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
1470
+ debug('iFrame switched');
1471
+ destroyPlayer();
1472
+ }
1473
+ }, [elementHasChanged]);
1474
+
1475
+ // Create player
1476
+ react.useEffect(() => {
1477
+ const {
1478
+ current: YT = null
1479
+ } = apiRef;
1480
+ const {
1481
+ current: currentPlayer = null
1482
+ } = playerRef;
1483
+ const {
1484
+ current: element = null
1485
+ } = elementRef;
1486
+ if (!apiLoaded || videoId === null || element === null) {
1487
+ destroyPlayer();
1488
+ return;
1489
+ }
1490
+ let player = currentPlayer;
1491
+ if (player !== null && typeof player.loadVideoById !== 'undefined') {
1492
+ debug('Switch video [ID: %s]', videoId);
1493
+ player.loadVideoById(videoId);
1494
+ } else {
1495
+ debug('Create player [ID: %s]', videoId);
1496
+ const onReady = _ref => {
1497
+ let {
1498
+ target
1499
+ } = _ref;
1500
+ player = target;
1501
+ playerRef.current = target;
1502
+ setReady(true);
1503
+ const newDuration = target.getDuration();
1504
+ if (newDuration !== metadata.duration) {
1505
+ setMetadata({
1506
+ ...metadata,
1507
+ duration: newDuration
1508
+ });
1509
+ }
1510
+ debug('onReady [ID: %s]', videoId);
1511
+ };
1512
+ const onStateChange = _ref2 => {
1513
+ let {
1514
+ data: state
1515
+ } = _ref2;
1516
+ const newState = {
1517
+ playing: state === YT.PlayerState.PLAYING,
1518
+ paused: state === YT.PlayerState.PAUSED,
1519
+ ended: state === YT.PlayerState.ENDED,
1520
+ buffering: state === YT.PlayerState.BUFFERING
1521
+ };
1522
+ setPlayState(newState);
1523
+ let stateLabel = null;
1524
+ if (newState.playing) {
1525
+ stateLabel = 'playing';
1526
+ } else if (newState.paused) {
1527
+ stateLabel = 'paused';
1528
+ } else if (newState.ended) {
1529
+ stateLabel = 'ended';
1530
+ } else if (newState.buffering) {
1531
+ stateLabel = 'buffering';
1532
+ } else if (state === -1) {
1533
+ stateLabel = 'not started';
1534
+ } else if (state === 0) {
1535
+ stateLabel = 'stopped';
1536
+ }
1537
+ debug('onStateChange %s [ID: %s]', stateLabel, videoId);
1538
+ };
1539
+ player = new YT.Player(element, {
1540
+ videoId,
1541
+ playerVars: {
1542
+ controls,
1543
+ autoplay: autoplay ? 1 : 0,
1544
+ mute: initialMuted,
1545
+ playsinline: true,
1546
+ rel: 0,
1547
+ showinfo: 0,
1548
+ modestbranding: 1
1549
+ },
1550
+ events: {
1551
+ onReady,
1552
+ onStateChange
1553
+ }
1554
+ });
1555
+ }
1556
+ playerRef.current = player;
1557
+ playerElementRef.current = element;
1558
+ }, [apiLoaded, videoId, elementHasChanged, setPlayState, setReady, setMetadata, destroyPlayer]);
1559
+ const {
1560
+ playing
1561
+ } = playState;
1562
+ const getCurrentTime = react.useCallback(p => p.getCurrentTime(), []);
1563
+ const currentTime = usePlayerCurrentTime(playerRef.current, {
1564
+ id: videoId,
1565
+ disabled: !playing || timeUpdateInterval === null,
1566
+ updateInterval: timeUpdateInterval,
1567
+ onUpdate: customOnTimeUpdate,
1568
+ getCurrentTime
1569
+ });
1570
+ return {
1571
+ ref: elementRef,
1572
+ player: playerRef.current,
1573
+ play,
1574
+ pause,
1575
+ mute,
1576
+ unmute,
1577
+ setVolume,
1578
+ seek,
1579
+ setLoop,
1580
+ ready,
1581
+ currentTime,
1582
+ muted,
1583
+ loaded: ready,
1584
+ ...metadata,
1585
+ ...playState
1586
+ };
1587
+ }
1588
1588
 
1589
1589
  function useVideoPlayer(params) {
1590
1590
  const {
@@ -1668,7 +1668,7 @@ const getWindowScroll = () => ({
1668
1668
  y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
1669
1669
  });
1670
1670
  const currentScroll = getWindowScroll();
1671
- const useWindowScroll = function () {
1671
+ function useWindowScroll() {
1672
1672
  let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1673
1673
  const {
1674
1674
  onChange = null
@@ -1695,7 +1695,7 @@ const useWindowScroll = function () {
1695
1695
  onScroll();
1696
1696
  }, []);
1697
1697
  return scroll;
1698
- };
1698
+ }
1699
1699
 
1700
1700
  const getWindowSize = () => ({
1701
1701
  width: typeof window !== 'undefined' ? window.innerWidth || 0 : 0,