@inertiaui/modal-react 0.20.5 → 1.0.0-beta-1

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.
@@ -0,0 +1,100 @@
1
+ // See: https://github.com/inertiajs/inertia/blob/48bcd21fb7daf467d0df1bfde2408f161f94a579/packages/react/src/WhenVisible.ts
2
+ import { createElement, useCallback, useEffect, useRef, useState } from 'react'
3
+ import useModal from './useModal'
4
+
5
+ const WhenVisible = ({ children, data, params, buffer, as, always, fallback }) => {
6
+ always = always ?? false
7
+ as = as ?? 'div'
8
+ fallback = fallback ?? null
9
+
10
+ const [loaded, setLoaded] = useState(false)
11
+ const hasFetched = useRef(false)
12
+ const fetching = useRef(false)
13
+ const ref = useRef(null)
14
+
15
+ const modal = useModal()
16
+
17
+ const getReloadParams = useCallback(() => {
18
+ if (data) {
19
+ return {
20
+ only: Array.isArray(data) ? data : [data],
21
+ }
22
+ }
23
+
24
+ if (!params) {
25
+ throw new Error('You must provide either a `data` or `params` prop.')
26
+ }
27
+
28
+ return params
29
+ }, [params, data])
30
+
31
+ useEffect(() => {
32
+ if (!ref.current) {
33
+ return
34
+ }
35
+
36
+ const observer = new IntersectionObserver(
37
+ (entries) => {
38
+ if (!entries[0].isIntersecting) {
39
+ return
40
+ }
41
+
42
+ if (!always && hasFetched.current) {
43
+ observer.disconnect()
44
+ }
45
+
46
+ if (fetching.current) {
47
+ return
48
+ }
49
+
50
+ hasFetched.current = true
51
+ fetching.current = true
52
+
53
+ const reloadParams = getReloadParams()
54
+
55
+ modal.reload({
56
+ ...reloadParams,
57
+ onStart: (e) => {
58
+ fetching.current = true
59
+ reloadParams.onStart?.(e)
60
+ },
61
+ onFinish: (e) => {
62
+ setLoaded(true)
63
+ fetching.current = false
64
+ reloadParams.onFinish?.(e)
65
+
66
+ if (!always) {
67
+ observer.disconnect()
68
+ }
69
+ },
70
+ })
71
+ },
72
+ {
73
+ rootMargin: `${buffer || 0}px`,
74
+ },
75
+ )
76
+
77
+ observer.observe(ref.current)
78
+
79
+ return () => {
80
+ observer.disconnect()
81
+ }
82
+ }, [ref, getReloadParams, buffer])
83
+
84
+ if (always || !loaded) {
85
+ return createElement(
86
+ as,
87
+ {
88
+ props: null,
89
+ ref,
90
+ },
91
+ loaded ? children : fallback,
92
+ )
93
+ }
94
+
95
+ return loaded ? children : null
96
+ }
97
+
98
+ WhenVisible.displayName = 'InertiaWhenVisible'
99
+
100
+ export default WhenVisible
package/src/helpers.js CHANGED
@@ -1,2 +1,2 @@
1
- import { generateId, sameUrlPath, except, only, rejectNullValues, waitFor, kebabCase } from './../../vue/src/helpers.js'
2
- export { generateId, sameUrlPath, except, only, rejectNullValues, waitFor, kebabCase }
1
+ import { generateId, sameUrlPath, except, only, rejectNullValues, kebabCase } from './../../vue/src/helpers.js'
2
+ export { generateId, sameUrlPath, except, only, rejectNullValues, kebabCase }
@@ -2,10 +2,12 @@ import { createElement } from 'react'
2
2
  import { getConfig, putConfig, resetConfig } from './config.js'
3
3
  import { useModalIndex } from './ModalRenderer.jsx'
4
4
  import { useModalStack, ModalRoot, ModalStackProvider, renderApp, initFromPageProps } from './ModalRoot.jsx'
5
+ import useModal from './useModal.js'
6
+ import Deferred from './Deferred.jsx'
5
7
  import HeadlessModal from './HeadlessModal.jsx'
6
8
  import Modal from './Modal.jsx'
7
9
  import ModalLink from './ModalLink.jsx'
8
- import useModal from './useModal.js'
10
+ import WhenVisible from './WhenVisible.jsx'
9
11
 
10
12
  const setPageLayout = (layout) => (module) => {
11
13
  module.default.layout = (page) => createElement(layout, {}, page)
@@ -13,18 +15,20 @@ const setPageLayout = (layout) => (module) => {
13
15
  }
14
16
 
15
17
  export {
16
- getConfig,
17
- putConfig,
18
- resetConfig,
19
- useModalStack,
20
- useModalIndex,
18
+ Deferred,
21
19
  HeadlessModal,
22
20
  Modal,
23
21
  ModalLink,
24
22
  ModalRoot,
25
23
  ModalStackProvider,
26
- renderApp,
27
- useModal,
24
+ WhenVisible,
25
+ getConfig,
28
26
  initFromPageProps,
27
+ putConfig,
28
+ renderApp,
29
+ resetConfig,
29
30
  setPageLayout,
31
+ useModal,
32
+ useModalIndex,
33
+ useModalStack,
30
34
  }