Demo
Markdown editor in a form
Documented in Markdown editor.
Markdown editor in a form
MarkdownEditor as a native form field - name, required, submit and reset - with placeholder, readOnly and disabled, a resizable textarea, a server-rendered split preview, right to left, the Turkish locale for case changes, localized labels and part styles.
Demo.tsx
'use client';
import { useState, type ReactNode } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { MarkdownEditor } from '@salmonui/editors/markdown-editor';
import { Radio } from '@salmonui/inputs/radio-group';
const TR_TEXT = {
toolbar: 'Biçimlendirme',
bold: 'Kalın',
italic: 'İtalik',
uppercase: 'Büyük harf',
lowercase: 'Küçük harf',
link: 'Bağlantı',
preview: 'Önizleme',
previewRegion: 'Önizleme',
more: 'Daha fazla',
};
/** A tiny renderer for the preview (paragraphs only): the application owns Markdown parsing. */
const renderParagraphs = (source: string): ReactNode =>
source
.split(/\n{2,}/)
.filter((block) => block.trim())
.map((block, index) => (
<p key={index} style={{ margin: '0 0 8px' }}>
{block}
</p>
));
export default function MarkdownEditorFormDemo() {
const [state, setState] = useState<'editable' | 'readOnly' | 'disabled'>('editable');
const [rtl, setRtl] = useState(false);
const [turkish, setTurkish] = useState(false);
const [submitted, setSubmitted] = useState<string | null>(null);
return (
<form
style={{ display: 'grid', gap: 12 }}
onSubmit={(event) => {
event.preventDefault();
setSubmitted(String(new FormData(event.currentTarget).get('summary') ?? ''));
}}
onReset={() => setSubmitted(null)}
>
<fieldset style={{ display: 'flex', flexWrap: 'wrap', gap: 12, border: 0, padding: 0, margin: 0 }}>
<legend style={{ fontWeight: 600, padding: 0 }}>Options</legend>
{(['editable', 'readOnly', 'disabled'] as const).map((option) => (
<Radio key={option} name="md-state" value={option} checked={state === option} onChange={() => setState(option)}>
{option}
</Radio>
))}
<Checkbox checked={rtl} onCheckedChange={(checked) => setRtl(checked)}>
dir="rtl"
</Checkbox>
<Checkbox checked={turkish} onCheckedChange={(checked) => setTurkish(checked)}>
Turkish locale and labels
</Checkbox>
</fieldset>
<div dir={rtl ? 'rtl' : 'ltr'} style={{ display: 'grid', gap: 6 }}>
<label htmlFor="markdown-summary" style={{ fontWeight: 600 }}>
Summary (required)
</label>
<MarkdownEditor
id="markdown-summary"
name="summary"
required
defaultValue={'The team meets in izmir next week.\n\nSelect "izmir" and press Upper case: the Turkish locale gives İZMİR, English gives IZMIR.'}
placeholder="Write a short summary in Markdown"
readOnly={state === 'readOnly'}
disabled={state === 'disabled'}
toolbar={['bold', 'italic', 'uppercase', 'lowercase', '|', 'link', 'preview']}
defaultPreview="split"
renderPreview={renderParagraphs}
locale={turkish ? 'tr' : undefined}
localeText={turkish ? TR_TEXT : undefined}
resizable
rows={6}
style={{ maxInlineSize: 760 }}
styles={{ preview: { fontStyle: 'italic' }, textarea: { fontFamily: 'ui-monospace, monospace' } }}
/>
</div>
<div style={{ display: 'flex', gap: 8 }}>
<Button type="submit">Submit</Button>
<Button type="reset" variant="outline">Reset</Button>
</div>
<p aria-live="polite" style={{ margin: 0 }}>
{submitted === null ? 'Not submitted yet. Empty the editor and submit to see the required check.' : `Submitted summary: ${submitted.length} characters`}
</p>
</form>
);
}
The editor is a native <textarea name="summary" required> inside a
<form>: Submit reads it with FormData, an empty value is stopped by the
browser's constraint validation, and Reset restores the default text. Empty
it to see the placeholder; switch to readOnly or disabled with the
radio buttons. Drag the corner to resize the textarea (resizable); style
caps the width and styles set the monospace textarea and italic preview.
Tick dir="rtl" to mirror the toolbar (the text keeps dir="auto"), and
"Turkish locale" to pass locale="tr" and Turkish localeText: select
"izmir" and press Upper case to get "İZMİR". The split preview (rendered by
a tiny application function) is part of the server HTML of this page.