效果预览

代码范例
技术栈: react19 + Tailwind CSS
ts
const [isSearchEnable, setIsSearchEnable] = useState(false);
const [searchValue, setSearchValue] = useState("");
html
{isSearchEnable && (
<div className="relative">
<input
id="search-input"
className="peer border border-gray-300 px-4 py-2 w-64 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition"
placeholder=" "
value={searchValue}
onChange={(e) => setSearchValue(e.target.value)}
/>
<label
htmlFor="search-input"
className="absolute left-2 -top-2 bg-white px-2 text-xs text-gray-500 transition-all duration-200 cursor-pointer
peer-placeholder-shown:top-2
peer-placeholder-shown:text-sm
peer-focus:-top-2
peer-focus:left-2
peer-focus:text-blue-500
peer-focus:text-xs"
>
搜索
</label>
{searchValue && (
<button
onClick={() => setSearchValue("")}
className="absolute right-4 top-2 text-gray-400 hover:text-gray-600 cursor-pointer peer-focus:text-blue-500"
>
✕
</button>
)}
</div>
)}