Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"browser-image-compression": "2.0.2",
"diff": "9.0.0",
"lucide-react": "1.75.0",
"markdown-it": "14.1.0",
"motion": "^14.14.0",
"papaparse": "5.7.0",
"react": "^19.3.1",
Expand All @@ -38,6 +39,7 @@
"@testing-library/jest-dom": "^7.0.1",
"@testing-library/react": "^16.3.3",
"@testing-library/user-event": "^14.6.12",
"@types/markdown-it": "14.1.2",
"@types/node": "^24.19.2",
"@types/papaparse": "5.5.2",
"@types/react": "^19.3.0",
Expand Down
71 changes: 71 additions & 0 deletions apps/admin/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions apps/admin/src/components/events/DescriptionField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* The event description on the event form: a textarea with buttons for the little Markdown a description may use
* (bold, italic, a link, a bullet list), and a preview of how the site will show it.
*/
import { Bold, Italic, Link, List, type LucideIcon } from 'lucide-react';
import { useRef, useState } from 'react';
import type { UseFormRegister, UseFormSetValue } from 'react-hook-form';
import type { EventFormValues } from '../../lib/form/event-form';
import { applyFormat, type DescriptionFormat, renderDescription } from '../../lib/ui/markdown';
import { FIELD_INPUT } from '../ui/field-styles';

const TOOLS: readonly Readonly<{ format: DescriptionFormat; label: string; icon: LucideIcon }>[] = [
{ format: 'bold', label: 'Bold', icon: Bold },
{ format: 'italic', label: 'Italic', icon: Italic },
{ format: 'link', label: 'Link', icon: Link },
{ format: 'list', label: 'Bullet list', icon: List },
];

const TAB = 'min-h-11 rounded-full px-4 font-display text-sm font-bold transition-colors duration-150 aria-pressed:bg-card aria-pressed:text-white text-text-2 hover:text-white';

type DescriptionFieldProps = {
register: UseFormRegister<EventFormValues>;
setValue: UseFormSetValue<EventFormValues>;
description: string;
};

export function DescriptionField({ register, setValue, description }: DescriptionFieldProps) {
const [previewing, setPreviewing] = useState(false);
const textarea = useRef<HTMLTextAreaElement | null>(null);
const { ref, ...field } = register('description');

function format(tool: DescriptionFormat) {
const element = textarea.current;
if (!element) return;
const next = applyFormat({ text: element.value, start: element.selectionStart, end: element.selectionEnd }, tool);
setValue('description', next.text, { shouldDirty: true });
// React writes the new text on its next render, so the selection is put back after it.
requestAnimationFrame(() => {
element.focus();
element.setSelectionRange(next.start, next.end);
});
}

return (
<div>
<div className="mb-2 flex flex-wrap items-center justify-between gap-2">
<label htmlFor="event-description" className="font-display text-base font-bold text-white">
Description
</label>
<div className="flex gap-1">
<button type="button" aria-pressed={!previewing} onClick={() => setPreviewing(false)} className={TAB}>
Write
</button>
<button type="button" aria-pressed={previewing} onClick={() => setPreviewing(true)} className={TAB}>
Preview
</button>
</div>
</div>
<div className={previewing ? 'hidden' : 'grid gap-2'}>
<div role="toolbar" aria-label="Formatting" className="flex gap-1">
{TOOLS.map(({ format: tool, label, icon: Icon }) => (
<button
key={tool}
type="button"
aria-label={label}
title={label}
onClick={() => format(tool)}
className="grid size-11 place-items-center rounded-field border-2 border-line text-text-2 transition-[color,border-color,transform] duration-150 hover:border-muted hover:text-white active:scale-97"
>
<Icon aria-hidden="true" className="size-4.5" />
</button>
))}
</div>
<textarea
{...field}
ref={(element) => {
ref(element);
textarea.current = element;
}}
id="event-description"
aria-describedby="event-description-hint"
rows={6}
className={FIELD_INPUT}
placeholder="What should people know?"
/>
<p id="event-description-hint" className="text-sm text-text-2">
**bold**, _italic_, [words](https://link) and lines starting with "- " for a list.
</p>
</div>
{previewing && <DescriptionPreview description={description} />}
</div>
);
}

function DescriptionPreview({ description }: { description: string }) {
if (description.trim() === '') return <p className="rounded-field border-2 border-dashed border-line px-4 py-6 text-text-2">Nothing written yet.</p>;
return (
<div
aria-label="Description preview"
className="grid gap-3 rounded-field border-2 border-line bg-deep px-4 py-4 text-text-2 [&_a]:text-sky [&_a]:underline [&_strong]:text-white [&_ul]:grid [&_ul]:list-disc [&_ul]:gap-1 [&_ul]:pl-5"
// markdown-it escapes any HTML in the text and allows only the formats above, so this is safe to insert.
dangerouslySetInnerHTML={{ __html: renderDescription(description) }}
/>
);
}
Loading
Loading