キャッシュ前リセット
stimeo--reset-before-cache
turbo:before-cache で一時的な UI を初期化し、戻る復元を綺麗に保つ。
stimeo--reset-before-cache コントローラは最も Hotwire 固有の gap パーツです。 turbo:before-cache 時に、開いたメニュー/モーダル・入力中の値・残ったトーストなど一時的な UI を初期状態へ戻し、「戻る」で復元したページが操作途中で固まらないようにします。<body> に 1 つ置きます。 scope 内の宣言的な data-reset-* を適用し、data-reset-attr は指定属性(open / aria-expanded など)を除去、data-reset-class は指定クラス(is-open / is-loading など)を除去、data-reset-form は form.reset() を実行、data-reset-value は単体フィールドをクリア、 data-reset-hidden は要素を再非表示、data-reset-remove はノードを除去します。dispatchReset が有効なときは先に stimeo--reset-before-cache:request を発火して各コントローラの閉じ処理を促し、続いて stimeo--reset-before-cache:reset を発火します。冪等(何度実行しても同じ状態へ収束)で、リスナは connect/disconnect と対で管理します。ライブラリは挙動のみを提供し、状態フックは付与せず除去する側です。
詳細表示を開き、値を入力し、トーストを表示してから、キャッシュを擬似実行してください。
開いてみる
このパネルは開いています。キャッシュ前リセット後は閉じた状態に戻ります。
<%# Markup for the before-cache reset demo.
On turbo:before-cache the controller returns transient UI to its initial state so a
Back-button restore is not frozen mid-interaction. This Playground has no real Turbo
navigation, so demo.js fakes the turbo:before-cache event. Open the details, type in
the field, and show the toast — then "Simulate before-cache" resets them all. %>
<div class="reset-demo" data-controller="stimeo--reset-before-cache">
<p class="reset-demo__note"><%= t("components.reset_before_cache.demo.note") %></p>
<details class="reset-demo__details" data-reset-attr="open">
<summary><%= t("components.reset_before_cache.demo.details_summary") %></summary>
<p><%= t("components.reset_before_cache.demo.details_body") %></p>
</details>
<label class="reset-demo__field">
<span><%= t("components.reset_before_cache.demo.input_label") %></span>
<input type="text" class="demo-input" data-reset-value
placeholder="<%= t("components.reset_before_cache.demo.input_placeholder") %>">
</label>
<div class="reset-demo__toast" data-reset-demo="toast" data-reset-hidden hidden role="status">
<%= t("components.reset_before_cache.demo.toast_text") %>
</div>
<div class="reset-demo__controls">
<button type="button" class="demo-trigger" data-reset-demo="show-toast">
<%= t("components.reset_before_cache.demo.show_toast") %>
</button>
<button type="button" class="demo-trigger" data-reset-demo="simulate">
<%= t("components.reset_before_cache.demo.simulate") %>
</button>
</div>
</div>
/*
* Presentation-only styles for the before-cache reset demo.
* The library only mutates the marked elements on turbo:before-cache (removing
* attributes, clearing values, re-hiding, removing nodes); this CSS owns layout.
*/
.reset-demo {
display: flex;
flex-direction: column;
gap: 0.75rem;
max-width: 32rem;
}
.reset-demo__note {
margin: 0;
font-size: 0.85rem;
color: var(--color-text-muted);
}
.reset-demo__details {
padding: 0.6rem 0.8rem;
border: 1px solid var(--border);
border-radius: 0.5rem;
}
.reset-demo__field {
display: flex;
flex-direction: column;
gap: 0.25rem;
font-size: 0.85rem;
}
.reset-demo__toast {
padding: 0.5rem 0.75rem;
border: 1px solid var(--leaf-500);
border-radius: 0.5rem;
background: var(--leaf-50);
color: var(--leaf-500);
font-size: 0.9rem;
}
.reset-demo__toast[hidden] {
display: none;
}
.reset-demo__controls {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
// Consumer-side JS for the before-cache reset demo (demo-only).
// Real Turbo navigation fires turbo:before-cache when caching a snapshot; this
// Playground has none, so the "Simulate" button dispatches that event to show the
// reset. The "Show toast" button reveals the transient toast the reset re-hides.
const toast = document.querySelector("[data-reset-demo='toast']");
const showToast = document.querySelector("[data-reset-demo='show-toast']");
const simulate = document.querySelector("[data-reset-demo='simulate']");
if (showToast && toast) {
showToast.addEventListener("click", () => {
toast.hidden = false;
});
}
if (simulate) {
simulate.addEventListener("click", () => {
document.dispatchEvent(new Event("turbo:before-cache"));
});
}
// The controller emits this once it has reset the snapshot's transient UI.
document.addEventListener("stimeo--reset-before-cache:reset", () => {
console.log("[reset-before-cache] transient UI reset before caching");
});
これらのスタイルは共通のデザイントークン(ライト/ダーク両対応)を使います。 共通スタイルも一緒にコピーし、ルート要素の data-theme を切り替えればダークになります。
このコンポーネントを動かすために HTML へ記述する data-* 属性です。ルート要素に下の data-controller を付け、その内側に各 target / value / action を配置します。
ルート要素に付与
data-controller="stimeo--reset-before-cache"
値(Values)
| 名前 | 説明 | 属性 |
|---|---|---|
scope
|
走査を子孫に絞るセレクタ。空なら配下全体を走査する。 | data-stimeo--reset-before-cache-scope-value |
dispatchReset
|
各コントローラ向けの request イベントを送るか(既定 true)。 |
data-stimeo--reset-before-cache-dispatch-reset-value |
アクション
| 名前 | 説明 | アクション |
|---|---|---|
reset
|
リセット走査を今すぐ実行する(turbo:before-cache にも内部結線)。 |
stimeo--reset-before-cache#reset |
イベント
| 名前 | 説明 | イベント |
|---|---|---|
reset
|
リセット走査の実行後に発火する。 | stimeo--reset-before-cache:reset |
request
|
各コントローラが自前の閉じ処理を行えるよう送出する。 | stimeo--reset-before-cache:request |