|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import { useComposedRefs } from 'soybean-react-ui/compose-refs'; |
| 4 | + |
| 5 | + |
| 6 | +import { useDirection } from 'soybean-react-ui/direction'; |
| 7 | + |
| 8 | +import { Primitive, composeEventHandlers } from 'soybean-react-ui/primitive'; |
| 9 | +import { useCallbackRef } from 'soybean-react-ui/use-callback-ref'; |
| 10 | +import { useControllableState } from 'soybean-react-ui/use-controllable-state'; |
| 11 | +import { Collection, GROUP_NAME, RovingFocusProvider, useCollection } from './context'; |
| 12 | +import { RovingFocusGroupElement, RovingFocusGroupImplElement, RovingFocusGroupImplProps, RovingFocusGroupProps, ScopedProps } from './types'; |
| 13 | +import { focusFirst } from './shared'; |
| 14 | + |
| 15 | +const ENTRY_FOCUS = 'rovingFocusGroup.onEntryFocus'; |
| 16 | + |
| 17 | +const EVENT_OPTIONS = { bubbles: false, cancelable: true }; |
| 18 | + |
| 19 | + |
| 20 | +const RovingFocusGroupImpl = React.forwardRef<RovingFocusGroupImplElement, RovingFocusGroupImplProps>( |
| 21 | + (props: ScopedProps<RovingFocusGroupImplProps>, forwardedRef) => { |
| 22 | + const { |
| 23 | + __scopeRovingFocusGroup, |
| 24 | + currentTabStopId: currentTabStopIdProp, |
| 25 | + defaultCurrentTabStopId, |
| 26 | + dir, |
| 27 | + loop = false, |
| 28 | + onCurrentTabStopIdChange, |
| 29 | + onEntryFocus, |
| 30 | + orientation, |
| 31 | + preventScrollOnEntryFocus = false, |
| 32 | + ...groupProps |
| 33 | + } = props; |
| 34 | + const ref = React.useRef<RovingFocusGroupImplElement>(null); |
| 35 | + const composedRefs = useComposedRefs(forwardedRef, ref); |
| 36 | + const direction = useDirection(dir); |
| 37 | + const [currentTabStopId, setCurrentTabStopId] = useControllableState({ |
| 38 | + caller: GROUP_NAME, |
| 39 | + defaultProp: defaultCurrentTabStopId ?? null, |
| 40 | + onChange: onCurrentTabStopIdChange, |
| 41 | + prop: currentTabStopIdProp |
| 42 | + }); |
| 43 | + const [isTabbingBackOut, setIsTabbingBackOut] = React.useState(false); |
| 44 | + const handleEntryFocus = useCallbackRef(onEntryFocus); |
| 45 | + const getItems = useCollection(__scopeRovingFocusGroup); |
| 46 | + const isClickFocusRef = React.useRef(false); |
| 47 | + const [focusableItemsCount, setFocusableItemsCount] = React.useState(0); |
| 48 | + |
| 49 | + React.useEffect(() => { |
| 50 | + const node = ref.current; |
| 51 | + if (node) { |
| 52 | + node.addEventListener(ENTRY_FOCUS, handleEntryFocus); |
| 53 | + |
| 54 | + return () => node.removeEventListener(ENTRY_FOCUS, handleEntryFocus); |
| 55 | + } |
| 56 | + |
| 57 | + return () => {}; |
| 58 | + }, [handleEntryFocus]); |
| 59 | + |
| 60 | + return ( |
| 61 | + <RovingFocusProvider |
| 62 | + currentTabStopId={currentTabStopId} |
| 63 | + dir={direction} |
| 64 | + loop={loop} |
| 65 | + orientation={orientation} |
| 66 | + scope={__scopeRovingFocusGroup} |
| 67 | + onFocusableItemAdd={React.useCallback(() => setFocusableItemsCount(prevCount => prevCount + 1), [])} |
| 68 | + onFocusableItemRemove={React.useCallback(() => setFocusableItemsCount(prevCount => prevCount - 1), [])} |
| 69 | + onItemFocus={React.useCallback(tabStopId => setCurrentTabStopId(tabStopId), [setCurrentTabStopId])} |
| 70 | + onItemShiftTab={React.useCallback(() => setIsTabbingBackOut(true), [])} |
| 71 | + > |
| 72 | + <Primitive.div |
| 73 | + data-orientation={orientation} |
| 74 | + tabIndex={isTabbingBackOut || focusableItemsCount === 0 ? -1 : 0} |
| 75 | + {...groupProps} |
| 76 | + ref={composedRefs as React.Ref<HTMLDivElement>} |
| 77 | + style={{ outline: 'none', ...props.style }} |
| 78 | + onBlur={composeEventHandlers(props.onBlur, () => setIsTabbingBackOut(false))} |
| 79 | + onFocus={composeEventHandlers(props.onFocus, event => { |
| 80 | + // We normally wouldn't need this check, because we already check |
| 81 | + // that the focus is on the current target and not bubbling to it. |
| 82 | + // We do this because Safari doesn't focus buttons when clicked, and |
| 83 | + // instead, the wrapper will get focused and not through a bubbling event. |
| 84 | + const isKeyboardFocus = !isClickFocusRef.current; |
| 85 | + |
| 86 | + if (event.target === event.currentTarget && isKeyboardFocus && !isTabbingBackOut) { |
| 87 | + const entryFocusEvent = new CustomEvent(ENTRY_FOCUS, EVENT_OPTIONS); |
| 88 | + event.currentTarget.dispatchEvent(entryFocusEvent); |
| 89 | + |
| 90 | + if (!entryFocusEvent.defaultPrevented) { |
| 91 | + const items = getItems().filter(item => item.focusable); |
| 92 | + const activeItem = items.find(item => item.active); |
| 93 | + const currentItem = items.find(item => item.id === currentTabStopId); |
| 94 | + const candidateItems = [activeItem, currentItem, ...items].filter(Boolean) as typeof items; |
| 95 | + const candidateNodes = candidateItems.map(item => item.ref.current!); |
| 96 | + focusFirst(candidateNodes, preventScrollOnEntryFocus); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + isClickFocusRef.current = false; |
| 101 | + })} |
| 102 | + onMouseDown={composeEventHandlers(props.onMouseDown, () => { |
| 103 | + isClickFocusRef.current = true; |
| 104 | + })} |
| 105 | + /> |
| 106 | + </RovingFocusProvider> |
| 107 | + ); |
| 108 | + } |
| 109 | +); |
| 110 | + |
| 111 | +RovingFocusGroupImpl.displayName = `${GROUP_NAME}Impl`; |
| 112 | + |
| 113 | +const RovingFocusGroup = React.forwardRef<RovingFocusGroupElement, RovingFocusGroupProps>( |
| 114 | + (props: ScopedProps<RovingFocusGroupProps>, forwardedRef) => { |
| 115 | + return ( |
| 116 | + <Collection.Provider scope={props.__scopeRovingFocusGroup}> |
| 117 | + <Collection.Slot scope={props.__scopeRovingFocusGroup}> |
| 118 | + <RovingFocusGroupImpl |
| 119 | + {...props} |
| 120 | + ref={forwardedRef} |
| 121 | + /> |
| 122 | + </Collection.Slot> |
| 123 | + </Collection.Provider> |
| 124 | + ); |
| 125 | + } |
| 126 | +); |
| 127 | + |
| 128 | +RovingFocusGroup.displayName = GROUP_NAME; |
| 129 | + |
| 130 | +export { |
| 131 | + RovingFocusGroup, |
| 132 | +}; |
| 133 | + |
0 commit comments