denoland / fresh

The next-gen web framework.
https://fresh.deno.dev
MIT License
12.48k stars 644 forks source link

feat: Allow partials on every element #1952

Open marvinhagemeister opened 1 year ago

marvinhagemeister commented 1 year ago

There is a strong desire to expand support for partials to more scenarios. Fresh 1.5 shipped with partial support for navigation and https://github.com/denoland/fresh/pull/1930 landed recently in main that enables it for form elements. But there are lots of scenarios where you want to trigger a partial update based on an event. Like when changing the value of a select in a form or something. We need a way to allow that.

Something I was pondering about is whether we should introduce some sort of object syntax for events.

<select onChange={{ partial: "/foo/bar" }}>...</select>

// or maybe abstracted via a function
<select onChange={partialTrigger("/foo/bar")}>...</select>

I haven't yet decided what it should look like and open to ideas.

adamgreg commented 1 year ago

The abstracted via function idea seems most straightforward to me, and let's the user put extra JS in the handler if needed.

marvinhagemeister commented 1 year ago

@adamgreg The main thing I'm not sure how to solve is how would you send these functions to the browser when you add them outside of an island. Only thing I can think of is to serialize the function but that breaks if it captures variables via a closure.

adamgreg commented 1 year ago

Oh, that's a tricky one.

A general solution to serialising little handler functions to the client would be useful for other cases, if it's possible. Interestingly, Chrome DevTools lets you inspect a function's closure using console.dir() (but Firefox doesn't). Maybe it's something that could be exposed via a new Deno API?

osddeitf commented 11 months ago

@marvinhagemeister I don't really understand what you have said about "serialize the function" and "that breaks if it captures variables via a closure".

But there is my idea: