@inertia-solid/solid 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +47 -0
- package/README.md +10 -2
- package/dist/App.d.ts +13 -0
- package/dist/App.jsx +90 -0
- package/dist/Deferred.d.ts +17 -0
- package/dist/Deferred.jsx +70 -0
- package/dist/Form.d.ts +13 -0
- package/dist/Form.jsx +237 -0
- package/dist/Head.d.ts +6 -0
- package/dist/Head.js +117 -0
- package/dist/HeadContext.d.ts +4 -0
- package/dist/HeadContext.js +3 -0
- package/dist/InfiniteScroll.d.ts +23 -0
- package/dist/InfiniteScroll.jsx +186 -0
- package/dist/Link.d.ts +16 -0
- package/dist/Link.jsx +155 -0
- package/dist/PageContext.d.ts +4 -0
- package/dist/PageContext.js +3 -0
- package/dist/Render.d.ts +8 -0
- package/dist/Render.jsx +27 -0
- package/dist/WhenMounted.d.ts +8 -0
- package/dist/WhenMounted.jsx +12 -0
- package/dist/WhenVisible.d.ts +22 -0
- package/dist/WhenVisible.jsx +67 -0
- package/dist/createInertiaApp.d.ts +40 -0
- package/dist/createInertiaApp.js +77 -0
- package/dist/echo.d.ts +6 -0
- package/dist/echo.js +3 -0
- package/dist/formHelper.d.ts +44 -0
- package/dist/formHelper.js +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +18 -0
- package/dist/layoutProps.d.ts +11 -0
- package/dist/layoutProps.js +16 -0
- package/dist/renderTree.d.ts +21 -0
- package/dist/renderTree.js +72 -0
- package/dist/server.d.ts +1 -0
- package/dist/server.js +1 -0
- package/dist/slot.d.ts +10 -0
- package/dist/slot.js +13 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +1 -0
- package/dist/useForm.d.ts +11 -0
- package/dist/useForm.js +106 -0
- package/dist/useFormState.d.ts +33 -0
- package/dist/useFormState.js +278 -0
- package/dist/useHttp.d.ts +10 -0
- package/dist/useHttp.js +172 -0
- package/dist/usePage.d.ts +6 -0
- package/dist/usePage.js +9 -0
- package/dist/usePoll.d.ts +12 -0
- package/dist/usePoll.js +28 -0
- package/dist/usePrefetch.d.ts +10 -0
- package/dist/usePrefetch.js +42 -0
- package/dist/useProp.d.ts +8 -0
- package/dist/useProp.js +27 -0
- package/dist/useRemember.d.ts +14 -0
- package/dist/useRemember.js +36 -0
- package/package.json +62 -3
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { getScrollableParent, useInfiniteScroll, } from '@inertiajs/core';
|
|
2
|
+
import { dynamic } from '@solidjs/web';
|
|
3
|
+
import { createEffect, createMemo, createSignal, createStore, flush, omit, onSettled, Show } from 'solid-js';
|
|
4
|
+
import { renderSlot } from './slot';
|
|
5
|
+
import usePage from './usePage';
|
|
6
|
+
/** Props of the component that must not reach the items element. */
|
|
7
|
+
const componentOnlyProps = new Set([
|
|
8
|
+
'data', 'buffer', 'as', 'manual', 'manualAfter', 'preserveUrl', 'reverse', 'autoScroll', 'onlyNext', 'onlyPrevious',
|
|
9
|
+
'startElement', 'endElement', 'itemsElement', 'previous', 'next', 'loading', 'params', 'children', 'ref',
|
|
10
|
+
]);
|
|
11
|
+
const resolveElement = (element, fallback) => {
|
|
12
|
+
if (!element) {
|
|
13
|
+
return fallback ?? null;
|
|
14
|
+
}
|
|
15
|
+
return typeof element === 'string' ? document.querySelector(element) : (element() ?? null);
|
|
16
|
+
};
|
|
17
|
+
const scrollToBottom = (scrollableParent) => {
|
|
18
|
+
if (scrollableParent) {
|
|
19
|
+
scrollableParent.scrollTo({ top: scrollableParent.scrollHeight, behavior: 'instant' });
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
window.scrollTo({ top: document.body.scrollHeight, behavior: 'instant' });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Loads the previous and next pages of a paginated prop as the start and end triggers scroll into view. Core's
|
|
27
|
+
* `useInfiniteScroll` owns the requests, the observers, the URL and the scroll position. This component owns the
|
|
28
|
+
* state the slots read: one `{ loading, hasMore }` record per side and the request count.
|
|
29
|
+
*/
|
|
30
|
+
export default function InfiniteScroll(props) {
|
|
31
|
+
const page = usePage();
|
|
32
|
+
let instance;
|
|
33
|
+
// Seeded from the scroll props until an instance exists, then from its data manager, as React's useState seed and
|
|
34
|
+
// sync are. A re-derive drafts the current store, so `loading` and `requestCount` keep their values.
|
|
35
|
+
const [state, setState] = createStore((draft) => {
|
|
36
|
+
const scrollProp = page.scrollProps?.[props.data];
|
|
37
|
+
const dataManager = instance?.dataManager;
|
|
38
|
+
draft.previous.hasMore = dataManager ? dataManager.hasPrevious() : !!scrollProp?.previousPage;
|
|
39
|
+
draft.next.hasMore = dataManager ? dataManager.hasNext() : !!scrollProp?.nextPage;
|
|
40
|
+
}, { previous: { loading: false, hasMore: false }, next: { loading: false, hasMore: false }, requestCount: 0 });
|
|
41
|
+
// The items element is a signal so that a changed `as`, which makes `dynamic()` build a new element, re-creates the
|
|
42
|
+
// scroll instance. The ref callback runs while the component renders, which is an owned scope.
|
|
43
|
+
const [itemsRef, setItemsRef] = createSignal(undefined, { ownedWrite: true });
|
|
44
|
+
// Counts the instances created, so the effect that enables the triggers runs again for each one.
|
|
45
|
+
const [generation, setGeneration] = createSignal(0, { ownedWrite: true });
|
|
46
|
+
const triggerElements = {};
|
|
47
|
+
const customElement = (side) => (side === 'previous' ? props.startElement : props.endElement);
|
|
48
|
+
const fetchSide = (side) => side === 'previous' ? instance?.dataManager.fetchPrevious() : instance?.dataManager.fetchNext();
|
|
49
|
+
const autoLoad = createMemo(() => !(props.manual || ((props.manualAfter ?? 0) > 0 && state.requestCount >= (props.manualAfter ?? 0))));
|
|
50
|
+
const syncFromDataManager = () => {
|
|
51
|
+
const dataManager = instance?.dataManager;
|
|
52
|
+
if (dataManager) {
|
|
53
|
+
setState((draft) => {
|
|
54
|
+
draft.requestCount = dataManager.getRequestCount();
|
|
55
|
+
draft.previous.hasMore = dataManager.hasPrevious();
|
|
56
|
+
draft.next.hasMore = dataManager.hasNext();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const finishRequest = (side, completed) => {
|
|
61
|
+
setState((draft) => {
|
|
62
|
+
draft[side].loading = false;
|
|
63
|
+
});
|
|
64
|
+
if (completed) {
|
|
65
|
+
syncFromDataManager();
|
|
66
|
+
}
|
|
67
|
+
flush();
|
|
68
|
+
};
|
|
69
|
+
createEffect(() => ({
|
|
70
|
+
data: props.data,
|
|
71
|
+
fromRef: itemsRef(),
|
|
72
|
+
itemsElement: props.itemsElement,
|
|
73
|
+
startElement: props.startElement,
|
|
74
|
+
endElement: props.endElement,
|
|
75
|
+
ref: props.ref,
|
|
76
|
+
}), ({ fromRef, itemsElement, startElement, endElement, ref }) => {
|
|
77
|
+
const items = resolveElement(itemsElement, fromRef);
|
|
78
|
+
if (!items) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const start = resolveElement(startElement, triggerElements.previous);
|
|
82
|
+
const end = resolveElement(endElement, triggerElements.next);
|
|
83
|
+
const scrollableParent = getScrollableParent(items);
|
|
84
|
+
const scroll = useInfiniteScroll({
|
|
85
|
+
getPropName: () => props.data,
|
|
86
|
+
inReverseMode: () => props.reverse ?? false,
|
|
87
|
+
shouldFetchNext: () => !props.onlyPrevious,
|
|
88
|
+
shouldFetchPrevious: () => !props.onlyNext,
|
|
89
|
+
shouldPreserveUrl: () => props.preserveUrl ?? false,
|
|
90
|
+
getReloadOptions: () => props.params ?? {},
|
|
91
|
+
getTriggerMargin: () => props.buffer ?? 0,
|
|
92
|
+
getStartElement: () => start,
|
|
93
|
+
getEndElement: () => end,
|
|
94
|
+
getItemsElement: () => items,
|
|
95
|
+
getScrollableParent: () => scrollableParent,
|
|
96
|
+
onBeforePreviousRequest: () => setState((draft) => void (draft.previous.loading = true)),
|
|
97
|
+
onBeforeNextRequest: () => setState((draft) => void (draft.next.loading = true)),
|
|
98
|
+
onCompletePreviousRequest: ({ completed }) => finishRequest('previous', completed),
|
|
99
|
+
onCompleteNextRequest: ({ completed }) => finishRequest('next', completed),
|
|
100
|
+
onDataReset: syncFromDataManager,
|
|
101
|
+
});
|
|
102
|
+
instance = scroll;
|
|
103
|
+
syncFromDataManager();
|
|
104
|
+
scroll.elementManager.setupObservers();
|
|
105
|
+
scroll.elementManager.processServerLoadedElements(scroll.dataManager.getLastLoadedPage());
|
|
106
|
+
setGeneration((count) => count + 1);
|
|
107
|
+
const { fetchNext, fetchPrevious, hasPrevious, hasNext } = scroll.dataManager;
|
|
108
|
+
ref?.({ fetchNext, fetchPrevious, hasPrevious, hasNext });
|
|
109
|
+
return () => {
|
|
110
|
+
scroll.flush();
|
|
111
|
+
instance = undefined;
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
createEffect(() => [generation(), autoLoad(), props.onlyNext, props.onlyPrevious], ([, auto]) => {
|
|
115
|
+
if (auto) {
|
|
116
|
+
instance?.elementManager.enableTriggers();
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
instance?.elementManager.disableTriggers();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
onSettled(() => {
|
|
123
|
+
if (props.autoScroll ?? props.reverse) {
|
|
124
|
+
scrollToBottom(getScrollableParent(resolveElement(props.itemsElement, itemsRef())));
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
const autoMode = (side) => autoLoad() && !(side === 'previous' ? props.onlyNext : props.onlyPrevious);
|
|
128
|
+
const actionSlot = (side) => ({
|
|
129
|
+
get loading() {
|
|
130
|
+
return state[side].loading;
|
|
131
|
+
},
|
|
132
|
+
get loadingPrevious() {
|
|
133
|
+
return state.previous.loading;
|
|
134
|
+
},
|
|
135
|
+
get loadingNext() {
|
|
136
|
+
return state.next.loading;
|
|
137
|
+
},
|
|
138
|
+
fetch: () => fetchSide(side),
|
|
139
|
+
get autoMode() {
|
|
140
|
+
return autoMode(side);
|
|
141
|
+
},
|
|
142
|
+
get manualMode() {
|
|
143
|
+
return !autoMode(side);
|
|
144
|
+
},
|
|
145
|
+
get hasMore() {
|
|
146
|
+
return state[side].hasMore;
|
|
147
|
+
},
|
|
148
|
+
get hasPrevious() {
|
|
149
|
+
return state.previous.hasMore;
|
|
150
|
+
},
|
|
151
|
+
get hasNext() {
|
|
152
|
+
return state.next.hasMore;
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
const itemsSlot = {
|
|
156
|
+
get loading() {
|
|
157
|
+
return state.previous.loading || state.next.loading;
|
|
158
|
+
},
|
|
159
|
+
get loadingPrevious() {
|
|
160
|
+
return state.previous.loading;
|
|
161
|
+
},
|
|
162
|
+
get loadingNext() {
|
|
163
|
+
return state.next.loading;
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
const trigger = (side) => {
|
|
167
|
+
const slot = actionSlot(side);
|
|
168
|
+
const content = () => (side === 'previous' ? props.previous : props.next);
|
|
169
|
+
return (<Show when={!customElement(side)}>
|
|
170
|
+
<div ref={(element) => (triggerElements[side] = element)}>
|
|
171
|
+
<Show when={content()} keyed fallback={<Show when={state[side].loading}>{renderSlot(props.loading, slot)}</Show>}>
|
|
172
|
+
{(value) => renderSlot(value, slot)}
|
|
173
|
+
</Show>
|
|
174
|
+
</div>
|
|
175
|
+
</Show>);
|
|
176
|
+
};
|
|
177
|
+
const Items = dynamic(() => props.as ?? 'div');
|
|
178
|
+
const elements = [
|
|
179
|
+
trigger('previous'),
|
|
180
|
+
<Items {...omit(props, (key) => componentOnlyProps.has(key))} ref={(element) => setItemsRef(element)}>
|
|
181
|
+
{renderSlot(props.children, itemsSlot)}
|
|
182
|
+
</Items>,
|
|
183
|
+
trigger('next'),
|
|
184
|
+
];
|
|
185
|
+
return <>{props.reverse ? [...elements].reverse() : elements}</>;
|
|
186
|
+
}
|
package/dist/Link.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type LinkComponentBaseProps } from '@inertiajs/core';
|
|
2
|
+
import { type JSX, type ValidComponent } from '@solidjs/web';
|
|
3
|
+
interface BaseInertiaLinkProps extends LinkComponentBaseProps {
|
|
4
|
+
/** Tag name or component to render instead of `a`. */
|
|
5
|
+
as?: ValidComponent;
|
|
6
|
+
onClick?: JSX.EventHandler<HTMLElement, MouseEvent>;
|
|
7
|
+
ref?: (el: HTMLElement) => void;
|
|
8
|
+
children?: JSX.Element;
|
|
9
|
+
}
|
|
10
|
+
export type InertiaLinkProps = BaseInertiaLinkProps & Omit<JSX.AnchorHTMLAttributes<HTMLElement>, keyof BaseInertiaLinkProps>;
|
|
11
|
+
/**
|
|
12
|
+
* A visit link. It renders `a`, or `button` for a non-GET method, and intercepts plain clicks to visit through the
|
|
13
|
+
* router. Props are read where they are used, so a prop that changes after mount takes effect on the next event.
|
|
14
|
+
*/
|
|
15
|
+
export default function Link(props: InertiaLinkProps): JSX.Element;
|
|
16
|
+
export {};
|
package/dist/Link.jsx
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { config, isUrlMethodPair, mergeDataIntoQueryString, resolveUrlMethodPairComponent, router, shouldIntercept, shouldNavigate, } from '@inertiajs/core';
|
|
2
|
+
import { dynamic } from '@solidjs/web';
|
|
3
|
+
import { createEffect, createMemo, createSignal, omit, onSettled } from 'solid-js';
|
|
4
|
+
/** Props of the link that must not reach the element. */
|
|
5
|
+
const linkOnlyProps = new Set([
|
|
6
|
+
'as', 'href', 'data', 'method', 'component', 'replace', 'preserveScroll', 'preserveState', 'preserveUrl', 'only',
|
|
7
|
+
'except', 'headers', 'queryStringArrayFormat', 'async', 'viewTransition', 'instant', 'pageProps', 'prefetch',
|
|
8
|
+
'cacheFor', 'cacheTags', 'onClick', 'children', 'onCancelToken', 'onBefore', 'onStart', 'onProgress', 'onFinish',
|
|
9
|
+
'onCancel', 'onSuccess', 'onError', 'onHttpException', 'onNetworkError', 'onFlash', 'onPrefetching', 'onPrefetched',
|
|
10
|
+
]);
|
|
11
|
+
const toModes = (prefetch) => {
|
|
12
|
+
if (prefetch === true) {
|
|
13
|
+
return ['hover'];
|
|
14
|
+
}
|
|
15
|
+
if (!prefetch) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
return Array.isArray(prefetch) ? prefetch : [prefetch];
|
|
19
|
+
};
|
|
20
|
+
const toTrigger = (modes) => modes.includes('hover') ? 'hover' : modes.includes('click') ? 'click' : null;
|
|
21
|
+
/**
|
|
22
|
+
* A visit link. It renders `a`, or `button` for a non-GET method, and intercepts plain clicks to visit through the
|
|
23
|
+
* router. Props are read where they are used, so a prop that changes after mount takes effect on the next event.
|
|
24
|
+
*/
|
|
25
|
+
export default function Link(props) {
|
|
26
|
+
const [visitsInFlight, setVisitsInFlight] = createSignal(0);
|
|
27
|
+
let hoverTimer;
|
|
28
|
+
const request = createMemo(() => {
|
|
29
|
+
const method = isUrlMethodPair(props.href) ? props.href.method : (props.method ?? 'get').toLowerCase();
|
|
30
|
+
const [url, data] = mergeDataIntoQueryString(method, isUrlMethodPair(props.href) ? props.href.url : (props.href ?? ''), props.data ?? {}, props.queryStringArrayFormat ?? 'brackets');
|
|
31
|
+
return { method, url, data };
|
|
32
|
+
});
|
|
33
|
+
const tag = createMemo(() => {
|
|
34
|
+
const as = props.as ?? 'a';
|
|
35
|
+
if (typeof as === 'string' && as.toLowerCase() === 'a') {
|
|
36
|
+
return request().method === 'get' ? 'a' : 'button';
|
|
37
|
+
}
|
|
38
|
+
return as;
|
|
39
|
+
});
|
|
40
|
+
const Tag = dynamic(tag);
|
|
41
|
+
const tagAttributes = createMemo(() => {
|
|
42
|
+
const current = tag();
|
|
43
|
+
if (current === 'button') {
|
|
44
|
+
return { type: 'button' };
|
|
45
|
+
}
|
|
46
|
+
return current === 'a' || typeof current !== 'string' ? { href: request().url } : {};
|
|
47
|
+
});
|
|
48
|
+
const modes = createMemo(() => toModes(props.prefetch));
|
|
49
|
+
const trigger = createMemo(() => toTrigger(modes()));
|
|
50
|
+
const prefetchOnMount = createMemo(() => modes().includes('mount'));
|
|
51
|
+
const pageComponent = () => {
|
|
52
|
+
if (props.component) {
|
|
53
|
+
return props.component;
|
|
54
|
+
}
|
|
55
|
+
return props.instant && isUrlMethodPair(props.href) ? resolveUrlMethodPairComponent(props.href) : null;
|
|
56
|
+
};
|
|
57
|
+
const baseOptions = () => ({
|
|
58
|
+
data: request().data,
|
|
59
|
+
method: request().method,
|
|
60
|
+
preserveScroll: props.preserveScroll ?? false,
|
|
61
|
+
preserveState: props.preserveState ?? request().method !== 'get',
|
|
62
|
+
preserveUrl: props.preserveUrl ?? false,
|
|
63
|
+
replace: props.replace ?? false,
|
|
64
|
+
only: props.only ?? [],
|
|
65
|
+
except: props.except ?? [],
|
|
66
|
+
headers: props.headers ?? {},
|
|
67
|
+
async: props.async ?? false,
|
|
68
|
+
component: pageComponent(),
|
|
69
|
+
pageProps: props.pageProps ?? null,
|
|
70
|
+
});
|
|
71
|
+
const visit = () => router.visit(request().url, {
|
|
72
|
+
...baseOptions(),
|
|
73
|
+
viewTransition: props.viewTransition ?? false,
|
|
74
|
+
onCancelToken: props.onCancelToken,
|
|
75
|
+
onBefore: props.onBefore,
|
|
76
|
+
onStart: (pending) => {
|
|
77
|
+
setVisitsInFlight((count) => count + 1);
|
|
78
|
+
props.onStart?.(pending);
|
|
79
|
+
},
|
|
80
|
+
onProgress: props.onProgress,
|
|
81
|
+
onFinish: (finished) => {
|
|
82
|
+
setVisitsInFlight((count) => count - 1);
|
|
83
|
+
props.onFinish?.(finished);
|
|
84
|
+
},
|
|
85
|
+
onCancel: props.onCancel,
|
|
86
|
+
onSuccess: props.onSuccess,
|
|
87
|
+
onError: props.onError,
|
|
88
|
+
onHttpException: props.onHttpException,
|
|
89
|
+
onNetworkError: props.onNetworkError,
|
|
90
|
+
onFlash: props.onFlash,
|
|
91
|
+
});
|
|
92
|
+
// With only `click` to prefetch, the response is meant for the visit that follows, so it is not cached.
|
|
93
|
+
const cacheFor = () => {
|
|
94
|
+
const given = props.cacheFor ?? 0;
|
|
95
|
+
if (given !== 0) {
|
|
96
|
+
return given;
|
|
97
|
+
}
|
|
98
|
+
return modes().length === 1 && modes()[0] === 'click' ? 0 : config.get('prefetch.cacheFor');
|
|
99
|
+
};
|
|
100
|
+
const prefetch = () => router.prefetch(request().url, { ...baseOptions(), onPrefetching: props.onPrefetching, onPrefetched: props.onPrefetched }, { cacheFor: cacheFor(), cacheTags: props.cacheTags ?? [] });
|
|
101
|
+
const cancelHover = () => clearTimeout(hoverTimer);
|
|
102
|
+
const startHover = () => {
|
|
103
|
+
cancelHover();
|
|
104
|
+
hoverTimer = window.setTimeout(prefetch, config.get('prefetch.hoverDelay'));
|
|
105
|
+
};
|
|
106
|
+
// A `click` link visits on release, so that the prefetch started on press has a head start.
|
|
107
|
+
// Its click only has to be swallowed. Every other link visits on the click itself.
|
|
108
|
+
const click = (event) => {
|
|
109
|
+
cancelHover();
|
|
110
|
+
props.onClick?.(event);
|
|
111
|
+
if (shouldIntercept(event)) {
|
|
112
|
+
event.preventDefault();
|
|
113
|
+
if (trigger() !== 'click') {
|
|
114
|
+
visit();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const pressOrRelease = (act) => (event) => {
|
|
119
|
+
if (shouldIntercept(event)) {
|
|
120
|
+
event.preventDefault();
|
|
121
|
+
act();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const keyPressOrRelease = (act) => (event) => {
|
|
125
|
+
if (shouldNavigate(event)) {
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
act();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const pressHandlers = {
|
|
131
|
+
onMouseDown: pressOrRelease(prefetch),
|
|
132
|
+
onKeyDown: keyPressOrRelease(prefetch),
|
|
133
|
+
};
|
|
134
|
+
const releaseHandlers = {
|
|
135
|
+
onMouseUp: pressOrRelease(visit),
|
|
136
|
+
onKeyUp: keyPressOrRelease(visit),
|
|
137
|
+
};
|
|
138
|
+
const hoverHandlers = { onMouseEnter: startHover, onMouseLeave: cancelHover };
|
|
139
|
+
const visitTriggerHandlers = (kind) => kind === 'click' ? { ...releaseHandlers, onClick: click } : { onClick: click };
|
|
140
|
+
const prefetchTriggerHandlers = (kind) => kind === 'hover' ? hoverHandlers : kind === 'click' ? pressHandlers : {};
|
|
141
|
+
const handlers = createMemo(() => ({
|
|
142
|
+
...visitTriggerHandlers(trigger()),
|
|
143
|
+
...prefetchTriggerHandlers(trigger()),
|
|
144
|
+
}));
|
|
145
|
+
createEffect(prefetchOnMount, (onMount) => {
|
|
146
|
+
if (onMount) {
|
|
147
|
+
const timer = window.setTimeout(prefetch);
|
|
148
|
+
return () => clearTimeout(timer);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
onSettled(() => cancelHover);
|
|
152
|
+
return (<Tag {...omit(props, (key) => linkOnlyProps.has(key))} {...tagAttributes()} {...handlers()} data-loading={visitsInFlight() > 0 ? '' : undefined}>
|
|
153
|
+
{props.children}
|
|
154
|
+
</Tag>);
|
|
155
|
+
}
|
package/dist/Render.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type JSX } from '@solidjs/web';
|
|
2
|
+
import type { RenderNode } from './types';
|
|
3
|
+
type NodeProps = {
|
|
4
|
+
node: () => RenderNode;
|
|
5
|
+
};
|
|
6
|
+
/** Renders one level of the tree: a layout around its children, or the page. */
|
|
7
|
+
export default function Render(props: NodeProps): JSX.Element;
|
|
8
|
+
export {};
|
package/dist/Render.jsx
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { dynamic } from '@solidjs/web';
|
|
2
|
+
import { createMemo, For, Show } from 'solid-js';
|
|
3
|
+
const componentOf = (node) => node.component;
|
|
4
|
+
/** A layout. It stays mounted while its component is the same function, and reads its props from the latest node. */
|
|
5
|
+
function Branch(props) {
|
|
6
|
+
const Layout = dynamic(() => componentOf(props.node()));
|
|
7
|
+
return (<Layout {...props.node().props}>
|
|
8
|
+
<For each={props.node().children} keyed={false}>
|
|
9
|
+
{(child) => <Render node={child}/>}
|
|
10
|
+
</For>
|
|
11
|
+
</Layout>);
|
|
12
|
+
}
|
|
13
|
+
/** The page. A new key (a visit without `preserveState`) re-creates it. */
|
|
14
|
+
function Leaf(props) {
|
|
15
|
+
const Page = dynamic(() => componentOf(props.node()));
|
|
16
|
+
const identity = createMemo(() => ({ key: props.node().key }), { equals: (a, b) => a.key === b.key });
|
|
17
|
+
return (<Show when={identity()} keyed>
|
|
18
|
+
<Page {...props.node().props}/>
|
|
19
|
+
</Show>);
|
|
20
|
+
}
|
|
21
|
+
/** Renders one level of the tree: a layout around its children, or the page. */
|
|
22
|
+
export default function Render(props) {
|
|
23
|
+
const isLeaf = createMemo(() => props.node().children.length === 0);
|
|
24
|
+
return (<Show when={isLeaf()} fallback={<Branch node={props.node}/>}>
|
|
25
|
+
<Leaf node={props.node}/>
|
|
26
|
+
</Show>);
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { JSX } from '@solidjs/web';
|
|
2
|
+
import { type Slot } from './slot';
|
|
3
|
+
export interface WhenMountedProps {
|
|
4
|
+
children: Slot<undefined>;
|
|
5
|
+
fallback?: Slot<undefined>;
|
|
6
|
+
}
|
|
7
|
+
/** Renders `fallback` on the server and while hydrating, and `children` everywhere else, remounts included. */
|
|
8
|
+
export default function WhenMounted(props: WhenMountedProps): JSX.Element;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createSignal, isHydrating, onSettled, Show } from 'solid-js';
|
|
2
|
+
import { renderSlot } from './slot';
|
|
3
|
+
/** Renders `fallback` on the server and while hydrating, and `children` everywhere else, remounts included. */
|
|
4
|
+
export default function WhenMounted(props) {
|
|
5
|
+
const [mounted, setMounted] = createSignal(typeof window !== 'undefined' && !isHydrating());
|
|
6
|
+
onSettled(() => {
|
|
7
|
+
setMounted(true);
|
|
8
|
+
});
|
|
9
|
+
return (<Show when={mounted()} fallback={renderSlot(props.fallback, undefined)}>
|
|
10
|
+
{renderSlot(props.children, undefined)}
|
|
11
|
+
</Show>);
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ReloadOptions } from '@inertiajs/core';
|
|
2
|
+
import { type JSX } from '@solidjs/web';
|
|
3
|
+
import { type Slot } from './slot';
|
|
4
|
+
export interface WhenVisibleSlotProps {
|
|
5
|
+
/** Getter: true while a reload this component started is in flight. */
|
|
6
|
+
readonly fetching: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface WhenVisibleProps {
|
|
9
|
+
/** Plain children, or a render function receiving `{ fetching }`. */
|
|
10
|
+
children: Slot<WhenVisibleSlotProps>;
|
|
11
|
+
fallback: JSX.Element | (() => JSX.Element);
|
|
12
|
+
data?: string | string[];
|
|
13
|
+
params?: ReloadOptions;
|
|
14
|
+
buffer?: number;
|
|
15
|
+
as?: string;
|
|
16
|
+
always?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Renders `fallback` until it scrolls into view, then reloads `data` and renders its children. With `always` it
|
|
20
|
+
* reloads every time it becomes visible, and stays in its wrapper element.
|
|
21
|
+
*/
|
|
22
|
+
export default function WhenVisible(props: WhenVisibleProps): JSX.Element;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { router } from '@inertiajs/core';
|
|
2
|
+
import { dynamic } from '@solidjs/web';
|
|
3
|
+
import { get } from 'es-toolkit/compat';
|
|
4
|
+
import { createEffect, createMemo, createSignal, Show } from 'solid-js';
|
|
5
|
+
import { renderSlot } from './slot';
|
|
6
|
+
import usePage from './usePage';
|
|
7
|
+
/**
|
|
8
|
+
* Renders `fallback` until it scrolls into view, then reloads `data` and renders its children. With `always` it
|
|
9
|
+
* reloads every time it becomes visible, and stays in its wrapper element.
|
|
10
|
+
*/
|
|
11
|
+
export default function WhenVisible(props) {
|
|
12
|
+
const page = usePage();
|
|
13
|
+
const keys = createMemo(() => (props.data ? (Array.isArray(props.data) ? props.data : [props.data]) : []));
|
|
14
|
+
const [loaded, setLoaded] = createSignal(() => keys().length > 0 && keys().every((key) => get(page.props, key) !== undefined));
|
|
15
|
+
const [fetching, setFetching] = createSignal(false);
|
|
16
|
+
let inFlight = false;
|
|
17
|
+
// A signal, so that a changed `as`, which makes `dynamic()` build a new element, moves the observer to it. The ref
|
|
18
|
+
// callback runs while the component renders, which is an owned scope.
|
|
19
|
+
const [wrapper, setWrapper] = createSignal(undefined, { ownedWrite: true });
|
|
20
|
+
const reloadParams = () => ({
|
|
21
|
+
preserveErrors: true,
|
|
22
|
+
...props.params,
|
|
23
|
+
...(props.data ? { only: keys() } : {}),
|
|
24
|
+
});
|
|
25
|
+
createEffect(() => [wrapper(), props.always ?? false, loaded(), props.buffer], ([element, always, isLoaded, buffer]) => {
|
|
26
|
+
if (!element || (isLoaded && !always)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const observer = new IntersectionObserver((entries) => {
|
|
30
|
+
if (!entries[0].isIntersecting || inFlight || (!always && isLoaded)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
inFlight = true;
|
|
34
|
+
setFetching(true);
|
|
35
|
+
const params = reloadParams();
|
|
36
|
+
router.reload({
|
|
37
|
+
...params,
|
|
38
|
+
onStart: (visit) => {
|
|
39
|
+
inFlight = true;
|
|
40
|
+
setFetching(true);
|
|
41
|
+
params.onStart?.(visit);
|
|
42
|
+
},
|
|
43
|
+
onFinish: (visit) => {
|
|
44
|
+
setLoaded(true);
|
|
45
|
+
inFlight = false;
|
|
46
|
+
setFetching(false);
|
|
47
|
+
params.onFinish?.(visit);
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
}, { rootMargin: `${buffer || 0}px` });
|
|
51
|
+
observer.observe(element);
|
|
52
|
+
return () => observer.disconnect();
|
|
53
|
+
});
|
|
54
|
+
const slot = {
|
|
55
|
+
get fetching() {
|
|
56
|
+
return fetching();
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
const Wrapper = dynamic(() => props.as ?? 'div');
|
|
60
|
+
return (<Show when={props.always || !loaded()} fallback={renderSlot(props.children, slot)}>
|
|
61
|
+
<Wrapper ref={(element) => setWrapper(element)}>
|
|
62
|
+
<Show when={loaded()} fallback={renderSlot(props.fallback, slot)}>
|
|
63
|
+
{renderSlot(props.children, slot)}
|
|
64
|
+
</Show>
|
|
65
|
+
</Wrapper>
|
|
66
|
+
</Show>);
|
|
67
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type CreateInertiaAppOptions, type CreateInertiaAppOptionsForSSR, type InertiaAppSSRResponse, type LiveAppOption, type Page, type PageProps, type SharedPageProps } from '@inertiajs/core';
|
|
2
|
+
import { type JSX } from '@solidjs/web';
|
|
3
|
+
import { type Component } from 'solid-js';
|
|
4
|
+
import { type InertiaAppProps } from './App';
|
|
5
|
+
import type { ResolvedComponent } from './types';
|
|
6
|
+
type Shared = PageProps & SharedPageProps;
|
|
7
|
+
/** `renderToString` and `await renderToStream(...)` both fit: the adapter awaits the result. */
|
|
8
|
+
type Render = (fn: () => JSX.Element, options?: {
|
|
9
|
+
nonce?: string;
|
|
10
|
+
}) => string | PromiseLike<string>;
|
|
11
|
+
type Resolved = ResolvedComponent | {
|
|
12
|
+
default: ResolvedComponent;
|
|
13
|
+
};
|
|
14
|
+
type Resolve<S extends PageProps> = (name: string, page?: Page<S>) => Resolved | Promise<Resolved>;
|
|
15
|
+
type Setup<S extends PageProps, El> = {
|
|
16
|
+
el: El;
|
|
17
|
+
App: Component<InertiaAppProps<S>>;
|
|
18
|
+
props: InertiaAppProps<S>;
|
|
19
|
+
};
|
|
20
|
+
/** Server rendering through `createServer`: the caller passes the page and Solid's `renderToString`. */
|
|
21
|
+
type ServerOptions<S extends PageProps> = CreateInertiaAppOptionsForSSR<S, Resolve<S>, Setup<S, null>, JSX.Element, object> & {
|
|
22
|
+
render: Render;
|
|
23
|
+
live?: undefined;
|
|
24
|
+
withApp?: undefined;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Browser mounting through `setup`, or the vite plugin's zero-config mode. `withApp` wraps the app on both
|
|
28
|
+
* sides; `app` is a thunk so Solid creates the tree inside the wrapper: `<Ctx value={v}>{app()}</Ctx>`.
|
|
29
|
+
*/
|
|
30
|
+
type BrowserOptions<S extends PageProps> = CreateInertiaAppOptions<Resolve<S>, Setup<S, HTMLElement>, void, object> & {
|
|
31
|
+
page?: Page<S>;
|
|
32
|
+
render?: undefined;
|
|
33
|
+
withApp?: (app: () => JSX.Element, context: {
|
|
34
|
+
ssr: boolean;
|
|
35
|
+
page: Page<S>;
|
|
36
|
+
}) => JSX.Element;
|
|
37
|
+
} & LiveAppOption;
|
|
38
|
+
export default function createInertiaApp<S extends PageProps = Shared>(options: ServerOptions<S>): Promise<InertiaAppSSRResponse>;
|
|
39
|
+
export default function createInertiaApp<S extends PageProps = Shared>(options?: BrowserOptions<S>): Promise<void | ((page: Page<S>, render: Render) => Promise<InertiaAppSSRResponse>)>;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { buildSSRBody, config, configureLive, exposeInterceptors, getInitialPageFromDOM, http as httpModule, router, setupProgress, } from '@inertiajs/core';
|
|
2
|
+
import { generateHydrationScript, hydrate, render as renderToDom } from '@solidjs/web';
|
|
3
|
+
import { createComponent } from 'solid-js';
|
|
4
|
+
import App, {} from './App';
|
|
5
|
+
export default async function createInertiaApp({ id = 'app', resolve, setup, title, progress = {}, page, render, defaults = {}, nonce, http, layout, live, serverHead, withApp, dev = !!import.meta.env?.DEV, } = {}) {
|
|
6
|
+
config.replace(defaults);
|
|
7
|
+
if (nonce) {
|
|
8
|
+
config.set('nonce', nonce);
|
|
9
|
+
}
|
|
10
|
+
if (http) {
|
|
11
|
+
httpModule.setClient(http);
|
|
12
|
+
}
|
|
13
|
+
if (live) {
|
|
14
|
+
configureLive(live);
|
|
15
|
+
}
|
|
16
|
+
if (dev) {
|
|
17
|
+
exposeInterceptors();
|
|
18
|
+
}
|
|
19
|
+
const isServer = typeof window === 'undefined';
|
|
20
|
+
const resolveComponent = (name, page) => Promise.resolve(resolve(name, page)).then((module) => (typeof module === 'function' ? module : module.default));
|
|
21
|
+
/** Renders `page` to `{ head, body }`. `renderApp` is the caller's `render`: it gets a thunk, so Solid builds the tree inside its root. */
|
|
22
|
+
const renderOnServer = async (page, renderApp) => {
|
|
23
|
+
let head = [];
|
|
24
|
+
const props = {
|
|
25
|
+
initialPage: page,
|
|
26
|
+
initialComponent: await resolveComponent(page.component, page),
|
|
27
|
+
resolveComponent,
|
|
28
|
+
titleCallback: title,
|
|
29
|
+
onHeadUpdate: (elements) => (head = elements),
|
|
30
|
+
defaultLayout: layout,
|
|
31
|
+
serverHead,
|
|
32
|
+
};
|
|
33
|
+
const app = () => createComponent(App, props);
|
|
34
|
+
const html = await renderApp(() => setup
|
|
35
|
+
? setup({ el: null, App, props })
|
|
36
|
+
: withApp
|
|
37
|
+
? withApp(app, { ssr: true, page })
|
|
38
|
+
: app(), { nonce });
|
|
39
|
+
// `hydrate` needs Solid's bootstrap (`_$HY`) in the document before the app script runs
|
|
40
|
+
return { head: [generateHydrationScript({ nonce }), ...head], body: buildSSRBody(id, page, html) };
|
|
41
|
+
};
|
|
42
|
+
if (isServer) {
|
|
43
|
+
// The Vite plugin's SSR transform calls createInertiaApp without a page and renders later
|
|
44
|
+
return !page && !render ? renderOnServer : renderOnServer(page, render);
|
|
45
|
+
}
|
|
46
|
+
const initialPage = page ?? getInitialPageFromDOM(id);
|
|
47
|
+
const [initialComponent] = await Promise.all([
|
|
48
|
+
resolveComponent(initialPage.component, initialPage),
|
|
49
|
+
router.decryptHistory().catch(() => { }),
|
|
50
|
+
]);
|
|
51
|
+
const props = {
|
|
52
|
+
initialPage,
|
|
53
|
+
initialComponent,
|
|
54
|
+
resolveComponent,
|
|
55
|
+
titleCallback: title,
|
|
56
|
+
defaultLayout: layout,
|
|
57
|
+
serverHead,
|
|
58
|
+
};
|
|
59
|
+
const el = document.getElementById(id);
|
|
60
|
+
if (setup) {
|
|
61
|
+
;
|
|
62
|
+
setup({ el, App, props });
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const app = () => createComponent(App, props);
|
|
66
|
+
const wrapped = withApp ? () => withApp(app, { ssr: false, page: initialPage }) : app;
|
|
67
|
+
if (el.hasAttribute('data-server-rendered')) {
|
|
68
|
+
hydrate(wrapped, el);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
renderToDom(wrapped, el);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (progress) {
|
|
75
|
+
setupProgress(progress);
|
|
76
|
+
}
|
|
77
|
+
}
|
package/dist/echo.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { LiveOptions } from '@inertiajs/core';
|
|
2
|
+
import { type EchoOptions as CoreEchoOptions } from '@inertiajs/core/echo';
|
|
3
|
+
/** `@laravel/echo-solid` does not exist, so there is no bound Echo instance to fall back on: `resolve` is required. */
|
|
4
|
+
export type EchoOptions = CoreEchoOptions & Required<Pick<CoreEchoOptions, 'resolve'>>;
|
|
5
|
+
/** Delivers live prop updates over the Laravel Echo instance that `resolve` returns. */
|
|
6
|
+
export declare const echo: (options: EchoOptions) => LiveOptions;
|
package/dist/echo.js
ADDED