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(Nav): allow RTL scrolling #9637

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Changes from 3 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
34 changes: 26 additions & 8 deletions packages/react-core/src/components/Nav/NavList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export interface NavListProps
children?: React.ReactNode;
/** Additional classes added to the list */
className?: string;
/** Aria-label for the left scroll button */
/** Aria-label for the back scroll button */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you deprecate this and the ariaRightScroll prop and add ariaBackScroll and ariaLeftScroll props.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. I also made the new props take precedence for the default aria label if that's ok, or LMK if the old props would be better to leave as the higher priority default.

ariaLeftScroll?: string;
/** Aria-label for the right scroll button */
/** Aria-label for the forward scroll button */
ariaRightScroll?: string;
}

Expand All @@ -28,6 +28,7 @@ class NavList extends React.Component<NavListProps> {
ariaLeftScroll: 'Scroll left',
ariaRightScroll: 'Scroll right'
};
private direction = 'ltr';

state = {
scrollViewAtStart: false,
Expand All @@ -51,7 +52,7 @@ class NavList extends React.Component<NavListProps> {
}
};

scrollLeft = () => {
scrollBack = () => {
// find first Element that is fully in view on the left, then scroll to the element before it
const container = this.navList.current;
if (container) {
Expand All @@ -65,13 +66,19 @@ class NavList extends React.Component<NavListProps> {
}
}
if (lastElementOutOfView) {
container.scrollLeft -= lastElementOutOfView.scrollWidth;
if (this.direction === 'ltr') {
// LTR scrolls left to go back
container.scrollLeft -= lastElementOutOfView.scrollWidth;
} else {
// RTL scrolls right to go back
container.scrollLeft += lastElementOutOfView.scrollWidth;
}
}
this.handleScrollButtons();
}
};

scrollRight = () => {
scrollForward = () => {
// find last Element that is fully in view on the right, then scroll to the element after it
const container = this.navList.current;
if (container) {
Expand All @@ -85,21 +92,32 @@ class NavList extends React.Component<NavListProps> {
}
}
if (firstElementOutOfView) {
container.scrollLeft += firstElementOutOfView.scrollWidth;
if (this.direction === 'ltr') {
// LTR scrolls right to go forward
container.scrollLeft += firstElementOutOfView.scrollWidth;
} else {
// RTL scrolls left to go forward
container.scrollLeft -= firstElementOutOfView.scrollWidth;
}
}
this.handleScrollButtons();
}
};

componentDidMount() {
this.observer = getResizeObserver(this.navList.current, this.handleScrollButtons, true);
this.direction = getComputedStyle(this.navList.current).getPropertyValue('direction');
this.handleScrollButtons();
}

componentWillUnmount() {
this.observer();
}

componentDidUpdate() {
this.direction = getComputedStyle(this.navList.current).getPropertyValue('direction');
}

render() {
const { children, className, ariaLeftScroll, ariaRightScroll, ...props } = this.props;
const { scrollViewAtStart, scrollViewAtEnd } = this.state;
Expand All @@ -114,7 +132,7 @@ class NavList extends React.Component<NavListProps> {
<button
className={css(styles.navScrollButton)}
aria-label={ariaLeftScroll}
onClick={this.scrollLeft}
onClick={this.scrollBack}
disabled={scrollViewAtStart}
tabIndex={isSidebarOpen ? null : -1}
>
Expand All @@ -134,7 +152,7 @@ class NavList extends React.Component<NavListProps> {
<button
className={css(styles.navScrollButton)}
aria-label={ariaRightScroll}
onClick={this.scrollRight}
onClick={this.scrollForward}
disabled={scrollViewAtEnd}
tabIndex={isSidebarOpen ? null : -1}
>
Expand Down