Skip to content

Commit e332d9a

Browse files
committed
チャットのイメージ画像にアニメーションを追加
1 parent 5550dcf commit e332d9a

File tree

1 file changed

+65
-14
lines changed

1 file changed

+65
-14
lines changed

app/featureCard.tsx

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import clsx from "clsx";
4-
import { ReactNode } from "react";
4+
import { ReactNode, useEffect, useRef, useState } from "react";
55
import { StyledMarkdown } from "./markdown/markdown";
66
import { Heading } from "./markdown/heading";
77
import {
@@ -117,6 +117,51 @@ export function PracticeImage() {
117117

118118
export function ChatImage() {
119119
// public/docs/python/1-basics/2-1-str.md より
120+
const [chatAreaText, setChatAreaText] = useState<string>("");
121+
const [docsReplaced, setDocsReplaced] = useState<boolean>(false);
122+
const replacedDocsRef = useRef<HTMLDivElement | null>(null);
123+
useEffect(() => {
124+
let animTimeout: ReturnType<typeof setTimeout> | null = null;
125+
let chatInterval: ReturnType<typeof setInterval> | null = null;
126+
const chatAreaFullText = `Pythonには「double型」は存在しません。他の言語(C++、Java、C#など)でおなじみのdouble型に相当するのは、Pythonの**float型**です。
127+
128+
Pythonのfloat型は**倍精度浮動小数点数(double-precision floating point)** を表し、`;
129+
// 他の言語のdouble型と同じ桁数の精度(通常15〜17桁)を提供します。そのため、Pythonでは単一のfloat型だけで十分です。
130+
const animLoop = () => {
131+
let chatAreaLen = 0;
132+
setDocsReplaced(false);
133+
chatInterval = setInterval(() => {
134+
if (chatAreaLen < chatAreaFullText.length) {
135+
setChatAreaText(chatAreaFullText.slice(0, chatAreaLen));
136+
chatAreaLen += 4;
137+
} else {
138+
setDocsReplaced(true);
139+
replacedDocsRef.current?.animate(
140+
[{ opacity: "0" }, { opacity: "1" }],
141+
{
142+
duration: 500,
143+
fill: "forwards",
144+
easing: "ease-out",
145+
}
146+
);
147+
if (chatInterval) {
148+
clearInterval(chatInterval);
149+
chatInterval = null;
150+
}
151+
animTimeout = setTimeout(animLoop, 1500);
152+
}
153+
}, 50);
154+
};
155+
animLoop();
156+
return () => {
157+
if (animTimeout) {
158+
clearTimeout(animTimeout);
159+
}
160+
if (chatInterval) {
161+
clearInterval(chatInterval);
162+
}
163+
};
164+
}, []);
120165
return (
121166
<>
122167
<div className="absolute right-2/5 w-[85%] md:w-[70%] top-0 scale-70 md:scale-85 origin-top-right pointer-events-none">
@@ -125,14 +170,26 @@ export function ChatImage() {
125170
*/}
126171
<ChatAreaStateProvider>
127172
<ChatAreaStateUpdater chatId="sample" />
128-
<StyledMarkdown
129-
content={`### 数値(int, float)
173+
<div
174+
className={clsx(!docsReplaced && "hidden", "opacity-0")}
175+
ref={replacedDocsRef}
176+
>
177+
<StyledMarkdown
178+
content={`### 数値(int, float)
130179
131180
Pythonは整数 (\`int\`) と浮動小数点数 (\`float\`) を区別します。
132181
浮動小数点数 (\`float\`) は、他の言語の double 型に相当する倍精度浮動小数点数です。
133182
`}
134-
replacedRange={[{ start: 64, end: 120, id: "sample" }]}
135-
/>
183+
replacedRange={[{ start: 64, end: 120, id: "sample" }]}
184+
/>
185+
</div>
186+
<div className={clsx(docsReplaced && "hidden")}>
187+
<StyledMarkdown
188+
content={`### 数値(int, float)
189+
190+
Pythonは整数 (\`int\`) と浮動小数点数 (\`float\`) を区別します。`}
191+
/>
192+
</div>
136193
</ChatAreaStateProvider>
137194
<pre
138195
className={clsx(
@@ -165,13 +222,11 @@ Pythonは整数 (\`int\`) と浮動小数点数 (\`float\`) を区別します
165222
</div>
166223
<aside
167224
className={clsx(
168-
"absolute left-3/5 md:w-80 top-0 scale-70 md:scale-85 origin-top-left",
225+
"absolute left-3/5 md:w-80 top-0 h-200 scale-70 md:scale-85 origin-top-left",
169226
"bg-base-300 shadow-md p-3 flex flex-col"
170227
)}
171228
>
172-
<span className="flex-1 text-base font-bold opacity-40">
173-
AIへの質問
174-
</span>
229+
<span className="text-base font-bold opacity-40">AIへの質問</span>
175230
<Heading level={2} className="mt-1! mb-1! text-nowrap">
176231
Pythonのdouble型について
177232
</Heading>
@@ -184,11 +239,7 @@ Pythonは整数 (\`int\`) と浮動小数点数 (\`float\`) を区別します
184239
<StyledMarkdown content="double型はありますか?" />
185240
</div>
186241
</div>
187-
<StyledMarkdown
188-
content={`Pythonには「double型」は存在しません。他の言語(C++、Java、C#など)でおなじみのdouble型に相当するのは、Pythonの**float型**です。
189-
190-
Pythonのfloat型は**倍精度浮動小数点数(double-precision floating point)** を表し、他の言語のdouble型と同じ桁数の精度(通常15〜17桁)を提供します。そのため、Pythonでは単一のfloat型だけで十分です。`}
191-
/>
242+
<StyledMarkdown content={chatAreaText} />
192243
</aside>
193244
</>
194245
);

0 commit comments

Comments
 (0)