@@ -62,25 +62,33 @@ export default function AnimatedTerminal({ version }: AnimatedTerminalProps) {
6262 const [ currentExample , setCurrentExample ] = useState ( 0 ) ;
6363 const [ displayedLines , setDisplayedLines ] = useState < string [ ] > ( [ ] ) ;
6464 const [ currentLineIndex , setCurrentLineIndex ] = useState ( 0 ) ;
65+ const [ isVisible , setIsVisible ] = useState ( true ) ;
6566 const isVisibleRef = useRef ( true ) ;
6667
6768 useEffect ( ( ) => {
6869 const handleVisibilityChange = ( ) => {
69- isVisibleRef . current = ! document . hidden ;
70+ const visible = ! document . hidden ;
71+ isVisibleRef . current = visible ;
72+ setIsVisible ( visible ) ;
7073 } ;
7174
7275 const handleWindowBlur = ( ) => {
7376 isVisibleRef . current = false ;
77+ setIsVisible ( false ) ;
7478 } ;
7579
7680 const handleWindowFocus = ( ) => {
77- isVisibleRef . current = true ;
81+ const visible = ! document . hidden ;
82+ isVisibleRef . current = visible ;
83+ setIsVisible ( visible ) ;
7884 } ;
7985
8086 document . addEventListener ( 'visibilitychange' , handleVisibilityChange ) ;
8187 window . addEventListener ( 'blur' , handleWindowBlur ) ;
8288 window . addEventListener ( 'focus' , handleWindowFocus ) ;
83- isVisibleRef . current = ! document . hidden && document . hasFocus ( ) ;
89+ const initialVisible = ! document . hidden && document . hasFocus ( ) ;
90+ isVisibleRef . current = initialVisible ;
91+ setIsVisible ( initialVisible ) ;
8492
8593 return ( ) => {
8694 document . removeEventListener ( 'visibilitychange' , handleVisibilityChange ) ;
@@ -91,7 +99,7 @@ export default function AnimatedTerminal({ version }: AnimatedTerminalProps) {
9199
92100 useEffect ( ( ) => {
93101 // Pause animation when page is not visible
94- if ( ! isVisibleRef . current ) {
102+ if ( ! isVisible ) {
95103 return ;
96104 }
97105
@@ -122,7 +130,7 @@ export default function AnimatedTerminal({ version }: AnimatedTerminalProps) {
122130
123131 return ( ) => clearTimeout ( timer ) ;
124132 }
125- } , [ currentExample , currentLineIndex , examples ] ) ;
133+ } , [ currentExample , currentLineIndex , examples , isVisible ] ) ;
126134
127135 const getLinePrefix = ( line : string ) : { prefix : string ; content : string ; prefixColor : string } => {
128136 if ( line . startsWith ( 'In [' ) ) {
0 commit comments