この例は、スムーズにスクロールする進行状況バーを実装する方法を示しています。
ジョブを開始するためにPOST
で/start
に発行するボタンがある初期状態で始めます
<div hx-target="this" hx-swap="outerHTML">
<h3>Start Progress</h3>
<button class="btn primary" hx-post="/start">
Start Job
</button>
</div>
このdivは、状態と600msごとに自分をリロードする進行状況バーを含む新しいdivに置き換えられます
<div hx-trigger="done" hx-get="/job" hx-swap="outerHTML" hx-target="this">
<h3 role="status" id="pblabel" tabindex="-1" autofocus>Running</h3>
<div
hx-get="/job/progress"
hx-trigger="every 600ms"
hx-target="this"
hx-swap="innerHTML">
<div class="progress" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" aria-labelledby="pblabel">
<div id="pb" class="progress-bar" style="width:0%">
</div>
</div>
</div>
この進行状況バーは600ミリ秒ごとに、「width」スタイル属性と現在の進行状況の値に設定されたaria-valuenow
属性で更新されます。進行状況バーのdivにはIDがあるため、htmxはスタイル属性を新しい値に解決することで要求間のスムーズな移行を行います。これは、CSSトランジションと組み合わせると、視覚的な遷移が飛び飛びではなく連続したものになります。
最後に、プロセスが完了すると、サーバーはHX-Trigger: done
ヘッダーを返します。これによりUIが「完了」状態に更新され、UIに再起動ボタンが追加されます(この例では、class-tools
拡張を使用してボタンにフェードイン効果を追加しています)
<div hx-trigger="done" hx-get="/job" hx-swap="outerHTML" hx-target="this">
<h3 role="status" id="pblabel" tabindex="-1" autofocus>Complete</h3>
<div
hx-get="/job/progress"
hx-trigger="none"
hx-target="this"
hx-swap="innerHTML">
<div class="progress" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="122" aria-labelledby="pblabel">
<div id="pb" class="progress-bar" style="width:122%">
</div>
</div>
</div>
<button id="restart-btn" class="btn primary" hx-post="/start" classes="add show:600ms">
Restart Job
</button>
</div>
この例では、bootstrapの進行状況バーからスタイルをコピーしました
.progress {
height: 20px;
margin-bottom: 20px;
overflow: hidden;
background-color: #f5f5f5;
border-radius: 4px;
box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
}
.progress-bar {
float: left;
width: 0%;
height: 100%;
font-size: 12px;
line-height: 20px;
color: #fff;
text-align: center;
background-color: #337ab7;
-webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
-webkit-transition: width .6s ease;
-o-transition: width .6s ease;
transition: width .6s ease;
}