-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for custom app mounting (see #54)
- Loading branch information
1 parent
f45940f
commit 4e9d4f2
Showing
11 changed files
with
237 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ModalRoot } from '@inertiaui/modal-react' | ||
|
||
export default function Layout({ children }) { | ||
return ( | ||
<> | ||
{children} | ||
<ModalRoot /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import './bootstrap' | ||
import '../css/app.css' | ||
|
||
import { createApp, h } from 'vue' | ||
import { createInertiaApp } from '@inertiajs/vue3' | ||
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers' | ||
import { ZiggyVue } from '../../vendor/tightenco/ziggy' | ||
import { putConfig, initFromPageProps, ModalRoot } from '@inertiaui/modal-vue' | ||
|
||
const appName = import.meta.env.VITE_APP_NAME || 'Laravel' | ||
|
||
createInertiaApp({ | ||
title: (title) => `${title} - ${appName}`, | ||
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')), | ||
setup({ el, App, props, plugin }) { | ||
initFromPageProps(props) | ||
|
||
return createApp({ render: () => h(ModalRoot, () => h(App, props)) }) | ||
.use(plugin) | ||
.use(ZiggyVue) | ||
.mount(el) | ||
}, | ||
progress: { | ||
color: '#4B5563', | ||
}, | ||
}) | ||
|
||
if(window.location.pathname === '/props-from-config') { | ||
putConfig({ | ||
type: 'slideover', | ||
slideover: { | ||
closeButton: false, | ||
closeExplicitly: true, | ||
maxWidth: '2xl', | ||
paddingClasses: 'p-8', | ||
panelClasses: 'min-h-screen bg-red-100', | ||
position: 'left', | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import './bootstrap'; | ||
import '../css/app.css'; | ||
|
||
import { createRoot } from 'react-dom/client'; | ||
import { createInertiaApp } from '@inertiajs/react'; | ||
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers'; | ||
import { putConfig, ModalStackProvider, initFromPageProps, setPageLayout } from '@inertiaui/modal-react' | ||
import ModalLayout from './ModalLayout'; | ||
|
||
const appName = import.meta.env.VITE_APP_NAME || 'Laravel'; | ||
|
||
createInertiaApp({ | ||
title: (title) => `${title} - ${appName}`, | ||
resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')) | ||
.then(setPageLayout(ModalLayout)), | ||
setup({ el, App, props }) { | ||
const root = createRoot(el); | ||
|
||
initFromPageProps(props); | ||
|
||
root.render( | ||
<ModalStackProvider> | ||
<App {...props} /> | ||
</ModalStackProvider> | ||
); | ||
}, | ||
progress: { | ||
color: '#4B5563', | ||
}, | ||
}); | ||
|
||
if (window.location.pathname === '/props-from-config') { | ||
putConfig({ | ||
type: 'slideover', | ||
slideover: { | ||
closeButton: false, | ||
closeExplicitly: true, | ||
maxWidth: '2xl', | ||
paddingClasses: 'p-8', | ||
panelClasses: 'min-h-screen bg-red-100', | ||
position: 'left', | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Custom App Mounting | ||
|
||
If you need more refined control over the mounting process, contrary to the regular [Inertia.js installation](/installation.html#inertia-js-configuration), you may ignore the `renderApp` helper method and perform the mounting manually. | ||
|
||
## React | ||
|
||
In React, you need to make changes to the `app.jsx` file and use a Layout component that renders the `ModalRoot` component after your main content. | ||
|
||
### Create a Layout Component | ||
|
||
First, create a Layout component that renders the `ModalRoot` component after your main content. In this example, we are naming the component `ModalLayout.jsx`: | ||
|
||
```jsx [React] | ||
import { ModalRoot } from '@inertiaui/modal-react' | ||
|
||
export default function Layout({ children }) { | ||
return ( | ||
<> | ||
{children} | ||
<ModalRoot /> | ||
</> | ||
) | ||
} | ||
``` | ||
|
||
### Update `app.jsx` | ||
|
||
First, import your new Layout component and set it as the layout for your pages: | ||
|
||
```jsx [React] | ||
createInertiaApp({ | ||
resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')) | ||
.then(function (page) { // [!code ++] | ||
page.default.layout = (page => <ModalLayout children={page} />) // [!code ++] | ||
return page; // [!code ++] | ||
}), // [!code ++] | ||
}); | ||
``` | ||
If you find this cumbersome, you can also use the `setPageLayout` helper function to set the layout for all pages: | ||
```jsx [React] | ||
import { setPageLayout } from '@inertiaui/modal-react' // [!code ++] | ||
|
||
createInertiaApp({ | ||
resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')) | ||
.then(setPageLayout(ModalLayout)), // [!code ++] | ||
}); | ||
``` | ||
Next, you need to call the `initFromPageProps` method with the `props` object. Lastly, you need to wrap the `App` component within the `ModalStackProvider` component: | ||
```jsx [React] | ||
import { ModalStackProvider, initFromPageProps } from '@inertiaui/modal-react' // [!code ++] | ||
|
||
createInertiaApp({ | ||
setup({ el, App, props }) { | ||
const root = createRoot(el); | ||
initFromPageProps(props) // [!code ++] | ||
|
||
root.render( | ||
<ModalStackProvider> // [!code ++] | ||
<App {...props} /> | ||
</ModalStackProvider> // [!code ++] | ||
); | ||
} | ||
}); | ||
``` | ||
## Vue | ||
In Vue, it is a little bit simpler because you only need to make changes to the main `app.js` file. In this file, you need to call the `initFromPageProps` method with the `props` object. Then, you need to wrap the `App` component within the `ModalRoot` component: | ||
```js [Vue] | ||
import { ModalRoot, initFromPageProps } from '@inertiaui/modal-vue' // [!code ++] | ||
|
||
createInertiaApp({ | ||
setup({ el, App, props, plugin }) { | ||
initFromPageProps(props) // [!code ++] | ||
|
||
return | ||
createApp({ render: () => h(App, props) }) // [!code --] | ||
createApp({ render: () => h(ModalRoot, () => h(App, props)) }) // [!code ++] | ||
.use(plugin) | ||
.mount(el) | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
import { createElement } from 'react' | ||
import { getConfig, putConfig, resetConfig } from './config.js' | ||
import { useModalIndex } from './ModalRenderer.jsx' | ||
import { useModalStack, ModalRoot, ModalStackProvider, renderApp } from './ModalRoot.jsx' | ||
import { useModalStack, ModalRoot, ModalStackProvider, renderApp, initFromPageProps } from './ModalRoot.jsx' | ||
import HeadlessModal from './HeadlessModal.jsx' | ||
import Modal from './Modal.jsx' | ||
import ModalLink from './ModalLink.jsx' | ||
import useModal from './useModal.js' | ||
|
||
export { getConfig, putConfig, resetConfig, useModalStack, useModalIndex, HeadlessModal, Modal, ModalLink, ModalRoot, ModalStackProvider, renderApp, useModal } | ||
const setPageLayout = (layout) => (module) => { | ||
module.default.layout = (page) => createElement(layout, { children: page }) | ||
return module | ||
} | ||
|
||
export { | ||
getConfig, | ||
putConfig, | ||
resetConfig, | ||
useModalStack, | ||
useModalIndex, | ||
HeadlessModal, | ||
Modal, | ||
ModalLink, | ||
ModalRoot, | ||
ModalStackProvider, | ||
renderApp, | ||
useModal, | ||
initFromPageProps, | ||
setPageLayout, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters