|
| 1 | +// RefreshButton (#592) — shared "refetch this page's data" affordance |
| 2 | +// for the list-page filter toolbar (between Reset and Customize) and |
| 3 | +// the detail-page header (next to History / Edit / Delete). |
| 4 | +// |
| 5 | +// Same code-organization pattern as ResetButton (#590): generic icon- |
| 6 | +// agnostic primitive in @dar/ui, callers pass the refetch closure |
| 7 | +// they already hold (`useList().refresh`, `useDetail().refresh`, …). |
| 8 | +// Both surfaces get the same visual contract — same border, same |
| 9 | +// pending spin, same a11y behaviour — by construction. |
| 10 | + |
| 11 | +import { cloneElement, isValidElement, useState, type ReactNode } from 'react'; |
| 12 | + |
| 13 | +export interface RefreshButtonProps { |
| 14 | + /** Refetch closure. The button shows a pending state while the |
| 15 | + * promise is in flight; on resolve / reject the spinner stops. */ |
| 16 | + onRefresh: () => Promise<void> | void; |
| 17 | + /** Visible tooltip (and aria-label, since the button is icon-only). |
| 18 | + * Default "Refresh". */ |
| 19 | + tooltip?: string; |
| 20 | + /** Leading icon. If it accepts `className` the button applies |
| 21 | + * `animate-spin` while pending — the lucide convention. Pass |
| 22 | + * `null` to opt out. */ |
| 23 | + icon?: ReactNode; |
| 24 | + /** Extra classes — kept narrow so the visual contract stays uniform. */ |
| 25 | + className?: string; |
| 26 | +} |
| 27 | + |
| 28 | +export function RefreshButton({ |
| 29 | + onRefresh, |
| 30 | + tooltip = 'Refresh', |
| 31 | + icon = null, |
| 32 | + className, |
| 33 | +}: RefreshButtonProps) { |
| 34 | + const [pending, setPending] = useState(false); |
| 35 | + |
| 36 | + async function run(): Promise<void> { |
| 37 | + if (pending) return; |
| 38 | + setPending(true); |
| 39 | + try { |
| 40 | + await onRefresh(); |
| 41 | + } finally { |
| 42 | + setPending(false); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // If the caller's icon is a valid element that accepts a `className`, |
| 47 | + // augment it with `animate-spin` during a pending promise. |
| 48 | + const renderedIcon = |
| 49 | + pending && isValidElement<{ className?: string }>(icon) |
| 50 | + ? cloneElement(icon, { |
| 51 | + className: `${icon.props.className ?? ''} animate-spin`.trim(), |
| 52 | + }) |
| 53 | + : icon; |
| 54 | + |
| 55 | + return ( |
| 56 | + <button |
| 57 | + type="button" |
| 58 | + onClick={() => { |
| 59 | + void run(); |
| 60 | + }} |
| 61 | + disabled={pending} |
| 62 | + title={tooltip} |
| 63 | + aria-label={tooltip} |
| 64 | + className={[ |
| 65 | + 'inline-flex shrink-0 items-center justify-center rounded-md border border-gray-300 px-2 py-1.5 text-sm', |
| 66 | + pending |
| 67 | + ? 'cursor-wait text-gray-400' |
| 68 | + : 'text-gray-700 hover:bg-gray-100 hover:text-gray-900', |
| 69 | + 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500', |
| 70 | + className ?? '', |
| 71 | + ].join(' ')} |
| 72 | + > |
| 73 | + {renderedIcon} |
| 74 | + </button> |
| 75 | + ); |
| 76 | +} |
0 commit comments