Skip to content

Commit 8a8f0b3

Browse files
committed
Add entrance animation for sticky mode and restrict hook execution to active components
- when going back from scrolled home, when the chatbots/api calls was visible before, it is no longer - when going back from scrolled home, when the chatbots/api calls was visible before, it is no longer
1 parent 7ea27e4 commit 8a8f0b3

3 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎apps/editor/src/views/panel/frontend/Home/Home.module.scss‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,17 @@
8888
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
8989
position: sticky;
9090
top: 10px;
91-
animation: stickyFadeIn var(--transition-duration-250)
92-
var(--transition-timing-function);
9391

9492
&::after {
9593
height: 20px;
9694
}
9795
}
9896

97+
&--animating-in {
98+
animation: stickyFadeIn var(--transition-duration-250)
99+
var(--transition-timing-function);
100+
}
101+
99102
&--hiding {
100103
animation: stickyFadeOut var(--transition-duration-250)
101104
var(--transition-timing-function) forwards;

‎apps/editor/src/views/panel/frontend/Home/Home.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ export const Home: React.FC<Props> = (props) => {
5252
'tasks'
5353
)
5454
const [active_workspace_root, set_active_workspace_root] = useState<string>()
55-
const { is_mode_sticky, is_hiding, responses_ref, mode_ref, handle_scroll } =
56-
use_sticky_mode()
55+
const {
56+
is_mode_sticky,
57+
is_hiding,
58+
is_animating_in,
59+
responses_ref,
60+
mode_ref,
61+
handle_scroll
62+
} = use_sticky_mode(props.is_active)
5763

5864
const roots = Object.keys(props.tasks)
5965
const active_root =
@@ -145,6 +151,7 @@ export const Home: React.FC<Props> = (props) => {
145151
<div
146152
className={cn(styles.inner__mode, {
147153
[styles['inner__mode--sticky']]: is_mode_sticky,
154+
[styles['inner__mode--animating-in']]: is_animating_in,
148155
[styles['inner__mode--hiding']]: is_hiding
149156
})}
150157
ref={mode_ref}

‎apps/editor/src/views/panel/frontend/Home/hooks/use-sticky-mode.ts‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
1-
import { useState, useRef, useEffect, useCallback } from 'react'
1+
import {
2+
useState,
3+
useRef,
4+
useEffect,
5+
useCallback,
6+
useLayoutEffect
7+
} from 'react'
28

3-
export const use_sticky_mode = () => {
9+
export const use_sticky_mode = (is_active: boolean) => {
410
const [state, set_state] = useState<'normal' | 'sticky' | 'hiding'>('normal')
11+
const [is_animating_in, set_is_animating_in] = useState(false)
512
const timeout = useRef<ReturnType<typeof setTimeout> | null>(null)
13+
const animate_in_timeout = useRef<ReturnType<typeof setTimeout> | null>(null)
614
const normal_height = useRef(0)
715
const responses_ref = useRef<HTMLDivElement>(null)
816
const mode_ref = useRef<HTMLDivElement>(null)
917
const prev_top = useRef(0)
18+
const prev_state = useRef(state)
1019

1120
const is_mode_sticky = state !== 'normal'
1221

22+
useLayoutEffect(() => {
23+
if (state == 'sticky' && prev_state.current !== 'sticky') {
24+
set_is_animating_in(true)
25+
if (animate_in_timeout.current) clearTimeout(animate_in_timeout.current)
26+
animate_in_timeout.current = setTimeout(() => {
27+
set_is_animating_in(false)
28+
}, 300)
29+
} else if (state != 'sticky') {
30+
set_is_animating_in(false)
31+
if (animate_in_timeout.current) clearTimeout(animate_in_timeout.current)
32+
}
33+
prev_state.current = state
34+
}, [state])
35+
1336
useEffect(() => {
14-
return () => { if (timeout.current) clearTimeout(timeout.current) }
37+
return () => {
38+
if (timeout.current) clearTimeout(timeout.current)
39+
if (animate_in_timeout.current) clearTimeout(animate_in_timeout.current)
40+
}
1541
}, [])
1642

1743
const handle_scroll = useCallback(
1844
(top: number) => {
45+
if (!is_active) return
46+
1947
const r_height = responses_ref.current?.clientHeight || 0
2048
const m_height = mode_ref.current?.offsetHeight || 0
2149

22-
if (!is_mode_sticky && m_height > 0) {
50+
if (m_height == 0) return
51+
52+
if (!is_mode_sticky) {
2353
normal_height.current = m_height
2454
}
2555

@@ -48,12 +78,13 @@ export const use_sticky_mode = () => {
4878
return prev
4979
})
5080
},
51-
[is_mode_sticky]
81+
[is_mode_sticky, is_active]
5282
)
5383

5484
return {
5585
is_mode_sticky,
5686
is_hiding: state === 'hiding',
87+
is_animating_in,
5788
responses_ref,
5889
mode_ref,
5990
handle_scroll

0 commit comments

Comments
 (0)