Skip to content
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
5 changes: 4 additions & 1 deletion src/components/LoginModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import {
DialogContent,
DialogHeader,
} from '~/components/ds/ui'
import type { RestoreFocusRef } from '~/components/ds/ui/Dialog'

interface LoginModalProps {
open: boolean
description?: string
onOpenChange: (open: boolean) => void
restoreFocusRef?: RestoreFocusRef
onSocialSignIn: (provider: OAuthProvider) => void
}

export function LoginModal({
open,
description,
onOpenChange,
restoreFocusRef,
onSocialSignIn,
}: LoginModalProps) {
const providers = useQuery({
Expand All @@ -30,7 +33,7 @@ export function LoginModal({
})
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent size="xs">
<DialogContent size="xs" restoreFocusRef={restoreFocusRef}>
<DialogHeader title="Sign in to continue" description={description} />
<DialogBody className="pb-6">
<div className="space-y-3">
Expand Down
12 changes: 11 additions & 1 deletion src/components/ds/ui/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ const sizeStyles: Record<DialogSize, string> = {
xl: 'max-w-xl',
}

export type RestoreFocusRef = React.RefObject<HTMLElement | null>

type DialogContentProps = {
/** Explicit opener or fallback for externally controlled dialogs. */
restoreFocusRef?: RestoreFocusRef
children: React.ReactNode
size?: DialogSize
className?: string
Expand All @@ -48,7 +52,10 @@ type DialogContentProps = {
export const DialogContent = React.forwardRef<
HTMLDivElement,
DialogContentProps
>(function DialogContent({ children, size = 'sm', className }, ref) {
>(function DialogContent(
{ children, size = 'sm', className, restoreFocusRef },
ref,
) {
return (
<DialogPrimitive.Portal>
<DialogPrimitive.Backdrop
Expand All @@ -58,6 +65,9 @@ export const DialogContent = React.forwardRef<
<DialogPrimitive.Popup
ref={ref}
data-ds-dialog-panel=""
finalFocus={() =>
restoreFocusRef?.current?.isConnected ? restoreFocusRef.current : true
}
className={twMerge(
// Centring uses the independent `translate` property (that is what
// Tailwind v4 compiles these to), which leaves `transform` free for
Expand Down
53 changes: 26 additions & 27 deletions src/components/npm-stats/BaselineSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DialogBody,
DialogContent,
DialogHeader,
DialogTrigger,
} from '~/components/ds/ui'
import { PackageSearch } from './PackageSearch'
import { getBaselineDisplayName, type PackageGroup } from './shared'
Expand Down Expand Up @@ -137,14 +138,14 @@ export function BaselineSection({

const addButton = (
<Tooltip content="Search for a package to add as baseline">
<button
onClick={() => setShowSearch(true)}
<DialogTrigger
type="button"
className="flex items-center gap-1 px-1.5 py-0.5 text-xs rounded
text-blue-700 dark:text-blue-300 hover:bg-blue-500/10 font-medium"
>
<PlusIcon className="w-3 h-3" />
Add
</button>
</DialogTrigger>
</Tooltip>
)

Expand Down Expand Up @@ -234,7 +235,7 @@ export function BaselineSection({
)

return (
<>
<Dialog open={showSearch} onOpenChange={setShowSearch}>
<div className="flex flex-wrap items-center gap-x-2 gap-y-1 text-xs">
{labelButton}

Expand All @@ -250,29 +251,27 @@ export function BaselineSection({
</div>
</div>

<Dialog open={showSearch} onOpenChange={setShowSearch}>
<DialogContent size="md">
<DialogHeader
title={
<span className="flex items-center gap-2">
<PushPinIcon className="w-4 h-4 text-icon-accent" />
Add baseline package
</span>
}
<DialogContent size="md">
<DialogHeader
title={
<span className="flex items-center gap-2">
<PushPinIcon className="w-4 h-4 text-icon-accent" />
Add baseline package
</span>
}
/>
<DialogBody className="pb-6">
<PackageSearch
onSelect={(pkg) => {
onAddBaseline(pkg)
setShowSearch(false)
}}
placeholder="Search for baseline package..."
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={true}
/>
<DialogBody className="pb-6">
<PackageSearch
onSelect={(pkg) => {
onAddBaseline(pkg)
setShowSearch(false)
}}
placeholder="Search for baseline package..."
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={true}
/>
</DialogBody>
</DialogContent>
</Dialog>
</>
</DialogBody>
</DialogContent>
</Dialog>
)
}
9 changes: 9 additions & 0 deletions src/contexts/LoginModalContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ export function LoginModalProvider({ children }: LoginModalProviderProps) {
> | null>(null)
const pendingOnSuccessRef = React.useRef<(() => void) | undefined>(undefined)

const openerRef = React.useRef<HTMLElement | null>(null)

const openLoginModal = React.useCallback(
(options?: { description?: string; onSuccess?: () => void }) => {
const active = document.activeElement
openerRef.current =
active instanceof HTMLElement && active !== document.body
? active
: null

pendingOnSuccessRef.current = options?.onSuccess
setDescription(options?.description)
setHasLoadedModal(true)
Expand Down Expand Up @@ -129,6 +137,7 @@ export function LoginModalProvider({ children }: LoginModalProviderProps) {
open={isOpen}
description={description}
onOpenChange={handleOpenChange}
restoreFocusRef={openerRef}
onSocialSignIn={openSocialPopup}
/>
</React.Suspense>
Expand Down
22 changes: 17 additions & 5 deletions src/routes/admin/roles.$roleId.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Link, redirect, createFileRoute } from '@tanstack/react-router'
import { useState, useMemo, useCallback } from 'react'
import { useState, useMemo, useCallback, useRef } from 'react'
import { useRemoveUsersFromRole } from '~/utils/mutations'
import { useQuery } from '@tanstack/react-query'
import { getRole, getUsersWithRole } from '~/utils/roles.functions'
Expand Down Expand Up @@ -47,6 +47,9 @@ function RoleDetailPage() {
name: string
} | null>(null)

const openerRef = useRef<HTMLElement | null>(null)
const tableFocusRef = useRef<HTMLDivElement>(null)

const userQuery = useCurrentUserQuery()
const user = userQuery.data
const roleQuery = useQuery({
Expand Down Expand Up @@ -200,12 +203,16 @@ function RoleDetailPage() {
header: 'Actions',
cell: ({ row }) => {
const user = row.original
const name = user.name || user.email
return (
<button
onClick={() => {
type="button"
aria-label={`Remove ${name} from role`}
onClick={(e) => {
openerRef.current = e.currentTarget
setConfirmRemove({
userId: user._id,
name: user.name || user.email,
name,
})
}}
className="text-red-600 hover:text-red-900 dark:text-red-400 dark:hover:text-red-300"
Expand Down Expand Up @@ -336,7 +343,7 @@ function RoleDetailPage() {
if (!open) setConfirmRemove(null)
}}
>
<DialogContent size="sm">
<DialogContent size="sm" restoreFocusRef={openerRef}>
<DialogHeader
title="Confirm Removal"
description={
Expand All @@ -360,6 +367,7 @@ function RoleDetailPage() {
roleId: roleId,
userIds: [confirmRemove.userId],
})
openerRef.current = tableFocusRef.current
setConfirmRemove(null)
} catch (error) {
console.error(
Expand All @@ -381,7 +389,11 @@ function RoleDetailPage() {
</DialogContent>
</Dialog>

<div className="bg-white dark:bg-gray-800 rounded-lg shadow-lg overflow-hidden">
<div
ref={tableFocusRef}
tabIndex={-1}
className="bg-white dark:bg-gray-800 rounded-lg shadow-lg overflow-hidden"
>
<div className="overflow-x-auto">
<table className="w-full">
<thead className="bg-gray-50 dark:bg-gray-700">
Expand Down
Loading