Skip to content

Commit

Permalink
adding optional chaining checks to aid with hot-module reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
dusave committed Nov 1, 2024
1 parent d349cfc commit d7e8fe1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
4 changes: 3 additions & 1 deletion packages/react/src/ActionMenu/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ const Overlay: React.FC<React.PropsWithChildren<MenuOverlayProps>> = ({
// If the menu anchor is an icon button, we need to label the menu by tooltip that also labelled the anchor.
const [anchorAriaLabelledby, setAnchorAriaLabelledby] = useState<null | string>(null)
useEffect(() => {
if (anchorRef.current) {
// Necessary for HMR reloads
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (anchorRef?.current) {
const ariaLabelledby = anchorRef.current.getAttribute('aria-labelledby')
if (ariaLabelledby) {
setAnchorAriaLabelledby(ariaLabelledby)
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/hooks/useMenuInitialFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {iterateFocusableElements} from '@primer/behaviors/utils'

export const useMenuInitialFocus = (
open: boolean,
containerRef: React.RefObject<HTMLElement>,
anchorRef: React.RefObject<HTMLElement>,
containerRef?: React.RefObject<HTMLElement>,
anchorRef?: React.RefObject<HTMLElement>,
) => {
/**
* We need to pick the first element to focus based on how the menu was opened,
Expand All @@ -15,7 +15,7 @@ export const useMenuInitialFocus = (

React.useEffect(
function inferOpeningKey() {
const anchorElement = anchorRef.current
const anchorElement = anchorRef?.current

const clickHandler = (event: MouseEvent) => {
// event.detail === 0 means onClick was fired by Enter/Space key
Expand Down Expand Up @@ -46,12 +46,12 @@ export const useMenuInitialFocus = (
*/
React.useEffect(
function moveFocusOnOpen() {
if (!open || !containerRef.current) return // wait till the menu is open
if (!open || !containerRef?.current) return // wait till the menu is open

const iterable = iterateFocusableElements(containerRef.current)

if (openingGesture === 'mouse-click') {
if (anchorRef.current) anchorRef.current.focus()
if (anchorRef?.current) anchorRef.current.focus()
else throw new Error('For focus management, please attach anchorRef')
} else if (openingGesture && ['ArrowDown', 'Space', 'Enter'].includes(openingGesture)) {
const firstElement = iterable.next().value
Expand Down
26 changes: 13 additions & 13 deletions packages/react/src/hooks/useMenuKeyboardNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import type {MenuCloseHandler} from '../ActionMenu'
export const useMenuKeyboardNavigation = (
open: boolean,
onClose: MenuCloseHandler | undefined,
containerRef: React.RefObject<HTMLElement>,
anchorRef: React.RefObject<HTMLElement>,
isSubmenu: boolean,
containerRef?: React.RefObject<HTMLElement>,
anchorRef?: React.RefObject<HTMLElement>,
isSubmenu: boolean = false,
) => {
useMenuInitialFocus(open, containerRef, anchorRef)
useMnemonics(open, containerRef)
Expand All @@ -32,12 +32,12 @@ export const useMenuKeyboardNavigation = (
const useCloseMenuOnTab = (
open: boolean,
onClose: MenuCloseHandler | undefined,
containerRef: React.RefObject<HTMLElement>,
anchorRef: React.RefObject<HTMLElement>,
containerRef?: React.RefObject<HTMLElement>,
anchorRef?: React.RefObject<HTMLElement>,
) => {
React.useEffect(() => {
const container = containerRef.current
const anchor = anchorRef.current
const container = containerRef?.current
const anchor = anchorRef?.current

const handler = (event: KeyboardEvent) => {
if (open && event.key === 'Tab') onClose?.('tab')
Expand All @@ -59,10 +59,10 @@ const useCloseSubmenuOnArrow = (
open: boolean,
isSubmenu: boolean,
onClose: MenuCloseHandler | undefined,
containerRef: React.RefObject<HTMLElement>,
containerRef?: React.RefObject<HTMLElement>,
) => {
React.useEffect(() => {
const container = containerRef.current
const container = containerRef?.current

const handler = (event: KeyboardEvent) => {
if (open && isSubmenu && event.key === 'ArrowLeft') onClose?.('arrow-left')
Expand All @@ -81,12 +81,12 @@ const useCloseSubmenuOnArrow = (
*/
const useMoveFocusToMenuItem = (
open: boolean,
containerRef: React.RefObject<HTMLElement>,
anchorRef: React.RefObject<HTMLElement>,
containerRef?: React.RefObject<HTMLElement>,
anchorRef?: React.RefObject<HTMLElement>,
) => {
React.useEffect(() => {
const container = containerRef.current
const anchor = anchorRef.current
const container = containerRef?.current
const anchor = anchorRef?.current

const handler = (event: KeyboardEvent) => {
if (!open || !container) return
Expand Down

0 comments on commit d7e8fe1

Please sign in to comment.