Last active
November 15, 2024 12:05
-
-
Save stctheproducer/a70772088a74fbbc4627540be122e40d to your computer and use it in GitHub Desktop.
This is the Svelte implementation of the `Link` component provided by the `Tuyau` package (https://github.com/Julien-R44/tuyau). The types are a little hacky because the Inertia Link component was built in Svelte 4.
This file contains hidden or 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
// ...imports here | |
import { setTuyauState, TUYAU_KEY } from '../components/tuyau_state' | |
const context = new Map([[TUYAU_KEY, setTuyauState()]]) | |
createInertiaApp({ | |
// ...other configs | |
setup({ el, App, props }) { | |
if (el?.dataset.serverRendered === 'true') { | |
hydrate(App, { target: el, props, context }) | |
} else { | |
mount(App, { target: el!, props, context }) | |
} | |
}, | |
}) |
This file contains hidden or 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
// ...imports here | |
import { TUYAU_KEY, setTuyauState } from '../components/tuyau_state' | |
export default function render(page: any) { | |
const context = new Map([[TUYAU_KEY, setTuyauState()]]) | |
return createInertiaApp({ | |
// ...other configs | |
setup({ App, props }) { | |
return renderSvelte(App, { props, context }) | |
}, | |
}) | |
} |
This file contains hidden or 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
<script lang="ts" generics="Route extends RouteName<ValidatedApi['routes']>"> | |
import { type ComponentEvents } from 'svelte' | |
import type { RouteName } from '@tuyau/client' | |
import { type ValidatedApi, type LinkParams } from '@tuyau/inertia/types' | |
import { type ComponentProps, type Snippet } from 'svelte' | |
import { Link as InertiaLink } from '@inertiajs/svelte' | |
import { getTuyauState } from '../tuyau_state' | |
type InertiaLinkProps = ComponentProps<InertiaLink> | |
type InertiaLinkEvents = ComponentEvents<InertiaLink> | |
type BaseProps = { | |
[x: string]: any | |
as?: InertiaLinkProps['as'] | |
data?: InertiaLinkProps['data'] | |
replace?: InertiaLinkProps['replace'] | |
preserveScroll?: InertiaLinkProps['preserveScroll'] | |
preserveState?: InertiaLinkProps['preserveState'] | |
only?: InertiaLinkProps['only'] | |
except?: InertiaLinkProps['except'] | |
headers?: InertiaLinkProps['headers'] | |
queryStringArrayFormat?: InertiaLinkProps['queryStringArrayFormat'] | |
async?: InertiaLinkProps['async'] | |
prefetch?: InertiaLinkProps['prefetch'] | |
cacheFor?: InertiaLinkProps['cacheFor'] | |
} & Partial<InertiaLinkEvents> | |
interface Props extends BaseProps { | |
route: Route | |
children?: Snippet | |
params?: LinkParams<Route> | |
} | |
let { children, ...baseProps }: Props = $props() | |
let tuyau = getTuyauState() | |
let route = tuyau.$route(baseProps.route, (baseProps as any).params) | |
let url = tuyau.$url(baseProps.route, { params: baseProps.params } as any) | |
let linkProps = { ...baseProps, route: undefined, params: undefined } | |
</script> | |
<InertiaLink href={url} method={route.method[0].toLowerCase() as any} {...linkProps}> | |
{@render children?.()} | |
</InertiaLink> |
This file contains hidden or 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
import { createTuyau } from '@tuyau/client' | |
import { api } from '#brisk/.adonisjs/api' | |
import { getContext } from 'svelte' | |
export const TUYAU_KEY = Symbol('tuyau') | |
export function setTuyauState() { | |
const tuyau = createTuyau({ | |
api, | |
baseUrl: import.meta.env.VITE_APP_TUYAU_BASE_URL, | |
timeout: 10_000, | |
}) | |
return tuyau | |
} | |
export function getTuyauState() { | |
const tuyau = getContext<ReturnType<typeof setTuyauState> | null>(TUYAU_KEY) | |
if (!tuyau) { | |
throw new Error('Tuyau client not found in context') | |
} | |
return tuyau | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment