Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fully scroll toolbar items into view when keyboard navigating #7538

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@react-aria/toolbar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@react-aria/focus": "^3.19.0",
"@react-aria/i18n": "^3.12.4",
"@react-aria/interactions": "^3.22.5",
"@react-aria/utils": "^3.26.0",
"@react-types/shared": "^3.26.0",
"@swc/helpers": "^0.5.0"
Expand Down
7 changes: 6 additions & 1 deletion packages/@react-aria/toolbar/src/useToolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import {AriaLabelingProps, Orientation, RefObject} from '@react-types/shared';
import {createFocusManager} from '@react-aria/focus';
import {filterDOMProps, useLayoutEffect} from '@react-aria/utils';
import {filterDOMProps, scrollIntoViewport, useLayoutEffect} from '@react-aria/utils';
import {getInteractionModality} from '@react-aria/interactions';
import {HTMLAttributes, KeyboardEventHandler, useRef, useState} from 'react';
import {useLocale} from '@react-aria/i18n';

Expand Down Expand Up @@ -110,10 +111,14 @@ export function useToolbar(props: AriaToolbarProps, ref: RefObject<HTMLElement |
// If the element was removed, do nothing, either the first item in the first group,
// or the last item in the last group will be focused, depending on direction.
const onFocus = (e) => {
let modality = getInteractionModality();
if (lastFocused.current && !e.currentTarget.contains(e.relatedTarget) && ref.current?.contains(e.target)) {
lastFocused.current?.focus();
lastFocused.current = null;
}
if (modality === 'keyboard') {
scrollIntoViewport(document.activeElement, {containingElement: ref.current});
}
};

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-aria-components/stories/Toolbar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ToolbarExample = (props: ToolbarProps) => {
<div>
<label htmlFor="before">Input Before Toolbar</label>
<input id="before" type="text" />
<Toolbar {...props}>
<Toolbar {...props} style={{position: 'relative'}}>
<div role="group" aria-label="Text style">
<ToggleButton className={classNames(styles, 'toggleButtonExample')}><strong>B</strong></ToggleButton>
<ToggleButton className={classNames(styles, 'toggleButtonExample')}><div style={{textDecoration: 'underline'}}>U</div></ToggleButton>
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6738,6 +6738,7 @@ __metadata:
dependencies:
"@react-aria/focus": "npm:^3.19.0"
"@react-aria/i18n": "npm:^3.12.4"
"@react-aria/interactions": "npm:^3.22.5"
"@react-aria/utils": "npm:^3.26.0"
"@react-types/shared": "npm:^3.26.0"
"@swc/helpers": "npm:^0.5.0"
Expand Down
Loading