Skip to content

Commit

Permalink
refactor: cleanup implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Nov 19, 2024
1 parent 0f5b8da commit a018b63
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 176 deletions.
11 changes: 6 additions & 5 deletions demos/src/Examples/Accessibility/React/InsertMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Editor, FloatingMenu, useEditorState } from '@tiptap/react'
import React, { useRef } from 'react'

import { useFocusMenubar } from './useFocusMenubar.js'
import { useMenubarNav } from './useMenubarNav.js'

/**
* A floating menu for inserting new elements like lists, horizontal rules, etc.
Expand All @@ -21,10 +21,11 @@ export function InsertMenu({ editor }: { editor: Editor }) {
})

// Handle arrow navigation within a menu bar container, and allow to escape to the editor
const { focusButton } = useFocusMenubar({
const { getFocusableElements } = useMenubarNav({
editor,
ref: containerRef,
onEscape: () => {
onEscape: e => {
e.preventDefault()
// On escape, focus the editor
editor.chain().focus().run()
},
Expand All @@ -43,8 +44,8 @@ export function InsertMenu({ editor }: { editor: Editor }) {
onFocus={e => {
// The ref we have is to the container, not the menu itself
if (containerRef.current === e.target?.parentNode) {
// Focus the first button when the menu bar is focused
focusButton(containerRef.current?.querySelector('button'))
// Focus the first enabled button-like when the menu bar is focused
getFocusableElements()?.[0]?.focus()
}
}}
tabIndex={0}
Expand Down
22 changes: 18 additions & 4 deletions demos/src/Examples/Accessibility/React/MenuBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Editor } from '@tiptap/core'
import { useEditorState } from '@tiptap/react'
import React, { useEffect, useRef, useState } from 'react'

import { useFocusMenubar } from './useFocusMenubar.js'
import { useMenubarNav } from './useMenubarNav.js'

/**
* Handles the heading dropdown
Expand Down Expand Up @@ -48,10 +48,24 @@ function NodeTypeDropdown({ editor }: { editor: Editor }) {
}, [])

return (
<div className="node-type-dropdown__container" ref={menuRef}>
<div
className="node-type-dropdown__container"
ref={menuRef}
onKeyDown={e => {
// Escape or tab should close the dropdown if it's open
if (isOpen && (e.key === 'Escape' || e.key === 'Tab')) {
setIsOpen(false)
if (e.key === 'Escape') {
// Prevent the editor from handling the escape key
e.preventDefault()
}
}
}}
>
<button
onClick={() => {
onClick={e => {
setIsOpen(open => !open)
e.stopPropagation()
}}
onKeyDown={e => {
if (e.key === 'Enter' || e.key === ' ') {
Expand Down Expand Up @@ -257,7 +271,7 @@ export function MenuBar({ editor }: { editor: Editor }) {
},
})

useFocusMenubar({
useMenubarNav({
ref: containerRef,
editor,
onKeydown: event => {
Expand Down
9 changes: 5 additions & 4 deletions demos/src/Examples/Accessibility/React/TextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Selection } from '@tiptap/pm/state'
import { BubbleMenu, Editor, useEditorState } from '@tiptap/react'
import React, { useRef } from 'react'

import { useFocusMenubar } from './useFocusMenubar.js'
import { useMenubarNav } from './useMenubarNav.js'

/**
* Handles formatting text with marks like bold, italic, etc.
Expand Down Expand Up @@ -33,10 +33,11 @@ export function TextMenu({ editor }: { editor: Editor }) {
})

// Handle arrow navigation within a menu bar container, and allow to escape to the editor
const { focusButton } = useFocusMenubar({
const { getFocusableElements } = useMenubarNav({
editor,
ref: containerRef,
onEscape: () => {
onEscape: e => {
e.preventDefault()
// On escape, focus the editor & dismiss the menu by moving the selection to the end of the selection
editor
.chain()
Expand All @@ -63,7 +64,7 @@ export function TextMenu({ editor }: { editor: Editor }) {
// The ref we have is to the container, not the menu itself
if (containerRef.current === e.target?.parentNode) {
// Focus the first button when the menu bar is focused
focusButton(containerRef.current?.querySelector('button'))
getFocusableElements()?.[0]?.focus()
}
}}
tabIndex={0}
Expand Down
163 changes: 0 additions & 163 deletions demos/src/Examples/Accessibility/React/useFocusMenubar.ts

This file was deleted.

Loading

0 comments on commit a018b63

Please sign in to comment.