Skip to content

Commit

Permalink
fix(core/form/inputs): fix issue with navigating to toolbarbuttons wi…
Browse files Browse the repository at this point in the history
…th tab

It should be possible to hit Tab and get to the annotation and inline block edit toolbar buttons.
  • Loading branch information
skogsmaskin committed Oct 24, 2023
1 parent 3c64bec commit 301a283
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
const [cursorRect, setCursorRect] = useState<DOMRect | null>(null)
const rangeRef = useRef<Range | null>(null)
const {sanity} = useTheme()
const popoverRef = useRef<HTMLDivElement | null>(null)
const buttonRef = useRef<HTMLButtonElement | null>(null)
const popoverScheme = sanity.color.dark ? 'light' : 'dark'

// This is a "virtual element" (supported by Popper.js)
Expand All @@ -68,11 +68,19 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
if (!popoverOpen) {
return
}
if (event.key === 'Tab' && buttonRef.current && selected) {
event.stopImmediatePropagation()
if (document.activeElement !== buttonRef.current) {
setTimeout(() => {
if (buttonRef.current) buttonRef.current.focus()
})
}
}
if (event.key === 'Escape') {
setPopoverOpen(false)
}
},
[popoverOpen],
[popoverOpen, selected],
),
)

Expand Down Expand Up @@ -167,6 +175,7 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
</Text>
</Box>
<Button
ref={buttonRef}
icon={EditIcon}
mode="bleed"
onClick={handleEditButtonClicked}
Expand All @@ -190,7 +199,6 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
placement="top"
portal
preventOverflow
ref={popoverRef}
referenceBoundary={referenceBoundary}
referenceElement={cursorElement}
scheme={popoverScheme}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export const DefaultInlineObjectComponent = (props: BlockProps) => {
open={popoverOpen}
referenceBoundary={referenceBoundary}
referenceElement={referenceElement}
selected={selected}
title={popoverTitle}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface InlineObjectToolbarPopoverProps {
onEdit: (event: React.MouseEvent<HTMLButtonElement>) => void
referenceBoundary: HTMLElement | null
referenceElement: HTMLElement | null
selected: boolean
title: string
}

Expand All @@ -39,6 +40,7 @@ export function InlineObjectToolbarPopover(props: InlineObjectToolbarPopoverProp
onDelete,
referenceBoundary,
referenceElement,
selected,
title,
open,
} = props
Expand All @@ -52,13 +54,21 @@ export function InlineObjectToolbarPopover(props: InlineObjectToolbarPopoverProp
useGlobalKeyDown(
useCallback(
(event) => {
if (event.key === 'Tab' && editButtonRef.current && selected) {
event.stopImmediatePropagation()
if (document.activeElement !== editButtonRef.current) {
setTimeout(() => {
if (editButtonRef.current) editButtonRef.current.focus()
})
}
}
if (event.key === 'Escape') {
event.preventDefault()
event.stopPropagation()
onClosePopover()
}
},
[onClosePopover],
[onClosePopover, selected],
),
)

Expand All @@ -84,7 +94,7 @@ export function InlineObjectToolbarPopover(props: InlineObjectToolbarPopoverProp

const popoverContent = useMemo(
() => (
<Box padding={1}>
<Box padding={1} data-testid="inline-object-toolbar-popover">
<Inline space={1}>
<Box padding={2}>
<Text weight="semibold" size={1}>
Expand All @@ -98,6 +108,7 @@ export function InlineObjectToolbarPopover(props: InlineObjectToolbarPopoverProp
padding={2}
ref={editButtonRef}
alt="Edit object"
tabIndex={0}
/>
<Button
ref={deleteButtonRef}
Expand All @@ -107,6 +118,7 @@ export function InlineObjectToolbarPopover(props: InlineObjectToolbarPopoverProp
onClick={handleDelete}
tone="critical"
alt="Remove object"
tabIndex={0}
/>
</Inline>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ function Content(props: PopoverEditDialogProps) {
scrollElement={contentElement}
containerElement={containerElement}
>
<Flex ref={containerElement} direction="column" height="fill">
<Flex
ref={containerElement}
direction="column"
height="fill"
data-testid="popover-edit-object-dialog"
>
<ContentHeaderBox flex="none" padding={1}>
<Flex align="center">
<Box flex={1} padding={2}>
Expand Down

0 comments on commit 301a283

Please sign in to comment.