|
| 1 | +/* |
| 2 | + * Copyright 2023 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {AriaLabelingProps, MultipleSelection, Orientation} from '@react-types/shared'; |
| 14 | +import {createFocusManager} from '@react-aria/focus'; |
| 15 | +import {HTMLAttributes, KeyboardEventHandler, RefObject, useRef, useState} from 'react'; |
| 16 | +import {useLayoutEffect} from '@react-aria/utils'; |
| 17 | +import {useLocale} from '@react-aria/i18n'; |
| 18 | + |
| 19 | +export interface AriaToolbarProps extends AriaLabelingProps, MultipleSelection { |
| 20 | + /** |
| 21 | + * The orientation of the entire toolbar. |
| 22 | + * @default 'horizontal' |
| 23 | + */ |
| 24 | + orientation?: Orientation |
| 25 | +} |
| 26 | + |
| 27 | +interface ToolbarAria { |
| 28 | + toolbarProps: HTMLAttributes<HTMLElement> |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Handles interactions for toolbar elements, such as keyboard navigation between elements. |
| 33 | + */ |
| 34 | +export function useToolbar(props: AriaToolbarProps, ref: RefObject<HTMLDivElement>): ToolbarAria { |
| 35 | + const { |
| 36 | + 'aria-label': ariaLabel, |
| 37 | + 'aria-labelledby': ariaLabelledBy, |
| 38 | + orientation = 'horizontal' |
| 39 | + } = props; |
| 40 | + let [isInToolbar, setInToolbar] = useState(false); |
| 41 | + // should be safe because re-calling set state with the same value it already has is a no-op |
| 42 | + // this will allow us to react should a parent re-render and change its role though |
| 43 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 44 | + useLayoutEffect(() => { |
| 45 | + setInToolbar(!!(ref.current && ref.current.parentElement?.closest('[role="toolbar"]'))); |
| 46 | + }); |
| 47 | + const {direction} = useLocale(); |
| 48 | + const shouldReverse = direction === 'rtl' && orientation === 'horizontal'; |
| 49 | + let focusManager = createFocusManager(ref); |
| 50 | + |
| 51 | + const onKeyDown: KeyboardEventHandler = (e) => { |
| 52 | + // don't handle portalled events |
| 53 | + if (!e.currentTarget.contains(e.target as HTMLElement)) { |
| 54 | + return; |
| 55 | + } |
| 56 | + if ( |
| 57 | + (orientation === 'horizontal' && e.key === 'ArrowRight') |
| 58 | + || (orientation === 'vertical' && e.key === 'ArrowDown')) { |
| 59 | + if (shouldReverse) { |
| 60 | + focusManager.focusPrevious(); |
| 61 | + } else { |
| 62 | + focusManager.focusNext(); |
| 63 | + } |
| 64 | + } else if ( |
| 65 | + (orientation === 'horizontal' && e.key === 'ArrowLeft') |
| 66 | + || (orientation === 'vertical' && e.key === 'ArrowUp')) { |
| 67 | + if (shouldReverse) { |
| 68 | + focusManager.focusNext(); |
| 69 | + } else { |
| 70 | + focusManager.focusPrevious(); |
| 71 | + } |
| 72 | + } else if (e.key === 'Tab') { |
| 73 | + // When the tab key is pressed, we want to move focus |
| 74 | + // out of the entire toolbar. To do this, move focus |
| 75 | + // to the first or last focusable child, and let the |
| 76 | + // browser handle the Tab key as usual from there. |
| 77 | + e.stopPropagation(); |
| 78 | + lastFocused.current = document.activeElement as HTMLElement; |
| 79 | + if (e.shiftKey) { |
| 80 | + focusManager.focusFirst(); |
| 81 | + } else { |
| 82 | + focusManager.focusLast(); |
| 83 | + } |
| 84 | + return; |
| 85 | + } else { |
| 86 | + // if we didn't handle anything, return early so we don't preventDefault |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Prevent arrow keys from being handled by nested action groups. |
| 91 | + e.stopPropagation(); |
| 92 | + e.preventDefault(); |
| 93 | + }; |
| 94 | + |
| 95 | + // Record the last focused child when focus moves out of the toolbar. |
| 96 | + const lastFocused = useRef<HTMLElement | null>(null); |
| 97 | + const onBlur = (e) => { |
| 98 | + if (!e.currentTarget.contains(e.relatedTarget) && !lastFocused.current) { |
| 99 | + lastFocused.current = e.target; |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + // Restore focus to the last focused child when focus returns into the toolbar. |
| 104 | + // If the element was removed, do nothing, either the first item in the first group, |
| 105 | + // or the last item in the last group will be focused, depending on direction. |
| 106 | + const onFocus = (e) => { |
| 107 | + if (lastFocused.current && !e.currentTarget.contains(e.relatedTarget) && ref.current?.contains(e.target)) { |
| 108 | + lastFocused.current?.focus(); |
| 109 | + lastFocused.current = null; |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + return { |
| 114 | + toolbarProps: { |
| 115 | + role: !isInToolbar ? 'toolbar' : 'group', |
| 116 | + 'aria-orientation': orientation, |
| 117 | + 'aria-label': ariaLabel, |
| 118 | + 'aria-labelledby': ariaLabel == null ? ariaLabelledBy : undefined, |
| 119 | + onKeyDownCapture: !isInToolbar ? onKeyDown : undefined, |
| 120 | + onFocusCapture: !isInToolbar ? onFocus : undefined, |
| 121 | + onBlurCapture: !isInToolbar ? onBlur : undefined |
| 122 | + } |
| 123 | + }; |
| 124 | +} |
0 commit comments