This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
textarea.tsx
64 lines (58 loc) · 1.72 KB
/
textarea.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @module @edtr-io/renderer-ui
*/
/** Comment needed because of https://github.com/christopherthielen/typedoc-plugin-external-module-name/issues/337 */
import { IgnoreKeys } from '@edtr-io/core'
import { styled } from '@edtr-io/ui'
import * as React from 'react'
import TextareaAutosize, {
TextareaAutosizeProps
} from 'react-textarea-autosize'
const Textarea = styled(TextareaAutosize)({
minHeight: '100px',
width: '100%',
margin: 'auto',
padding: '10px',
resize: 'none',
fontFamily: 'Menlo, Monaco, "Courier New", monospace',
border: 'none',
outline: 'none',
boxShadow: '0 1px 1px 0 rgba(0,0,0,0.50)',
'&::-webkit-input-placeholder': {
color: 'rgba(0,0,0,0.5)'
}
})
const StyledIgnoreKeys = styled(IgnoreKeys)({
width: '100%'
})
const EditorTextareaRefForward: React.RefForwardingComponent<
HTMLTextAreaElement,
Omit<TextareaAutosizeProps, 'as' | 'ref'>
> = (props, ref) => {
return (
<StyledIgnoreKeys except={['up', 'down']}>
<Textarea
{...props}
inputRef={ref || undefined}
onKeyDown={e => {
if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') {
return
}
if (ref && typeof ref !== 'function' && ref.current) {
const { selectionStart, selectionEnd, value } = ref.current
if (selectionStart !== selectionEnd) {
return
}
if (e.key === 'ArrowUp' && selectionStart !== 0) {
e.stopPropagation()
}
if (e.key === 'ArrowDown' && selectionStart !== value.length) {
e.stopPropagation()
}
}
}}
/>
</StyledIgnoreKeys>
)
}
export const EditorTextarea = React.forwardRef(EditorTextareaRefForward)