···11----
22-title: "Astro and HTMX: An Interesting Stack"
33-description: Strap a horse on a rocket and use Astro Partials to stream UI components via HTMX.
44-date: "2024-09-10"
55-draft: true
66-link: https://github.com/zeucapua/
77----
88-99-### What is HTMX?
1010-1111-### Astro Partials
1212-1313-### Using HTMX Attributes
1414-1515-### Forms and Values via Astro Partials
1616-1717-### Streaming in Client Components as Astro Islands
+100
src/content/blog/rash-stack.md
···11+---
22+title: "The RASH Stack: React, Astro, Svelte, HTMX"
33+description: Strap a horse on a rocket and use Astro Partials to stream UI components via HTMX.
44+date: "2024-09-10"
55+draft: true
66+link: https://github.com/zeucapua/
77+---
88+99+## The Project
1010+1111+I'm building a Notion-like note taking system that'll (probably) make me more productive with as
1212+little fluff and features as possible. Each block can be added, edited, and deleted freely, with
1313+the full page data is saved locally.
1414+1515+For the features to demo the RASH stack, we are making:
1616+- Plain text (Astro + HTMX)
1717+- Checkbox (Svelte)
1818+- Whiteboard (React via tldraw library)
1919+2020+## What is HTMX?
2121+2222+The basic idea behind HTMX is that APIs should return HTML that shows the updated part of the page
2323+after a request. For example, let's make a button that adds our feature, simple plain text input:
2424+2525+```html
2626+<h1>Hodgepodge App</h1>
2727+<ul id="blocks">
2828+2929+</ul>
3030+3131+<button
3232+ hx-post="/addTextBlock"
3333+ hx-target="#blocks"
3434+ hx-swap="beforeend"
3535+>
3636+ Text
3737+</button>
3838+```
3939+4040+The button has a few new attributes to use HTMX: `hx-post` specifies a `POST` API request
4141+at the endpoint `/addTextBlock`; `hx-target` is to determine what element we want to update;
4242+`hx-swap` tells us how we should handle the HTML after the response, in this case adding the
4343+response before the button. Now the endpoint can return HTML like so...
4444+4545+```html
4646+<li><textarea /></li>
4747+```
4848+4949+And on the button press, the initial page will now be:
5050+5151+```html
5252+<h1>Hodgepodge App</h1>
5353+<ul id="blocks">
5454+ <li><textarea /></li>
5555+</ul>
5656+5757+<button
5858+ hx-post="/addTextBlock"
5959+ hx-target="#blocks"
6060+ hx-swap="beforeend"
6161+>
6262+ Text
6363+</button>
6464+```
6565+6666+While we will be using more attributes during the rest of the blog, there are plenty more
6767+I can't possibly go over. To find more information on the list of attributes and HTMX in
6868+general, go to [htmx.org](https://htmx.org) or read their book
6969+[Hypermedia Systems](https://hypermedia.systems) which is free online, but you can be cool
7070+like me and get the physical book:
7171+7272+# TODO: ADD PHOTO OF HYPERMEDIA SYSTEMS
7373+7474+## Astro Partials
7575+7676+Since we are using Astro as our meta framework, we can use its feature **Astro Partials** in
7777+conjunction with HTMX for a smooth developer experience. It allows Astro components inside the
7878+`/src/pages` directory to be easily called via its endpoint and returned as HTML.
7979+8080+For the example above, the only thing needed is for the Lemon to be in the
8181+`/src/pages/addTextBlock.astro` file with one extra line in the *Component Script* (the code
8282+inside the frontmatter like section ran on the server):
8383+8484+```astro
8585+---
8686+export const partial = true;
8787+---
8888+<li><textarea /></li>
8989+```
9090+9191+Now we can use this page via HTMX under the `/revealSecret` endpoint, like the example above!
9292+9393+## Forms and Values via Astro Partials
9494+9595+Swapping HTML is all fine and dandy, but if we want to do more, we can leverage existing
9696+hypermedia controls to extend functionality like the `<form>` and `<input>` elements.
9797+9898+## Streaming in Client Components as Astro Islands
9999+100100+## Using the HTMX Javascript API