@inertiaui/modal-react 0.5.4 → 0.6.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.
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.5.4",
4
+ "version": "0.6.0",
5
5
  "private": false,
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -15,20 +15,20 @@ const HeadlessModal = forwardRef(({ name, children, ...props }, ref) => {
15
15
  return stack.find((m) => m.shouldRender && m.index > modalContext?.index)?.index
16
16
  }, [modalIndex, stack])
17
17
 
18
- const modalPropsSlideover = useMemo(() => modalContext?.modalProps.slideover ?? props.slideover ?? getConfig('type') === 'slideover', [props.slideover])
18
+ const configSlideover = useMemo(() => modalContext?.config.slideover ?? props.slideover ?? getConfig('type') === 'slideover', [props.slideover])
19
19
 
20
- const modalProps = useMemo(
20
+ const config = useMemo(
21
21
  () => ({
22
- slideover: modalPropsSlideover,
23
- closeButton: props.closeButton ?? getConfigByType(modalPropsSlideover, 'closeButton'),
24
- closeExplicitly: props.closeExplicitly ?? getConfigByType(modalPropsSlideover, 'closeExplicitly'),
25
- maxWidth: props.maxWidth ?? getConfigByType(modalPropsSlideover, 'maxWidth'),
26
- paddingClasses: props.paddingClasses ?? getConfigByType(modalPropsSlideover, 'paddingClasses'),
27
- panelClasses: props.panelClasses ?? getConfigByType(modalPropsSlideover, 'panelClasses'),
28
- position: props.position ?? getConfigByType(modalPropsSlideover, 'position'),
29
- ...modalContext?.modalProps,
22
+ slideover: configSlideover,
23
+ closeButton: props.closeButton ?? getConfigByType(configSlideover, 'closeButton'),
24
+ closeExplicitly: props.closeExplicitly ?? getConfigByType(configSlideover, 'closeExplicitly'),
25
+ maxWidth: props.maxWidth ?? getConfigByType(configSlideover, 'maxWidth'),
26
+ paddingClasses: props.paddingClasses ?? getConfigByType(configSlideover, 'paddingClasses'),
27
+ panelClasses: props.panelClasses ?? getConfigByType(configSlideover, 'panelClasses'),
28
+ position: props.position ?? getConfigByType(configSlideover, 'position'),
29
+ ...modalContext?.config,
30
30
  }),
31
- [props, modalContext?.modalProps],
31
+ [props, modalContext?.config],
32
32
  )
33
33
 
34
34
  useEffect(() => {
@@ -55,6 +55,7 @@ const HeadlessModal = forwardRef(({ name, children, ...props }, ref) => {
55
55
  () => ({
56
56
  afterLeave: () => modalContext.afterLeave(),
57
57
  close: () => modalContext.close(),
58
+ config,
58
59
  emit: (...args) => modalContext.emit(...args),
59
60
  getChildModal: () => modalContext.getChildModal(),
60
61
  getParentModal: () => modalContext.getParentModal(),
@@ -62,7 +63,6 @@ const HeadlessModal = forwardRef(({ name, children, ...props }, ref) => {
62
63
  index: modalContext?.index,
63
64
  isOpen: modalContext?.isOpen,
64
65
  modalContext,
65
- modalProps,
66
66
  onTopOfStack: modalContext?.onTopOfStack,
67
67
  reload: () => modalContext.reload(),
68
68
  setOpen: () => modalContext.setOpen(),
@@ -78,6 +78,7 @@ const HeadlessModal = forwardRef(({ name, children, ...props }, ref) => {
78
78
  ? children({
79
79
  afterLeave: modalContext.afterLeave,
80
80
  close: modalContext.close,
81
+ config,
81
82
  emit: modalContext.emit,
82
83
  getChildModal: modalContext.getChildModal,
83
84
  getParentModal: modalContext.getParentModal,
@@ -85,7 +86,6 @@ const HeadlessModal = forwardRef(({ name, children, ...props }, ref) => {
85
86
  index: modalContext.index,
86
87
  isOpen: modalContext.isOpen,
87
88
  modalContext,
88
- modalProps,
89
89
  onTopOfStack: modalContext.onTopOfStack,
90
90
  reload: modalContext.reload,
91
91
  setOpen: modalContext.setOpen,
package/src/Modal.jsx CHANGED
@@ -26,6 +26,7 @@ const Modal = forwardRef(({ name, children, ...props }, ref) => {
26
26
  {({
27
27
  afterLeave,
28
28
  close,
29
+ config,
29
30
  emit,
30
31
  getChildModal,
31
32
  getParentModal,
@@ -33,7 +34,6 @@ const Modal = forwardRef(({ name, children, ...props }, ref) => {
33
34
  index,
34
35
  isOpen,
35
36
  modalContext,
36
- modalProps,
37
37
  onTopOfStack,
38
38
  reload,
39
39
  setOpen,
@@ -46,7 +46,7 @@ const Modal = forwardRef(({ name, children, ...props }, ref) => {
46
46
  <Dialog
47
47
  as="div"
48
48
  className="im-dialog relative z-20"
49
- onClose={() => (modalProps.closeExplicitly ? null : close())}
49
+ onClose={() => (config.closeExplicitly ? null : close())}
50
50
  data-inertiaui-modal-id={id}
51
51
  data-inertiaui-modal-index={index}
52
52
  >
@@ -75,14 +75,15 @@ const Modal = forwardRef(({ name, children, ...props }, ref) => {
75
75
  {index > 0 && onTopOfStack ? <div className="im-backdrop fixed inset-0 z-30 bg-black/75" /> : null}
76
76
 
77
77
  {/* The modal/slideover content itself */}
78
- {modalProps.slideover ? (
78
+ {config.slideover ? (
79
79
  <SlideoverContent
80
80
  modalContext={modalContext}
81
- modalProps={modalProps}
81
+ config={config}
82
82
  >
83
83
  {renderChildren({
84
84
  afterLeave,
85
85
  close,
86
+ config,
86
87
  emit,
87
88
  getChildModal,
88
89
  getParentModal,
@@ -90,7 +91,6 @@ const Modal = forwardRef(({ name, children, ...props }, ref) => {
90
91
  index,
91
92
  isOpen,
92
93
  modalContext,
93
- modalProps,
94
94
  onTopOfStack,
95
95
  reload,
96
96
  setOpen,
@@ -100,11 +100,12 @@ const Modal = forwardRef(({ name, children, ...props }, ref) => {
100
100
  ) : (
101
101
  <ModalContent
102
102
  modalContext={modalContext}
103
- modalProps={modalProps}
103
+ config={config}
104
104
  >
105
105
  {renderChildren({
106
106
  afterLeave,
107
107
  close,
108
+ config,
108
109
  emit,
109
110
  getChildModal,
110
111
  getParentModal,
@@ -112,7 +113,6 @@ const Modal = forwardRef(({ name, children, ...props }, ref) => {
112
113
  index,
113
114
  isOpen,
114
115
  modalContext,
115
- modalProps,
116
116
  onTopOfStack,
117
117
  reload,
118
118
  setOpen,
@@ -2,14 +2,14 @@ import { TransitionChild, DialogPanel } from '@headlessui/react'
2
2
  import CloseButton from './CloseButton'
3
3
  import clsx from 'clsx'
4
4
 
5
- const ModalContent = ({ modalContext, modalProps, children }) => {
5
+ const ModalContent = ({ modalContext, config, children }) => {
6
6
  return (
7
7
  <div className="im-modal-container fixed inset-0 z-40 overflow-y-auto p-4">
8
8
  <div
9
9
  className={clsx('im-modal-positioner flex min-h-full justify-center', {
10
- 'items-start': modalProps.position === 'top',
11
- 'items-center': modalProps.position === 'center',
12
- 'items-end': modalProps.position === 'bottom',
10
+ 'items-start': config.position === 'top',
11
+ 'items-center': config.position === 'center',
12
+ 'items-end': config.position === 'bottom',
13
13
  })}
14
14
  >
15
15
  <TransitionChild
@@ -19,25 +19,25 @@ const ModalContent = ({ modalContext, modalProps, children }) => {
19
19
  leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
20
20
  afterLeave={modalContext.afterLeave}
21
21
  className={clsx('im-modal-wrapper w-full transition duration-300 ease-in-out', modalContext.onTopOfStack ? '' : 'blur-sm', {
22
- 'sm:max-w-sm': modalProps.maxWidth === 'sm',
23
- 'sm:max-w-md': modalProps.maxWidth === 'md',
24
- 'sm:max-w-md md:max-w-lg': modalProps.maxWidth === 'lg',
25
- 'sm:max-w-md md:max-w-xl': modalProps.maxWidth === 'xl',
26
- 'sm:max-w-md md:max-w-xl lg:max-w-2xl': modalProps.maxWidth === '2xl',
27
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl': modalProps.maxWidth === '3xl',
28
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-4xl': modalProps.maxWidth === '4xl',
29
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl': modalProps.maxWidth === '5xl',
30
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl 2xl:max-w-6xl': modalProps.maxWidth === '6xl',
31
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl 2xl:max-w-7xl': modalProps.maxWidth === '7xl',
22
+ 'sm:max-w-sm': config.maxWidth === 'sm',
23
+ 'sm:max-w-md': config.maxWidth === 'md',
24
+ 'sm:max-w-md md:max-w-lg': config.maxWidth === 'lg',
25
+ 'sm:max-w-md md:max-w-xl': config.maxWidth === 'xl',
26
+ 'sm:max-w-md md:max-w-xl lg:max-w-2xl': config.maxWidth === '2xl',
27
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl': config.maxWidth === '3xl',
28
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-4xl': config.maxWidth === '4xl',
29
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl': config.maxWidth === '5xl',
30
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl 2xl:max-w-6xl': config.maxWidth === '6xl',
31
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl 2xl:max-w-7xl': config.maxWidth === '7xl',
32
32
  })}
33
33
  >
34
- <DialogPanel className={`im-modal-content relative ${modalProps.paddingClasses} ${modalProps.panelClasses}`}>
35
- {modalProps.closeButton && (
34
+ <DialogPanel className={`im-modal-content relative ${config.paddingClasses} ${config.panelClasses}`}>
35
+ {config.closeButton && (
36
36
  <div className="absolute right-0 top-0 pr-3 pt-3">
37
37
  <CloseButton onClick={modalContext.close} />
38
38
  </div>
39
39
  )}
40
- {typeof children === 'function' ? children({ modalContext, modalProps }) : children}
40
+ {typeof children === 'function' ? children({ modalContext, config }) : children}
41
41
  </DialogPanel>
42
42
  </TransitionChild>
43
43
  </div>
@@ -23,7 +23,7 @@ const ModalRenderer = ({ index }) => {
23
23
  modalContext?.component && (
24
24
  <ModalIndexContext.Provider value={index}>
25
25
  <modalContext.component
26
- {...modalContext.componentProps}
26
+ {...modalContext.props}
27
27
  onModalEvent={(...args) => modalContext.emit(...args)}
28
28
  />
29
29
  </ModalIndexContext.Provider>
package/src/ModalRoot.jsx CHANGED
@@ -72,16 +72,16 @@ export const ModalStackProvider = ({ children }) => {
72
72
  }, [stack])
73
73
 
74
74
  class Modal {
75
- constructor(component, response, modalProps, onClose, afterLeave) {
75
+ constructor(component, response, config, onClose, afterLeave) {
76
76
  this.id = Modal.generateId()
77
77
  this.isOpen = false
78
78
  this.shouldRender = false
79
79
  this.listeners = {}
80
80
 
81
81
  this.component = component
82
- this.componentProps = response.props
82
+ this.props = response.props
83
83
  this.response = response
84
- this.modalProps = modalProps
84
+ this.config = config
85
85
  this.onCloseCallback = onClose
86
86
  this.afterLeaveCallback = afterLeave
87
87
 
@@ -99,11 +99,11 @@ export const ModalStackProvider = ({ children }) => {
99
99
  return `inertiaui_modal_${Date.now().toString(36)}_${Math.random().toString(36).substr(2, 9)}`
100
100
  }
101
101
 
102
- update = (modalProps, onClose, afterLeave) => {
102
+ update = (config, onClose, afterLeave) => {
103
103
  updateStack((prevStack) =>
104
104
  prevStack.map((modal) => {
105
105
  if (modal.id === this.id) {
106
- modal.modalProps = modalProps
106
+ modal.config = config
107
107
  modal.onCloseCallback = onClose
108
108
  modal.afterLeaveCallback = afterLeave
109
109
  }
@@ -226,18 +226,18 @@ export const ModalStackProvider = ({ children }) => {
226
226
  'X-InertiaUI-Modal-Use-Router': 0,
227
227
  },
228
228
  }).then((response) => {
229
- Object.assign(this.componentProps, response.data.props)
229
+ Object.assign(this.props, response.data.props)
230
230
  updateStack((prevStack) => prevStack) // Trigger re-render
231
231
  })
232
232
  }
233
233
  }
234
234
 
235
- const pushFromResponseData = (responseData, modalProps = {}, onClose = null, onAfterLeave = null) => {
236
- return resolveComponent(responseData.component).then((component) => push(component, responseData, modalProps, onClose, onAfterLeave))
235
+ const pushFromResponseData = (responseData, config = {}, onClose = null, onAfterLeave = null) => {
236
+ return resolveComponent(responseData.component).then((component) => push(component, responseData, config, onClose, onAfterLeave))
237
237
  }
238
238
 
239
- const push = (component, response, modalProps, onClose, afterLeave) => {
240
- const newModal = new Modal(component, response, modalProps, onClose, afterLeave)
239
+ const push = (component, response, config, onClose, afterLeave) => {
240
+ const newModal = new Modal(component, response, config, onClose, afterLeave)
241
241
  newModal.index = stack.length
242
242
 
243
243
  updateStack((prevStack) => [...prevStack, newModal])
@@ -247,12 +247,12 @@ export const ModalStackProvider = ({ children }) => {
247
247
  return newModal
248
248
  }
249
249
 
250
- function pushLocalModal(name, modalProps, onClose, afterLeave) {
250
+ function pushLocalModal(name, config, onClose, afterLeave) {
251
251
  if (!localModals[name]) {
252
252
  throw new Error(`The local modal "${name}" has not been registered.`)
253
253
  }
254
254
 
255
- const modal = push(null, {}, modalProps, onClose, afterLeave)
255
+ const modal = push(null, {}, config, onClose, afterLeave)
256
256
  modal.name = name
257
257
  localModals[name].callback(modal)
258
258
  return modal
@@ -276,7 +276,7 @@ export const ModalStackProvider = ({ children }) => {
276
276
  method,
277
277
  payload = {},
278
278
  headers = {},
279
- modalProps = {},
279
+ config = {},
280
280
  onClose = null,
281
281
  onAfterLeave = null,
282
282
  queryStringArrayFormat = 'brackets',
@@ -284,7 +284,7 @@ export const ModalStackProvider = ({ children }) => {
284
284
  ) => {
285
285
  return new Promise((resolve, reject) => {
286
286
  if (href.startsWith('#')) {
287
- resolve(pushLocalModal(href.substring(1), modalProps, onClose, onAfterLeave))
287
+ resolve(pushLocalModal(href.substring(1), config, onClose, onAfterLeave))
288
288
  return
289
289
  }
290
290
 
@@ -321,7 +321,7 @@ export const ModalStackProvider = ({ children }) => {
321
321
  const originalAfterLeave = modal.afterLeaveCallback
322
322
 
323
323
  modal.update(
324
- modalProps,
324
+ config,
325
325
  () => {
326
326
  onClose?.()
327
327
  originalOnClose?.()
@@ -347,7 +347,7 @@ export const ModalStackProvider = ({ children }) => {
347
347
  data,
348
348
  headers,
349
349
  })
350
- .then((response) => resolve(pushFromResponseData(response.data, modalProps, onClose, onAfterLeave)))
350
+ .then((response) => resolve(pushFromResponseData(response.data, config, onClose, onAfterLeave)))
351
351
  .catch((error) => {
352
352
  reject(error)
353
353
  })
@@ -2,41 +2,41 @@ import { TransitionChild, DialogPanel } from '@headlessui/react'
2
2
  import CloseButton from './CloseButton'
3
3
  import clsx from 'clsx'
4
4
 
5
- const SlideoverContent = ({ modalContext, modalProps, children }) => {
5
+ const SlideoverContent = ({ modalContext, config, children }) => {
6
6
  return (
7
7
  <div className="im-slideover-container fixed inset-0 z-40 overflow-y-auto overflow-x-hidden">
8
8
  <div
9
9
  className={clsx('im-slideover-positioner flex min-h-full items-center', {
10
- 'justify-start': modalProps.position === 'left',
11
- 'justify-end': modalProps.position === 'right',
10
+ 'justify-start': config.position === 'left',
11
+ 'justify-end': config.position === 'right',
12
12
  })}
13
13
  >
14
14
  <TransitionChild
15
- enterFrom={`opacity-0 ${modalProps.position === 'left' ? '-translate-x-full' : 'translate-x-full'}`}
15
+ enterFrom={`opacity-0 ${config.position === 'left' ? '-translate-x-full' : 'translate-x-full'}`}
16
16
  enterTo="opacity-100 translate-x-0"
17
17
  leaveFrom="opacity-100 translate-x-0"
18
- leaveTo={`opacity-0 ${modalProps.position === 'left' ? '-translate-x-full' : 'translate-x-full'}`}
18
+ leaveTo={`opacity-0 ${config.position === 'left' ? '-translate-x-full' : 'translate-x-full'}`}
19
19
  afterLeave={modalContext.afterLeave}
20
20
  className={clsx('im-slideover-wrapper w-full transition duration-300 ease-in-out', modalContext.onTopOfStack ? '' : 'blur-sm', {
21
- 'sm:max-w-sm': modalProps.maxWidth === 'sm',
22
- 'sm:max-w-md': modalProps.maxWidth === 'md',
23
- 'sm:max-w-md md:max-w-lg': modalProps.maxWidth === 'lg',
24
- 'sm:max-w-md md:max-w-xl': modalProps.maxWidth === 'xl',
25
- 'sm:max-w-md md:max-w-xl lg:max-w-2xl': modalProps.maxWidth === '2xl',
26
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl': modalProps.maxWidth === '3xl',
27
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-4xl': modalProps.maxWidth === '4xl',
28
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl': modalProps.maxWidth === '5xl',
29
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl 2xl:max-w-6xl': modalProps.maxWidth === '6xl',
30
- 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl 2xl:max-w-7xl': modalProps.maxWidth === '7xl',
21
+ 'sm:max-w-sm': config.maxWidth === 'sm',
22
+ 'sm:max-w-md': config.maxWidth === 'md',
23
+ 'sm:max-w-md md:max-w-lg': config.maxWidth === 'lg',
24
+ 'sm:max-w-md md:max-w-xl': config.maxWidth === 'xl',
25
+ 'sm:max-w-md md:max-w-xl lg:max-w-2xl': config.maxWidth === '2xl',
26
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl': config.maxWidth === '3xl',
27
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-4xl': config.maxWidth === '4xl',
28
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl': config.maxWidth === '5xl',
29
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl 2xl:max-w-6xl': config.maxWidth === '6xl',
30
+ 'sm:max-w-md md:max-w-xl lg:max-w-3xl xl:max-w-5xl 2xl:max-w-7xl': config.maxWidth === '7xl',
31
31
  })}
32
32
  >
33
- <DialogPanel className={`im-slideover-content relative ${modalProps.paddingClasses} ${modalProps.panelClasses}`}>
34
- {modalProps.closeButton && (
33
+ <DialogPanel className={`im-slideover-content relative ${config.paddingClasses} ${config.panelClasses}`}>
34
+ {config.closeButton && (
35
35
  <div className="absolute right-0 top-0 pr-3 pt-3">
36
36
  <CloseButton onClick={modalContext.close} />
37
37
  </div>
38
38
  )}
39
- {typeof children === 'function' ? children({ modalContext, modalProps }) : children}
39
+ {typeof children === 'function' ? children({ modalContext, config }) : children}
40
40
  </DialogPanel>
41
41
  </TransitionChild>
42
42
  </div>
@@ -4,5 +4,6 @@ import { useModalStack, ModalRoot, ModalStackProvider, renderApp } from './Modal
4
4
  import HeadlessModal from './HeadlessModal.jsx'
5
5
  import Modal from './Modal.jsx'
6
6
  import ModalLink from './ModalLink.jsx'
7
+ import useModal from './useModal.js'
7
8
 
8
- export { getConfig, putConfig, resetConfig, useModalStack, useModalIndex, HeadlessModal, Modal, ModalLink, ModalRoot, ModalStackProvider, renderApp }
9
+ export { getConfig, putConfig, resetConfig, useModalStack, useModalIndex, HeadlessModal, Modal, ModalLink, ModalRoot, ModalStackProvider, renderApp, useModal }
@@ -0,0 +1,6 @@
1
+ import { useModalIndex } from './ModalRenderer.jsx'
2
+ import { useModalStack } from './ModalRoot.jsx'
3
+
4
+ export default function useModal() {
5
+ return useModalStack().stack[useModalIndex()] ?? null
6
+ }