Skip to content

Commit 286f5c7

Browse files
committed
Refactor the ChatInput component to introduce a dropdown menu for submit actions Select/Copy and remove redundant CSS properties
1 parent 8dc684d commit 286f5c7

6 files changed

Lines changed: 228 additions & 55 deletions

File tree

‎packages/ui/src/components/editor/panel/ChatInput/ChatInput.module.scss‎

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
padding: var(--padding-6px) var(--padding-10px) var(--padding-6px)
113113
var(--padding-6px);
114114
justify-content: space-between;
115-
overflow: hidden;
116115

117116
> div {
118117
flex-shrink: 0;
@@ -187,35 +186,85 @@
187186
}
188187
}
189188
}
190-
&__button {
189+
190+
&__submit {
191+
position: relative;
191192
display: flex;
192193
align-items: center;
193-
justify-content: center;
194-
gap: 3px;
195-
height: 20px;
196-
padding: 0 var(--padding-2px);
197-
border-radius: var(--border-radius-5px);
198194
pointer-events: all;
199195

200-
> svg {
201-
width: 10px;
202-
fill: var(--vscode-icon-foreground);
203-
}
196+
&__button {
197+
display: flex;
198+
align-items: center;
199+
justify-content: center;
200+
gap: 3px;
201+
height: 20px;
202+
border-radius: var(--border-radius-5px);
203+
pointer-events: all;
204204

205-
&.codicon::before {
206-
font-size: 16px;
207-
}
205+
> svg {
206+
width: 10px;
207+
fill: var(--vscode-icon-foreground);
208+
}
208209

209-
&:hover:enabled {
210-
background-color: var(--vscode-toolbar-hoverBackground);
210+
&.codicon::before {
211+
font-size: 16px;
212+
}
213+
214+
&:hover:enabled {
215+
background-color: var(--vscode-toolbar-hoverBackground);
216+
}
217+
218+
&:disabled {
219+
opacity: 0.5;
220+
cursor: default;
221+
222+
&:hover {
223+
background-color: none;
224+
}
225+
}
226+
227+
&--submit {
228+
padding: 0 var(--padding-2px);
229+
}
230+
231+
&--chevron.codicon::before {
232+
font-size: 11px;
233+
}
211234
}
212235

213-
&:disabled {
214-
opacity: 0.5;
215-
cursor: default;
236+
&__dropdown {
237+
position: absolute;
238+
top: calc(100% + 4px);
239+
right: 0;
240+
background-color: var(--vscode-menu-background);
241+
border: 1px solid var(--vscode-menu-border);
242+
border-radius: var(--border-radius-5px);
243+
box-shadow: 0 2px 8px var(--vscode-widget-shadow);
244+
z-index: 10;
245+
padding: var(--padding-4px) 0;
246+
width: max-content;
247+
text-align: left;
216248

217-
&:hover {
218-
background-color: none;
249+
&__item {
250+
display: flex;
251+
justify-content: space-between;
252+
gap: var(--padding-12px);
253+
align-items: center;
254+
padding: var(--padding-4px) var(--padding-8px);
255+
cursor: pointer;
256+
color: var(--vscode-menu-foreground);
257+
white-space: nowrap;
258+
259+
&:hover {
260+
background-color: var(--vscode-menu-selectionBackground);
261+
color: var(--vscode-menu-selectionForeground);
262+
}
263+
264+
&__shortcut {
265+
color: var(--vscode-descriptionForeground);
266+
font-size: var(--font-size-12px);
267+
}
219268
}
220269
}
221270
}

‎packages/ui/src/components/editor/panel/ChatInput/ChatInput.stories.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const Empty = () => (
2121
on_at_sign_click={() => console.log('@ clicked')}
2222
on_hash_sign_click={() => console.log('# clicked')}
2323
on_search_click={() => console.log('Search clicked')}
24-
on_submit_with_control={() => {}}
24+
on_submit_with_control={() => console.log('Submitted with control')}
2525
on_curly_braces_click={() => {}}
2626
/>
2727
)
@@ -42,7 +42,7 @@ export const WithText = () => (
4242
on_at_sign_click={() => console.log('@ clicked')}
4343
on_hash_sign_click={() => console.log('# clicked')}
4444
on_search_click={() => console.log('Search clicked')}
45-
on_submit_with_control={() => {}}
45+
on_submit_with_control={() => console.log('Submitted with control')}
4646
on_curly_braces_click={() => {}}
4747
/>
4848
)
@@ -63,7 +63,7 @@ export const LongText = () => (
6363
on_at_sign_click={() => console.log('@ clicked')}
6464
on_hash_sign_click={() => console.log('# clicked')}
6565
on_search_click={() => console.log('Search clicked')}
66-
on_submit_with_control={() => {}}
66+
on_submit_with_control={() => console.log('Submitted with control')}
6767
on_curly_braces_click={() => {}}
6868
/>
6969
)
@@ -84,7 +84,7 @@ export const WithPlaceholderSavedContext = () => (
8484
on_at_sign_click={() => console.log('@ clicked')}
8585
on_hash_sign_click={() => console.log('# clicked')}
8686
is_web_mode={false}
87-
on_submit_with_control={() => {}}
87+
on_submit_with_control={() => console.log('Submitted with control')}
8888
on_curly_braces_click={() => {}}
8989
/>
9090
)
@@ -105,7 +105,7 @@ export const WithPlaceholderSelection = () => (
105105
on_at_sign_click={() => console.log('@ clicked')}
106106
on_hash_sign_click={() => console.log('# clicked')}
107107
is_web_mode={false}
108-
on_submit_with_control={() => {}}
108+
on_submit_with_control={() => console.log('Submitted with control')}
109109
on_curly_braces_click={() => {}}
110110
/>
111111
)
@@ -128,7 +128,7 @@ export const WithEditFormatSelector = () => {
128128
on_at_sign_click={() => console.log('@ clicked')}
129129
on_hash_sign_click={() => console.log('# clicked')}
130130
on_search_click={() => console.log('Search clicked')}
131-
on_submit_with_control={() => {}}
131+
on_submit_with_control={() => console.log('Submitted with control')}
132132
on_curly_braces_click={() => {}}
133133
show_edit_format_selector={true}
134134
edit_format={edit_format}

‎packages/ui/src/components/editor/panel/ChatInput/ChatInput.tsx‎

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { useRef, useEffect, useMemo } from 'react'
1+
import { useRef, useEffect, useMemo, useState } from 'react'
22
import styles from './ChatInput.module.scss'
33
import TextareaAutosize from 'react-textarea-autosize'
44
import cn from 'classnames'
55
import { Icon } from '../../common/Icon'
66
import { get_highlighted_text } from './utils/get-highlighted-text'
77
import { use_handlers } from './hooks/use-handlers'
8+
import { use_dropdown } from './hooks/use-dropdown'
89

910
export type EditFormat = 'whole' | 'truncated' | 'diff'
1011

@@ -40,7 +41,13 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
4041
const textarea_ref = useRef<HTMLTextAreaElement>(null)
4142
const highlight_ref = useRef<HTMLDivElement>(null)
4243
const container_ref = useRef<HTMLDivElement>(null)
43-
44+
const {
45+
is_dropdown_open,
46+
toggle_dropdown,
47+
dropdown_ref,
48+
handle_copy_click,
49+
handle_select_click
50+
} = use_dropdown(props)
4451
const {
4552
handle_select,
4653
handle_input_change,
@@ -49,6 +56,12 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
4956
is_history_enabled
5057
} = use_handlers(props)
5158

59+
const is_mac = useMemo(
60+
() => navigator.platform.toUpperCase().indexOf('MAC') >= 0,
61+
[]
62+
)
63+
const mod_key = is_mac ? 'cmd' : 'ctrl'
64+
5265
useEffect(() => {
5366
if (textarea_ref.current) {
5467
textarea_ref.current.focus()
@@ -238,7 +251,7 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
238251
]]: props.edit_format == format
239252
}
240253
)}
241-
title={`Edit format instructions: ${props.edit_format_instructions?.[format]}`}
254+
title={`Attached edit format instructions: ${props.edit_format_instructions?.[format]}`}
242255
onClick={() => props.on_edit_format_change?.(format)}
243256
data-text={format}
244257
>
@@ -248,35 +261,86 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
248261
</div>
249262
)}
250263

251-
{(!props.is_web_mode ||
252-
(props.is_web_mode && props.is_connected)) && (
253-
<>
264+
<div className={styles['footer__right__submit']} ref={dropdown_ref}>
265+
{(!props.is_web_mode ||
266+
(props.is_web_mode && props.is_connected)) && (
267+
<>
268+
<button
269+
className={cn(
270+
styles['footer__right__submit__button'],
271+
styles['footer__right__submit__button--submit'],
272+
'codicon',
273+
'codicon-send'
274+
)}
275+
onClick={handle_submit}
276+
title={props.use_last_choice_button_title}
277+
/>
278+
<button
279+
className={cn(
280+
styles['footer__right__submit__button'],
281+
styles['footer__right__submit__button--chevron'],
282+
'codicon',
283+
'codicon-chevron-down'
284+
)}
285+
onClick={toggle_dropdown}
286+
title="More actions"
287+
/>
288+
{is_dropdown_open && (
289+
<div className={styles['footer__right__submit__dropdown']}>
290+
<div
291+
className={
292+
styles['footer__right__submit__dropdown__item']
293+
}
294+
onClick={handle_select_click}
295+
>
296+
Select...
297+
<span
298+
className={
299+
styles[
300+
'footer__right__submit__dropdown__item__shortcut'
301+
]
302+
}
303+
>
304+
{mod_key}+{is_mac ? 'return' : 'enter'}
305+
</span>
306+
</div>
307+
<div
308+
className={
309+
styles['footer__right__submit__dropdown__item']
310+
}
311+
onClick={handle_copy_click}
312+
>
313+
Copy
314+
<span
315+
className={
316+
styles[
317+
'footer__right__submit__dropdown__item__shortcut'
318+
]
319+
}
320+
>
321+
{mod_key}+{is_mac ? 'option' : 'alt'}+c
322+
</span>
323+
</div>
324+
</div>
325+
)}
326+
</>
327+
)}
328+
329+
{props.is_web_mode && !props.is_connected && (
254330
<button
255331
className={cn(
256-
styles.footer__right__button,
332+
styles['footer__right__submit__button'],
257333
'codicon',
258-
'codicon-send'
334+
'codicon-copy'
259335
)}
260-
onClick={handle_submit}
261-
title={props.use_last_choice_button_title}
336+
onClick={(e) => {
337+
e.stopPropagation()
338+
props.on_copy()
339+
}}
340+
title="Copy to clipboard"
262341
/>
263-
</>
264-
)}
265-
266-
{props.is_web_mode && !props.is_connected && (
267-
<button
268-
className={cn(
269-
styles.footer__right__button,
270-
'codicon',
271-
'codicon-copy'
272-
)}
273-
onClick={(e) => {
274-
e.stopPropagation()
275-
props.on_copy()
276-
}}
277-
title="Copy to clipboard"
278-
/>
279-
)}
342+
)}
343+
</div>
280344
</div>
281345
</div>
282346
</div>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useState, useRef, useEffect } from 'react'
2+
import type { ChatInputProps } from '../ChatInput'
3+
4+
type UseDropdownParams = Pick<
5+
ChatInputProps,
6+
'on_copy' | 'on_submit_with_control'
7+
>
8+
9+
export const use_dropdown = ({
10+
on_copy,
11+
on_submit_with_control
12+
}: UseDropdownParams) => {
13+
const dropdown_ref = useRef<HTMLDivElement>(null)
14+
const [is_dropdown_open, set_is_dropdown_open] = useState(false)
15+
16+
const toggle_dropdown = () => {
17+
set_is_dropdown_open((prev) => !prev)
18+
}
19+
20+
const close_dropdown = () => {
21+
set_is_dropdown_open(false)
22+
}
23+
24+
useEffect(() => {
25+
const handle_click_outside = (event: MouseEvent) => {
26+
if (
27+
dropdown_ref.current &&
28+
!dropdown_ref.current.contains(event.target as Node)
29+
) {
30+
close_dropdown()
31+
}
32+
}
33+
34+
document.addEventListener('mousedown', handle_click_outside)
35+
return () => {
36+
document.removeEventListener('mousedown', handle_click_outside)
37+
}
38+
}, [])
39+
40+
const handle_copy_click = (e: React.MouseEvent) => {
41+
e.stopPropagation()
42+
on_copy()
43+
close_dropdown()
44+
}
45+
46+
const handle_select_click = (e: React.MouseEvent) => {
47+
e.stopPropagation()
48+
on_submit_with_control()
49+
close_dropdown()
50+
}
51+
52+
return {
53+
is_dropdown_open,
54+
toggle_dropdown,
55+
dropdown_ref,
56+
handle_copy_click,
57+
handle_select_click
58+
}
59+
}

‎packages/ui/src/components/editor/panel/modals/Modal/Modal.module.scss‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
display: flex;
88
align-items: center;
99
justify-content: center;
10-
padding: var(--padding-24px) var(--padding-24px) var(--padding-30px)
10+
padding: var(--padding-24px) var(--padding-24px) var(--padding-40px)
1111
var(--padding-24px);
1212

1313
&::before {

0 commit comments

Comments
 (0)