How can I make make a popover be triggered by an input? #2746
Unanswered
qiaoandrew
asked this question in
Q&A
Replies: 2 comments 1 reply
-
Popover has a prop called |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hey @benjick, thanks for the response! I tried that, but am getting some weird results. When I focus on the input, it seems like the state change is re-rendering the entire component, causing it the blur instantly. This is my code. I am using the basic shadcn Input and Popover here. Any advice? 'use client';
import { useState } from 'react';
import { useSearchParams, usePathname, useRouter } from 'next/navigation';
import { useDebouncedCallback } from 'use-debounce';
import { SearchIcon } from 'lucide-react';
import FiltersButton from '@/components/buttons/FiltersButton';
import GradientText from '@/components/ui/GradientText';
import { Input } from '@/components/ui/Input';
import {
Popover,
PopoverAnchor,
PopoverContent,
} from '@/components/ui/Popover';
export default function CompareSaleListings() {
const [isOpen, setIsOpen] = useState(false);
const searchParams = useSearchParams();
const pathname = usePathname();
const { replace } = useRouter();
const handleSearch = useDebouncedCallback((query: string) => {
const params = new URLSearchParams(searchParams);
if (query) {
params.set('query', query);
} else {
params.delete('query');
}
replace(`${pathname}?${params.toString()}`);
}, 300);
return (
<>
<div className="container-xl mb-9 xl:mb-12">
<h1 className="mb-3 font-display text-8 font-[750] xl:mb-4 xl:text-10">
<GradientText>Compare Listings</GradientText>
</h1>
<Popover open={isOpen}>
<PopoverAnchor asChild>
<div className="flex w-full max-w-[760px] gap-x-2">
<div className="relative grow p-px">
<SearchIcon
size={18}
className="absolute left-3.5 top-1/2 -translate-y-1/2 text-muted-foreground"
/>
<Input
placeholder="Search by address, city, zip..."
className="rounded-2.5 pl-[40px] text-4"
onChange={(e) => handleSearch(e.target.value)}
onFocus={() => setIsOpen(true)}
onBlur={() => setIsOpen(false)}
defaultValue={searchParams.get('query')?.toString()}
/>
</div>
<FiltersButton />
</div>
</PopoverAnchor>
<PopoverContent>Test</PopoverContent>
</Popover>
</div>
</>
);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I want the popover content to be visible when the input is focused, and make the content hidden when the input is blurred. How could I do this?
Beta Was this translation helpful? Give feedback.
All reactions