feat: add _freshIndicator support for partial form submits (#3753)
## Summary - Extends the `_freshIndicator` loading signal to form submissions, not just anchor clicks - Checks the submitter element first, falling back to the form element - Wraps the fetch in try/finally to ensure the indicator resets on success or failure - Adds 3 browser tests: indicator on button, indicator on form, and submitter-over-form precedence Closes #3504 --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
af3f30dbaa
commit
28cce5f607
@@ -208,6 +208,66 @@ Partial updates can be animated using the browser's
|
||||
alongside `f-client-nav` to enable smooth animated transitions between pages
|
||||
with zero JavaScript animation code.
|
||||
|
||||
## Loading indicators
|
||||
|
||||
When a partial request is in flight, you may want to show a loading spinner or
|
||||
disable a button. Fresh supports this through the `_freshIndicator` property.
|
||||
|
||||
Attach an object with a `value` property to any element that triggers a partial
|
||||
navigation. Fresh will set `value` to `true` when the request starts and back to
|
||||
`false` when it completes (or fails).
|
||||
|
||||
```tsx
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
function NavLink() {
|
||||
const loading = useSignal(false);
|
||||
|
||||
return (
|
||||
<a
|
||||
href="/next-page"
|
||||
f-partial="/partials/next-page"
|
||||
ref={(el) => {
|
||||
if (el) el._freshIndicator = loading;
|
||||
}}
|
||||
>
|
||||
{loading.value ? "Loading..." : "Go"}
|
||||
</a>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
This works for links, forms, and submit buttons. For form submissions, Fresh
|
||||
checks the submitter element (e.g. the clicked button) first, then falls back to
|
||||
the form element itself. This lets you show per-button indicators when a form
|
||||
has multiple submit buttons.
|
||||
|
||||
```tsx
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
function MyForm() {
|
||||
const saving = useSignal(false);
|
||||
|
||||
return (
|
||||
<form action="/save" f-partial="/partials/save">
|
||||
{/* indicator is on the button, not the form */}
|
||||
<button
|
||||
type="submit"
|
||||
ref={(el) => {
|
||||
if (el) el._freshIndicator = saving;
|
||||
}}
|
||||
>
|
||||
{saving.value ? "Saving..." : "Save"}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
> [info]: Any object with a mutable `value` property works — Preact signals are
|
||||
> the most convenient choice because they automatically re-render the component
|
||||
> when the value changes.
|
||||
|
||||
## Bypassing or disabling Partials
|
||||
|
||||
If you want to exempt a particular element from triggering a partial request
|
||||
|
||||
Reference in New Issue
Block a user