diff --git a/CHANGELOG.md b/CHANGELOG.md index e8005429..7422cac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Select のアニメーションをモーショントークンで動かすようにしました。リストはトリガーの側から出て、閉じるときは退場のアニメーションを再生してから閉じます。トリガーの矢印は開閉に合わせて 180° 回ります。項目を選ぶと、その項目が点滅(160ms)してから閉じます。点滅と退場は見た目だけで、`onValueChange` と `onOpenChange` は操作した時点で呼ばれます。`open` を制御している場合も、`false` にすると退場を再生してから閉じます - Switch のつまみの移動とトラックの色の変化を、モーショントークン(Small)で同じタイミングで動かすようにしました。ホバーの色はトラックに重ねた層(`::after`)で表し、ホバーの出入りではすぐ切り替わります - Tabs の選択中の見た目(solid の塗り・line の下線・ghost の背景と枠)を、タブとは別のインジケーター(`data-slot="tabs-indicator"`)で表すようにしました。選択が変わると、インジケーターが状態変化のスプリング(`--motion-settle-*`)で次のタブへ移動します。トリガーのホバー・フォーカス・無効時の色は、これまでどおりトリガー自身が持ち、すぐ切り替わります。インジケーターは位置を測ってから表示するので、測れるまで(SSR で JS が動くまでなど)は、これまでどおりトリガーが選択中の見た目を持ちます +- InlineMessage を、後から表示されたときに上から出るようにしました(モーショントークン)。ページを開いた時点で(SSR で)描かれていたものは動きません。閉じるときの退場のアニメーションは無く、`onClose` は閉じるボタンを押した時点で呼ばれます - `TabsList` の `scrollable` で、隠れたタブがある側の端をフェード(2rem)させるようにしました。スクロールバーを隠しているので、続きがあることを示すためです。見えていないタブを選ぶと、端のフェードに掛からない位置までリストだけをスクロールします(ページは動きません。`prefers-reduced-motion: reduce` では一瞬で動きます)。リストには `data-fade-left` / `data-fade-right` が付きます(RTL でも物理方向の左右)。最初の選択がはみ出しているときも、表示した時点でそのタブが見える位置まで動かします ### Fixed diff --git a/src/components/ui/inline-message/index.test.tsx b/src/components/ui/inline-message/index.test.tsx index 8de8823b..c987e00d 100644 --- a/src/components/ui/inline-message/index.test.tsx +++ b/src/components/ui/inline-message/index.test.tsx @@ -3,8 +3,10 @@ */ import React from "react"; -import { describe, it, expect, beforeEach, afterEach } from "vitest"; -import { screen } from "@testing-library/react"; +import { renderToString } from "react-dom/server"; +import { hydrateRoot } from "react-dom/client"; +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; +import { act, fireEvent, screen } from "@testing-library/react"; import { TestContainer } from "@/test/helpers"; import { InlineMessage, @@ -62,3 +64,76 @@ describe("InlineMessage", () => { expect(root.getAttribute("aria-describedby")).toBe(desc.id); }); }); + +describe("InlineMessage のモーション", () => { + let testContainer: TestContainer; + + beforeEach(() => { + testContainer = new TestContainer(); + testContainer.setup(); + }); + + afterEach(() => { + testContainer.cleanup(); + }); + + const message = (onClose?: () => void) => ( + + タイトル + + ); + + it("後から表示されたものは、上から出る", () => { + // GIVEN: クライアントで表示したインラインメッセージ + // en: GIVEN an inline message shown on the client + testContainer.render(message()); + const root = screen.getByRole("alert"); + + // THEN: 出現はモーショントークンで、上から出る + // en: THEN it enters with the motion tokens, dropping in from above + expect(root).toHaveAttribute("data-appear"); + ["data-[appear]:animate-motion-pop-in", "[--motion-pop-from-y:-1]"].forEach( + cls => expect(root.className).toContain(cls) + ); + }); + + it("ページを開いた時点で(SSR で)描かれていたものは、hydration のあとも動かない", async () => { + // GIVEN: サーバーで描いた HTML + // en: GIVEN server-rendered HTML + const html = renderToString(message()); + const container = document.createElement("div"); + container.innerHTML = html; + document.body.appendChild(container); + const root = container.querySelector('[role="alert"]') as HTMLElement; + expect(root).not.toHaveAttribute("data-appear"); + + // WHEN: クライアントで hydration する + // en: WHEN it is hydrated on the client + const errors = vi.spyOn(console, "error").mockImplementation(() => {}); + const hydrated = await act(async () => hydrateRoot(container, message())); + + // THEN: 出現は再生せず、hydration の不一致も起きない + // en: THEN the entrance does not play and there is no hydration mismatch + expect(root).not.toHaveAttribute("data-appear"); + expect(errors).not.toHaveBeenCalled(); + + act(() => hydrated.unmount()); + container.remove(); + errors.mockRestore(); + }); + + it("閉じるボタンを押すと、その時点で onClose を呼ぶ", () => { + // GIVEN: 閉じるボタンのあるインラインメッセージ + // en: GIVEN an inline message with a close button + const onClose = vi.fn(); + testContainer.render(message(onClose)); + + // WHEN: 閉じるボタンを押す + // en: WHEN the close button is pressed + fireEvent.click(screen.getByRole("button", { name: "閉じる" })); + + // THEN: アニメーションを待たずに onClose を呼ぶ + // en: THEN onClose is called without waiting for an animation + expect(onClose).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/components/ui/inline-message/index.tsx b/src/components/ui/inline-message/index.tsx index cbcd48ef..fc68c8e9 100644 --- a/src/components/ui/inline-message/index.tsx +++ b/src/components/ui/inline-message/index.tsx @@ -11,7 +11,12 @@ import { cn } from "@/lib/utils"; // ステータスごとのスタイル定義 const inlineMessageVariants = cva( - "relative w-[448px] max-w-full rounded-notice border p-3 flex gap-2 items-start", + [ + "relative w-[448px] max-w-full rounded-notice border p-3 flex gap-2 items-start", + // 後から表示されたときだけ上から出る(data-appear)。閉じるときはすぐ消える + // en: Drops in from above only when shown later (data-appear). It disappears right away on close + "data-[appear]:animate-motion-pop-in [--motion-pop-from-y:-1]", + ].join(" "), { variants: { status: { @@ -82,6 +87,20 @@ export interface InlineMessageProps children?: React.ReactNode; } +const subscribeNothing = () => () => {}; + +/** + * サーバーで描いた HTML を hydration しているあいだ(とサーバー上)だけ true を返す + * en: Returns true only on the server and while hydrating server-rendered HTML + */ +function useIsHydrating() { + return React.useSyncExternalStore( + subscribeNothing, + () => false, + () => true + ); +} + /** * **概要 / Overview** * @@ -139,6 +158,13 @@ function InlineMessage({ [titleId, descId, registerTitle, registerDesc] ); + // ページを開いた時点で(SSR で)描かれていたものは動かさず、後から表示されたものだけ出現を再生する。 + // 最初に決めた値を保つので、hydration が終わっても途中から動き出さない + // en: Messages already on the page (server-rendered) stay still; only ones shown later play the entrance. + // The first value is kept, so nothing starts moving once hydration ends + const isHydrating = useIsHydrating(); + const [appear] = React.useState(!isHydrating); + // ステータスに対応するアイコン情報の取得 const statusIcon = status ? statusIcons[status] : statusIcons.info; @@ -154,6 +180,7 @@ function InlineMessage({ role="alert" aria-labelledby={hasTitle ? titleId : undefined} aria-describedby={hasDesc ? descId : undefined} + data-appear={appear ? "" : undefined} className={cn(inlineMessageVariants({ status }), className)} {...props} >