Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 3x 3x 3x 41x 41x 41x 41x 41x 38x 38x 11x 11x 11x 5x 4x 4x 11x 11x 11x 11x 7x 4x 4x 41x 1x 1x 164x 369x 11x 10x 123x | import {
AtSign,
Bold,
Code,
Italic,
Link,
List,
ListOrdered,
Plus,
SendHorizontal,
Smile,
SquareCode,
Strikethrough,
TextQuote,
type LucideIcon,
} from 'lucide-react'
import {
Fragment,
useLayoutEffect,
useRef,
useState,
type KeyboardEvent,
type ReactElement,
} from 'react'
import { IconButton } from '@/components/IconButton'
import { Separator } from '@/components/Separator'
import { applyFormat, type FormatAction } from '../../lib/format'
interface ComposerProps {
/** Shown in the placeholder: "#granbull" or a person's name. */
target: string
onSend: (body: string) => void
}
interface FormatTool {
action: FormatAction
label: string
icon: LucideIcon
}
// The toolbar's groups, as in the prototype: marks | link | lists | quote and code.
const FORMAT_GROUPS: FormatTool[][] = [
[
{ action: 'bold', label: 'Bold', icon: Bold },
{ action: 'italic', label: 'Italic', icon: Italic },
{ action: 'strike', label: 'Strikethrough', icon: Strikethrough },
],
[{ action: 'link', label: 'Link', icon: Link }],
[
{ action: 'numbered', label: 'Numbered list', icon: ListOrdered },
{ action: 'bulleted', label: 'Bulleted list', icon: List },
],
[
{ action: 'quote', label: 'Quote', icon: TextQuote },
{ action: 'code', label: 'Code', icon: Code },
{ action: 'codeBlock', label: 'Code block', icon: SquareCode },
],
]
// shortcut: attach, emoji and mention are drawn but open nothing; files are M4, the pickers M3.
const TOOLS = [
{ label: 'Attach a file', icon: Plus },
{ label: 'Insert emoji', icon: Smile },
{ label: 'Mention someone', icon: AtSign },
] as const
export const Composer = ({ target, onSend }: ComposerProps): ReactElement => {
const [draft, setDraft] = useState('')
const box = useRef<HTMLTextAreaElement>(null)
// Where the selection goes once the formatted draft has rendered.
const nextSelection = useRef<[start: number, end: number] | null>(null)
const body = draft.trim()
// The textarea's value only changes on render, so the selection is set after it.
useLayoutEffect(() => {
const selection = nextSelection.current
if (!selection || !box.current) return
nextSelection.current = null
box.current.focus()
box.current.setSelectionRange(selection[0], selection[1])
}, [draft])
function send(): void {
if (!body) return
onSend(body)
setDraft('')
}
function handleFormat(action: FormatAction): void {
Iif (!box.current) return
const edit = applyFormat(draft, box.current.selectionStart, box.current.selectionEnd, action)
nextSelection.current = [edit.selectionStart, edit.selectionEnd]
setDraft(edit.value)
}
function handleKeyDown(event: KeyboardEvent<HTMLTextAreaElement>): void {
// While a Vietnamese (Telex, VNI) or other IME is composing, Enter confirms the word, not the message.
if (event.key !== 'Enter' || event.shiftKey || event.nativeEvent.isComposing) return
event.preventDefault()
send()
}
return (
<form
onSubmit={(event) => {
event.preventDefault()
send()
}}
className="mx-3 mb-3 rounded-3xl border border-border-strong bg-glass-strong shadow-soft focus-within:border-primary focus-within:ring-3 focus-within:ring-ring md:mx-5"
>
<div role="group" aria-label="Formatting" className="flex items-center gap-px px-2.5 pt-2">
{FORMAT_GROUPS.map((group, index) => (
<Fragment key={group[0]?.action}>
{index > 0 ? (
<Separator
orientation="vertical"
className="mx-1 data-vertical:h-4 data-vertical:self-center"
/>
) : null}
{group.map((tool) => (
<IconButton
key={tool.action}
label={tool.label}
onClick={() => handleFormat(tool.action)}
>
<tool.icon />
</IconButton>
))}
</Fragment>
))}
</div>
<textarea
ref={box}
value={draft}
onChange={(event) => setDraft(event.target.value)}
onKeyDown={handleKeyDown}
rows={1}
aria-label={`Message ${target}`}
placeholder={`Message ${target}`}
className="block field-sizing-content max-h-60 min-h-11 w-full resize-none bg-transparent px-3.5 pt-3 pb-1 leading-normal outline-none placeholder:text-muted-foreground"
/>
<div className="flex items-center gap-0.5 px-2 pb-2">
{TOOLS.map((tool) => (
<IconButton key={tool.label} label={tool.label}>
<tool.icon />
</IconButton>
))}
<span className="flex-1" />
<IconButton label="Send" type="submit" variant="default" disabled={!body}>
<SendHorizontal />
</IconButton>
</div>
</form>
)
}
|