Proposal : New component "Include" #1968
Replies: 8 comments 4 replies
-
Oh that's very interesting. I'd be interested to see how this manages state, as well as managing state at the parent level - where the props for that component etc are kept, how this would work with things like state restoration, scroll keeping etc. If it's just a case of a completely new nested component I guess it would handle all that internally? I have a feeling this is something that could be extremely useful for modals etc, it would reduce the need for an I like the idea that this could be controlled by the current page state, and indirectly by the URL, rather than something that relies on setting a base URL like how Hybridly handles it, or the Momentum Modal package handles it. I realise your concept is not limited to modals btw, just an interesting use case that i've encountered personally. Colour me interested as a concept for sure. Subcomponents.One other thing that interests me is the idea that these could be included as "subcomponents" passed to Inertia::render instead, so all the HTTP traffic would still flow from this same route instead.
inertia('Pages/MyPage', [
'data' => Inertia::defer(fn () => MyModel::all()),
)
->withComponent('size-modal', 'Components/Modal', [
'sizes' => Inertia::defer(fn () => ClothingSizes::all()),
]) <InertiaComponent key="size-modal" v-slot="{sizes}" on:mounted="sizes = Object.entries(sizes)">
<select>
<option v-for="(label, value) in sizes" :value="value">{label}</option>
</select>
</InertiaComponent> |
Beta Was this translation helpful? Give feedback.
-
That's pretty much identical to the Frames component I proposed ages ago: https://github.com/buhrmi/inertia/blob/x/readme.md#frames |
Beta Was this translation helpful? Give feedback.
-
I think I prefer the proposal framed as One thing I would love is NOT having to register another route - that's the beauty of Inertia for me (and even MORE so with the 2.0 release), and if I have to register a route that's only serving a partial, then it becomes useless in itself, unless you have some sort of if/else going in the controller, which isn't very elegant. I.e. : calling I'm working on a package to provide class-like components to Inertia at the moment and it would be so neat if you could pass the class-based component to inertia::render and it lazy loaded it when needed: inertia('Pages/Show', [
'edit_modal' => new EditModal(/* You could pass any constructor arguments here to pass info in */),
]); OR, if you just wanted to do it using a function: inertia('Pages/Show', [
'edit_modal' => Inertia::component('Components/EditModal', [
'user' => Inertia::defer(fn() => $user),
'lookup_options' => Lookups::userEditFormLookupValues(), // Used to populate select lists etc.
]);
]); Inertia would:
|
Beta Was this translation helpful? Give feedback.
-
This feature is very useful. I came from For me, like @buhrmi said, I prefer |
Beta Was this translation helpful? Give feedback.
-
Screen.Recording.2024-09-17.at.03.42.37.movI've been hard at work on my Inertia fork with support for this feature. The possibilities that a Frame/Include component opens up are really amazing and I do hope to see something like this get implemented in an official release. |
Beta Was this translation helpful? Give feedback.
-
More demos: https://x.com/einbuhrmi/status/1835844702324797930 Please note I'm just spamming this stuff to convince the core team to add this feature to the official adapters. |
Beta Was this translation helpful? Give feedback.
-
Hey guys, if you're using Svelte, I've released an alpha of my new version of Inertia X, completely rewritten, based on Inertia 2.0. |
Beta Was this translation helpful? Give feedback.
-
In case you want to try it, I made it for Vue. <script lang="ts" setup>
import { resolvePageComponent } from '@adonisjs/inertia/helpers';
import { usePage } from '@inertiajs/vue3';
import { markRaw, onMounted, ref } from 'vue';
const { source } = defineProps<{
source: string;
}>();
const assetVersion = usePage().version;
const componentProps = ref<any>(null);
const component = ref<any>(null);
onMounted(async () => {
const response = await fetch(source, {
headers: {
'X-Inertia': 'true',
'X-Inertia-Fragment': 'true',
'X-Inertia-Version': assetVersion,
},
}).then((r) => r.json());
componentProps.value = response.props;
component.value = await resolvePageComponent(
`../fragments/${response.component}.vue`,
import.meta.glob('../fragments/**/*.vue')
).then((c: any) => markRaw(c.default));
});
</script>
<template>
<div v-if="component === null">
<slot name="fallback" />
</div>
<Component :is="component" v-bind="componentProps" v-else />
</template> Then you can use it like so: <template>
<TopBar :user="currentUser">
<template #after-logo>
<LoadFragment source="/live/status" />
</template>
<template #not-connected>
<LoginDialog />
</template>
</TopBar>
</template Direct example:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there! 👋🏻
I'd like to propose adding a new default component across all Inertia 2 Adapters. This component would replicate a feature I enjoy from unpoly, which allows you to lazy-load a component from another endpoint.
Note
Real example using Vue: #1968 (comment)
Example
Imagine you have a badge in your website header that indicates whether you are live on Twitch. There are several ways to achieve this:
The new component would offer a simpler alternative:
How It Works
The
Include
component would fetch data from the/live
endpoint and render the component returned by the server, similar to how apage
component works. Additionally, the request will include a special header,X-Inertia-Include: true
, so the server knows the request is specifically for an include component and can return different content if needed.It will also integrate seamlessly with the
WhenVisible
component.I've got a small proof of concept working in VueJS and AdonisJS, and I can share it later if needed.
Beta Was this translation helpful? Give feedback.
All reactions