Skip to content

Commit 7c5046c

Browse files
committed
feat: resizable input box (#246, #266)
1 parent 9fa91b0 commit 7c5046c

File tree

3 files changed

+67
-22
lines changed

3 files changed

+67
-22
lines changed

src/components/ConversationCard/index.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ function ConversationCard(props) {
365365
</div>
366366
<InputBox
367367
enabled={isReady}
368+
port={port}
369+
reverseResizeDir={props.pageMode}
368370
onSubmit={(question) => {
369371
const newQuestion = new ConversationItemData('question', question + '\n<hr/>')
370372
const newAnswer = new ConversationItemData(
@@ -382,7 +384,6 @@ function ConversationCard(props) {
382384
updateAnswer(e, false, 'error')
383385
}
384386
}}
385-
port={port}
386387
/>
387388
</div>
388389
)

src/components/InputBox/index.jsx

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,37 @@ import PropTypes from 'prop-types'
33
import { updateRefHeight } from '../../utils'
44
import { useTranslation } from 'react-i18next'
55

6-
export function InputBox({ onSubmit, enabled, port }) {
6+
export function InputBox({ onSubmit, enabled, port, reverseResizeDir }) {
77
const { t } = useTranslation()
88
const [value, setValue] = useState('')
9+
const reverseDivRef = useRef(null)
910
const inputRef = useRef(null)
11+
const resizedRef = useRef(false)
12+
13+
const virtualInputRef = reverseResizeDir ? reverseDivRef : inputRef
1014

1115
useEffect(() => {
12-
updateRefHeight(inputRef)
16+
const onResizeY = () => {
17+
if (virtualInputRef.current.h !== virtualInputRef.current.offsetHeight) {
18+
virtualInputRef.current.h = virtualInputRef.current.offsetHeight
19+
if (!resizedRef.current) {
20+
resizedRef.current = true
21+
virtualInputRef.current.style.maxHeight = ''
22+
}
23+
}
24+
}
25+
virtualInputRef.current.h = virtualInputRef.current.offsetHeight
26+
virtualInputRef.current.addEventListener('mousemove', onResizeY)
27+
}, [])
28+
29+
useEffect(() => {
30+
if (!resizedRef.current) {
31+
if (!reverseResizeDir) {
32+
updateRefHeight(inputRef)
33+
virtualInputRef.current.h = virtualInputRef.current.offsetHeight
34+
virtualInputRef.current.style.maxHeight = '160px'
35+
}
36+
}
1337
})
1438

1539
useEffect(() => {
@@ -33,20 +57,37 @@ export function InputBox({ onSubmit, enabled, port }) {
3357

3458
return (
3559
<div className="input-box">
36-
<textarea
37-
dir="auto"
38-
ref={inputRef}
39-
disabled={false}
40-
className="interact-input"
41-
placeholder={
42-
enabled
43-
? t('Type your question here\nEnter to send\nShift + enter to break line')
44-
: t('Type your question here\nEnter to stop generating\nShift + enter to break line')
60+
<div
61+
ref={reverseDivRef}
62+
style={
63+
reverseResizeDir && {
64+
transform: 'rotateX(180deg)',
65+
resize: 'vertical',
66+
overflow: 'hidden',
67+
minHeight: '160px',
68+
}
4569
}
46-
value={value}
47-
onChange={(e) => setValue(e.target.value)}
48-
onKeyDown={handleKeyDownOrClick}
49-
/>
70+
>
71+
<textarea
72+
dir="auto"
73+
ref={inputRef}
74+
disabled={false}
75+
className="interact-input"
76+
style={
77+
reverseResizeDir
78+
? { transform: 'rotateX(180deg)', resize: 'none' }
79+
: { resize: 'vertical', minHeight: '70px' }
80+
}
81+
placeholder={
82+
enabled
83+
? t('Type your question here\nEnter to send\nShift + enter to break line')
84+
: t('Type your question here\nEnter to stop generating\nShift + enter to break line')
85+
}
86+
value={value}
87+
onChange={(e) => setValue(e.target.value)}
88+
onKeyDown={handleKeyDownOrClick}
89+
/>
90+
</div>
5091
<button
5192
className="submit-button"
5293
style={{
@@ -62,8 +103,9 @@ export function InputBox({ onSubmit, enabled, port }) {
62103

63104
InputBox.propTypes = {
64105
onSubmit: PropTypes.func.isRequired,
65-
enabled: PropTypes.bool,
66-
port: PropTypes.func.isRequired,
106+
enabled: PropTypes.bool.isRequired,
107+
reverseResizeDir: PropTypes.bool,
108+
port: PropTypes.object.isRequired,
67109
}
68110

69111
export default InputBox

src/content-script/styles.scss

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,19 @@
224224
border: 0;
225225
border-top: 1px solid var(--theme-border-color);
226226
width: 100%;
227+
height: 100%;
227228
background-color: var(--theme-color);
228229
color: var(--font-color);
229-
resize: none;
230-
min-height: 70px;
231-
max-height: 160px;
230+
231+
&:focus {
232+
outline: none;
233+
}
232234
}
233235

234236
.submit-button {
235237
position: absolute;
236238
right: 1.1em;
237-
bottom: 0.2em;
239+
bottom: 0.4em;
238240
padding: 1px 6px;
239241
cursor: pointer;
240242
background-color: #30a14e;

0 commit comments

Comments
 (0)