* Provides the behavior and state of a step within a wizard: whether it is the current step, * whether it is completed, and its position. The current step is focused when `state.isFocused` * becomes true.
( props: AriaWizardStepProps, state: WizardState<T>, ref: RefObject<FocusableElement | null> )
| 621 | * becomes true. |
| 622 | */ |
| 623 | function useWizardStep<T>( |
| 624 | props: AriaWizardStepProps, |
| 625 | state: WizardState<T>, |
| 626 | ref: RefObject<FocusableElement | null> |
| 627 | ): WizardStepAria { |
| 628 | let {key} = props; |
| 629 | let {collection, selectedKey, completedKeys} = state; |
| 630 | let index = getStepIndex(collection, key); |
| 631 | let isCurrent = key === selectedKey; |
| 632 | let isFocused = isCurrent && state.isFocused; |
| 633 | |
| 634 | // Like a calendar cell, the step is focused in the DOM when the state updates. This is a layout |
| 635 | // effect so that a field in the new panel that focuses itself when it mounts (e.g. with autoFocus) |
| 636 | // does so afterward, and keeps focus. |
| 637 | useLayoutEffect(() => { |
| 638 | if (ref.current) { |
| 639 | if (isFocused) { |
| 640 | focusWithoutScrolling(ref.current); |
| 641 | } |
| 642 | |
| 643 | // Scroll into view if navigating with a keyboard, otherwise |
| 644 | // try not to shift the view under the user's mouse/finger. |
| 645 | // If in a overlay, scrollIntoViewport will only cause scrolling |
| 646 | // up to the overlay scroll body to prevent overlay shifting. |
| 647 | // Also only scroll into view if the cell actually got focused. |
| 648 | // There are some cases where the cell might be disabled or inside, |
| 649 | // an inert container and we don't want to scroll then. |
| 650 | if (getInteractionModality() !== 'pointer' && getActiveElement() === ref.current) { |
| 651 | scrollIntoViewport(ref.current, {containingElement: getScrollParent(ref.current)}); |
| 652 | } |
| 653 | } |
| 654 | }, [isFocused, ref]); |
| 655 | |
| 656 | return { |
| 657 | stepProps: { |
| 658 | onFocus() { |
| 659 | if (isCurrent) { |
| 660 | state.setFocused(true); |
| 661 | } |
| 662 | }, |
| 663 | onBlur() { |
| 664 | state.setFocused(false); |
| 665 | } |
| 666 | }, |
| 667 | isCurrent, |
| 668 | isCompleted: index >= 0 && completedKeys.has(key), |
| 669 | stepNumber: index + 1 |
| 670 | }; |
| 671 | } |
| 672 | |
| 673 | class WizardStepNode extends CollectionNode<unknown> { |
| 674 | static readonly type = 'item'; |
no test coverage detected