Skip to main content

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.
Tip

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>
  • src loads Alpine from a CDN, just like HTMX.
  • defer tells the browser to wait until the whole page has loaded before running Alpine. That way, Alpine can see all the x- attributes on the page when it starts.
  • alpinejs@3.x.x means “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 called open, starting as false.
  • 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.

3. Add a button to open and close the description

Inside the <div> with the checkbox and title, add a <button> after the title:

todo/templates/todo/partials/todo_item.html
<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"
        x-on:click="open = ! open">
    Toggle
</button>
  • x-on:click="open = ! open" flips open every time the button is clicked.
  • ml-auto pushes the button over to the right-hand side of the row.
  • border-0 bg-transparent undoes the solid button style from _base.html, so this looks like a small icon button.

Refresh the page and click Toggle on an item. Its description opens. Click it again and it closes. 🎉

Look at the Elements tab while you click. You’ll see style="display: none;" appear and disappear on the description.

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, so text-primary makes it our primary colour.
  • group on the button and group-hover:stroke-3 on each arrow make the arrow thicker when you hover anywhere over the button. See styling based on parent state.

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-data to give each todo item its own open state.
  • We used x-on:click to flip open when the button is clicked.
  • We used x-show to show the description, and the right arrow, only when they match open.
  • Showing and hiding needs no server, so Alpine handles it entirely in the browser.