-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: layout shift caused by Dropdownmenu and make it to a separate co…
…mponent
- Loading branch information
Rajesh Gollapudi
committed
Jan 17, 2025
1 parent
83f5a69
commit 8468b01
Showing
2 changed files
with
78 additions
and
74 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useRef } from 'react' | ||
import { useSubmit, Link, Form } from 'react-router' | ||
import { getUserImgSrc } from '#app/utils/misc.tsx' | ||
import { useUser } from '#app/utils/user.ts' | ||
import { Button } from './ui/button' | ||
import { | ||
DropdownMenu, | ||
DropdownMenuTrigger, | ||
DropdownMenuPortal, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
} from './ui/dropdown-menu' | ||
import { Icon } from './ui/icon' | ||
|
||
export function UserDropdown() { | ||
const user = useUser() | ||
const submit = useSubmit() | ||
const formRef = useRef<HTMLFormElement>(null) | ||
return ( | ||
<DropdownMenu modal={false}> | ||
<DropdownMenuTrigger asChild> | ||
<Button asChild variant="secondary"> | ||
<Link | ||
to={`/users/${user.username}`} | ||
// this is for progressive enhancement | ||
onClick={(e) => e.preventDefault()} | ||
className="flex items-center gap-2" | ||
> | ||
<img | ||
className="h-8 w-8 rounded-full object-cover" | ||
alt={user.name ?? user.username} | ||
src={getUserImgSrc(user.image?.id)} | ||
/> | ||
<span className="text-body-sm font-bold"> | ||
{user.name ?? user.username} | ||
</span> | ||
</Link> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuPortal> | ||
<DropdownMenuContent sideOffset={8} align="end"> | ||
<DropdownMenuItem asChild> | ||
<Link prefetch="intent" to={`/users/${user.username}`}> | ||
<Icon className="text-body-md" name="avatar"> | ||
Profile | ||
</Icon> | ||
</Link> | ||
</DropdownMenuItem> | ||
<DropdownMenuItem asChild> | ||
<Link prefetch="intent" to={`/users/${user.username}/notes`}> | ||
<Icon className="text-body-md" name="pencil-2"> | ||
Notes | ||
</Icon> | ||
</Link> | ||
</DropdownMenuItem> | ||
<DropdownMenuItem | ||
asChild | ||
// this prevents the menu from closing before the form submission is completed | ||
onSelect={async (event) => { | ||
event.preventDefault() | ||
await submit(formRef.current) | ||
}} | ||
> | ||
<Form action="/logout" method="POST" ref={formRef}> | ||
<Icon className="text-body-md" name="exit"> | ||
<button type="submit">Logout</button> | ||
</Icon> | ||
</Form> | ||
</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenuPortal> | ||
</DropdownMenu> | ||
) | ||
} |
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