@ilha/void 0.1.2 → 0.1.3

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/README.md CHANGED
@@ -57,7 +57,7 @@ Scaffold an app with `npx giget@latest gh:ilhajs/ilha/templates/void`.
57
57
 
58
58
  | Import path | Exports |
59
59
  | --- | --- |
60
- | `@ilha/void` | `Link`, `Form`, `useForm`, `useIslandForm`, `useParams`, `useShared`, `useNavigation`, `useRouter`, `action` |
60
+ | `@ilha/void` | `Link`, `useLink`, `Form`, `useForm`, `useIslandForm`, `useParams`, `useShared`, `useNavigation`, `useRouter`, `action` |
61
61
  | `@ilha/void/plugin` | `voidIlha()` |
62
62
 
63
63
  Docs: [ilha.build/guide/void](https://ilha.build/guide/void)
package/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@ export { Form } from "./form";
7
7
  export type { FormProps } from "./form";
8
8
  export type { FormFields } from "./form-state";
9
9
  export { useNavigation, usePageErrors, useParams, useRouter, useShared, } from "./hooks";
10
+ export { useLink } from "./use-link";
11
+ export type { UseLink, UseLinkOptions, UseLinkProps } from "./use-link";
10
12
  export { Link } from "./link";
11
13
  export type { LinkMethod, LinkProps, PrefetchStrategy } from "./link";
12
14
  export { useForm } from "./use-form";
package/dist/index.js CHANGED
@@ -56,6 +56,17 @@ const strategiesOf = ({ prefetch, reloadDocument }) => {
56
56
  return isStrategyList(prefetch) ? prefetch : [prefetch];
57
57
  };
58
58
  const isSameOrigin = (href) => new URL(href, window.location.origin).origin === window.location.origin;
59
+ /**
60
+ * Whether a click should navigate through the Void Router. Call it after the
61
+ * caller's own `onclick`, since a prevented event falls back to the browser.
62
+ * Modifier keys, non-left buttons, non-`_self` targets, `download` links,
63
+ * cross-origin hrefs, and a missing router all fall back to the browser.
64
+ * Shared by `Link` and `useLink` so both apply exactly the same rules.
65
+ */
66
+ const shouldInterceptClick = (event, { reloadDocument, router, target }) => {
67
+ if (reloadDocument || !router || event.defaultPrevented || isModifiedEvent(event) || event.currentTarget instanceof Element && event.currentTarget.hasAttribute("download") || !isSameOrigin(target)) return false;
68
+ return true;
69
+ };
59
70
  const unlessPrevented = (handler, then) => (event) => {
60
71
  handler?.(event);
61
72
  if (!event.defaultPrevented) then();
@@ -87,7 +98,11 @@ const Link = (props) => {
87
98
  });
88
99
  const handleClick = (event) => {
89
100
  onclick?.(event);
90
- if (reloadDocument || !router || event.defaultPrevented || isModifiedEvent(event) || event.currentTarget instanceof Element && event.currentTarget.hasAttribute("download") || !isSameOrigin(target)) return;
101
+ if (!router || !shouldInterceptClick(event, {
102
+ reloadDocument,
103
+ router,
104
+ target
105
+ })) return;
91
106
  event.preventDefault();
92
107
  let cancelled = false;
93
108
  onNavigate?.({ preventDefault: () => {
@@ -146,6 +161,53 @@ const Link = (props) => {
146
161
  }, children);
147
162
  };
148
163
 
164
+ //#endregion
165
+ //#region src/use-link.ts
166
+ /**
167
+ * Bind router navigation to a native `<a>` without rendering a component.
168
+ *
169
+ * Call it once at the top level of a component, then spread the result onto
170
+ * a plain anchor: `<a {...link("/users/7")}>Ada</a>`. Clicks follow exactly
171
+ * `Link`'s GET rules (modifier keys, `download`, cross-origin, and prevented
172
+ * clicks fall back to the browser).
173
+ *
174
+ * GET only: there is no prefetch and no `data`. For those, use `Link`.
175
+ *
176
+ * It exists so a link can sit where CSS uses child combinators
177
+ * (`ul.menu > li > a`). Every component, `Link` included, paints into a
178
+ * `<span data-ilha-slot style="display:contents">` host, which breaks those
179
+ * selectors; a bound native `<a>` stays a direct child of its parent.
180
+ */
181
+ const useLink = () => {
182
+ const router = context(VoidContext)()?.router ?? null;
183
+ return (href, options = {}) => {
184
+ const { onNavigate, onclick, preserveScroll, preserveState, replace, viewTransition } = options;
185
+ return {
186
+ href,
187
+ onclick: (event) => {
188
+ onclick?.(event);
189
+ if (!shouldInterceptClick(event, {
190
+ router,
191
+ target: href
192
+ })) return;
193
+ event.preventDefault();
194
+ let cancelled = false;
195
+ onNavigate?.({ preventDefault: () => {
196
+ cancelled = true;
197
+ } });
198
+ if (cancelled) return;
199
+ router?.visit(href, {
200
+ method: "GET",
201
+ preserveScroll,
202
+ preserveState,
203
+ replace,
204
+ viewTransition
205
+ });
206
+ }
207
+ };
208
+ };
209
+ };
210
+
149
211
  //#endregion
150
212
  //#region src/form-state.ts
151
213
  const RECENT_SUCCESS_MS = 2e3;
@@ -339,4 +401,4 @@ const useIslandForm = (defaults) => {
339
401
  };
340
402
 
341
403
  //#endregion
342
- export { Form, Link, VoidContext, action, setActionRouter, useForm, useIslandForm, useNavigation, usePageErrors, useParams, useRouter, useShared };
404
+ export { Form, Link, VoidContext, action, setActionRouter, useForm, useIslandForm, useLink, useNavigation, usePageErrors, useParams, useRouter, useShared };
package/dist/link.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { View } from "ilha";
2
2
  import type { JSX } from "ilha/jsx-runtime";
3
- import type { PrefetchOptions } from "void/pages-client";
3
+ import type { PrefetchOptions, VoidRouter } from "void/pages-client";
4
4
  import type { FormValues } from "./types";
5
5
  export type PrefetchStrategy = "hover" | "click" | "mount" | "visible";
6
6
  export type LinkMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
@@ -32,6 +32,22 @@ interface LinkOptions {
32
32
  }
33
33
  /** Link options plus `<a>` attributes, which pass through to the element. */
34
34
  export type LinkProps = LinkOptions & Omit<JSX.IntrinsicElements["a"], keyof LinkOptions>;
35
+ export interface RouterClickGate {
36
+ /** The Void Router; `null` on island pages, which navigate with full loads. */
37
+ router: VoidRouter | null;
38
+ /** The resolved href the click would navigate to. */
39
+ target: string;
40
+ /** Skip the Void Router and load the document. */
41
+ reloadDocument?: boolean;
42
+ }
43
+ /**
44
+ * Whether a click should navigate through the Void Router. Call it after the
45
+ * caller's own `onclick`, since a prevented event falls back to the browser.
46
+ * Modifier keys, non-left buttons, non-`_self` targets, `download` links,
47
+ * cross-origin hrefs, and a missing router all fall back to the browser.
48
+ * Shared by `Link` and `useLink` so both apply exactly the same rules.
49
+ */
50
+ export declare const shouldInterceptClick: (event: MouseEvent, { reloadDocument, router, target }: RouterClickGate) => boolean;
35
51
  /**
36
52
  * Navigate through the Void Router. GET links render an `<a>`; other methods
37
53
  * render a `<button>` that submits `data`. Outside a regular page (island
@@ -0,0 +1,31 @@
1
+ export interface UseLinkOptions {
2
+ replace?: boolean;
3
+ preserveScroll?: boolean;
4
+ preserveState?: boolean;
5
+ viewTransition?: boolean;
6
+ onNavigate?: (event: {
7
+ preventDefault: () => void;
8
+ }) => void;
9
+ onclick?: (event: MouseEvent) => void;
10
+ }
11
+ export interface UseLinkProps {
12
+ href: string;
13
+ onclick: (event: MouseEvent) => void;
14
+ }
15
+ export type UseLink = (href: string, options?: UseLinkOptions) => UseLinkProps;
16
+ /**
17
+ * Bind router navigation to a native `<a>` without rendering a component.
18
+ *
19
+ * Call it once at the top level of a component, then spread the result onto
20
+ * a plain anchor: `<a {...link("/users/7")}>Ada</a>`. Clicks follow exactly
21
+ * `Link`'s GET rules (modifier keys, `download`, cross-origin, and prevented
22
+ * clicks fall back to the browser).
23
+ *
24
+ * GET only: there is no prefetch and no `data`. For those, use `Link`.
25
+ *
26
+ * It exists so a link can sit where CSS uses child combinators
27
+ * (`ul.menu > li > a`). Every component, `Link` included, paints into a
28
+ * `<span data-ilha-slot style="display:contents">` host, which breaks those
29
+ * selectors; a bound native `<a>` stays a direct child of its parent.
30
+ */
31
+ export declare const useLink: () => UseLink;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ilha/void",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Ilha pages, server rendering, islands, forms, and navigation for Void",
5
5
  "keywords": [
6
6
  "frontend",