UI画面の移動/切り替え
異なるUI画面間を移動するには、ルーティングシステムを使用する必要があります。ルーティングシステムを使用すると、さまざまな画面のルート(またはパス)を定義し、それらの間のナビゲーションを管理できます。
TanStack Router
TanStack Routerは、異なるページやコンポーネント間のナビゲーションをシンプルかつ効率的に管理できる、Webアプリケーション向けのルーティングシステムです。柔軟で使いやすいように設計されており、動的パラメータのサポート、ルーターの状態管理、React、Vue、Angularなどのレンダリングライブラリとの統合といった高度な機能を提供します。
TanStack Routerの主な利点は、createFileRouteを使用してルート処理を自動生成できることです。 createFileRouteを使用すると、特定のフォルダ構造でファイルを作成するだけでルートを定義できます。例えば、src/routesフォルダにabout.tsxファイルを作成すると、TanStack Routerはabout.tsxで定義されたコンポーネントをレンダリングする/aboutのルートを自動的に生成します。
import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/about")({
component: About,
});
function About() {
return (
<main className="page-wrap px-4 py-12">
<section className="island-shell rounded-2xl p-6 sm:p-8">
<p className="island-kicker mb-2">About</p>
<h1 className="display-title mb-3 text-4xl font-bold text-[var(--sea-ink)] sm:text-5xl">
A small starter with room to grow.
</h1>
<p className="m-0 max-w-3xl text-base leading-8 text-[var(--sea-ink-soft)]">
TanStack Start gives you type-safe routing, server functions, and modern SSR defaults. Use this as a
clean foundation, then layer in your own routes, styling, and add-ons.
</p>
</section>
</main>
);
}画面/ルート間を移動するには?
画面/ルート間を移動するには、ルーティングシステムが提供するナビゲーション関数を使用します。
テンプレート
すべてのテンプレートでは、StepLabelPropsインターフェースがすでに拡張されており、navigate関数が含まれています。 pixi-vn.d.ts ファイルで確認できます。
これを有効にするには、StepLabelPropsインターフェースにナビゲーション関数を含める必要があります。
import { narration } from "@drincs/pixi-vn";
declare module "@drincs/pixi-vn" {
interface StepLabelProps {
navigate: (route: string) => void;
}
}テスト
Vite.jsプラグインのGame.testing(上記の「Vite.jsプラグイン」セクションとpixi-vn-testing
Agent Skillを参照)を使用してAIエージェントにゲームをプレイテストさせる場合は、
Game.testing.setProps
もここで呼び出し、navigate(およびこのフックが返すその他の値)をテストセッションでも利用できるようにしてください。
import { Game, StepLabelProps } from "@drincs/pixi-vn";
import useMyNavigate from "./useMyNavigate";
export default function useGameProps(): StepLabelProps {
const navigate = useMyNavigate();
const props = {
navigate,
};
Game.testing.setProps(props);
return props;
}ink
すべての_ink_テンプレートでは、ルート間を移動するためにカスタムハッシュタグコマンドが導入されています。 To do this, you need to use the following syntax:
# navigate [route]route:移動先のルート/パス。例:# navigate /new-route。
新しいナラティブノード(label)を作成する際は、navigate関数を使用してルートを切り替えることができます。
export const startLabel = newLabel("start", [
({ navigate }) => {
navigate("/new-route");
},
]);その他の機能
ブラウザの戻る・進むボタンを使用すると、ゲームが不整合な状態になる可能性があります。これは、これらのキーによってプレイヤーがナラティブステップ(steps)ではなくルート間を移動できてしまうためです。
ブラウザの戻るボタンが押されたときにパスが変更されないようにすることが推奨されます。これを実現する方法は複数ありますが、次のアプローチが推奨されます。
ブラウザの戻るボタンが押された後にトリガーされるpopstateイベントをインターセプトし、history.forward()を呼び出してナビゲーションをキャンセルします。ユーザーが戻るボタンを連続して2回押した場合に戻れるようにするため、ルートが変更されるたびに履歴スタックに新しい状態をプッシュできます。
例:
import { useLocation } from "@tanstack/react-router";
import { useEffect } from "react";
/**
* useConfirmBackNavigation
*
* Prevent accidental browser "back" navigation.
* Behavior:
* - On each `href` change it pushes a history state (`pushState`).
* - Listens for `popstate` and calls `history.forward()` to cancel a single
* press of the browser back button.
* - If the user presses back twice in succession (i.e. the second press occurs
* before the first is handled), the browser will navigate back.
*
* Usage: call this hook at the app root to avoid unintended back navigations
* (e.g. accidental taps or gestures). Returns `null`.
*/
export default function useConfirmBackNavigation() {
const { href } = useLocation();
useEffect(() => {
const isInIframe =
typeof window !== "undefined" && window.self !== window.top;
if (isInIframe) return;
const listener = () => {
window.history.forward();
};
window.addEventListener("popstate", listener);
return () => {
window.removeEventListener("popstate", listener);
};
}, []);
useEffect(() => {
window.history.pushState(null, href, href);
}, [href]);
}