Files
supabase/apps/studio/components/layouts/Navigation/LayoutHeader/FeedbackDropdown/FeedbackWidget.tsx
T
Danny White e3d7267845 fix(studio): chip away explicit-tabindex ratchet debt (#48040)
## What kind of change does this PR introduce?

A11y cleanup follow-up to #47984 /
[DEPR-626](https://linear.app/supabase/issue/DEPR-626).

## What is the current behavior?

Studio had 82 ratcheted `supabase/require-explicit-tabindex` violations
(raw `<button>` / `role="button"` without explicit `tabIndex`).

## What is the new behavior?

- Explicit `tabIndex={0}` (or disabled → `-1`) on those Studio call
sites across nav, `components/ui`, Database, Storage, and the remainder
- Ratchet baseline cleared (**82 → 0**) and the rule **removed from the
Studio ratchet** (debt is gone; ratchet is temporary)
- Rule remains a shared **`warn`** for now — promoting to `error` (and
sweeping www/docs/design-system) is a follow-up
- Also fixed the learn/ui-library call sites that surfaced while
experimenting with error promotion
- Small follow-ups where making controls focusable exposed gaps:
accessible names, disabled/focus consistency, focus-ring polish on
To-test surfaces, home section `KeyboardSensor`, and an E2E locator
tightened after `aria-label="Remove column"`

Prefer migrating to `Button` from `ui` in future touch-ups; this PR
takes the minimal path so Studio debt can stay at zero.

## Additional context

Batches landed together so baseline conflicts stayed simple while
chipping away:

- Hotspots / nav (FirstLevelNav, Marketplace, AttachmentUpload, Column,
Tabs, …)
- `components/ui` shared
- Database + Storage
- Remainder

**Out of scope / intentional deferrals**

- Promoting `supabase/require-explicit-tabindex` to a lint **error**
(follow-up after www/docs/design-system sweeps)
- Tabs/Radio roving, tooltips, context menus, in-menu items
- Full keyboard-accessible tab-close UX (close stays hover +
`tabIndex={-1}`; context menu still closes tabs)
- Data API docs links (`/project/<ref>/api` redirect)

**Reviewer notes**

- Rule only flags raw `<button>` / `role="button"` without a `tabIndex`
prop. `Button` from `ui` already bakes this in
- `tabIndex={-1}` is intentional for disabled controls, in-menu /
roving-focus children, and hover-only tab close
- For dnd-kit grips, put `tabIndex` **after** `{...attributes}` so it
isn’t overwritten (TS2783)

### To test

Use **Safari** with macOS Keyboard navigation **off** (System Settings →
Keyboard). Chrome once for a sanity pass. For each surface below: Tab
until the control is focused, then activate with Enter/Space where
relevant.

1. **API Docs side panel** (Table Editor → open a table → **API docs**)
- Floating API Docs panel — **not** `/project/<ref>/api` (that redirects
to Data API docs; language ToggleGroup uses arrow keys; links are out of
scope)
- Left nav buttons — Tab through several and activate one; active
highlight / navigation still works

2. **Integrations → Marketplace**
- Enable **Integrations layout** feature preview first (avatar menu →
Feature previews)
   - `/org/<slug>/integrations` or project integrations marketplace
   - “Clear all”, grid/list toggles — Tab + activate

3. **Table Editor → create a table → Columns**
- Drag handles only appear while **creating** (not when editing an
existing table)
   - Tab to grip / remove (X) / sensitive-data eye if shown

4. **Project Home** — section drag handles
   - Tab to a grip (visible focus ring)
- Optional: Space to pick up, arrows to move, Space/Esc to drop
(KeyboardSensor added)
   - Mouse dnd still works

5. **Storage → Policies** — expand/collapse bucket list chevron
(design-system focus ring, no stuck grey open bg)

6. **Support form** (Help → Support) — attachment remove (×) and
add-attachment control when visible

Disabled controls should be **skipped** by Tab.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Accessibility Improvements**
* Improved keyboard navigation throughout Studio by explicitly managing
focus (`tabIndex`) across many interactive controls (menus, tabs,
tables, charts, dialogs, navigation, and form actions).
* Disabled or non-interactive controls are now removed from the tab
order (or made unfocusable), while available actions remain reachable.
* Ensured `type="button"` on relevant controls to prevent unintended
submissions, and refined keyboard focus behavior for various toggles and
copy/remove actions.
* **Chores**
* Updated the ESLint rule baseline configuration to match the new focus
behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-21 08:22:43 +10:00

326 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useDebounce } from '@uidotdev/usehooks'
import { LOCAL_STORAGE_KEYS, useParams } from 'common'
import { AnimatePresence, motion } from 'framer-motion'
import { toPng } from 'html-to-image'
import { Camera, CircleCheck, Image as ImageIcon, Upload, X } from 'lucide-react'
import { useRouter } from 'next/router'
import { ChangeEvent, useEffect, useRef, useState } from 'react'
import { toast } from 'sonner'
import {
Button,
cn,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
PopoverSeparator,
TextArea,
} from 'ui'
import { Admonition } from 'ui-patterns/admonition'
import {
convertB64toBlob,
isLikelySupportRequest,
uploadAttachment,
} from './FeedbackDropdown.utils'
import { SupportLink } from '@/components/interfaces/Support/SupportLink'
import { InlineLinkClassName } from '@/components/ui/InlineLink'
import { useFeedbackCategoryQuery } from '@/data/feedback/feedback-category'
import { useSendFeedbackMutation } from '@/data/feedback/feedback-send'
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { timeout } from '@/lib/helpers'
import { useProfile } from '@/lib/profile'
import { useTrack } from '@/lib/telemetry/track'
interface FeedbackWidgetProps {
onClose: () => void
onSwitchToIssueOptions: () => void
}
export const FeedbackWidget = ({ onClose, onSwitchToIssueOptions }: FeedbackWidgetProps) => {
const router = useRouter()
const { profile } = useProfile()
const { ref, slug } = useParams()
const track = useTrack()
const uploadButtonRef = useRef(null)
const [feedback, setFeedback] = useState('')
const [isSending, setSending] = useState(false)
const [isSavingScreenshot, setIsSavingScreenshot] = useState(false)
const [isFeedbackSent, setIsFeedbackSent] = useState(false)
const debouncedFeedback = useDebounce(feedback, 500)
const [storedFeedback, setStoredFeedback] = useLocalStorageQuery<string | null>(
LOCAL_STORAGE_KEYS.FEEDBACK_WIDGET_CONTENT,
null
)
const [screenshot, setScreenshot, { isSuccess }] = useLocalStorageQuery<string | null>(
LOCAL_STORAGE_KEYS.FEEDBACK_WIDGET_SCREENSHOT,
null
)
const { data: category } = useFeedbackCategoryQuery({ prompt: debouncedFeedback })
// Use client-side heuristic for immediate feedback, AI result takes precedence when available
const isLikelySupport = isLikelySupportRequest(feedback)
const effectiveCategory = category ?? (isLikelySupport ? 'support' : null)
const { mutate: submitFeedback } = useSendFeedbackMutation({
onSuccess: () => {
setIsFeedbackSent(true)
setFeedback('')
setStoredFeedback(null)
setScreenshot(null)
setSending(false)
},
onError: (error) => {
toast.error(`Failed to submit feedback: ${error.message}`)
setSending(false)
},
})
const captureScreenshot = async () => {
setIsSavingScreenshot(true)
function filter(node: HTMLElement) {
if ((node?.children ?? []).length > 0) {
return node.children[0].id !== 'feedback-widget'
}
return true
}
// Give time for dropdown to close
await timeout(100)
toPng(document.body, { filter })
.then((dataUrl: any) => setScreenshot(dataUrl))
.catch(() => toast.error('Failed to capture screenshot'))
.finally(() => setIsSavingScreenshot(false))
}
const onFilesUpload = async (event: ChangeEvent<HTMLInputElement>) => {
event.persist()
const [file] = event.target.files || (event as any).dataTransfer.items
const reader = new FileReader()
reader.onload = function (event) {
const dataUrl = event.target?.result
if (typeof dataUrl === 'string') setScreenshot(dataUrl)
}
reader.readAsDataURL(file)
event.target.value = ''
}
const handlePasteEvent = async () => {
// [Joshen] Support pasting images via Cmd / Ctrl + V
const [data] = await navigator.clipboard.read()
if (screenshot === undefined && data.types[0] === 'image/png') {
const blob = await data.getType('image/png')
const reader = new FileReader()
reader.onload = function (event) {
const dataUrl = event.target?.result
if (typeof dataUrl === 'string') setScreenshot(dataUrl)
}
reader.readAsDataURL(blob)
}
}
const sendFeedback = async () => {
if (feedback.length === 0 && screenshot !== undefined) {
return toast.error('Please include a message in your feedback.')
} else if (feedback.length > 0) {
setSending(true)
const attachmentUrl =
screenshot && profile?.gotrue_id
? await uploadAttachment({
image: screenshot,
userId: profile.gotrue_id,
})
: undefined
const formattedFeedback =
attachmentUrl !== undefined ? `${feedback}\n\nAttachments:\n${attachmentUrl}` : feedback
submitFeedback({
projectRef: ref,
organizationSlug: slug,
message: formattedFeedback,
pathname: router.asPath,
})
}
}
// Hydrate form from localStorage once it's ready; deps intentionally omit storedFeedback/screenshot
// so we don't overwrite user edits when those values change after initial load.
useEffect(() => {
if (storedFeedback) setFeedback(storedFeedback)
if (screenshot) setScreenshot(screenshot)
// eslint-disable-next-line react-hooks/exhaustive-deps -- hydrate once when localStorage is ready only
}, [isSuccess])
// Persist debounced input to localStorage; only re-run when debounced value changes.
useEffect(() => {
if (debouncedFeedback.length > 0) setStoredFeedback(debouncedFeedback)
// eslint-disable-next-line react-hooks/exhaustive-deps -- setStoredFeedback is stable; only sync on debounced value
}, [debouncedFeedback])
const ThanksMessageView = () => (
<>
<div className="py-6 px-4 grid gap-4 text-center text-foreground-light">
<CircleCheck className="mx-auto text-brand-500" size={24} />
<div className="flex flex-col gap-1">
<p className="text-foreground text-base">Your feedback has been sent. Thanks!</p>
<p className="text-sm text-balance">
We don’t always respond to feedback. If you need help with your project, use the button
below.
</p>
</div>
</div>
<PopoverSeparator />
<div className="px-4 pt-4 pb-4 flex flex-row items-center justify-between">
<Button variant="default" size="tiny" onClick={onSwitchToIssueOptions}>
Get help
</Button>
<Button variant="default" size="tiny" onClick={onClose}>
Close
</Button>
</div>
</>
)
return isFeedbackSent ? (
<ThanksMessageView />
) : (
<>
<div className="p-4">
<TextArea
placeholder="My idea for improving Supabase is..."
rows={6}
value={feedback}
onChange={(e) => setFeedback(e.target.value)}
onPaste={handlePasteEvent}
className="text-sm mb-1 resize-none"
/>
</div>
<AnimatePresence>
{effectiveCategory === 'support' && (
<motion.div
key="support-alert"
initial={{ opacity: 0, y: 16 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 16 }}
transition={{ duration: 0.2 }}
>
<Admonition
type="caution"
title="This looks like an issue that’s better handled by support"
className="rounded-none border-x-0 border-b-0"
>
<p>
Please{' '}
<SupportLink
className={cn(InlineLinkClassName)}
queryParams={{ projectRef: slug, message: feedback }}
>
open a support ticket
</SupportLink>{' '}
to get help, as we do not reply to all product feedback.
</p>
</Admonition>
</motion.div>
)}
</AnimatePresence>
<PopoverSeparator />
<div className="px-4 pt-4 pb-4 flex flex-row items-center justify-between">
<Button variant="default" size="tiny" onClick={onSwitchToIssueOptions}>
Get help instead
</Button>
<div className="flex items-center gap-2 flex-row">
{!!screenshot ? (
<div
style={{ backgroundImage: `url("${screenshot}")` }}
onClick={() => {
const blob = convertB64toBlob(screenshot)
const blobUrl = URL.createObjectURL(blob)
window.open(blobUrl, '_blank')
}}
className="cursor-pointer rounded-sm h-[26px] w-[26px] border border-control relative bg-cover bg-center bg-no-repeat"
>
<button
type="button"
tabIndex={0}
aria-label="Remove screenshot"
className={[
'cursor-pointer rounded-full bg-red-900 h-3 w-3',
'flex items-center justify-center absolute -top-1 -right-1',
].join(' ')}
onClick={(event) => {
event.stopPropagation()
setScreenshot(null)
}}
>
<X size={8} strokeWidth={3} />
</button>
</div>
) : (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="default"
disabled={isSavingScreenshot}
loading={isSavingScreenshot}
className="w-7"
icon={<ImageIcon size={14} />}
/>
</DropdownMenuTrigger>
<DropdownMenuContent side="bottom" align="end" className="w-fit">
<DropdownMenuItem
className="flex gap-2"
key="upload-screenshot"
onSelect={() => {
if (uploadButtonRef.current) (uploadButtonRef.current as any).click()
}}
>
<Upload size={14} />
Upload screenshot
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
className="flex gap-2"
key="capture-screenshot"
onSelect={() => captureScreenshot()}
>
<Camera size={14} />
Capture screenshot
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)}
<input
type="file"
ref={uploadButtonRef}
className="hidden"
accept="image/png"
onChange={onFilesUpload}
/>
<Button
disabled={feedback.length === 0 || isSending}
loading={isSending}
onClick={() => {
sendFeedback()
track('send_feedback_button_clicked')
}}
>
Send
</Button>
</div>
</div>
</>
)
}