Skip to content

Commit 0934c49

Browse files
committed
Refactor sticky mode to transition header on scroll
- after the threshold the mode button can appear in the compact form, it should not wait for up-scroll, instead be visible at all times - as we scroll down, the mode button appears. the issue is that we experience a slight jitter - once scrolled past mode button, together with showing the mode button in sticky position, repalce header contents with centered 'modes' text (use __modes class) - rename 'is mode sticky' to 'has scrolled past mode button'. also rename the hook appropriately. - place [Fragment] - rendering sticky mode buttons is redundant when we have them in the header - dont use inline css in `apps/editor/src/views/prompt/frontend/Home/Home.tsx` when possible - modes in header should take full width - remove this padding [Fragment] when showing modes in header - mode buttons should animate from bottom to top. - the old header should not disappear just like that, it should be visible while moving out the screen. Make sure the animation of it returning is the same but in reverse. Make sure to not break compactable action buttons. - 'is_animating_in' is declared but its value is never read. ├── apps │ └── editor │ └── src │ └── views │ └── prompt │ └── frontend │ └── Home │ ├── Home.module.scss │ ├── Home.tsx │ ├── hooks │ │ ├── use-has-scrolled-past-mode-button.ts │ │ ├── use-sticky-mode.ts │ │ └── use-tasks.ts │ └── index.ts └── packages └── ui └── src └── components └── editor └── prompt ├── CompactableActionButton │ ├── CompactableActionButton.module.scss │ ├── CompactableActionButton.tsx │ └── index.ts └── ModeButton ├── ModeButton.module.scss ├── ModeButton.stories.tsx └── ModeButton.tsx
1 parent cf53b3a commit 0934c49

5 files changed

Lines changed: 182 additions & 194 deletions

File tree

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

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
.header {
2-
display: flex;
3-
align-items: center;
4-
justify-content: space-between;
5-
gap: 12px;
2+
--height: 32px;
3+
position: relative;
64
width: 100%;
7-
padding: 4px 12px;
5+
height: var(--height);
86

9-
:global([data-modern-ui='true']) & {
10-
padding: 4px 10px;
7+
&__normal {
8+
position: absolute;
9+
inset: 0;
10+
display: flex;
11+
align-items: center;
12+
justify-content: space-between;
13+
gap: 12px;
14+
padding: 0 12px;
15+
opacity: 1;
16+
transform: translateY(0);
17+
transition:
18+
transform var(--transition-duration-250) var(--transition-timing-function),
19+
opacity var(--transition-duration-250) var(--transition-timing-function);
20+
21+
:global([data-modern-ui='true']) & {
22+
padding: 0 10px;
23+
}
24+
25+
&--hidden {
26+
opacity: 0;
27+
transform: translateY(-15px);
28+
pointer-events: none;
29+
}
1130
}
1231

1332
&__left {
@@ -43,6 +62,34 @@
4362
letter-spacing: 1px;
4463
padding-top: 1px;
4564
}
65+
66+
&__modes {
67+
position: absolute;
68+
inset: 0;
69+
display: flex;
70+
align-items: center;
71+
justify-content: center;
72+
width: 100%;
73+
height: 100%;
74+
opacity: 0;
75+
transform: translateY(15px);
76+
transition:
77+
transform var(--transition-duration-250) var(--transition-timing-function),
78+
opacity var(--transition-duration-250) var(--transition-timing-function);
79+
pointer-events: none;
80+
81+
&--visible {
82+
opacity: 1;
83+
transform: translateY(0);
84+
pointer-events: auto;
85+
}
86+
87+
&-divider {
88+
width: 1px;
89+
height: 16px;
90+
background-color: var(--cwc-border-color-dimmed);
91+
}
92+
}
4693
}
4794

4895
.content {
@@ -53,10 +100,6 @@
53100
gap: 20px;
54101
height: 100%;
55102
padding-top: 4px;
56-
57-
&--sticky {
58-
margin-top: 44px;
59-
}
60103
}
61104

62105
.inner {
@@ -97,26 +140,6 @@
97140
background-color: var(--cwc-border-color-dimmed);
98141
pointer-events: none;
99142
}
100-
101-
&--sticky {
102-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
103-
position: sticky;
104-
top: 10px;
105-
106-
&::after {
107-
height: 20px;
108-
}
109-
}
110-
111-
&--animating-in {
112-
animation: stickyFadeIn var(--transition-duration-250)
113-
var(--transition-timing-function);
114-
}
115-
116-
&--hiding {
117-
animation: stickyFadeOut var(--transition-duration-250)
118-
var(--transition-timing-function) forwards;
119-
}
120143
}
121144

