better auto focus for delete
This commit is contained in:
@ -1,4 +1,3 @@
|
|||||||
import React from 'react';
|
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
@ -15,34 +14,6 @@ export default function DeleteClipDialog({
|
|||||||
onCancel: () => void;
|
onCancel: () => void;
|
||||||
onDelete: () => void;
|
onDelete: () => void;
|
||||||
}) {
|
}) {
|
||||||
const cancelRef = React.useRef<HTMLButtonElement>(null);
|
|
||||||
const deleteRef = React.useRef<HTMLButtonElement>(null);
|
|
||||||
const [focusedIdx, setFocusedIdx] = React.useState(1); // 0: Cancel, 1: Delete
|
|
||||||
|
|
||||||
// Focus Delete on open
|
|
||||||
React.useEffect(() => {
|
|
||||||
if (open) {
|
|
||||||
setFocusedIdx(1);
|
|
||||||
setTimeout(() => {
|
|
||||||
deleteRef.current?.focus();
|
|
||||||
}, 0);
|
|
||||||
}
|
|
||||||
}, [open]);
|
|
||||||
|
|
||||||
// Keyboard navigation
|
|
||||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
||||||
if (e.key === 'Tab') {
|
|
||||||
e.preventDefault();
|
|
||||||
const nextIdx = (focusedIdx + 1) % 2;
|
|
||||||
setFocusedIdx(nextIdx);
|
|
||||||
if (nextIdx === 0) cancelRef.current?.focus();
|
|
||||||
else deleteRef.current?.focus();
|
|
||||||
} else if (e.key === 'Enter') {
|
|
||||||
if (focusedIdx === 0) onCancel();
|
|
||||||
else onDelete();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog
|
<Dialog
|
||||||
open={open}
|
open={open}
|
||||||
@ -50,26 +21,22 @@ export default function DeleteClipDialog({
|
|||||||
slotProps={{
|
slotProps={{
|
||||||
paper: { sx: { backgroundColor: '#1a1a1a', color: 'white' } },
|
paper: { sx: { backgroundColor: '#1a1a1a', color: 'white' } },
|
||||||
}}
|
}}
|
||||||
onKeyDown={handleKeyDown}
|
|
||||||
>
|
>
|
||||||
<DialogTitle>Confirm Delete</DialogTitle>
|
<DialogTitle>Confirm Delete</DialogTitle>
|
||||||
<DialogContent>Are you sure you want to delete this clip?</DialogContent>
|
<DialogContent>Are you sure you want to delete this clip?</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
ref={cancelRef}
|
|
||||||
tabIndex={focusedIdx === 0 ? 0 : -1}
|
|
||||||
onClick={onCancel}
|
onClick={onCancel}
|
||||||
className={`bg-plum hover:bg-plumDark text-white font-bold h-10 px-4 rounded-md${focusedIdx === 0 ? ' ring-2 ring-plumDark' : ''}`}
|
className="bg-plum hover:bg-plumDark text-white font-bold h-10 px-4 rounded-md"
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
ref={deleteRef}
|
|
||||||
tabIndex={focusedIdx === 1 ? 0 : -1}
|
|
||||||
onClick={onDelete}
|
onClick={onDelete}
|
||||||
className={`bg-plum hover:bg-plumDark text-white font-bold h-10 px-4 rounded-md${focusedIdx === 1 ? ' ring-2 ring-plumDark' : ''}`}
|
autoFocus
|
||||||
|
className="bg-plum hover:bg-plumDark text-white font-bold h-10 px-4 rounded-md"
|
||||||
>
|
>
|
||||||
Delete
|
Delete
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@ -42,10 +42,8 @@ export default function NameEditDialog({
|
|||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
setInput(e.target.value);
|
setInput(e.target.value);
|
||||||
}}
|
}}
|
||||||
rows={3}
|
|
||||||
onFocus={(event) => event.target.select()}
|
onFocus={(event) => event.target.select()}
|
||||||
aria-label="Edit clip name"
|
aria-label="Edit clip name"
|
||||||
style={{ minHeight: '3em' }}
|
|
||||||
/>
|
/>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
|
|||||||
Reference in New Issue
Block a user