Skip to content

Add router configuration for displaying modal on non-Inertia responses #2332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Router } from './router'

export { default as createHeadManager } from './head'
export { hide as hideProgress, reveal as revealProgress, default as setupProgress } from './progress'
export { setupModal } from './modal'
export { default as setupProgress, hide as hideProgress, reveal as revealProgress } from './progress'
export { default as shouldIntercept } from './shouldIntercept'
export * from './types'
export { hrefToUrl, mergeDataIntoQueryString, urlWithoutHash } from './url'
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/modal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { ModalConfig } from './types'

const settings: ModalConfig = {
showOnNonInertiaResponse: true,
}

export const setupModal = (options: Partial<ModalConfig>): void => {
Object.assign(settings, options)
}

export const getConfig = (): ModalConfig => {
return { ...settings }
}

export default {
modal: null,
listener: null,
setupModal,
getConfig,

show(html: Record<string, unknown> | string): void {
if (typeof html === 'object') {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export class Response {
}

if (fireInvalidEvent(response)) {
return modal.show(response.data)
if (modal.getConfig().showOnNonInertiaResponse) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this logic could be moved inside the modal.show so the caller doesn't need to know about this config or any business rule

return modal.show(response.data)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ export type ProgressSettings = {
color: string
}

export type ModalConfig = {
showOnNonInertiaResponse?: boolean
}

declare global {
interface DocumentEventMap {
'inertia:before': GlobalEvent<'before'>
Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/createInertiaApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Page, PageProps, PageResolver, router, setupProgress } from '@inertiajs/core'
import { Page, PageProps, PageResolver, router, setupModal, setupProgress } from '@inertiajs/core'
import { ComponentType, FunctionComponent, Key, ReactElement, ReactNode, createElement } from 'react'
import { renderToString } from 'react-dom/server'
import App from './App'
Expand Down Expand Up @@ -30,6 +30,7 @@ export type SetupOptions<ElementType, SharedProps extends PageProps> = {
type BaseInertiaAppOptions = {
title?: HeadManagerTitleCallback
resolve: PageResolver
showModalOnNonInertiaResponse?: boolean
}

type CreateInertiaAppSetupReturnType = ReactInstance | void
Expand Down Expand Up @@ -71,6 +72,7 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
progress = {},
page,
render,
showModalOnNonInertiaResponse = true,
}: InertiaAppOptionsForCSR<SharedProps> | InertiaAppOptionsForSSR<SharedProps>): Promise<
CreateInertiaAppSetupReturnType | CreateInertiaAppSSRContent
> {
Expand All @@ -82,6 +84,10 @@ export default async function createInertiaApp<SharedProps extends PageProps = P

let head = []

if (!isServer) {
setupModal({ showOnNonInertiaResponse: showModalOnNonInertiaResponse })
}
Copy link
Author

@Andy9822 Andy9822 Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative Approach:

if we wanted to follow the same "API" used for the progress configs, we could instead have a modal = {} props and then call setupModal(modal)

Copy link
Author

@Andy9822 Andy9822 Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A usage example of the alternative proposal would then look like:

createInertiaApp({
 modal: {
   showOnNonInertiaResponse: false
 },
 progress: { 
   delay: 300,
   color: '#29d'
 },
 resolve: (name) => import(`./Pages/${name}`),
 setup({ el, App, props }) {
   render(<App {...props} />, el)
 }
})


const reactApp = await Promise.all([
resolveComponent(initialPage.component),
router.decryptHistory().catch(() => {}),
Expand Down
8 changes: 7 additions & 1 deletion packages/svelte/src/createInertiaApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { router, setupProgress, type InertiaAppResponse, type Page } from '@inertiajs/core'
import { router, setupModal, setupProgress, type InertiaAppResponse, type Page } from '@inertiajs/core'
import escape from 'html-escape'
import type { ComponentType } from 'svelte'
import App, { type InertiaAppProps } from './components/App.svelte'
Expand All @@ -24,6 +24,7 @@ interface CreateInertiaAppProps {
showSpinner?: boolean
}
page?: Page
showModalOnNonInertiaResponse?: boolean
}

export default async function createInertiaApp({
Expand All @@ -32,12 +33,17 @@ export default async function createInertiaApp({
setup,
progress = {},
page,
showModalOnNonInertiaResponse = true,
}: CreateInertiaAppProps): InertiaAppResponse {
const isServer = typeof window === 'undefined'
const el = isServer ? null : document.getElementById(id)
const initialPage: Page = page || JSON.parse(el?.dataset.page || '{}')
const resolveComponent = (name: string) => Promise.resolve(resolve(name))

if (!isServer) {
setupModal({ showOnNonInertiaResponse: showModalOnNonInertiaResponse })
}

const [initialComponent] = await Promise.all([
resolveComponent(initialPage.component),
router.decryptHistory().catch(() => {}),
Expand Down
8 changes: 7 additions & 1 deletion packages/vue3/src/createInertiaApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Page, router, setupProgress } from '@inertiajs/core'
import { Page, router, setupModal, setupProgress } from '@inertiajs/core'
import { DefineComponent, Plugin, App as VueApp, createSSRApp, h } from 'vue'
import App, { InertiaApp, InertiaAppProps, plugin } from './app'

Expand All @@ -17,6 +17,7 @@ interface CreateInertiaAppProps {
}
page?: Page
render?: (app: VueApp) => Promise<string>
showModalOnNonInertiaResponse?: boolean
}

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

let head = []

if (!isServer) {
setupModal({ showOnNonInertiaResponse: showModalOnNonInertiaResponse })
}

const vueApp = await Promise.all([
resolveComponent(initialPage.component),
router.decryptHistory().catch(() => {}),
Expand Down
Loading