@fringeworks/react-defer-rendering 0.8.0 → 0.9.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.
Files changed (172) hide show
  1. package/README.ja.md +313 -319
  2. package/README.md +313 -319
  3. package/_usePromiseState.cjs +13 -20
  4. package/_usePromiseState.d.cts +5 -2
  5. package/_usePromiseState.d.mts +6 -3
  6. package/_usePromiseState.mjs +10 -16
  7. package/_virtual/_rolldown/runtime.cjs +1 -2
  8. package/constants.cjs +0 -1
  9. package/constants.d.cts +3 -4
  10. package/constants.d.mts +3 -4
  11. package/index.cjs +41 -44
  12. package/index.d.cts +15 -4
  13. package/index.d.mts +15 -4
  14. package/index.mjs +4 -7
  15. package/package.json +113 -31
  16. package/types.cjs +1 -0
  17. package/types.d.cts +7 -8
  18. package/types.d.mts +7 -8
  19. package/useDeferUntilAsyncComplete/index.cjs +4 -4
  20. package/useDeferUntilAsyncComplete/index.d.cts +1 -1
  21. package/useDeferUntilAsyncComplete/index.d.mts +1 -1
  22. package/useDeferUntilAsyncComplete/index.mjs +1 -3
  23. package/useDeferUntilAsyncComplete/types.cjs +1 -0
  24. package/useDeferUntilAsyncComplete/types.d.cts +3 -4
  25. package/useDeferUntilAsyncComplete/types.d.mts +3 -4
  26. package/useDeferUntilAsyncComplete/useDeferUntilAsyncComplete.cjs +7 -5
  27. package/useDeferUntilAsyncComplete/useDeferUntilAsyncComplete.d.cts +0 -1
  28. package/useDeferUntilAsyncComplete/useDeferUntilAsyncComplete.d.mts +2 -4
  29. package/useDeferUntilAsyncComplete/useDeferUntilAsyncComplete.mjs +2 -1
  30. package/useDeferUntilBreakpoint/index.cjs +4 -4
  31. package/useDeferUntilBreakpoint/index.d.cts +1 -1
  32. package/useDeferUntilBreakpoint/index.d.mts +1 -1
  33. package/useDeferUntilBreakpoint/index.mjs +1 -3
  34. package/useDeferUntilBreakpoint/types.cjs +1 -0
  35. package/useDeferUntilBreakpoint/types.d.cts +5 -5
  36. package/useDeferUntilBreakpoint/types.d.mts +5 -5
  37. package/useDeferUntilBreakpoint/useDeferUntilBreakpoint.cjs +29 -9
  38. package/useDeferUntilBreakpoint/useDeferUntilBreakpoint.d.cts +0 -1
  39. package/useDeferUntilBreakpoint/useDeferUntilBreakpoint.d.mts +2 -4
  40. package/useDeferUntilBreakpoint/useDeferUntilBreakpoint.mjs +25 -6
  41. package/useDeferUntilCallThreshold/index.cjs +4 -4
  42. package/useDeferUntilCallThreshold/index.d.cts +1 -1
  43. package/useDeferUntilCallThreshold/index.d.mts +1 -1
  44. package/useDeferUntilCallThreshold/index.mjs +1 -3
  45. package/useDeferUntilCallThreshold/types.cjs +1 -0
  46. package/useDeferUntilCallThreshold/types.d.cts +3 -4
  47. package/useDeferUntilCallThreshold/types.d.mts +3 -4
  48. package/useDeferUntilCallThreshold/useDeferUntilCallThreshold.cjs +32 -19
  49. package/useDeferUntilCallThreshold/useDeferUntilCallThreshold.d.cts +0 -1
  50. package/useDeferUntilCallThreshold/useDeferUntilCallThreshold.d.mts +2 -4
  51. package/useDeferUntilCallThreshold/useDeferUntilCallThreshold.mjs +27 -15
  52. package/useDeferUntilChange/index.cjs +4 -4
  53. package/useDeferUntilChange/index.d.cts +1 -1
  54. package/useDeferUntilChange/index.d.mts +1 -1
  55. package/useDeferUntilChange/index.mjs +1 -3
  56. package/useDeferUntilChange/types.cjs +1 -0
  57. package/useDeferUntilChange/types.d.cts +3 -4
  58. package/useDeferUntilChange/types.d.mts +3 -4
  59. package/useDeferUntilChange/useDeferUntilChange.cjs +6 -9
  60. package/useDeferUntilChange/useDeferUntilChange.d.cts +0 -1
  61. package/useDeferUntilChange/useDeferUntilChange.d.mts +2 -4
  62. package/useDeferUntilChange/useDeferUntilChange.mjs +2 -4
  63. package/useDeferUntilDate/index.cjs +4 -4
  64. package/useDeferUntilDate/index.d.cts +1 -1
  65. package/useDeferUntilDate/index.d.mts +1 -1
  66. package/useDeferUntilDate/index.mjs +1 -3
  67. package/useDeferUntilDate/types.cjs +1 -0
  68. package/useDeferUntilDate/types.d.cts +3 -4
  69. package/useDeferUntilDate/types.d.mts +3 -4
  70. package/useDeferUntilDate/useDeferUntilDate.cjs +13 -4
  71. package/useDeferUntilDate/useDeferUntilDate.d.cts +0 -1
  72. package/useDeferUntilDate/useDeferUntilDate.d.mts +2 -4
  73. package/useDeferUntilDate/useDeferUntilDate.mjs +10 -4
  74. package/useDeferUntilIntersected/index.cjs +4 -4
  75. package/useDeferUntilIntersected/index.d.cts +1 -1
  76. package/useDeferUntilIntersected/index.d.mts +1 -1
  77. package/useDeferUntilIntersected/index.mjs +1 -3
  78. package/useDeferUntilIntersected/types.cjs +1 -0
  79. package/useDeferUntilIntersected/types.d.cts +8 -9
  80. package/useDeferUntilIntersected/types.d.mts +8 -9
  81. package/useDeferUntilIntersected/useDeferUntilIntersected.cjs +14 -13
  82. package/useDeferUntilIntersected/useDeferUntilIntersected.d.cts +0 -1
  83. package/useDeferUntilIntersected/useDeferUntilIntersected.d.mts +2 -4
  84. package/useDeferUntilIntersected/useDeferUntilIntersected.mjs +10 -8
  85. package/useDeferUntilOnReady/index.cjs +4 -4
  86. package/useDeferUntilOnReady/index.d.cts +1 -1
  87. package/useDeferUntilOnReady/index.d.mts +1 -1
  88. package/useDeferUntilOnReady/index.mjs +1 -3
  89. package/useDeferUntilOnReady/types.cjs +1 -0
  90. package/useDeferUntilOnReady/types.d.cts +3 -4
  91. package/useDeferUntilOnReady/types.d.mts +3 -4
  92. package/useDeferUntilOnReady/useDeferUntilOnReady.cjs +28 -21
  93. package/useDeferUntilOnReady/useDeferUntilOnReady.d.cts +2 -2
  94. package/useDeferUntilOnReady/useDeferUntilOnReady.d.mts +4 -5
  95. package/useDeferUntilOnReady/useDeferUntilOnReady.mjs +23 -15
  96. package/useDeferUntilReady/index.cjs +4 -4
  97. package/useDeferUntilReady/index.d.cts +1 -1
  98. package/useDeferUntilReady/index.d.mts +1 -1
  99. package/useDeferUntilReady/index.mjs +1 -3
  100. package/useDeferUntilReady/types.cjs +1 -0
  101. package/useDeferUntilReady/types.d.cts +2 -4
  102. package/useDeferUntilReady/types.d.mts +2 -4
  103. package/useDeferUntilReady/useDeferUntilReady.cjs +20 -32
  104. package/useDeferUntilReady/useDeferUntilReady.d.cts +0 -1
  105. package/useDeferUntilReady/useDeferUntilReady.d.mts +2 -4
  106. package/useDeferUntilReady/useDeferUntilReady.mjs +18 -29
  107. package/useDeferUntilRender/index.cjs +4 -4
  108. package/useDeferUntilRender/index.d.cts +1 -1
  109. package/useDeferUntilRender/index.d.mts +1 -1
  110. package/useDeferUntilRender/index.mjs +1 -3
  111. package/useDeferUntilRender/types.cjs +1 -0
  112. package/useDeferUntilRender/types.d.cts +5 -5
  113. package/useDeferUntilRender/types.d.mts +5 -5
  114. package/useDeferUntilRender/useDeferUntilRender.cjs +31 -12
  115. package/useDeferUntilRender/useDeferUntilRender.d.cts +0 -1
  116. package/useDeferUntilRender/useDeferUntilRender.d.mts +2 -4
  117. package/useDeferUntilRender/useDeferUntilRender.mjs +26 -8
  118. package/useDeferUntilResolved/index.cjs +4 -4
  119. package/useDeferUntilResolved/index.d.cts +1 -1
  120. package/useDeferUntilResolved/index.d.mts +1 -1
  121. package/useDeferUntilResolved/index.mjs +1 -3
  122. package/useDeferUntilResolved/types.cjs +1 -0
  123. package/useDeferUntilResolved/types.d.cts +3 -4
  124. package/useDeferUntilResolved/types.d.mts +3 -4
  125. package/useDeferUntilResolved/useDeferUntilResolved.cjs +8 -5
  126. package/useDeferUntilResolved/useDeferUntilResolved.d.cts +0 -1
  127. package/useDeferUntilResolved/useDeferUntilResolved.d.mts +2 -4
  128. package/useDeferUntilResolved/useDeferUntilResolved.mjs +3 -1
  129. package/useDeferUntilTimeout/index.cjs +4 -4
  130. package/useDeferUntilTimeout/index.d.cts +1 -1
  131. package/useDeferUntilTimeout/index.d.mts +1 -1
  132. package/useDeferUntilTimeout/index.mjs +1 -3
  133. package/useDeferUntilTimeout/types.cjs +1 -0
  134. package/useDeferUntilTimeout/types.d.cts +3 -4
  135. package/useDeferUntilTimeout/types.d.mts +3 -4
  136. package/useDeferUntilTimeout/useDeferUntilTimeout.cjs +23 -21
  137. package/useDeferUntilTimeout/useDeferUntilTimeout.d.cts +0 -1
  138. package/useDeferUntilTimeout/useDeferUntilTimeout.d.mts +2 -4
  139. package/useDeferUntilTimeout/useDeferUntilTimeout.mjs +16 -14
  140. package/useDeferUntilTrue/index.cjs +4 -4
  141. package/useDeferUntilTrue/index.d.cts +1 -1
  142. package/useDeferUntilTrue/index.d.mts +1 -1
  143. package/useDeferUntilTrue/index.mjs +1 -3
  144. package/useDeferUntilTrue/types.cjs +1 -0
  145. package/useDeferUntilTrue/types.d.cts +2 -4
  146. package/useDeferUntilTrue/types.d.mts +2 -4
  147. package/useDeferUntilTrue/useDeferUntilTrue.cjs +6 -5
  148. package/useDeferUntilTrue/useDeferUntilTrue.d.cts +0 -1
  149. package/useDeferUntilTrue/useDeferUntilTrue.d.mts +2 -4
  150. package/useDeferUntilWebFontReady/index.cjs +4 -4
  151. package/useDeferUntilWebFontReady/index.d.cts +1 -1
  152. package/useDeferUntilWebFontReady/index.d.mts +1 -1
  153. package/useDeferUntilWebFontReady/index.mjs +1 -3
  154. package/useDeferUntilWebFontReady/types.cjs +1 -0
  155. package/useDeferUntilWebFontReady/types.d.cts +6 -7
  156. package/useDeferUntilWebFontReady/types.d.mts +6 -7
  157. package/useDeferUntilWebFontReady/useDeferUntilWebFontReady.cjs +23 -14
  158. package/useDeferUntilWebFontReady/useDeferUntilWebFontReady.d.cts +0 -1
  159. package/useDeferUntilWebFontReady/useDeferUntilWebFontReady.d.mts +2 -4
  160. package/useDeferUntilWebFontReady/useDeferUntilWebFontReady.mjs +16 -9
  161. package/useDeferUntilScrolled/index.cjs +0 -5
  162. package/useDeferUntilScrolled/index.d.cts +0 -3
  163. package/useDeferUntilScrolled/index.d.mts +0 -3
  164. package/useDeferUntilScrolled/index.mjs +0 -5
  165. package/useDeferUntilScrolled/types.cjs +0 -0
  166. package/useDeferUntilScrolled/types.d.cts +0 -29
  167. package/useDeferUntilScrolled/types.d.mts +0 -29
  168. package/useDeferUntilScrolled/types.mjs +0 -1
  169. package/useDeferUntilScrolled/useDeferUntilScrolled.cjs +0 -94
  170. package/useDeferUntilScrolled/useDeferUntilScrolled.d.cts +0 -14
  171. package/useDeferUntilScrolled/useDeferUntilScrolled.d.mts +0 -15
  172. package/useDeferUntilScrolled/useDeferUntilScrolled.mjs +0 -91
