-
Notifications
You must be signed in to change notification settings - Fork 23
[UX] Confirmation Dialog System #256
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { AlertTriangle, Info } from 'lucide-react'; | ||
| import React from 'react'; | ||
| import { THEMES } from '../../constants'; | ||
| import { useTheme } from '../../contexts/ThemeContext'; | ||
| import { Button } from './Button'; | ||
| import { Modal } from './Modal'; | ||
|
|
||
| export type ConfirmVariant = 'danger' | 'warning' | 'info'; | ||
|
|
||
| export interface ConfirmDialogProps { | ||
| isOpen: boolean; | ||
| title: string; | ||
| description: string; | ||
| confirmText?: string; | ||
| cancelText?: string; | ||
| variant?: ConfirmVariant; | ||
| onConfirm: () => void; | ||
| onCancel: () => void; | ||
| } | ||
|
|
||
| export const ConfirmDialog: React.FC<ConfirmDialogProps> = ({ | ||
| isOpen, | ||
| title, | ||
| description, | ||
| confirmText = 'Confirm', | ||
| cancelText = 'Cancel', | ||
| variant = 'danger', | ||
| onConfirm, | ||
| onCancel, | ||
| }) => { | ||
| const { style } = useTheme(); | ||
| const isNeo = style === THEMES.NEOBRUTALISM; | ||
|
|
||
| // Determine styles based on variant | ||
| const getIcon = () => { | ||
| switch (variant) { | ||
| case 'danger': | ||
| return <AlertTriangle size={32} className={isNeo ? 'text-black' : 'text-red-500'} />; | ||
| case 'warning': | ||
| return <AlertTriangle size={32} className={isNeo ? 'text-black' : 'text-yellow-500'} />; | ||
| case 'info': | ||
| return <Info size={32} className={isNeo ? 'text-black' : 'text-blue-500'} />; | ||
| } | ||
| }; | ||
|
|
||
| const getIconBg = () => { | ||
| switch (variant) { | ||
| case 'danger': | ||
| return isNeo ? 'bg-red-400 border-2 border-black rounded-none' : 'bg-red-500/20 rounded-full'; | ||
| case 'warning': | ||
| return isNeo ? 'bg-yellow-400 border-2 border-black rounded-none' : 'bg-yellow-500/20 rounded-full'; | ||
| case 'info': | ||
| return isNeo ? 'bg-blue-400 border-2 border-black rounded-none' : 'bg-blue-500/20 rounded-full'; | ||
| } | ||
| }; | ||
|
|
||
| const getButtonVariant = () => { | ||
| switch (variant) { | ||
| case 'danger': return 'danger'; | ||
| case 'warning': return 'primary'; | ||
| case 'info': return 'primary'; | ||
| default: return 'primary'; | ||
| } | ||
| }; | ||
|
|
||
| const isDestructive = variant === 'danger' || variant === 'warning'; | ||
|
|
||
| return ( | ||
| <Modal | ||
| isOpen={isOpen} | ||
| onClose={onCancel} | ||
| title={title} | ||
| footer={ | ||
| <> | ||
| <Button variant="ghost" onClick={onCancel} autoFocus={isDestructive}> | ||
| {cancelText} | ||
| </Button> | ||
| <Button variant={getButtonVariant()} onClick={onConfirm} autoFocus={!isDestructive}> | ||
| {confirmText} | ||
| </Button> | ||
| </> | ||
| } | ||
| > | ||
| <div className="flex flex-col items-center text-center sm:flex-row sm:text-left sm:items-start gap-4"> | ||
| <div className={`p-3 shrink-0 ${getIconBg()}`}> | ||
| {getIcon()} | ||
| </div> | ||
| <div> | ||
| <p className={`text-base leading-relaxed ${isNeo ? 'text-black' : 'text-white/80'}`}> | ||
| {description} | ||
| </p> | ||
| </div> | ||
| </div> | ||
| </Modal> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import React, { createContext, useCallback, useContext, useRef, useState } from 'react'; | ||
| import { ConfirmDialog, ConfirmVariant } from '../components/ui/ConfirmDialog'; | ||
|
|
||
| interface ConfirmOptions { | ||
| title: string; | ||
| description: string; | ||
| confirmText?: string; | ||
| cancelText?: string; | ||
| variant?: ConfirmVariant; | ||
| } | ||
|
|
||
| interface ConfirmContextType { | ||
| confirm: (options: ConfirmOptions) => Promise<boolean>; | ||
| } | ||
|
|
||
| const ConfirmContext = createContext<ConfirmContextType | undefined>(undefined); | ||
|
|
||
| export const ConfirmProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [options, setOptions] = useState<ConfirmOptions>({ | ||
| title: '', | ||
| description: '', | ||
| }); | ||
|
|
||
| // Use useRef to keep track of the resolver synchronously across renders | ||
| const resolveRef = useRef<((value: boolean) => void) | null>(null); | ||
|
|
||
| const confirm = useCallback((options: ConfirmOptions) => { | ||
| // If there is an existing pending confirmation, resolve it with false (cancel) | ||
| // to prevent it from hanging indefinitely. | ||
| if (resolveRef.current) { | ||
| resolveRef.current(false); | ||
| } | ||
|
|
||
| setOptions(options); | ||
| setIsOpen(true); | ||
| return new Promise<boolean>((resolve) => { | ||
| resolveRef.current = resolve; | ||
| }); | ||
| }, []); | ||
|
|
||
| const handleConfirm = useCallback(() => { | ||
| setIsOpen(false); | ||
| if (resolveRef.current) { | ||
| resolveRef.current(true); | ||
| resolveRef.current = null; | ||
| } | ||
| }, []); | ||
|
|
||
| const handleCancel = useCallback(() => { | ||
| setIsOpen(false); | ||
| if (resolveRef.current) { | ||
| resolveRef.current(false); | ||
| resolveRef.current = null; | ||
| } | ||
| }, []); | ||
|
|
||
| return ( | ||
| <ConfirmContext.Provider value={{ confirm }}> | ||
| {children} | ||
| <ConfirmDialog | ||
| isOpen={isOpen} | ||
| title={options.title} | ||
| description={options.description} | ||
| confirmText={options.confirmText} | ||
| cancelText={options.cancelText} | ||
| variant={options.variant} | ||
| onConfirm={handleConfirm} | ||
| onCancel={handleCancel} | ||
| /> | ||
| </ConfirmContext.Provider> | ||
| ); | ||
| }; | ||
|
|
||
| export const useConfirm = () => { | ||
| const context = useContext(ConfirmContext); | ||
| if (context === undefined) { | ||
| throw new Error('useConfirm must be used within a ConfirmProvider'); | ||
| } | ||
| return context; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider adding default cases to switch statements for defensive coding.
The
getIcon()andgetIconBg()functions lack default cases. While currently safe due to the typedConfirmVariantunion and default prop value, adding explicit defaults would future-proof against accidental omissions if new variants are added.♻️ Optional: Add default cases
const getIcon = () => { switch (variant) { case 'danger': return <AlertTriangle size={32} className={isNeo ? 'text-black' : 'text-red-500'} />; case 'warning': return <AlertTriangle size={32} className={isNeo ? 'text-black' : 'text-yellow-500'} />; case 'info': return <Info size={32} className={isNeo ? 'text-black' : 'text-blue-500'} />; + default: + return <AlertTriangle size={32} className={isNeo ? 'text-black' : 'text-red-500'} />; } };📝 Committable suggestion
🤖 Prompt for AI Agents