Skip to content

Instantly share code, notes, and snippets.

@hujuu
Created February 20, 2024 10:12
Show Gist options
  • Save hujuu/4ff31632ef09de4da62f3940f24fc3b4 to your computer and use it in GitHub Desktop.
Save hujuu/4ff31632ef09de4da62f3940f24fc3b4 to your computer and use it in GitHub Desktop.
フォーカス時にツールチップを出す(案1)
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