diff --git a/src/components/EditableTitle.module.css b/src/components/EditableTitle.module.css new file mode 100644 index 0000000..7cf9b23 --- /dev/null +++ b/src/components/EditableTitle.module.css @@ -0,0 +1,17 @@ +@layer components { + .editable_layout { + display: flex; + flex-direction: row; + align-items: center; + gap: var(--spacing-m); + :where(h3, input) { + font-size: var(--font-size-xl); + font-weight: 700; + line-height: 1em; + } + h3 { + border: 1px solid transparent; + padding: var(--spacing-s) var(--spacing-m); + } + } +} diff --git a/src/components/EditableTitle.tsx b/src/components/EditableTitle.tsx new file mode 100644 index 0000000..dc59adf --- /dev/null +++ b/src/components/EditableTitle.tsx @@ -0,0 +1,43 @@ +import { Icon } from '@iconify/react/dist/iconify.js'; +import { useRef, useState } from 'react'; +import styles from './EditableTitle.module.css'; + +type EditableTitleProps = { + title: string; + onChange?: (newTitle: string) => void; +}; + +export function EditableTitle({ title, onChange }: EditableTitleProps) { + const [isEditing, setIsEditing] = useState(false); + const formRef = useRef(null); + const cancelEdit = () => { + setIsEditing(false); + formRef.current?.reset(); + }; + const handleSubmit = (data: FormData) => { + const newTitle = data.get('title') as string; + if (newTitle !== title && onChange) { + onChange?.(newTitle); + } + cancelEdit(); + }; + + return isEditing ? ( +
+ +
formRef.current?.requestSubmit()}> + +
+
+ +
+
+ ) : ( +
+

{title}

+
setIsEditing(true)}> + +
+
+ ); +}