Put HTML elements directly on top of the input using CSS. Apply pointer-events: none
to those elements. Add onFocus
and onBlur
behavior to the input. Control the hiding/showing of your placeholder content using local state. It should be hidden if the user is focused (your preference) or if there is text that already exists. Here is a small functional example. https://codesandbox.io/s/elated-galileo-k5zot
const Sample = () => {
const [showPlaceholder, setShowPlaceholder] = React.useState(true)
const [value, setValue] = React.useState('')
const show = () => setShowPlaceholder(true)
const hide = () => setShowPlaceholder(false)
const update = (e) => setValue(e.currentTarget.value)
return (
<div style={{ position: 'relative' }}>
{showPlaceholder && !value && (
<div style={{ position: 'absolute', left: 5, top: 0, pointerEvents: 'none' }}>
<span>
custom {' '}
</span>
<span style={{ color: 'red' }}>
element {' '}
</span>
<span style={{ color: 'blue' }}>
here {' '}
</span>
</div>
)}
<input onFocus={hide} onBlur={show} onChange={update} />
</div>
)
}