Skip to content

Commit

Permalink
Use real links in dropdown menus (#2343)
Browse files Browse the repository at this point in the history
add DropdownMenu.LinkItem so we can use real links when possible
  • Loading branch information
david-crespo authored Aug 14, 2024
1 parent 68e2dc8 commit 29398e7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
6 changes: 1 addition & 5 deletions app/components/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Copyright Oxide Computer Company
*/
import React from 'react'
import { useNavigate } from 'react-router-dom'

import { navToLogin, useApiMutation } from '@oxide/api'
import { DirectionDownIcon, Profile16Icon } from '@oxide/design-system/icons/react'
Expand All @@ -17,7 +16,6 @@ import { DropdownMenu } from '~/ui/lib/DropdownMenu'
import { pb } from '~/util/path-builder'

export function TopBar({ children }: { children: React.ReactNode }) {
const navigate = useNavigate()
const logout = useApiMutation('logout', {
onSuccess: () => navToLogin({ includeCurrent: false }),
})
Expand Down Expand Up @@ -63,9 +61,7 @@ export function TopBar({ children }: { children: React.ReactNode }) {
</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content align="end" sideOffset={8}>
<DropdownMenu.Item onSelect={() => navigate(pb.profile())}>
Settings
</DropdownMenu.Item>
<DropdownMenu.LinkItem to={pb.profile()}>Settings</DropdownMenu.LinkItem>
{loggedIn ? (
<DropdownMenu.Item onSelect={() => logout.mutate({})}>
Sign out
Expand Down
10 changes: 10 additions & 0 deletions app/ui/lib/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import {
} from '@radix-ui/react-dropdown-menu'
import cn from 'classnames'
import { forwardRef, type ForwardedRef } from 'react'
import { Link } from 'react-router-dom'

type DivRef = ForwardedRef<HTMLDivElement>

// remove possibility of disabling links for now. if we put it back, make sure
// to forwardRef on LinkItem so the disabled tooltip can work
type LinkitemProps = Omit<DropdownMenuItemProps, 'disabled'> & { to: string }

export const DropdownMenu = {
Root,
Trigger,
Expand All @@ -38,4 +43,9 @@ export const DropdownMenu = {
Item: forwardRef(({ className, ...props }: DropdownMenuItemProps, ref: DivRef) => (
<Item {...props} className={cn('DropdownMenuItem ox-menu-item', className)} ref={ref} />
)),
LinkItem: ({ className, children, to, ...props }: LinkitemProps) => (
<Item {...props} className={cn('DropdownMenuItem ox-menu-item', className)} asChild>
<Link to={to}>{children}</Link>
</Item>
),
}
2 changes: 1 addition & 1 deletion app/ui/styles/components/menu-button.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@apply z-30 min-w-36 rounded border p-0 bg-raise border-secondary;

& .DropdownMenuItem {
@apply block cursor-pointer select-none border-b py-2 pl-3 pr-6 text-left text-sans-md text-secondary border-secondary last-of-type:border-b-0;
@apply block cursor-pointer select-none border-b py-2 pl-3 pr-6 text-left text-sans-md text-secondary border-secondary last:border-b-0;

&.destructive {
@apply text-destructive;
Expand Down

0 comments on commit 29398e7

Please sign in to comment.