|
| 1 | +import { useEffect, useMemo, useState } from "react"; |
| 2 | +import { IconButton } from "./IconButton.component"; |
| 3 | + |
| 4 | +import styles from "./EditableTitle.component.module.css"; |
| 5 | + |
| 6 | +const EditableTitle = () => { |
| 7 | + const countdownTitleSettingKey = "title"; |
| 8 | + const [displayText, setDisplayText] = useState("Please stand by"); |
| 9 | + const [newText, setNewText] = useState("Please stand by"); |
| 10 | + const [isEditVisible, setEditVisible] = useState(false); |
| 11 | + const [isEditMode, setEditMode] = useState(false); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + const titleSettingValue = localStorage.getItem(countdownTitleSettingKey); |
| 15 | + if (titleSettingValue) { |
| 16 | + setDisplayText(titleSettingValue); |
| 17 | + } |
| 18 | + }, []); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + localStorage.setItem(countdownTitleSettingKey, displayText); |
| 22 | + }, [displayText]); |
| 23 | + |
| 24 | + const editButton = useMemo( |
| 25 | + () => ( |
| 26 | + <IconButton |
| 27 | + text="✏️" |
| 28 | + tooltip="Edit text" |
| 29 | + onClick={() => setEditMode(true)} |
| 30 | + /> |
| 31 | + ), |
| 32 | + [] |
| 33 | + ); |
| 34 | + |
| 35 | + const confirmDiscardButtons = useMemo( |
| 36 | + () => ( |
| 37 | + <> |
| 38 | + <IconButton |
| 39 | + text="✅" |
| 40 | + tooltip="Accept" |
| 41 | + onClick={() => { |
| 42 | + setDisplayText(newText); |
| 43 | + setEditMode(false); |
| 44 | + }} |
| 45 | + /> |
| 46 | + <IconButton |
| 47 | + text="❌" |
| 48 | + tooltip="Discard" |
| 49 | + onClick={() => { |
| 50 | + setEditMode(false); |
| 51 | + setEditVisible(false); |
| 52 | + }} |
| 53 | + /> |
| 54 | + </> |
| 55 | + ), |
| 56 | + [newText] |
| 57 | + ); |
| 58 | + |
| 59 | + return ( |
| 60 | + <div |
| 61 | + className="flex flex-row flex-wrap items-center justify-center mb-8 w-full" |
| 62 | + onMouseOver={() => setEditVisible(true)} |
| 63 | + onMouseOut={() => setEditVisible(false)} |
| 64 | + > |
| 65 | + {!isEditMode && ( |
| 66 | + <> |
| 67 | + <h1 className="text-5xl uppercase">{displayText}</h1> |
| 68 | + {isEditVisible && editButton} |
| 69 | + </> |
| 70 | + )} |
| 71 | + {isEditMode && ( |
| 72 | + <> |
| 73 | + <input |
| 74 | + type="text" |
| 75 | + style={styles} |
| 76 | + defaultValue={newText} |
| 77 | + autoFocus |
| 78 | + size={newText.length} |
| 79 | + onChange={(e) => { |
| 80 | + const text = e.target.value; |
| 81 | + if (text.length > 1) { |
| 82 | + setNewText(e.target.value); |
| 83 | + } |
| 84 | + }} |
| 85 | + onKeyDown={(e) => { |
| 86 | + switch (e.key) { |
| 87 | + case "Escape": |
| 88 | + setEditMode(false); |
| 89 | + break; |
| 90 | + case "Enter": |
| 91 | + setDisplayText(newText); |
| 92 | + setEditMode(false); |
| 93 | + break; |
| 94 | + default: |
| 95 | + break; |
| 96 | + } |
| 97 | + }} |
| 98 | + /> |
| 99 | + {confirmDiscardButtons} |
| 100 | + </> |
| 101 | + )} |
| 102 | + </div> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +export default EditableTitle; |
0 commit comments