Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a button that shows loading state while nav #46

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions src/app/user-nav.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use client'

import Link from 'next/link'
import { Button, Title } from '@mantine/core'

import { Title } from '@mantine/core'
import { useAuthInfo } from '@/components/auth'
import { ButtonNav } from '@/components/button'

export const UserNav = () => {
const auth = useAuthInfo()
Expand All @@ -14,25 +13,13 @@ export const UserNav = () => {
You appear to be a {auth.role} and likely want to{' '}
{auth.isResearcher && (
<>
<Link href="/researcher/studies" passHref>
<Button>View Studies</Button>
</Link>
<ButtonNav href="/researcher/studies">View Studies</ButtonNav>
<span> OR </span>
<Link href="/researcher/study/request/openstax" passHref>
<Button>Propose a Study</Button>
</Link>
<ButtonNav href="/researcher/study/request/openstax">Propose a Study</ButtonNav>
</>
)}
{auth.isMember && (
<Link href="/member/openstax/studies/review" passHref>
<Button>Review Studies</Button>
</Link>
)}
{auth.isAdmin && (
<Link href="/admin/members" passHref>
<Button>Administer Members</Button>
</Link>
)}
{auth.isMember && <ButtonNav href="/member/openstax/studies/review">Review Studies</ButtonNav>}
{auth.isAdmin && <ButtonNav href="/admin/members">Administer Members</ButtonNav>}
</Title>
</div>
)
Expand Down
31 changes: 31 additions & 0 deletions src/components/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Link from 'next/link'
import { Button, type ButtonProps } from '@mantine/core'
import { useDisclosure } from '@mantine/hooks'
import { MouseEventHandler } from 'react'

type ButtonLinkProps = ButtonProps & {
href: string
target?: string
rel?: string
}

export const ButtonNav: React.FC<ButtonLinkProps> = ({ href, children, target, rel, ...buttonProps }) => {
const [isBusy, { open: setBusy }] = useDisclosure()

const handleClicks = Boolean(target !== '_blank' && rel !== 'external')

const onClick: MouseEventHandler<HTMLAnchorElement> = (event) => {
if (event.button != null && event.button > 0) return // 0 == main button
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
if (event.defaultPrevented) return

setBusy()
}
return (
<Link href={href} onClick={handleClicks ? onClick : undefined} target={target} rel={rel}>
<Button {...buttonProps} aria-busy={isBusy} disabled={isBusy} loading={isBusy}>
{children}
</Button>
</Link>
)
}
Loading