@@ -1,5 +1,3 @@
1
- 'use client';
2
-
3
1
  import useDeferUntilWebFontReady from "./useDeferUntilWebFontReady.mjs";
4
2
 
5
- export { useDeferUntilWebFontReady as default };
3
+ export { useDeferUntilWebFontReady as default, useDeferUntilWebFontReady };
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
@@ -1,13 +1,13 @@
1
1
  import { RenderingState } from "../constants.cjs";
2
2
  import { UseDeferUntilReadyOptions } from "../useDeferUntilReady/types.cjs";
3
+ import "../useDeferUntilReady/index.cjs";
3
4
  import { ReactNode } from "react";
4
5
  import { WebFontMatchOptions } from "@fringeworks/web-font-observer";
5
-
6
6
  //#region src/useDeferUntilWebFontReady/types.d.ts
7
7
  /**
8
8
  * 太さ等を指定して待つWebフォント
9
9
  */
10
- type WebFont = WebFontMatchOptions & {
10
+ export type WebFont = WebFontMatchOptions & {
11
11
  /**
12
12
  * フォントファミリー
13
13
  */
@@ -17,13 +17,13 @@ type WebFont = WebFontMatchOptions & {
17
17
  * 待つWebフォント\
18
18
  * フォントファミリーのみで良い場合は文字列で指定できる
19
19
  */
20
- type WebFontTarget = string | WebFont;
20
+ export type WebFontTarget = string | WebFont;
21
21
  /**
22
22
  * 待つWebフォントの指定\
23
23
  * 複数のWebフォントを待つ場合は配列で指定する。配列内のnull/undefinedは無視される
24
24
  */
25
- type WebFontTargets = WebFontTarget | readonly (WebFontTarget | null | undefined)[];
26
- type UseDeferUntilWebFontReadyOptions<P extends ReactNode = ReactNode, E extends ReactNode = ReactNode> = UseDeferUntilReadyOptions<P, E> & {
25
+ export type WebFontTargets = WebFontTarget | readonly (WebFontTarget | null | undefined)[];
26
+ export type UseDeferUntilWebFontReadyOptions<P extends ReactNode = ReactNode, E extends ReactNode = ReactNode> = UseDeferUntilReadyOptions<P, E> & {
27
27
  /**
28
28
  * Webフォントが利用可能にならなかった場合に失敗とみなすまでの時間(ミリ秒)\
29
29
  * Webフォントごとに適用されるが、すべて並行して待つため実質的に全体の待ち時間となる\
@@ -45,5 +45,4 @@ type UseDeferUntilWebFontReadyOptions<P extends ReactNode = ReactNode, E extends
45
45
  */
46
46
  initialState?: RenderingState;
47
47
  };
48
- //#endregion
49
- export { UseDeferUntilWebFontReadyOptions, WebFont, WebFontTarget, WebFontTargets };
48
+ //#endregion
@@ -1,13 +1,13 @@
1
1
  import { RenderingState } from "../constants.mjs";
2
2
  import { UseDeferUntilReadyOptions } from "../useDeferUntilReady/types.mjs";
3
+ import "../useDeferUntilReady/index.mjs";
3
4
  import { ReactNode } from "react";
4
5
  import { WebFontMatchOptions } from "@fringeworks/web-font-observer";
5
-
6
6
  //#region src/useDeferUntilWebFontReady/types.d.ts
7
7
  /**
8
8
  * 太さ等を指定して待つWebフォント
9
9
  */
10
- type WebFont = WebFontMatchOptions & {
10
+ export type WebFont = WebFontMatchOptions & {
11
11
  /**
12
12
  * フォントファミリー
13
13
  */
@@ -17,13 +17,13 @@ type WebFont = WebFontMatchOptions & {
17
17
  * 待つWebフォント\
18
18
  * フォントファミリーのみで良い場合は文字列で指定できる
19
19
  */
20
- type WebFontTarget = string | WebFont;
20
+ export type WebFontTarget = string | WebFont;
21
21
  /**
22
22
  * 待つWebフォントの指定\
23
23
  * 複数のWebフォントを待つ場合は配列で指定する。配列内のnull/undefinedは無視される
24
24
  */
25
- type WebFontTargets = WebFontTarget | readonly (WebFontTarget | null | undefined)[];
26
- type UseDeferUntilWebFontReadyOptions<P extends ReactNode = ReactNode, E extends ReactNode = ReactNode> = UseDeferUntilReadyOptions<P, E> & {
25
+ export type WebFontTargets = WebFontTarget | readonly (WebFontTarget | null | undefined)[];
26
+ export type UseDeferUntilWebFontReadyOptions<P extends ReactNode = ReactNode, E extends ReactNode = ReactNode> = UseDeferUntilReadyOptions<P, E> & {
27
27
  /**
28
28
  * Webフォントが利用可能にならなかった場合に失敗とみなすまでの時間(ミリ秒)\
29
29
  * Webフォントごとに適用されるが、すべて並行して待つため実質的に全体の待ち時間となる\
@@ -45,5 +45,4 @@ type UseDeferUntilWebFontReadyOptions<P extends ReactNode = ReactNode, E extends
45
45
  */
46
46
  initialState?: RenderingState;
47
47
  };
48
- //#endregion
49
- export { UseDeferUntilWebFontReadyOptions, WebFont, WebFontTarget, WebFontTargets };
48
+ //#endregion
@@ -1,10 +1,13 @@
1
1
  'use client';
2
2
 
3
- const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
4
- const require_constants = require('../constants.cjs');
5
- const require_useDeferUntilReady_useDeferUntilReady = require('../useDeferUntilReady/useDeferUntilReady.cjs');
6
- require('../useDeferUntilReady/index.cjs');
3
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
4
+ const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
5
+ const require_constants = require("../constants.cjs");
6
+ const require_useDeferUntilReady_useDeferUntilReady = require("../useDeferUntilReady/useDeferUntilReady.cjs");
7
+ require("../useDeferUntilReady/index.cjs");
7
8
  let react = require("react");
9
+ let _fringeworks_react_utils_hooks_useLatestRef = require("@fringeworks/react-utils/hooks/useLatestRef");
10
+ _fringeworks_react_utils_hooks_useLatestRef = require_runtime.__toESM(_fringeworks_react_utils_hooks_useLatestRef, 1);
8
11
  let _fringeworks_web_font_observer_isWebFontLoaded = require("@fringeworks/web-font-observer/isWebFontLoaded");
9
12
  _fringeworks_web_font_observer_isWebFontLoaded = require_runtime.__toESM(_fringeworks_web_font_observer_isWebFontLoaded, 1);
10
13
  let _fringeworks_web_font_observer_waitForWebFont = require("@fringeworks/web-font-observer/waitForWebFont");
@@ -22,23 +25,24 @@ _fringeworks_web_font_observer_waitForWebFont = require_runtime.__toESM(_fringew
22
25
  */
23
26
  function useDeferUntilWebFontReady(target, fonts, options = {}) {
24
27
  const { timeout, loader, initialState = require_constants.RenderingState.pending, ...opts } = options;
25
- const normalizedFonts = normalizeFonts(fonts);
26
- const fontsKey = JSON.stringify(normalizedFonts);
27
- const webFonts = (0, react.useMemo)(() => normalizedFonts, [fontsKey]);
28
- const loaderRef = (0, react.useRef)(loader);
29
- loaderRef.current = loader;
28
+ const fontsKey = JSON.stringify(normalizeFonts(fonts));
29
+ const webFonts = (0, react.useMemo)(() => JSON.parse(fontsKey), [fontsKey]);
30
+ const loaderRef = (0, _fringeworks_react_utils_hooks_useLatestRef.default)(loader);
30
31
  const [result, setResult] = (0, react.useState)(() => ({
31
32
  key: fontsKey,
32
33
  state: initialState
33
34
  }));
34
- const isLoaded = (0, react.useSyncExternalStore)((0, react.useCallback)((onStoreChange) => {
35
+ const subscribe = (0, react.useCallback)((onStoreChange) => {
35
36
  const fontFaceSet = typeof document === "undefined" ? void 0 : document.fonts;
36
37
  if (webFonts.length === 0 || !fontFaceSet) return () => {};
37
38
  fontFaceSet.addEventListener("loadingdone", onStoreChange);
38
39
  return () => {
39
40
  fontFaceSet.removeEventListener("loadingdone", onStoreChange);
40
41
  };
41
- }, [webFonts]), (0, react.useCallback)(() => webFonts.every(({ family, ...rest }) => (0, _fringeworks_web_font_observer_isWebFontLoaded.default)(family, rest)), [webFonts]), (0, react.useCallback)(() => webFonts.length === 0, [webFonts]));
42
+ }, [webFonts]);
43
+ const getSnapshot = (0, react.useCallback)(() => webFonts.every(({ family, ...rest }) => (0, _fringeworks_web_font_observer_isWebFontLoaded.default)(family, rest)), [webFonts]);
44
+ const getServerSnapshot = (0, react.useCallback)(() => webFonts.length === 0, [webFonts]);
45
+ const isLoaded = (0, react.useSyncExternalStore)(subscribe, getSnapshot, getServerSnapshot);
42
46
  (0, react.useEffect)(() => {
43
47
  if (webFonts.length === 0) return;
44
48
  const controller = new AbortController();
@@ -62,12 +66,17 @@ function useDeferUntilWebFontReady(target, fonts, options = {}) {
62
66
  return () => {
63
67
  controller.abort();
64
68
  };
65
- }, [webFonts, timeout]);
69
+ }, [
70
+ webFonts,
71
+ fontsKey,
72
+ timeout,
73
+ loaderRef
74
+ ]);
66
75
  let state;
67
76
  if (isLoaded) state = require_constants.RenderingState.ready;
68
77
  else if (result.key === fontsKey) state = result.state;
69
78
  else state = require_constants.RenderingState.pending;
70
- return require_useDeferUntilReady_useDeferUntilReady(target, state, opts);
79
+ return require_useDeferUntilReady_useDeferUntilReady.default(target, state, opts);
71
80
  }
72
81
  /**
73
82
  * 待つWebフォントの指定を、比較可能な形の配列に正規化する
@@ -90,4 +99,4 @@ function normalizeFonts(fonts) {
90
99
  }
91
100
 
92
101
  //#endregion
93
- module.exports = useDeferUntilWebFontReady;
102
+ exports.default = useDeferUntilWebFontReady;
@@ -1,7 +1,6 @@
1
1
  import { DeferRenderingResult } from "../types.cjs";
2
2
  import { UseDeferUntilWebFontReadyOptions, WebFontTargets } from "./types.cjs";
3
3
  import { ReactNode } from "react";
4
-
5
4
  //#region src/useDeferUntilWebFontReady/useDeferUntilWebFontReady.d.ts
6
5
  /**
7
6
  * 指定のWebフォントがすべて利用可能になるまで描画を遅延させるhook\
@@ -1,7 +1,6 @@
1
1
  import { DeferRenderingResult } from "../types.mjs";
2
2
  import { UseDeferUntilWebFontReadyOptions, WebFontTargets } from "./types.mjs";
3
3
  import { ReactNode } from "react";
4
-
5
4
  //#region src/useDeferUntilWebFontReady/useDeferUntilWebFontReady.d.ts
6
5
  /**
7
6
  * 指定のWebフォントがすべて利用可能になるまで描画を遅延させるhook\
@@ -12,6 +11,5 @@ import { ReactNode } from "react";
12
11
  * @param options オプション
13
12
  * @returns state('pending', 'ready', 'fallback')と状態に応じたノード
14
13
  */
15
- declare function useDeferUntilWebFontReady<T extends ReactNode, P extends ReactNode = ReactNode, E extends ReactNode = ReactNode>(target: T, fonts: WebFontTargets | null | undefined, options?: UseDeferUntilWebFontReadyOptions<P, E>): DeferRenderingResult<T | P | E>;
16
- //#endregion
17
- export { useDeferUntilWebFontReady as default };
14
+ export default function useDeferUntilWebFontReady<T extends ReactNode, P extends ReactNode = ReactNode, E extends ReactNode = ReactNode>(target: T, fonts: WebFontTargets | null | undefined, options?: UseDeferUntilWebFontReadyOptions<P, E>): DeferRenderingResult<T | P | E>;
15
+ //#endregion
@@ -3,7 +3,8 @@
3
3
  import { RenderingState } from "../constants.mjs";
4
4
  import useDeferUntilReady from "../useDeferUntilReady/useDeferUntilReady.mjs";
5
5
  import "../useDeferUntilReady/index.mjs";
6
- import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
6
+ import { useCallback, useEffect, useMemo, useState, useSyncExternalStore } from "react";
7
+ import useLatestRef from "@fringeworks/react-utils/hooks/useLatestRef";
7
8
  import isWebFontLoaded from "@fringeworks/web-font-observer/isWebFontLoaded";
8
9
  import waitForWebFont from "@fringeworks/web-font-observer/waitForWebFont";
9
10
 
@@ -19,23 +20,24 @@ import waitForWebFont from "@fringeworks/web-font-observer/waitForWebFont";
19
20
  */
20
21
  function useDeferUntilWebFontReady(target, fonts, options = {}) {
21
22
  const { timeout, loader, initialState = RenderingState.pending, ...opts } = options;
22
- const normalizedFonts = normalizeFonts(fonts);
23
- const fontsKey = JSON.stringify(normalizedFonts);
24
- const webFonts = useMemo(() => normalizedFonts, [fontsKey]);
25
- const loaderRef = useRef(loader);
26
- loaderRef.current = loader;
23
+ const fontsKey = JSON.stringify(normalizeFonts(fonts));
24
+ const webFonts = useMemo(() => JSON.parse(fontsKey), [fontsKey]);
25
+ const loaderRef = useLatestRef(loader);
27
26
  const [result, setResult] = useState(() => ({
28
27
  key: fontsKey,
29
28
  state: initialState
30
29
  }));
31
- const isLoaded = useSyncExternalStore(useCallback((onStoreChange) => {
30
+ const subscribe = useCallback((onStoreChange) => {
32
31
  const fontFaceSet = typeof document === "undefined" ? void 0 : document.fonts;
33
32
  if (webFonts.length === 0 || !fontFaceSet) return () => {};
34
33
  fontFaceSet.addEventListener("loadingdone", onStoreChange);
35
34
  return () => {
36
35
  fontFaceSet.removeEventListener("loadingdone", onStoreChange);
37
36
  };
38
- }, [webFonts]), useCallback(() => webFonts.every(({ family, ...rest }) => isWebFontLoaded(family, rest)), [webFonts]), useCallback(() => webFonts.length === 0, [webFonts]));
37
+ }, [webFonts]);
38
+ const getSnapshot = useCallback(() => webFonts.every(({ family, ...rest }) => isWebFontLoaded(family, rest)), [webFonts]);
39
+ const getServerSnapshot = useCallback(() => webFonts.length === 0, [webFonts]);
40
+ const isLoaded = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
39
41
  useEffect(() => {
40
42
  if (webFonts.length === 0) return;
41
43
  const controller = new AbortController();
@@ -59,7 +61,12 @@ function useDeferUntilWebFontReady(target, fonts, options = {}) {
59
61
  return () => {
60
62
  controller.abort();
61
63
  };
62
- }, [webFonts, timeout]);
64
+ }, [
65
+ webFonts,
66
+ fontsKey,
67
+ timeout,
68
+ loaderRef
69
+ ]);
63
70
  let state;
64
71
  if (isLoaded) state = RenderingState.ready;
65
72
  else if (result.key === fontsKey) state = result.state;
@@ -1,5 +0,0 @@
1
- 'use client';
2
-
3
- const require_useDeferUntilScrolled_useDeferUntilScrolled = require('./useDeferUntilScrolled.cjs');
4
-
5
- module.exports = require_useDeferUntilScrolled_useDeferUntilScrolled;
@@ -1,3 +0,0 @@
1
- import { UseDeferUntilScrolledOptions } from "./types.cjs";
2
- import useDeferUntilScrolled from "./useDeferUntilScrolled.cjs";
3
- export { type UseDeferUntilScrolledOptions, useDeferUntilScrolled as default };
@@ -1,3 +0,0 @@
1
- import { UseDeferUntilScrolledOptions } from "./types.mjs";
2
- import useDeferUntilScrolled from "./useDeferUntilScrolled.mjs";
3
- export { type UseDeferUntilScrolledOptions, useDeferUntilScrolled as default };
@@ -1,5 +0,0 @@
1
- 'use client';
2
-
3
- import useDeferUntilScrolled from "./useDeferUntilScrolled.mjs";
4
-
5
- export { useDeferUntilScrolled as default };
File without changes
@@ -1,29 +0,0 @@
1
- import { UseDeferUntilTrueOptions } from "../useDeferUntilTrue/types.cjs";
2
- import { ReactNode, RefObject } from "react";
3
-
4
- //#region src/useDeferUntilScrolled/types.d.ts
5
- type UseDeferUntilScrolledOptions<P extends ReactNode = ReactNode> = UseDeferUntilTrueOptions<P> & {
6
- /**
7
- * 監視するコンテナーの参照
8
- */
9
- rootRef?: RefObject<Element | null | undefined>;
10
- rootMargin?: number;
11
- /**
12
- * スクロールイベントのデバウンス時間(ミリ秒)
13
- * デフォルトは100ms
14
- * @default 100
15
- */
16
- detectionDelay?: number;
17
- /**
18
- * 検知するスクロールの方向
19
- */
20
- direction?: 'vertical' | 'horizontal';
21
- /**
22
- * SSR時など、実際のスクロール位置を判定できない環境での初期値\
23
- * デフォルトはfalse
24
- * @default false
25
- */
26
- initialCondition?: boolean;
27
- };
28
- //#endregion
29
- export { UseDeferUntilScrolledOptions };
@@ -1,29 +0,0 @@
1
- import { UseDeferUntilTrueOptions } from "../useDeferUntilTrue/types.mjs";
2
- import { ReactNode, RefObject } from "react";
3
-
4
- //#region src/useDeferUntilScrolled/types.d.ts
5
- type UseDeferUntilScrolledOptions<P extends ReactNode = ReactNode> = UseDeferUntilTrueOptions<P> & {
6
- /**
7
- * 監視するコンテナーの参照
8
- */
9
- rootRef?: RefObject<Element | null | undefined>;
10
- rootMargin?: number;
11
- /**
12
- * スクロールイベントのデバウンス時間(ミリ秒)
13
- * デフォルトは100ms
14
- * @default 100
15
- */
16
- detectionDelay?: number;
17
- /**
18
- * 検知するスクロールの方向
19
- */
20
- direction?: 'vertical' | 'horizontal';
21
- /**
22
- * SSR時など、実際のスクロール位置を判定できない環境での初期値\
23
- * デフォルトはfalse
24
- * @default false
25
- */
26
- initialCondition?: boolean;
27
- };
28
- //#endregion
29
- export { UseDeferUntilScrolledOptions };
@@ -1 +0,0 @@
1
- export { };
@@ -1,94 +0,0 @@
1
- 'use client';
2
-
3
- const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
4
- const require_useDeferUntilTrue_useDeferUntilTrue = require('../useDeferUntilTrue/useDeferUntilTrue.cjs');
5
- require('../useDeferUntilTrue/index.cjs');
6
- let _fringeworks_react_utils_hooks_useIsMounted = require("@fringeworks/react-utils/hooks/useIsMounted");
7
- _fringeworks_react_utils_hooks_useIsMounted = require_runtime.__toESM(_fringeworks_react_utils_hooks_useIsMounted, 1);
8
- let react = require("react");
9
- let _fringeworks_utils_timer_debounce = require("@fringeworks/utils/timer/debounce");
10
- _fringeworks_utils_timer_debounce = require_runtime.__toESM(_fringeworks_utils_timer_debounce, 1);
11
-
12
- //#region src/useDeferUntilScrolled/useDeferUntilScrolled.tsx
13
- /**
14
- * 縦または横スクロール位置に基づいて描画を遅延させるhook
15
- * @param target 描画対象のノード
16
- * @param elementRef 基準となる要素の参照(参照自体がnull/undefinedの場合は待たずに描画する)
17
- * @param options オプション
18
- * @returns state('pending', 'ready')と状態に応じたノード
19
- */
20
- function useDeferUntilScrolled(target, elementRef, options = {}) {
21
- const defaultRootRef = (0, react.useRef)(null);
22
- const { rootRef = defaultRootRef, rootMargin = 0, detectionDelay = 100, preserveOnceReady, direction = "vertical", initialCondition = false, ...opts } = options;
23
- const [condition, setCondition] = (0, react.useState)(!elementRef || initialCondition);
24
- const isMounted = (0, _fringeworks_react_utils_hooks_useIsMounted.default)();
25
- const isFirstRun = (0, react.useRef)(true);
26
- (0, react.useEffect)(() => {
27
- const isFirst = isFirstRun.current;
28
- isFirstRun.current = false;
29
- if (!elementRef) {
30
- setCondition(true);
31
- return;
32
- }
33
- if (!isFirst) setCondition(false);
34
- const element = elementRef.current;
35
- if (!element) return;
36
- const container = rootRef.current;
37
- const scrollTarget = container ?? window;
38
- const getRootBounds = () => {
39
- if (container) {
40
- const rect = container.getBoundingClientRect();
41
- const top = rect.top + container.clientTop;
42
- const left = rect.left + container.clientLeft;
43
- return {
44
- top,
45
- left,
46
- bottom: top + container.clientHeight,
47
- right: left + container.clientWidth
48
- };
49
- }
50
- const viewport = document.documentElement;
51
- return {
52
- top: 0,
53
- left: 0,
54
- bottom: viewport.clientHeight,
55
- right: viewport.clientWidth
56
- };
57
- };
58
- const checkVisible = () => {
59
- const rect = element.getBoundingClientRect();
60
- const bounds = getRootBounds();
61
- return direction === "vertical" ? rect.top - rootMargin < bounds.bottom && rect.bottom + rootMargin >= bounds.top : rect.left - rootMargin < bounds.right && rect.right + rootMargin >= bounds.left;
62
- };
63
- if (checkVisible()) {
64
- setCondition(true);
65
- if (preserveOnceReady) return;
66
- }
67
- const debouncedHandleScroll = (0, _fringeworks_utils_timer_debounce.default)(() => {
68
- if (!isMounted()) return;
69
- const isVisible = checkVisible();
70
- setCondition(isVisible);
71
- if (preserveOnceReady && isVisible) scrollTarget.removeEventListener("scroll", debouncedHandleScroll);
72
- }, detectionDelay);
73
- scrollTarget.addEventListener("scroll", debouncedHandleScroll, { passive: true });
74
- return () => {
75
- debouncedHandleScroll.cancel();
76
- scrollTarget.removeEventListener("scroll", debouncedHandleScroll);
77
- };
78
- }, [
79
- elementRef,
80
- elementRef?.current,
81
- rootRef.current,
82
- rootMargin,
83
- detectionDelay,
84
- direction,
85
- preserveOnceReady
86
- ]);
87
- return require_useDeferUntilTrue_useDeferUntilTrue(target, condition, {
88
- preserveOnceReady,
89
- ...opts
90
- });
91
- }
92
-
93
- //#endregion
94
- module.exports = useDeferUntilScrolled;
@@ -1,14 +0,0 @@
1
- import { DeferRenderingResult } from "../types.cjs";
2
- import { UseDeferUntilScrolledOptions } from "./types.cjs";
3
- import { ReactNode, RefObject } from "react";
4
-
5
- //#region src/useDeferUntilScrolled/useDeferUntilScrolled.d.ts
6
- /**
7
- * 縦または横スクロール位置に基づいて描画を遅延させるhook
8
- * @param target 描画対象のノード
9
- * @param elementRef 基準となる要素の参照(参照自体がnull/undefinedの場合は待たずに描画する)
10
- * @param options オプション
11
- * @returns state('pending', 'ready')と状態に応じたノード
12
- */
13
- declare function useDeferUntilScrolled<T extends ReactNode, P extends ReactNode = ReactNode>(target: T, elementRef: RefObject<HTMLElement | null | undefined> | null | undefined, options?: UseDeferUntilScrolledOptions<P>): DeferRenderingResult<T | P>;
14
- export = useDeferUntilScrolled;
@@ -1,15 +0,0 @@
1
- import { DeferRenderingResult } from "../types.mjs";
2
- import { UseDeferUntilScrolledOptions } from "./types.mjs";
3
- import { ReactNode, RefObject } from "react";
4
-
5
- //#region src/useDeferUntilScrolled/useDeferUntilScrolled.d.ts
6
- /**
7
- * 縦または横スクロール位置に基づいて描画を遅延させるhook
8
- * @param target 描画対象のノード
9
- * @param elementRef 基準となる要素の参照(参照自体がnull/undefinedの場合は待たずに描画する)
10
- * @param options オプション
11
- * @returns state('pending', 'ready')と状態に応じたノード
12
- */
13
- declare function useDeferUntilScrolled<T extends ReactNode, P extends ReactNode = ReactNode>(target: T, elementRef: RefObject<HTMLElement | null | undefined> | null | undefined, options?: UseDeferUntilScrolledOptions<P>): DeferRenderingResult<T | P>;
14
- //#endregion
15
- export { useDeferUntilScrolled as default };
@@ -1,91 +0,0 @@
1
- 'use client';
2
-
3
- import useDeferUntilTrue from "../useDeferUntilTrue/useDeferUntilTrue.mjs";
4
- import "../useDeferUntilTrue/index.mjs";
5
- import useIsMounted from "@fringeworks/react-utils/hooks/useIsMounted";
6
- import { useEffect, useRef, useState } from "react";
7
- import debounce from "@fringeworks/utils/timer/debounce";
8
-
9
- //#region src/useDeferUntilScrolled/useDeferUntilScrolled.tsx
10
- /**
11
- * 縦または横スクロール位置に基づいて描画を遅延させるhook
12
- * @param target 描画対象のノード
13
- * @param elementRef 基準となる要素の参照(参照自体がnull/undefinedの場合は待たずに描画する)
14
- * @param options オプション
15
- * @returns state('pending', 'ready')と状態に応じたノード
16
- */
17
- function useDeferUntilScrolled(target, elementRef, options = {}) {
18
- const defaultRootRef = useRef(null);
19
- const { rootRef = defaultRootRef, rootMargin = 0, detectionDelay = 100, preserveOnceReady, direction = "vertical", initialCondition = false, ...opts } = options;
20
- const [condition, setCondition] = useState(!elementRef || initialCondition);
21
- const isMounted = useIsMounted();
22
- const isFirstRun = useRef(true);
23
- useEffect(() => {
24
- const isFirst = isFirstRun.current;
25
- isFirstRun.current = false;
26
- if (!elementRef) {
27
- setCondition(true);
28
- return;
29
- }
30
- if (!isFirst) setCondition(false);
31
- const element = elementRef.current;
32
- if (!element) return;
33
- const container = rootRef.current;
34
- const scrollTarget = container ?? window;
35
- const getRootBounds = () => {
36
- if (container) {
37
- const rect = container.getBoundingClientRect();
38
- const top = rect.top + container.clientTop;
39
- const left = rect.left + container.clientLeft;
40
- return {
41
- top,
42
- left,
43
- bottom: top + container.clientHeight,
44
- right: left + container.clientWidth
45
- };
46
- }
47
- const viewport = document.documentElement;
48
- return {
49
- top: 0,
50
- left: 0,
51
- bottom: viewport.clientHeight,
52
- right: viewport.clientWidth
53
- };
54
- };
55
- const checkVisible = () => {
56
- const rect = element.getBoundingClientRect();
57
- const bounds = getRootBounds();
58
- return direction === "vertical" ? rect.top - rootMargin < bounds.bottom && rect.bottom + rootMargin >= bounds.top : rect.left - rootMargin < bounds.right && rect.right + rootMargin >= bounds.left;
59
- };
60
- if (checkVisible()) {
61
- setCondition(true);
62
- if (preserveOnceReady) return;
63
- }
64
- const debouncedHandleScroll = debounce(() => {
65
- if (!isMounted()) return;
66
- const isVisible = checkVisible();
67
- setCondition(isVisible);
68
- if (preserveOnceReady && isVisible) scrollTarget.removeEventListener("scroll", debouncedHandleScroll);
69
- }, detectionDelay);
70
- scrollTarget.addEventListener("scroll", debouncedHandleScroll, { passive: true });
71
- return () => {
72
- debouncedHandleScroll.cancel();
73
- scrollTarget.removeEventListener("scroll", debouncedHandleScroll);
74
- };
75
- }, [
76
- elementRef,
77
- elementRef?.current,
78
- rootRef.current,
79
- rootMargin,
80
- detectionDelay,
81
- direction,
82
- preserveOnceReady
83
- ]);
84
- return useDeferUntilTrue(target, condition, {
85
- preserveOnceReady,
86
- ...opts
87
- });
88
- }
89
-
90
- //#endregion
91
- export { useDeferUntilScrolled as default };