Skip to content

Instantly share code, notes, and snippets.

@croxton
Last active December 4, 2023 02:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save croxton/53f9faf7e2504b19c73c877b40ba24ee to your computer and use it in GitHub Desktop.
Save croxton/53f9faf7e2504b19c73c877b40ba24ee to your computer and use it in GitHub Desktop.
Sprig / htmx full page transitions
{# _layouts/base.twig #}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ entry.title }}</title>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
{# Global header #}
<header hx-boost="true" hx-target="#main" hx-select="#main" hx-swap="outerHTML" hx-ext="preload">
<nav>
<ul>
<li><a href="/page1" preload="mousedown">Page 1</a></li>
<li><a href="/page2" preload="mousedown">Page 2</a></li>
<li><a href="/page3" preload="mousedown">Page 3</a></li>
<li><a href="/page3" hx-boost="false">This link won't be boosted</a></li>
</ul>
</nav>
</header>
{# Main content of page #}
<main id="main">
{% block content %}
<article>
</article>
{% endblock %}
</main>
{# Scripts #}
<script src="https://unpkg.com/htmx.org@1.5.0"></script>
</body>
</html>
{# _components/loadMore.twig #}
{# Sets a default value if not defined by the `s-val:*` attribute on the button #}
{% set offset = offset ?? 0 %}
{% set entryQuery = craft.entries.offset(offset).limit(limit) %}
{% set entries = entryQuery.all() %}
{% for entry in entries %}
<h6>{{ entry.title }}</h6>
{% endfor %}
{# If the total entry count is greater than the number that has been displayed #}
{% if entryQuery.count() > offset + entries|length %}
{# Increments `offset` by the value of `limit` and swaps itself out on click #}
<button sprig s-val:offset="{{ offset + limit }}"
s-target="this" s-swap="outerHTML">
Load another
</button>
{% endif %}
{# page1.twig #}
{% extends "_layouts/base" %}
{% block content %}
<article>
{# Elements with the same ID and markup structure can be animated with CSS transitions #}
<header id="banner" class="w-full h-72 transition-colors duration-1000 bg-blue-500">
<h1>Page 1</h1>
</header>
<ul>
<a href="/page2" hx-boost="true" hx-target="#main" hx-select="#main" hx-swap="outerHTML">
Page 2 boosted link
</a>
<a href="/page2">
Page 2 normal link
</a>
</ul>
</article>
{% endblock %}
{# page2.twig #}
{% extends "_layouts/base" %}
{% block content %}
<article>
{# Elements with the same ID and markup structure can be animated with CSS transitions #}
<header id="banner" class="w-full h-72 transition-colors duration-1000 bg-red-500">
<h1>Page 2</h1>
</header>
<ul>
<a href="/page1" hx-boost="true" hx-target="#main" hx-select="#main" hx-swap="outerHTML">
Page 1 boosted link
</a>
<a href="/page1">
Page 1 normal link
</a>
</ul>
{# Example Sprig component #}
{{ sprig('_components/loadMore', {'limit': 1}) }}
</article>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment