From dae0ea3340c5e2e74e97b25671c00ad30c95c523 Mon Sep 17 00:00:00 2001 From: Haruko Sugimoto Date: Wed, 4 Nov 2026 09:35:58 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20feat(inline-message):=20?= =?UTF-8?q?=E5=87=BA=E7=8F=BE=E3=81=A8=E9=80=80=E5=A0=B4=E3=82=92=E3=83=A2?= =?UTF-8?q?=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E3=83=88=E3=83=BC=E3=82=AF?= =?UTF-8?q?=E3=83=B3=E3=81=A7=E5=8B=95=E3=81=8B=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 表示されるときに animate-motion-pop-in で上から出す(--motion-pop-from-y: -1) - 閉じるボタンを押したら data-closing で退場(animate-motion-pop-out)を再生し、animationend で onClose を呼ぶ。アニメーションが無いときは待たずに呼ぶ - onClose のあとも利用側が外さなかったときは、元の表示に戻す - モーションのクラス、退場を待ってから onClose を呼ぶこと、アニメーションが無いときはすぐ呼ぶことのテストを追加 - CHANGELOG に追記 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + .../ui/inline-message/index.test.tsx | 87 ++++++++++++++++++- src/components/ui/inline-message/index.tsx | 52 ++++++++++- 3 files changed, 135 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29452f78..6b00a247 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 を、表示されるときに上から出るようにしました(モーショントークン)。閉じるボタンを押すと退場のアニメーションを再生し、終わってから `onClose` を呼びます。そのため `onClose` は退場のぶん(middle で 160ms)遅れて呼ばれます ### Fixed diff --git a/src/components/ui/inline-message/index.test.tsx b/src/components/ui/inline-message/index.test.tsx index 8de8823b..464edbe0 100644 --- a/src/components/ui/inline-message/index.test.tsx +++ b/src/components/ui/inline-message/index.test.tsx @@ -3,8 +3,8 @@ */ import React from "react"; -import { describe, it, expect, beforeEach, afterEach } from "vitest"; -import { screen } from "@testing-library/react"; +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; +import { fireEvent, screen } from "@testing-library/react"; import { TestContainer } from "@/test/helpers"; import { InlineMessage, @@ -62,3 +62,86 @@ describe("InlineMessage", () => { expect(root.getAttribute("aria-describedby")).toBe(desc.id); }); }); + +describe("InlineMessage のモーション", () => { + let testContainer: TestContainer; + + beforeEach(() => { + testContainer = new TestContainer(); + testContainer.setup(); + }); + + afterEach(() => { + testContainer.cleanup(); + vi.restoreAllMocks(); + }); + + const renderClosable = (onClose: () => void) => + testContainer.render( + + タイトル + + ); + + it("上から出て、閉じるときは退場のアニメーションで消える", () => { + // GIVEN: インラインメッセージ + // en: GIVEN an inline message + renderClosable(() => {}); + const root = screen.getByRole("alert"); + + // THEN: 出現・退場はモーショントークンで、上から出る + // en: THEN it enters and exits with the motion tokens, dropping in from above + [ + "animate-motion-pop-in", + "[--motion-pop-from-y:-1]", + "data-[closing]:animate-motion-pop-out", + ].forEach(cls => expect(root.className).toContain(cls)); + }); + + it("退場のアニメーションがあるときは、再生し終えてから onClose を呼ぶ", () => { + // GIVEN: 退場のアニメーションがかかる環境(jsdom は CSS を適用しないので差し替える) + // en: GIVEN the message has an exit animation (jsdom applies no CSS, so stub it) + const original = window.getComputedStyle; + vi.spyOn(window, "getComputedStyle").mockImplementation(element => { + const style = original(element); + return (element as HTMLElement).hasAttribute("data-closing") + ? { ...style, animationName: "motion-fade-out, motion-pop-out" } + : style; + }); + const onClose = vi.fn(); + renderClosable(onClose); + const root = screen.getByRole("alert"); + + // WHEN: 閉じるボタンを押す + // en: WHEN the close button is pressed + fireEvent.click(screen.getByRole("button", { name: "閉じる" })); + + // THEN: 退場を再生している間は onClose を呼ばない + // en: THEN onClose is not called while the exit plays + expect(root).toHaveAttribute("data-closing"); + expect(onClose).not.toHaveBeenCalled(); + + // WHEN: 退場のアニメーションが終わる + // en: WHEN the exit animation ends + fireEvent.animationEnd(root); + + // THEN: onClose を呼ぶ + // en: THEN onClose is called + expect(onClose).toHaveBeenCalledTimes(1); + }); + + it("アニメーションが無いときは、すぐ onClose を呼ぶ", () => { + // GIVEN: アニメーションの無い環境(jsdom) + // en: GIVEN no animation (jsdom) + const onClose = vi.fn(); + renderClosable(onClose); + + // WHEN: 閉じるボタンを押す + // en: WHEN the close button is pressed + fireEvent.click(screen.getByRole("button", { name: "閉じる" })); + + // THEN: 待たずに onClose を呼ぶ + // en: THEN onClose is called without waiting + expect(onClose).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/components/ui/inline-message/index.tsx b/src/components/ui/inline-message/index.tsx index cbcd48ef..0a8ef86b 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-closing で退場を再生してから onClose を呼ぶ + // en: Drops in from above. On close, data-closing plays the exit before onClose is called + "animate-motion-pop-in [--motion-pop-from-y:-1] data-[closing]:animate-motion-pop-out data-[closing]:pointer-events-none", + ].join(" "), { variants: { status: { @@ -139,6 +144,45 @@ function InlineMessage({ [titleId, descId, registerTitle, registerDesc] ); + // 閉じるボタンを押したら、退場のアニメーションを再生し終えてから onClose を呼ぶ + // en: When the close button is pressed, call onClose after the exit animation finishes + const [closing, setClosing] = React.useState(false); + const elementRef = React.useRef(null); + const onCloseRef = React.useRef(onClose); + onCloseRef.current = onClose; + + const setRefs = React.useCallback( + (node: HTMLDivElement | null) => { + elementRef.current = node; + if (typeof ref === "function") ref(node); + else if (ref) ref.current = node; + }, + [ref] + ); + + const finishClose = React.useCallback(() => { + // 利用側が外さなかったときは、元の表示に戻す + // en: If the consumer keeps the message mounted, it returns to its normal state + setClosing(false); + onCloseRef.current?.(); + }, []); + + React.useLayoutEffect(() => { + if (!closing) return; + const element = elementRef.current; + const animationName = element + ? getComputedStyle(element).animationName + : ""; + // アニメーションが無い(className で上書きされた、テスト環境など)ときは待たずに閉じる + // en: Close right away when there is no animation (overridden className, test environments, etc.) + if (!animationName || animationName === "none") finishClose(); + }, [closing, finishClose]); + + const handleAnimationEnd = (event: React.AnimationEvent) => { + props.onAnimationEnd?.(event); + if (closing && event.target === event.currentTarget) finishClose(); + }; + // ステータスに対応するアイコン情報の取得 const statusIcon = status ? statusIcons[status] : statusIcons.info; @@ -150,12 +194,14 @@ function InlineMessage({ return (
{/* ステータスアイコン */} setClosing(true)} aria-label="閉じる" className="shrink-0" /> From 560c7a8601cce437901ac61a390443049f5545b5 Mon Sep 17 00:00:00 2001 From: Haruko Sugimoto Date: Fri, 6 Nov 2026 18:15:35 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(inline-messag?= =?UTF-8?q?e):=20onClose=20=E3=82=92=E3=81=99=E3=81=90=E5=91=BC=E3=81=B3?= =?UTF-8?q?=E3=80=81=E5=87=BA=E7=8F=BE=E3=81=AF=E5=BE=8C=E3=81=8B=E3=82=89?= =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E3=81=95=E3=82=8C=E3=81=9F=E3=81=A8=E3=81=8D?= =?UTF-8?q?=E3=81=A0=E3=81=91=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - onClose は閉じるボタンを押した時点で呼ぶ。退場のアニメーションは外す(open + Presence は別 PR で検討) - SSR で描かれていたものは hydration のあとも動かさず、クライアントで後から表示されたものだけ上から出す Co-Authored-By: Claude Opus 5.5 --- .../ui/inline-message/index.test.tsx | 102 ++++++++---------- src/components/ui/inline-message/index.tsx | 71 +++++------- 2 files changed, 73 insertions(+), 100 deletions(-) diff --git a/src/components/ui/inline-message/index.test.tsx b/src/components/ui/inline-message/index.test.tsx index 464edbe0..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 { renderToString } from "react-dom/server"; +import { hydrateRoot } from "react-dom/client"; import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; -import { fireEvent, screen } from "@testing-library/react"; +import { act, fireEvent, screen } from "@testing-library/react"; import { TestContainer } from "@/test/helpers"; import { InlineMessage, @@ -73,75 +75,65 @@ describe("InlineMessage のモーション", () => { afterEach(() => { testContainer.cleanup(); - vi.restoreAllMocks(); }); - const renderClosable = (onClose: () => void) => - testContainer.render( - - タイトル - - ); + const message = (onClose?: () => void) => ( + + タイトル + + ); - it("上から出て、閉じるときは退場のアニメーションで消える", () => { - // GIVEN: インラインメッセージ - // en: GIVEN an inline message - renderClosable(() => {}); + it("後から表示されたものは、上から出る", () => { + // GIVEN: クライアントで表示したインラインメッセージ + // en: GIVEN an inline message shown on the client + testContainer.render(message()); const root = screen.getByRole("alert"); - // THEN: 出現・退場はモーショントークンで、上から出る - // en: THEN it enters and exits with the motion tokens, dropping in from above - [ - "animate-motion-pop-in", - "[--motion-pop-from-y:-1]", - "data-[closing]:animate-motion-pop-out", - ].forEach(cls => expect(root.className).toContain(cls)); + // 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("退場のアニメーションがあるときは、再生し終えてから onClose を呼ぶ", () => { - // GIVEN: 退場のアニメーションがかかる環境(jsdom は CSS を適用しないので差し替える) - // en: GIVEN the message has an exit animation (jsdom applies no CSS, so stub it) - const original = window.getComputedStyle; - vi.spyOn(window, "getComputedStyle").mockImplementation(element => { - const style = original(element); - return (element as HTMLElement).hasAttribute("data-closing") - ? { ...style, animationName: "motion-fade-out, motion-pop-out" } - : style; - }); - const onClose = vi.fn(); - renderClosable(onClose); - const root = screen.getByRole("alert"); - - // WHEN: 閉じるボタンを押す - // en: WHEN the close button is pressed - fireEvent.click(screen.getByRole("button", { name: "閉じる" })); - - // THEN: 退場を再生している間は onClose を呼ばない - // en: THEN onClose is not called while the exit plays - expect(root).toHaveAttribute("data-closing"); - expect(onClose).not.toHaveBeenCalled(); - - // WHEN: 退場のアニメーションが終わる - // en: WHEN the exit animation ends - fireEvent.animationEnd(root); - - // THEN: onClose を呼ぶ - // en: THEN onClose is called - expect(onClose).toHaveBeenCalledTimes(1); + 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: アニメーションの無い環境(jsdom) - // en: GIVEN no animation (jsdom) + it("閉じるボタンを押すと、その時点で onClose を呼ぶ", () => { + // GIVEN: 閉じるボタンのあるインラインメッセージ + // en: GIVEN an inline message with a close button const onClose = vi.fn(); - renderClosable(onClose); + 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 + // 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 0a8ef86b..fc68c8e9 100644 --- a/src/components/ui/inline-message/index.tsx +++ b/src/components/ui/inline-message/index.tsx @@ -13,9 +13,9 @@ import { cn } from "@/lib/utils"; const inlineMessageVariants = cva( [ "relative w-[448px] max-w-full rounded-notice border p-3 flex gap-2 items-start", - // 上から出る。閉じるときは data-closing で退場を再生してから onClose を呼ぶ - // en: Drops in from above. On close, data-closing plays the exit before onClose is called - "animate-motion-pop-in [--motion-pop-from-y:-1] data-[closing]:animate-motion-pop-out data-[closing]:pointer-events-none", + // 後から表示されたときだけ上から出る(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: { @@ -87,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** * @@ -144,44 +158,12 @@ function InlineMessage({ [titleId, descId, registerTitle, registerDesc] ); - // 閉じるボタンを押したら、退場のアニメーションを再生し終えてから onClose を呼ぶ - // en: When the close button is pressed, call onClose after the exit animation finishes - const [closing, setClosing] = React.useState(false); - const elementRef = React.useRef(null); - const onCloseRef = React.useRef(onClose); - onCloseRef.current = onClose; - - const setRefs = React.useCallback( - (node: HTMLDivElement | null) => { - elementRef.current = node; - if (typeof ref === "function") ref(node); - else if (ref) ref.current = node; - }, - [ref] - ); - - const finishClose = React.useCallback(() => { - // 利用側が外さなかったときは、元の表示に戻す - // en: If the consumer keeps the message mounted, it returns to its normal state - setClosing(false); - onCloseRef.current?.(); - }, []); - - React.useLayoutEffect(() => { - if (!closing) return; - const element = elementRef.current; - const animationName = element - ? getComputedStyle(element).animationName - : ""; - // アニメーションが無い(className で上書きされた、テスト環境など)ときは待たずに閉じる - // en: Close right away when there is no animation (overridden className, test environments, etc.) - if (!animationName || animationName === "none") finishClose(); - }, [closing, finishClose]); - - const handleAnimationEnd = (event: React.AnimationEvent) => { - props.onAnimationEnd?.(event); - if (closing && event.target === event.currentTarget) finishClose(); - }; + // ページを開いた時点で(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; @@ -194,14 +176,13 @@ function InlineMessage({ return (
{/* ステータスアイコン */} setClosing(true)} + onClick={onClose} aria-label="閉じる" className="shrink-0" /> From 02a3791a87320925db3304731728327bcd9ce576 Mon Sep 17 00:00:00 2001 From: Haruko Sugimoto Date: Fri, 6 Nov 2026 18:17:09 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9D=20docs(changelog):=20InlineMes?= =?UTF-8?q?sage=20=E3=81=AE=E9=A0=85=E7=9B=AE=E3=82=92=E3=80=81onClose=20?= =?UTF-8?q?=E3=82=92=E3=81=99=E3=81=90=E5=91=BC=E3=81=B6=E5=BD=A2=E3=81=AB?= =?UTF-8?q?=E5=90=88=E3=82=8F=E3=81=9B=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b00a247..5f5ad378 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +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 を、表示されるときに上から出るようにしました(モーショントークン)。閉じるボタンを押すと退場のアニメーションを再生し、終わってから `onClose` を呼びます。そのため `onClose` は退場のぶん(middle で 160ms)遅れて呼ばれます +- InlineMessage を、後から表示されたときに上から出るようにしました(モーショントークン)。ページを開いた時点で(SSR で)描かれていたものは動きません。閉じるときの退場のアニメーションは無く、`onClose` は閉じるボタンを押した時点で呼ばれます ### Fixed