キャッシュ前リセット
stimeo--reset-before-cache
戻ったときに操作の途中が残らないよう、一時的な状態をページごと片付けます。
Turbo はページを覚えておいて、戻ったときに素早く見せます。そのとき、開いたままのメニューや入力しかけの値、消し損ねたお知らせまで一緒に覚えられてしまい、戻った画面が操作の途中で固まって見えます。この部品は、覚えられる直前にそうした一時的な状態をまとめて片付けます。何を片付けるかはマークアップで指定します。属性を外す、クラスを外す、フォームを元に戻す、ひとつの入力欄だけを書いたときの状態へ戻す、隠し直す、といった指定を並べられます。ページに 1 つ置けば、その範囲の中をまとめて見ます。
実行中
詳細表示を開き、値を入力し、トーストを表示してから、キャッシュを擬似実行してください。
開いてみる
このパネルは開いています。キャッシュ前リセット後は閉じた状態に戻ります。
一時的なトースト — before-cache で再非表示になります。
キーボード操作
このコンポーネント自体はキーボード操作を持ちません。
<%# 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, restoring fields to their authored state, 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 |