import React, { Fragment, useState, useEffect, useLayoutEffect, useRef, FC } from "react" import { useDebounce } from "use-debounce"; import { useTypedCodeblocksStorage } from "../useCodeblocksStorage"; export interface LocalStorateTextProps { readOnly?: boolean storageKey: string initialValue?: string placeholder?: string stylePreset?: 'inline' | 'bold' | 'placeholder' sensitive?: boolean /** rendered before the value, not stored */ prefix?: string /** rendered after the value, not stored */ suffix?: string } // dashed box like starlight-codeblocks fill-in placeholders, so readers can tell it is the same value const placeholderStyle: React.CSSProperties = { border: '1px dashed color-mix(in srgb, currentColor 60%, transparent)', borderRadius: '3px', padding: '1px 3px', background: 'color-mix(in srgb, currentColor 10%, transparent)', fontFamily: 'var(--sl-font-mono, var(--sl-font-system-mono))', // form controls don't inherit font by default; keep the input and its measuring mirror on the same font fontSize: 'inherit', lineHeight: 'inherit', letterSpacing: 'inherit', }; // editable input that blends into a surrounding element; dashed underline marks it as editable const inlineInputStyle: React.CSSProperties = { border: 0, borderBottom: '1px dashed color-mix(in srgb, currentColor 60%, transparent)', padding: 0, background: 'transparent', color: 'inherit', font: 'inherit', letterSpacing: 'inherit', }; const copyButtonStyle: React.CSSProperties = { display: 'inline-flex', verticalAlign: 'middle', marginInlineStart: '0.25em', padding: '2px', border: 0, borderRadius: '3px', background: 'transparent', color: 'inherit', opacity: 0.7, cursor: 'pointer', }; // inputs are not part of the document text selection, so prefix + value + suffix can't be selected together; copy it instead const CopyButton: FC<{ text: string }> = ({ text }) => { const [copied, setCopied] = useState(false); useEffect(() => { if (!copied) return; const t = setTimeout(() => setCopied(false), 1500); return () => clearTimeout(t); }, [copied]); const copy = () => navigator.clipboard.writeText(text).then(() => setCopied(true), console.error); const label = copied ? 'Copied' : `Copy ${text}`; return ; }; export type FixedStorageProps = Omit; const InternalLocalStorageText: FC = (props) => { const { readOnly = false, stylePreset, placeholder, storageKey, initialValue = "", sensitive = false, prefix = '', suffix = '' } = props const [storageVal, setStorageVal] = useTypedCodeblocksStorage(storageKey, initialValue); const [inputValue, setInputValue] = React.useState(storageVal); const [value, setValue] = useDebounce(inputValue, 500); useEffect(() => { setStorageVal(value); }, [value, setStorageVal]); const mirrorRef = useRef(null); const [textWidth, setTextWidth] = useState(undefined); useLayoutEffect(() => { if (mirrorRef.current) setTextWidth(mirrorRef.current.getBoundingClientRect().width); }, [inputValue, placeholder]); const handleInputChange = (event: React.ChangeEvent) => { setInputValue(event.target.value); } useEffect(() => { setValue(storageVal); setInputValue(storageVal); }, [storageVal, setValue, setInputValue]) const sensitiveClass = sensitive ? 'rr-mask' : ''; const withCopy = (val: string) => prefix !== '' || suffix !== '' ? : null; if(readOnly) { let roVal: string = ''; if(storageVal !== undefined && storageVal !== null && storageVal.trim() !== '') { roVal = storageVal; } else if(initialValue !== '') { roVal = initialValue; } else if(placeholder !== undefined) { roVal = placeholder; } let styled: React.ReactNode; switch(stylePreset) { case 'inline': styled = {roVal}; break; case 'bold': styled = {roVal}; break; case 'placeholder': styled = {roVal}; break; default: styled = {roVal}; } return <>{prefix}{styled}{suffix}{withCopy(roVal)}; } // as wide as the value (or placeholder text). `ch` alone is not reliable: the rendered glyphs can be wider than the // `0` that `ch` measures (font fallback, sub-pixel rounding, caret), so measure the real text with a hidden mirror. const mirrorText = inputValue || placeholder || ' '; const width = textWidth === undefined ? `${Math.max(1, mirrorText.length)}ch` : `${Math.ceil(textWidth) + 2}px`; if (stylePreset !== 'placeholder' && stylePreset !== 'inline') { return <>{prefix}{suffix}{withCopy(inputValue || placeholder || '')} } const baseStyle = stylePreset === 'inline' ? inlineInputStyle : placeholderStyle; const field = <> ; return <>{prefix}{stylePreset === 'inline' ? {field}{suffix} : <>{field}{suffix}}{withCopy(inputValue || placeholder || '')} } export const LocalStorageText: FC = (props) => { // stored values only exist in the browser, so render nothing during SSR and the first hydration pass const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); return mounted ? : null; }