Files
supabase/apps/design-system/registry/default/example/sheet-confirm-on-close-demo.tsx
Danny White 476d4a5851 refactor(ui): drop redundant Button variant="default" props (#50161)
## What kind of change does this PR introduce?

Mechanical cleanup on top of the Button default-variant change (#50160).

## What is the current behavior?

Many callsites still pass `variant="default"` even though that is now
the component default.

## What is the new behavior?

Removes redundant static `variant="default"` from legacy `Button` and
`ButtonTooltip` callsites. Keeps explicit defaults where they document
the API:

- `button-default.tsx` and `button-sizes.tsx` demos
- `DocsButton`, which pins neutral styling at the wrapper boundary

## To test

Studio:

- [Auth → Rate
Limits](https://studio-staging-2s957kwc4-supabase.vercel.app/dashboard/project/_/auth/rate-limits):
dirty the form so Cancel appears; Cancel stays neutral, Save stays green
- [Project Settings → API
Keys](https://studio-staging-2s957kwc4-supabase.vercel.app/dashboard/project/_/settings/api-keys):
`DocsButton` in the header actions stays neutral

Design system:

- [Design system →
Button](https://design-system-git-dnywh-dc924ac1-supabase.vercel.app/design-system/docs/components/button):
`button-default` / `button-sizes` still show explicit default styling;
Primary (green) is restricted to the Primary section (and `asChild`)

WWW:

- [www → Brand
assets](https://zone-www-dot-com-git-dnywh-dc924ac1-supabase.vercel.app/brand-assets):
Download logo kit / Download button kit stay neutral
2026-09-11 17:05:26 +10:00

222 lines
5.3 KiB
TypeScript

import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
Button,
Input,
Label,
Separator,
Sheet,
SheetContent,
SheetFooter,
SheetHeader,
SheetSection,
SheetTitle,
} from 'ui'
interface EndpointValues {
endpointUrl: string
secretHeader: string
}
interface ConfirmOnCloseModalProps {
visible: boolean
onClose: () => void
onCancel: () => void
}
const defaultValues: EndpointValues = {
endpointUrl: '',
secretHeader: '',
}
const useConfirmOnClose = ({
checkIsDirty,
onClose,
}: {
checkIsDirty: () => boolean
onClose: () => void
}) => {
const [visible, setVisible] = useState(false)
const confirmOnClose = useCallback(() => {
if (checkIsDirty()) {
setVisible(true)
return
}
onClose()
}, [checkIsDirty, onClose])
const handleOpenChange = useCallback(
(open: boolean) => {
if (!open) {
confirmOnClose()
}
},
[confirmOnClose]
)
const onConfirm = useCallback(() => {
setVisible(false)
onClose()
}, [onClose])
const onCancel = useCallback(() => {
setVisible(false)
}, [])
const modalProps: ConfirmOnCloseModalProps = useMemo(
() => ({
visible,
onClose: onConfirm,
onCancel,
}),
[visible, onConfirm, onCancel]
)
return {
confirmOnClose,
handleOpenChange,
modalProps,
}
}
const DiscardChangesAlertDialog = ({ visible, onClose, onCancel }: ConfirmOnCloseModalProps) => {
const isConfirmingRef = useRef(false)
useEffect(() => {
if (visible) {
isConfirmingRef.current = false
}
}, [visible])
const handleConfirm = useCallback(() => {
isConfirmingRef.current = true
onClose()
}, [onClose])
const handleOpenChange = useCallback(
(open: boolean) => {
if (open) return
if (isConfirmingRef.current) {
isConfirmingRef.current = false
return
}
onCancel()
},
[onCancel]
)
return (
<AlertDialog open={visible} onOpenChange={handleOpenChange}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Discard changes?</AlertDialogTitle>
<AlertDialogDescription>
Any unsaved changes to this endpoint will be lost.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep editing</AlertDialogCancel>
<AlertDialogAction variant="danger" onClick={handleConfirm}>
Discard changes
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}
export default function SheetConfirmOnCloseDemo() {
const [open, setOpen] = useState(false)
const [savedValues, setSavedValues] = useState<EndpointValues>(defaultValues)
const [draftValues, setDraftValues] = useState<EndpointValues>(defaultValues)
const isDirty = useMemo(
() =>
draftValues.endpointUrl !== savedValues.endpointUrl ||
draftValues.secretHeader !== savedValues.secretHeader,
[draftValues, savedValues]
)
const { confirmOnClose, handleOpenChange, modalProps } = useConfirmOnClose({
checkIsDirty: () => isDirty,
onClose: () => {
setDraftValues(savedValues)
setOpen(false)
},
})
const openSheet = () => {
setDraftValues(savedValues)
setOpen(true)
}
const saveChanges = () => {
setSavedValues(draftValues)
setOpen(false)
}
return (
<>
<Button onClick={openSheet}>Open endpoint sheet</Button>
<Sheet open={open} onOpenChange={handleOpenChange}>
<SheetContent className="flex flex-col gap-0">
<SheetHeader>
<SheetTitle>Edit endpoint</SheetTitle>
</SheetHeader>
<Separator />
<SheetSection className="space-y-4">
<div className="space-y-2">
<Label htmlFor="endpoint-url">Endpoint URL</Label>
<Input
id="endpoint-url"
value={draftValues.endpointUrl}
placeholder="https://api.example.com/webhooks/supabase"
onChange={(event) =>
setDraftValues((current) => ({
...current,
endpointUrl: event.target.value,
}))
}
/>
</div>
<div className="space-y-2">
<Label htmlFor="secret-header">Secret header</Label>
<Input
id="secret-header"
value={draftValues.secretHeader}
placeholder="Bearer top-secret-value"
onChange={(event) =>
setDraftValues((current) => ({
...current,
secretHeader: event.target.value,
}))
}
/>
</div>
</SheetSection>
<Separator />
<SheetFooter>
<Button onClick={confirmOnClose}>Cancel</Button>
<Button variant="primary" onClick={saveChanges} disabled={!isDirty}>
Save changes
</Button>
</SheetFooter>
</SheetContent>
</Sheet>
<DiscardChangesAlertDialog {...modalProps} />
</>
)
}