Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
実践htmx入門 / practical-htmx
Search
kimihito
February 28, 2025
0
100
実践htmx入門 / practical-htmx
Shima Tech Hub#9
https://shima-tech-hub.connpass.com/event/344910/
で発表した「実践htmx入門」の記事です
kimihito
February 28, 2025
Tweet
Share
More Decks by kimihito
See All by kimihito
Vue2 EOL!? Alpine.js waits for you
kimihito_
0
140
htmx is not a typo
kimihito_
6
4.8k
もう一度死んだ話 / A story that I died
kimihito_
3
1.5k
Hackers Champloo 2016
kimihito_
0
530
AngularJSでモバイルアプリをつくる
kimihito_
1
580
もうギー沖はダメかもしれない
kimihito_
2
730
Killer Questions
kimihito_
0
120
沖縄高専×ギー沖ハッカソンの概要
kimihito_
0
300
ギークハウス沖縄のお金の話
kimihito_
0
360
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
12
650
Bash Introduction
62gerente
611
210k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.6k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
Into the Great Unknown - MozCon
thekraken
36
1.7k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.5k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7.2k
Transcript
実践htmx 入門
自己紹介 @kimihito 株式会社ミノタケ (4 月で12 周年) https://x.com/kimihito_ https://github.com/kimihito 好きなもの: (今どきじゃない)フルスタックWeb
フレームワーク Django (+ Alpine.js, htmx ) Rails (+ Hotwire) Phoenix (+ Phoenix Liveview)
AI の話しません Gamma もつかってません
htmx の話をしにきました 興味ないならまだご飯残っています、ご歓談ください
基本編
htmx とは https://htmx.org HTML 拡張のためのライブラリ(html extensions ) HTML にインタラクティブ性を付加する新しいアプローチ シンプルな属性ベースのAPI
htmx の誤解 JavaScript を書かなくていいとはいってない
htmx の基本コンセプト hx-post : HTTP メソッドと送信先 hx-trigger : イベントの種類 hx-target
: 結果の反映先 hx-swap : DOM 更新方法 <button hx-post="/clicked" hx-trigger="click" hx-target="#result" hx-swap="outerHTML"> クリックしてください </button> <div id="result"></div>
実践編 やってみてよかったことを紹介
実践例1: Alpine.js との組み合わせ htmx: サーバーとの非同期通信を担当 Alpine.js: UI 状態(送信中状態)を管理 イベント連携: htmx
のイベントをAlpine.js でキャッチ <form x-data="{ submitting: false }" hx-post="/api/submit" hx-indicator="#spinner" @htmx:before-request="submitting = true" @htmx:after-request="submitting = false"> <input type="text" name="username" required> <button type="submit" :disabled="submitting" x-text="submitting ? '送信中...' : '送信する'"> </button> <span id="spinner" class="htmx-indicator"> 処理中... </span> </form>
実践例2: リクエストの間引き delay : 入力が止まってから指定時間後に実行(デバウンス) throttle : 指定時間内は1 回だけ実行(スロットリング) <!--
入力が止まって300ms経過後にリクエスト --> <input type="text" name="search" hx-get="/search" hx-trigger="keyup delay:300ms" hx-target="#search-results"> <!-- 最大でも2秒に1回だけリクエスト --> <div hx-get="/api/status" hx-trigger="every 2s throttle:2000ms"> ステータス更新中... </div>
実践例3: サーバーからイベント サーバーからのレスポンス: <div x-data="{ messages: [] }" @showMessage.window=" this.messages.push({
text: $event.detail.value, time: new Date() }) "> <div class="notifications"> <template x-for="msg in messages" :key="msg.time"> <div class="toast" x-text="msg.text"></div> </template> </div> <div hx-get="/api/notifications" hx-trigger="every 10s" hx-target="#notification-area"> 最新のお知らせ </div> <div id="notification-area"></div> </div> HTTP/1.1 200 OK HX-Trigger: {"showMessage": "新しい通知があります"} Content-Type: text/html <div>更新された通知内容...</div>
htmx の立ち位置(感想) 出典: u/Abhilash26 on r/htmx
まとめ htmx はHTML を拡張して動的UI を実現 アトリビュートベースで直感的 他のJS ライブラリとの組み合わせで表現力UP HX-* ヘッダーでサーバーからクライアントを制御
参考リソース 公式サイト: htmx.org ドキュメント: htmx.org/docs GitHub: github.com/bigskysoftware/htmx
おしまい ご清聴ありがとうございました
あまったら余談
Node.prototype.moveBefore() がおもしろい!!! https://github.com/whatwg/dom/issues/1255 https://htmx.org/examples/move-before/ Chrome 133 (今月リリース)で有効になってます htmx の作者やReact 、Angular
が推進していたAPI
The future of htmx というエッセイがおもしろい 次のjQuery をめざす 安定こそ機能である 機能追加はしない などなど。おもしろいです
https://htmx.org/essays/future/