122145
&__empty {
@@ -159,6 +182,17 @@
159182
color: var(--vscode-icon-foreground);
160183
}
161184
}
185+
186+
&-text {
187+
overflow: hidden;
188+
text-overflow: ellipsis;
189+
white-space: nowrap;
190+
}
191+
192+
&-icon {
193+
font-size: 12px;
194+
flex-shrink: 0;
195+
}
162196
}
163197

164198
&__tasks {
@@ -189,24 +223,12 @@
189223
}
190224
}
191225

192-
@keyframes stickyFadeIn {
193-
from {
194-
opacity: 0;
195-
transform: translateY(-15px);
196-
}
197-
to {
198-
opacity: 1;
199-
transform: translateY(0);
200-
}
226+
.bottom-wrapper {
227+
display: flex;
228+
flex-direction: column;
229+
width: 100%;
201230
}
202231

203-
@keyframes stickyFadeOut {
204-
from {
205-
opacity: 1;
206-
transform: translateY(0);
207-
}
208-
to {
209-
opacity: 0;
210-
transform: translateY(-15px);
211-
}
232+
.bottom-spacer {
233+
flex-shrink: 0;
212234
}

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

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { IconButton as UiIconButton } from '@ui/components/editor/common/IconBut
1414
import { CompactableActionButton } from '@ui/components/editor/prompt/CompactableActionButton'
1515
import { Tasks as UiTasks } from '@ui/components/editor/prompt/Tasks'
1616
import { use_tasks } from './hooks/use-tasks'
17-
import { use_sticky_mode } from './hooks/use-sticky-mode'
17+
import { use_has_scrolled_past_mode_button } from './hooks/use-has-scrolled-past-mode-button'
1818
import { use_compacting } from '@shared/hooks'
1919

2020
type Props = {
@@ -39,13 +39,11 @@ export const Home: React.FC<Props> = (props) => {
3939
const { t } = use_translation()
4040
const [active_workspace_root, set_active_workspace_root] = useState<string>()
4141
const {
42-
is_mode_sticky,
43-
is_hiding,
44-
is_animating_in,
42+
has_scrolled_past_mode_button,
4543
responses_ref,
4644
mode_ref,
4745
handle_scroll
48-
} = use_sticky_mode(props.is_active)
46+
} = use_has_scrolled_past_mode_button(props.is_active)
4947

5048
const {
5149
tasks,
@@ -91,35 +89,62 @@ export const Home: React.FC<Props> = (props) => {
9189

9290
return (
9391
<>
94-
<div className={styles.header} ref={container_ref}>
95-
<div className={styles['header__left']}>
96-
<div className={styles['header__home']}>
97-
<span className="codicon codicon-home" />
92+
<div className={styles.header}>
93+
<div
94+
className={cn(styles.header__normal, {
95+
[styles['header__normal--hidden']]: has_scrolled_past_mode_button
96+
})}
97+
ref={container_ref}
98+
>
99+
<div className={styles['header__left']}>
100+
<div className={styles['header__home']}>
101+
<span className="codicon codicon-home" />
102+
</div>
103+
<span className={styles['header__text']}>CWC</span>
104+
</div>
105+
<div className={styles['header__right']}>
106+
<CompactableActionButton
107+
label={discord_label}
108+
href="https://discord.gg/KJySXsrSX5"
109+
icon="DISCORD"
110+
is_compact={compact_step >= 1}
111+
/>
112+
<CompactableActionButton
113+
label={coffee_label}
114+
on_click={props.on_donate_click}
115+
codicon="coffee"
116+
is_compact={compact_step >= 2}
117+
/>
98118
</div>
99-
<span className={styles['header__text']}>CWC</span>
100119
</div>
101-
<div className={styles['header__right']}>
102-
<CompactableActionButton
103-
label={discord_label}
104-
href="https://discord.gg/KJySXsrSX5"
105-
icon="DISCORD"
106-
is_compact={compact_step >= 1}
120+
121+
<div
122+
className={cn(styles.header__modes, {
123+
[styles['header__modes--visible']]: has_scrolled_past_mode_button
124+
})}
125+
>
126+
<UiModeButton
127+
pre={
128+
props.is_connected
129+
? t('home.mode.autofill')
130+
: t('home.mode.copy-prompt')
131+
}
132+
label={t('home.mode.chatbots')}
133+
on_click={props.on_chatbots_click}
134+
is_compact
107135
/>
108-
<CompactableActionButton
109-
label={coffee_label}
110-
on_click={props.on_donate_click}
111-
codicon="coffee"
112-
is_compact={compact_step >= 2}
136+
<div className={styles['header__modes-divider']} />
137+
<UiModeButton
138+
pre={t('home.mode.make')}
139+
label={t('home.mode.api-calls')}
140+
on_click={props.on_api_calls_click}
141+
is_compact
113142
/>
114143
</div>
115144
</div>
116145

117146
<UiScrollable on_scroll={handle_scroll} top_shadow>
118-
<div
119-
className={cn(styles.content, {
120-
[styles['content--sticky']]: is_mode_sticky
121-
})}
122-
>
147+
<div className={styles.content}>
123148
<div className={styles.inner}>
124149
{props.response_history.length > 0 && (
125150
<div className={styles.inner__responses} ref={responses_ref}>
@@ -141,14 +166,7 @@ export const Home: React.FC<Props> = (props) => {
141166
</div>
142167
)}
143168

144-
<div
145-
className={cn(styles.inner__mode, {
146-
[styles['inner__mode--sticky']]: is_mode_sticky,
147-
[styles['inner__mode--animating-in']]: is_animating_in,
148-
[styles['inner__mode--hiding']]: is_hiding
149-
})}
150-
ref={mode_ref}
151-
>
169+
<div className={styles.inner__mode} ref={mode_ref}>
152170
<UiModeButton
153171
pre={
154172
props.is_connected
@@ -157,13 +175,11 @@ export const Home: React.FC<Props> = (props) => {
157175
}
158176
label={t('home.mode.chatbots')}
159177
on_click={props.on_chatbots_click}
160-
is_compact={is_mode_sticky}
161178
/>
162179
<UiModeButton
163180
pre={t('home.mode.make')}
164181
label={t('home.mode.api-calls')}
165182
on_click={props.on_api_calls_click}
166-
is_compact={is_mode_sticky}
167183
/>
168184
</div>
169185

@@ -190,17 +206,15 @@ export const Home: React.FC<Props> = (props) => {
190206
title={t('home.folder')}
191207
>
192208
<span
193-
style={{
194-
overflow: 'hidden',
195-
textOverflow: 'ellipsis',
196-
whiteSpace: 'nowrap'
197-
}}
209+
className={styles['inner__workspace-dropdown-text']}
198210
>
199211
{active_root?.split(/[\\/]/).pop() || active_root}
200212
</span>
201213
<span
202-
className="codicon codicon-unfold"
203-
style={{ fontSize: '12px', flexShrink: 0 }}
214+
className={cn(
215+
'codicon codicon-unfold',
216+
styles['inner__workspace-dropdown-icon']
217+
)}
204218
/>
205219
</div>
206220
</div>
@@ -263,13 +277,7 @@ export const Home: React.FC<Props> = (props) => {
263277
)}
264278
</div>
265279

266-
<div
267-
style={{
268-
display: 'flex',
269-
flexDirection: 'column',
270-
width: '100%'
271-
}}
272-
>
280+
<div className={styles['bottom-wrapper']}>
273281
<div className={styles.bottom}>
274282
<div className={styles.bottom__links}>
275283
<div>{props.version}</div>
@@ -289,7 +297,8 @@ export const Home: React.FC<Props> = (props) => {
289297
{props.bottom_spacer_height !== undefined &&
290298
props.bottom_spacer_height > 0 && (
291299
<div
292-
style={{ height: props.bottom_spacer_height, flexShrink: 0 }}
300+
className={styles['bottom-spacer']}
301+
style={{ height: props.bottom_spacer_height }}
293302
/>
294303
)}
295304
</div>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { useState, useRef, useCallback } from 'react'
2+
3+
export const use_has_scrolled_past_mode_button = (is_active: boolean) => {
4+
const [state, set_state] = useState<'normal' | 'sticky'>('normal')
5+
const normal_height = useRef(0)
6+
const responses_ref = useRef<HTMLDivElement>(null)
7+
const mode_ref = useRef<HTMLDivElement>(null)
8+
9+
const has_scrolled_past_mode_button = state !== 'normal'
10+
11+
const handle_scroll = useCallback(
12+
(top: number) => {
13+
if (!is_active) return
14+
15+
const r_height = responses_ref.current?.clientHeight || 0
16+
const m_height = mode_ref.current?.offsetHeight || 0
17+
18+
if (m_height == 0) return
19+
20+
if (!has_scrolled_past_mode_button) {
21+
normal_height.current = m_height
22+
}
23+
24+
const height_to_use = has_scrolled_past_mode_button
25+
? normal_height.current
26+
: m_height
27+
const is_past = top > r_height + height_to_use + 4
28+
29+
set_state((prev) => {
30+
if (!is_past) {
31+
return 'normal'
32+
}
33+
if (prev !== 'sticky') {
34+
return 'sticky'
35+
}
36+
return prev
37+
})
38+
},
39+
[has_scrolled_past_mode_button, is_active]
40+
)
41+
42+
return {
43+
has_scrolled_past_mode_button,
44+
responses_ref,
45+
mode_ref,
46+
handle_scroll
47+
}
48+
}

0 commit comments

Comments
 (0)