Created
February 20, 2024 10:12
-
-
Save hujuu/4ff31632ef09de4da62f3940f24fc3b4 to your computer and use it in GitHub Desktop.
フォーカス時にツールチップを出す(案1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useRef, useState } from 'react'; | |
import { useFloating } from '@floating-ui/react'; | |
const TooltipInput = () => { | |
const [isFocused, setIsFocused] = useState(false); | |
const referenceElement = useRef(null); | |
const popperElement = useRef(null); | |
const { update } = useFloating(); | |
useEffect(() => { | |
if (isFocused) { | |
referenceElement.current && popperElement.current && update(); | |
} | |
}, [isFocused, update]); | |
return ( | |
<div> | |
<input | |
ref={referenceElement} | |
onFocus={() => setIsFocused(true)} | |
onBlur={() => setIsFocused(false)} | |
/> | |
{isFocused && ( | |
<div ref={popperElement} style={{ position: 'absolute' }}> | |
ここに入力してください | |
</div> | |
)} | |
</div> | |
); | |
}; | |
export default TooltipInput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment