@inertiaui/modal-react 0.6.1 → 0.6.3

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inertiaui/modal-react",
3
3
  "author": "Pascal Baljet <pascal@protone.media>",
4
- "version": "0.6.1",
4
+ "version": "0.6.3",
5
5
  "private": false,
6
6
  "license": "MIT",
7
7
  "type": "module",
package/src/ModalRoot.jsx CHANGED
@@ -1,7 +1,7 @@
1
- import { createElement, useEffect, useState } from 'react'
1
+ import { createElement, useEffect, useState, useRef } from 'react'
2
2
  import { default as Axios } from 'axios'
3
3
  import { except, only } from './helpers'
4
- import { router } from '@inertiajs/react'
4
+ import { router, usePage } from '@inertiajs/react'
5
5
  import { mergeDataIntoQueryString } from '@inertiajs/core'
6
6
  import { createContext, useContext } from 'react'
7
7
  import ModalRenderer from './ModalRenderer'
@@ -224,12 +224,17 @@ export const ModalStackProvider = ({ children }) => {
224
224
  'X-Inertia-Partial-Data': keys.join(','),
225
225
  'X-InertiaUI-Modal': true,
226
226
  'X-InertiaUI-Modal-Use-Router': 0,
227
+ 'X-InertiaUI-Modal-Base-Url': baseUrl,
227
228
  },
228
229
  }).then((response) => {
229
- Object.assign(this.props, response.data.props)
230
- updateStack((prevStack) => prevStack) // Trigger re-render
230
+ this.updateProps(response.data.props)
231
231
  })
232
232
  }
233
+
234
+ updateProps = (props) => {
235
+ Object.assign(this.props, props)
236
+ updateStack((prevStack) => prevStack) // Trigger re-render
237
+ }
233
238
  }
234
239
 
235
240
  const pushFromResponseData = (responseData, config = {}, onClose = null, onAfterLeave = null) => {
@@ -304,9 +309,11 @@ export const ModalStackProvider = ({ children }) => {
304
309
  'X-Inertia-Version': pageVersion,
305
310
  'X-InertiaUI-Modal': true,
306
311
  'X-InertiaUI-Modal-Use-Router': useInertiaRouter ? 1 : 0,
312
+ 'X-InertiaUI-Modal-Base-Url': baseUrl,
307
313
  }
308
314
 
309
315
  if (useInertiaRouter) {
316
+ newModalOnBase = null
310
317
  // Pushing the modal to the stack will be handled by the ModalRoot...
311
318
  return router.visit(url, {
312
319
  method,
@@ -333,7 +340,6 @@ export const ModalStackProvider = ({ children }) => {
333
340
  )
334
341
 
335
342
  resolve(modal)
336
- newModalOnBase = null
337
343
  })
338
344
  },
339
345
  })
@@ -485,7 +491,9 @@ export const ModalRoot = ({ children }) => {
485
491
  const axiosRequestInterceptor = (config) => {
486
492
  // A Modal is opened on top of a base route, so we need to pass this base route
487
493
  // so it can redirect back with the back() helper method...
488
- config.headers['X-InertiaUI-Modal-Base-Url'] = baseUrl
494
+ if (localStackCopy.length) {
495
+ config.headers['X-InertiaUI-Modal-Base-Url'] = baseUrl
496
+ }
489
497
  return config
490
498
  }
491
499
 
@@ -494,6 +502,21 @@ export const ModalRoot = ({ children }) => {
494
502
  return () => Axios.interceptors.request.eject(axiosRequestInterceptor)
495
503
  }, [])
496
504
 
505
+ const $page = usePage()
506
+ const previousModalRef = useRef()
507
+
508
+ useEffect(() => {
509
+ const newModal = $page.props?._inertiaui_modal
510
+ const previousModal = previousModalRef.current
511
+
512
+ // Store the current value for the next render
513
+ previousModalRef.current = newModal
514
+
515
+ if (newModal && previousModal && newModal.component === previousModal.component && newModal.url === previousModal.url) {
516
+ context.stack[0]?.updateProps(newModal.props ?? {})
517
+ }
518
+ }, [$page.props?._inertiaui_modal])
519
+
497
520
  return (
498
521
  <>
499
522
  {children}