सर्व कंपोनंट
MotionStream स्फोटांना गाठणाऱ्या स्थिर गतीने उघडणारा स्ट्रीम मजकूर.
वेब React Native संख्या आणि मजकूर
वापरून पाहा JavaScript बंद आहे. खालील कोड हेच संपूर्ण उदाहरण आहे.
App.jsx import { useEffect, useRef, useState } from 'react' ;
import { MotionStream } from '@kkwaenggwari/animated-chorus' ;
import './styles.css' ;
const answer =
'Motion helps people follow change. When a total updates, a rolling number shows which way it moved. ' +
'When a panel opens, the height that follows its content keeps the reader in place. ' +
'When an answer arrives in bursts, a steady reveal turns the bursts into reading: nothing waits for the ' +
'next packet, and nothing is dumped at once. The pace speeds up only when a backlog builds, so the text ' +
'never falls far behind what the server has already sent.' ;
const pauses = [300 , 1100 , 250 , 1500 , 400 , 900 ];
export default function App ({ speed = 90 , catchUp = 900 , chunk = 160 , effect = 'fade' } ) {
const [received, setReceived] = useState (0 );
const [chunks, setChunks] = useState (0 );
const renders = useRef (0 );
renders.current += 1 ;
useEffect (() => {
if (received >= answer.length ) return ;
const timer = setTimeout (
() => {
setReceived ((current ) => Math .min (answer.length , current + chunk));
setChunks ((count ) => count + 1 );
},
pauses[chunks % pauses.length ],
);
return () => clearTimeout (timer);
}, [received, chunks, chunk]);
const askAgain = ( ) => {
setReceived (0 );
setChunks (0 );
};
return (
<div className ="chat" >
<p className ="question" > Why does motion help an interface?</p >
<p className ="answer" >
<MotionStream
text ={answer.slice(0, received )}
speed ={speed}
catchUp ={catchUp}
effect ={effect}
/>
</p >
<footer >
<output >
{chunks} chunks · App rendered {renders.current} times
</output >
<button onClick ={askAgain} > Ask again</button >
</footer >
</div >
);
}styles.css .chat {
display : grid;
gap : 16px ;
max-width : 620px ;
margin : 0 auto;
padding : 24px 20px ;
}
.question {
justify-self : end;
max-width : 80% ;
margin : 0 ;
padding : 10px 16px ;
border-radius : 18px 18px 4px 18px ;
background : #176b52 ;
color : #fff ;
}
.answer {
min-height : 180px ;
margin : 0 ;
padding : 16px 18px ;
border : 1px solid #cfe8dc ;
border-radius : 18px 18px 18px 4px ;
background : #fff ;
color : #16181d ;
line-height : 1.65 ;
}
footer {
display : flex;
flex-wrap : wrap;
align-items : center;
justify-content : space-between;
gap : 10px ;
}
output {
font :
600 13px ui-monospace,
Consolas,
monospace;
color : #176b52 ;
}
footer button {
min-height : 40px ;
padding : 0 16px ;
border : 1px solid #cfe8dc ;
border-radius : 999px ;
background : #fff ;
font :
600 14px system-ui,
sans-serif;
cursor : pointer;
}सर्वात छोटा वापर Answer.jsx कोड कॉपी करा <MotionStream text={answerSoFar} initial />
<MotionStream text ={answerSoFar} >
{visible => <Markdown > {visible}</Markdown > }
</MotionStream > तुमच्यासाठी काय हाताळतो स्फोट वाचनीय होतात: मजकूर तुकड्यांत नाही, तर सेकंदाला speed अक्षरांनी उघडतो. अनुशेष असल्यास उघडणे वेगवान होऊन सुमारे catchUp मिलिसेकंदांत दाखवले जाते. Emoji, ध्वज आणि जोड चिन्हे कधीही फुटत नाहीत, आणि अर्धवट आलेले emoji उरलेल्याची वाट पाहते. पुन्हा लिहिलेले उत्तर पुन्हा सुरू न होता सामायिक सुरुवातीपासून पुढे जाते. उघडलेली अक्षरे थेट पानावर जातात; React सेकंदाला फक्त काही वेळा रेंडर होते. यासाठी वापरा चॅटमधील सहायकाचे उत्तर. build किंवा deploy लॉग. थेट उपशीर्षके. Props Prop प्रकार डीफॉल्ट वर्णन textstring—आवश्यक आतापर्यंतचा मजकूर. वाढणारा मजकूर स्थिर गतीने उघडतो; पुन्हा लिहिणे सामायिक सुरुवातीपासून पुढे जाते. speednumber90सेकंदाला अक्षरांमध्ये मूळ उघडण्याचा दर. catchUpnumber900अनुशेष वेगवान होऊन सुमारे इतक्या मिलिसेकंदांत उघडतो. initialbooleanfalseमाउंटवेळी असलेला मजकूरही उघडा (अन्यथा तो लगेच दिसतो). effect'fade' | 'none''fade'fade नवीन अक्षरे फिकटपणे आणते; none ती थेट दाखवते.children(visible: string) => ReactNodeगती नियंत्रित मजकूर मिळवणारे फंक्शन, उदाहरणार्थ Markdown रेंडर करण्यासाठी. aselement'span'रेंडर करायचा एलिमेंट. onMotionComplete(text) => voidउघडणे text ला गाठते तेव्हा प्रत्येक वेळी चालते.
इतर props रेंडर होणाऱ्या एलिमेंटकडे जातात, त्यामुळे className, style आणि इव्हेंट हँडलर नेहमीप्रमाणे चालतात.
या उदाहरणांमध्ये वापरलेले