Writing the code
Right now, every todo item shows its whole description, all the time.
Goal
- Each todo item remembers whether its description is open or closed.
- An arrow button next to the title opens and closes the description.
- The arrow points down when the description is closed, and up when it’s open.
If you get stuck, you can see the finished code for this goal on the 4-show-hide-desc branch.
Installing Alpine.js
Alpine.js is already installed. Open todo/templates/todo/_base.html and look inside the <head>, just below HTMX:
todo/templates/todo/_base.html
<script defer
src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js">
</script>srcloads Alpine from a CDN, just like HTMX.defertells the browser to wait until the whole page has loaded before running Alpine. That way, Alpine can see all thex-attributes on the page when it starts.alpinejs@3.x.xmeans “the latest version 3”. Handy for a tutorial, but your app could change without you changing anything.
This is fine for our purposes, but it’s not the recommended way to install Alpine in production. In a serious project you would pin an exact version, or install Alpine with a package manager and bundle it with the rest of your JavaScript. See the Alpine installation docs for the options.
All our other changes are in one file: todo/templates/todo/partials/todo_item.html.
1. Give each todo item some state
Open todo/templates/todo/partials/todo_item.html. Each todo item needs to remember if it’s open, so add x-data to the outer <div>:
todo/templates/todo/partials/todo_item.html
<div class="bg-white border border-gray-200 rounded-lg shadow-sm p-4"
id="todo-item-{{ item.id }}"
x-data="{open:false}">
x-data="{open:false}"gives this todo item a value calledopen, starting asfalse.- Every todo item on the page gets its own
open, so opening one won’t open the others.
Refresh the page. Nothing changes yet. We’ve given the item some state, but nothing uses it.
2. Hide the description when it’s closed
Find the description <p> at the bottom of the file, and add x-show:
todo/templates/todo/partials/todo_item.html
<p class="mb-0 {% if item.done %}line-through text-gray-400{% else %} text-gray-600 {% endif %}"
x-show="open">{{ item.description }}</p>
Refresh the page. All the descriptions are gone! 🎉
That’s because open is false, so Alpine hides them. Open your browser’s developer tools and look at the Elements tab (Inspector in Firefox). The description <p> is still there, but Alpine has added style="display: none;" to it.
Now we need a way to open them.
4. Swap the text for arrows
A word is a bit clunky. Let’s use an arrow instead: pointing down when the description is closed, and up when it’s open.
We’ll put both arrows in the button, and use x-show to show only the right one. Replace the word Toggle with two small SVG (Scalable Vector Graphics) icons:
todo/templates/todo/partials/todo_item.html
<button class="ml-auto border-0 bg-transparent text-primary hover:text-teal-800 cursor-pointer group"
x-on:click="open = ! open">
<svg class="w-4 h-4 group-hover:stroke-3"
fill="none"
stroke="currentColor"
stroke-width="2"
viewBox="0 0 24 24"
x-show="!open">
<path d="M6 9l6 6 6-6" />
</svg>
<svg class="w-4 h-4 group-hover:stroke-3"
fill="none"
stroke="currentColor"
stroke-width="2"
viewBox="0 0 24 24"
x-show="open">
<path d="M6 15l6-6 6 6" />
</svg>
</button>
- The first
<svg>is a down arrow.x-show="!open"shows it when the description is closed. - The second
<svg>is an up arrow.x-show="open"shows it when the description is open. stroke="currentColor"draws the arrow in the button’s text colour, sotext-primarymakes it our primary colour.groupon the button andgroup-hover:stroke-3on each arrow make the arrow thicker when you hover anywhere over the button. See styling based on parent state.
d="..." values mean?
They’re drawing instructions. M6 9 means “move to the point (6, 9)”, and each l draws a line from there. So M6 9l6 6 6-6 draws a “V” shape: a down arrow. You don’t need to learn these. Most people copy icons from a set like Heroicons.
Here’s the whole file now:
todo/templates/todo/partials/todo_item.html
<div class="bg-white border border-gray-200 rounded-lg shadow-sm p-4"
id="todo-item-{{ item.id }}"
x-data="{open:false}">
<div class="flex items-center gap-1 mb-1">
<input type="checkbox"
{% if item.done %}checked{% endif %}
hx-trigger="click"
hx-post="{% url 'toggle_todo_done' item.pk %}"
hx-target="#todo-list"
hx-swap="outerHTML">
<p class="font-bold mb-0 {% if item.done %}line-through text-gray-400{% endif %}">{{ item.title }}</p>
<button class="ml-auto border-0 bg-transparent text-primary hover:text-teal-800 cursor-pointer group"
x-on:click="open = ! open">
<svg class="w-4 h-4 group-hover:stroke-3"
fill="none"
stroke="currentColor"
stroke-width="2"
viewBox="0 0 24 24"
x-show="!open">
<path d="M6 9l6 6 6-6" />
</svg>
<svg class="w-4 h-4 group-hover:stroke-3"
fill="none"
stroke="currentColor"
stroke-width="2"
viewBox="0 0 24 24"
x-show="open">
<path d="M6 15l6-6 6 6" />
</svg>
</button>
</div>
<p class="mb-0 {% if item.done %}line-through text-gray-400{% else %} text-gray-600 {% endif %}"
x-show="open">{{ item.description }}</p>
</div>
5. Try it out
Make sure the server is running, and refresh http://127.0.0.1:8000/.
You’ll see just the titles, each with a down arrow on the right. Click an arrow. The description opens and the arrow flips to point up. 🎉 Click it again to close it.
Open the Network tab in your browser’s developer tools, then open and close a few descriptions. No requests appear. It all happens in the browser.
Now open a description and tick that item’s checkbox. The item moves to the bottom of the list, and its description is closed again. That’s because HTMX swapped in a brand new list from the server, and every new todo item starts with open:false. That’s fine for our purposes.
Recap
- We used
x-datato give each todo item its ownopenstate. - We used
x-on:clickto flipopenwhen the button is clicked. - We used
x-showto show the description, and the right arrow, only when they matchopen. - Showing and hiding needs no server, so Alpine handles it entirely in the browser.