Skip to content

Commit 789f062

Browse files
committed
Implement responsive shortening of edit format buttons using a new use_is_narrow_viewport hook
1 parent c9403a0 commit 789f062

2 files changed

Lines changed: 43 additions & 18 deletions

File tree

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Icon } from '../../common/Icon'
55
import { get_highlighted_text } from './utils/get-highlighted-text'
66
import { use_handlers } from './hooks/use-handlers'
77
import { use_dropdown } from './hooks/use-dropdown'
8+
import { use_is_narrow_viewport } from './hooks/use-is-narrow-viewport'
89
import { DropdownMenu } from '../../common/DropdownMenu'
910
import { search_paths } from '@shared/utils/search-paths'
1011
import { get_display_text } from './utils/get-display-text'
@@ -54,6 +55,7 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
5455
const container_ref = useRef<HTMLDivElement>(null)
5556
const [caret_position, set_caret_position] = useState(0)
5657
const [show_at_sign_tooltip, set_show_at_sign_tooltip] = useState(false)
58+
const is_narrow_viewport = use_is_narrow_viewport()
5759
const {
5860
is_dropdown_open,
5961
toggle_dropdown,
@@ -496,24 +498,29 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
496498
>
497499
{props.show_edit_format_selector && (
498500
<div className={styles['footer__right__edit-format']}>
499-
{(['whole', 'truncated', 'diff'] as const).map((format) => (
500-
<button
501-
key={format}
502-
className={cn(
503-
styles['footer__right__edit-format__button'],
504-
{
505-
[styles[
506-
'footer__right__edit-format__button--selected'
507-
]]: props.edit_format == format
508-
}
509-
)}
510-
title={`"${props.edit_format_instructions?.[format]}"`}
511-
onClick={() => props.on_edit_format_change?.(format)}
512-
data-text={format}
513-
>
514-
{format}
515-
</button>
516-
))}
501+
{(['whole', 'truncated', 'diff'] as const).map((format) => {
502+
const button_text = is_narrow_viewport
503+
? format.charAt(0).toUpperCase()
504+
: format
505+
return (
506+
<button
507+
key={format}
508+
className={cn(
509+
styles['footer__right__edit-format__button'],
510+
{
511+
[styles[
512+
'footer__right__edit-format__button--selected'
513+
]]: props.edit_format == format
514+
}
515+
)}
516+
title={`"${props.edit_format_instructions?.[format]}"`}
517+
onClick={() => props.on_edit_format_change?.(format)}
518+
data-text={button_text}
519+
>
520+
{button_text}
521+
</button>
522+
)
523+
})}
517524
</div>
518525
)}
519526

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useState, useEffect } from 'react'
2+
3+
export const use_is_narrow_viewport = () => {
4+
const [is_narrow_viewport, set_is_narrow_viewport] = useState(false)
5+
6+
useEffect(() => {
7+
const handle_resize = () => {
8+
set_is_narrow_viewport(window.innerWidth < 268)
9+
}
10+
11+
window.addEventListener('resize', handle_resize)
12+
handle_resize()
13+
14+
return () => window.removeEventListener('resize', handle_resize)
15+
}, [])
16+
17+
return is_narrow_viewport
18+
}

0 commit comments

Comments
 (0)