Skip to content

Commit dfcdfb3

Browse files
committed
Add router configuration for modal display on non-Inertia responses
- Introduced `showModalOnNonInertiaResponse` option in RouterConfig to control modal visibility. - Updated Response class to respect this configuration when handling non-Inertia responses. - Modified createInertiaApp functions in React, Svelte, and Vue3 to accept and configure this option.
1 parent 4efe682 commit dfcdfb3

File tree

7 files changed

+49
-8
lines changed

7 files changed

+49
-8
lines changed

packages/core/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Router } from './router'
22

33
export { default as createHeadManager } from './head'
4-
export { hide as hideProgress, reveal as revealProgress, default as setupProgress } from './progress'
4+
export { setupModal } from './modal'
5+
export { default as setupProgress, hide as hideProgress, reveal as revealProgress } from './progress'
56
export { default as shouldIntercept } from './shouldIntercept'
67
export * from './types'
78
export { hrefToUrl, mergeDataIntoQueryString, urlWithoutHash } from './url'

packages/core/src/modal.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
import { ModalConfig } from './types'
2+
3+
const settings: ModalConfig = {
4+
showOnNonInertiaResponse: true,
5+
}
6+
7+
export const setupModal = (options: Partial<ModalConfig>): void => {
8+
Object.assign(settings, options)
9+
}
10+
11+
export const getConfig = (): ModalConfig => {
12+
return { ...settings }
13+
}
14+
115
export default {
216
modal: null,
317
listener: null,
18+
setupModal,
19+
getConfig,
420

521
show(html: Record<string, unknown> | string): void {
622
if (typeof html === 'object') {

packages/core/src/response.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ export class Response {
9090
}
9191

9292
if (fireInvalidEvent(response)) {
93-
return modal.show(response.data)
93+
if (modal.getConfig().showOnNonInertiaResponse) {
94+
return modal.show(response.data)
95+
}
9496
}
9597
}
9698

packages/core/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ export type ProgressSettings = {
336336
color: string
337337
}
338338

339+
export type ModalConfig = {
340+
showOnNonInertiaResponse?: boolean
341+
}
342+
339343
declare global {
340344
interface DocumentEventMap {
341345
'inertia:before': GlobalEvent<'before'>

packages/react/src/createInertiaApp.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Page, PageProps, PageResolver, router, setupProgress } from '@inertiajs/core'
2-
import { ComponentType, FunctionComponent, Key, ReactElement, ReactNode, createElement } from 'react'
1+
import { Page, PageProps, PageResolver, router, setupModal, setupProgress } from '@inertiajs/core'
2+
import { ComponentType, createElement, FunctionComponent, Key, ReactElement, ReactNode } from 'react'
33
import { renderToString } from 'react-dom/server'
44
import App from './App'
55

@@ -30,6 +30,7 @@ export type SetupOptions<ElementType, SharedProps extends PageProps> = {
3030
type BaseInertiaAppOptions = {
3131
title?: HeadManagerTitleCallback
3232
resolve: PageResolver
33+
showModalOnNonInertiaResponse?: boolean
3334
}
3435

3536
type CreateInertiaAppSetupReturnType = ReactInstance | void
@@ -71,6 +72,7 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
7172
progress = {},
7273
page,
7374
render,
75+
showModalOnNonInertiaResponse = true,
7476
}: InertiaAppOptionsForCSR<SharedProps> | InertiaAppOptionsForSSR<SharedProps>): Promise<
7577
CreateInertiaAppSetupReturnType | CreateInertiaAppSSRContent
7678
> {
@@ -82,6 +84,10 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
8284

8385
let head = []
8486

87+
if (!isServer) {
88+
setupModal({ showOnNonInertiaResponse: showModalOnNonInertiaResponse })
89+
}
90+
8591
const reactApp = await Promise.all([
8692
resolveComponent(initialPage.component),
8793
router.decryptHistory().catch(() => {}),

packages/svelte/src/createInertiaApp.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { router, setupProgress, type InertiaAppResponse, type Page } from '@inertiajs/core'
1+
import { router, setupModal, setupProgress, type InertiaAppResponse, type Page } from '@inertiajs/core'
22
import escape from 'html-escape'
33
import type { ComponentType } from 'svelte'
44
import App, { type InertiaAppProps } from './components/App.svelte'
@@ -24,6 +24,7 @@ interface CreateInertiaAppProps {
2424
showSpinner?: boolean
2525
}
2626
page?: Page
27+
showModalOnNonInertiaResponse?: boolean
2728
}
2829

2930
export default async function createInertiaApp({
@@ -32,12 +33,17 @@ export default async function createInertiaApp({
3233
setup,
3334
progress = {},
3435
page,
36+
showModalOnNonInertiaResponse = true,
3537
}: CreateInertiaAppProps): InertiaAppResponse {
3638
const isServer = typeof window === 'undefined'
3739
const el = isServer ? null : document.getElementById(id)
3840
const initialPage: Page = page || JSON.parse(el?.dataset.page || '{}')
3941
const resolveComponent = (name: string) => Promise.resolve(resolve(name))
4042

43+
if (!isServer) {
44+
setupModal({ showOnNonInertiaResponse: showModalOnNonInertiaResponse })
45+
}
46+
4147
const [initialComponent] = await Promise.all([
4248
resolveComponent(initialPage.component),
4349
router.decryptHistory().catch(() => {}),
@@ -48,7 +54,7 @@ export default async function createInertiaApp({
4854
const svelteApp = setup({
4955
el,
5056
App: App as unknown as AppComponent,
51-
props
57+
props,
5258
})
5359

5460
if (isServer) {

packages/vue3/src/createInertiaApp.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Page, router, setupProgress } from '@inertiajs/core'
2-
import { DefineComponent, Plugin, App as VueApp, createSSRApp, h } from 'vue'
1+
import { Page, router, setupModal, setupProgress } from '@inertiajs/core'
2+
import { App as VueApp, createSSRApp, DefineComponent, h, Plugin } from 'vue'
33
import App, { InertiaApp, InertiaAppProps, plugin } from './app'
44

55
interface CreateInertiaAppProps {
@@ -17,6 +17,7 @@ interface CreateInertiaAppProps {
1717
}
1818
page?: Page
1919
render?: (app: VueApp) => Promise<string>
20+
showModalOnNonInertiaResponse?: boolean
2021
}
2122

2223
export default async function createInertiaApp({
@@ -27,6 +28,7 @@ export default async function createInertiaApp({
2728
progress = {},
2829
page,
2930
render,
31+
showModalOnNonInertiaResponse = true,
3032
}: CreateInertiaAppProps): Promise<{ head: string[]; body: string }> {
3133
const isServer = typeof window === 'undefined'
3234
const el = isServer ? null : document.getElementById(id)
@@ -35,6 +37,10 @@ export default async function createInertiaApp({
3537

3638
let head = []
3739

40+
if (!isServer) {
41+
setupModal({ showOnNonInertiaResponse: showModalOnNonInertiaResponse })
42+
}
43+
3844
const vueApp = await Promise.all([
3945
resolveComponent(initialPage.component),
4046
router.decryptHistory().catch(() => {}),

0 commit comments

Comments
 (0)