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(masthead): update overflow scrolling positions #4333

Merged
Show file tree
Hide file tree
Changes from 15 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
26 changes: 26 additions & 0 deletions packages/react/src/__tests__/__snapshots__/storyshots.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32579,6 +32579,7 @@ exports[`Storyshots Components|Dotcom Shell Default (footer language only) 1`] =
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -32610,6 +32611,7 @@ exports[`Storyshots Components|Dotcom Shell Default (footer language only) 1`] =
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -38766,6 +38768,7 @@ exports[`Storyshots Components|Dotcom Shell Default 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -38797,6 +38800,7 @@ exports[`Storyshots Components|Dotcom Shell Default 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -44399,6 +44403,7 @@ exports[`Storyshots Components|Dotcom Shell Search open 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -44430,6 +44435,7 @@ exports[`Storyshots Components|Dotcom Shell Search open 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -53200,6 +53206,7 @@ exports[`Storyshots Components|Dotcom Shell With L1 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -53231,6 +53238,7 @@ exports[`Storyshots Components|Dotcom Shell With L1 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -58891,6 +58899,7 @@ exports[`Storyshots Components|Dotcom Shell With micro footer (language only) 1`
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -58922,6 +58931,7 @@ exports[`Storyshots Components|Dotcom Shell With micro footer (language only) 1`
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -64718,6 +64728,7 @@ exports[`Storyshots Components|Dotcom Shell With micro footer 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -64749,6 +64760,7 @@ exports[`Storyshots Components|Dotcom Shell With micro footer 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -70277,6 +70289,7 @@ exports[`Storyshots Components|Dotcom Shell With platform 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -70308,6 +70321,7 @@ exports[`Storyshots Components|Dotcom Shell With platform 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -76203,6 +76217,7 @@ exports[`Storyshots Components|Dotcom Shell With short footer (language only) 1`
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -76234,6 +76249,7 @@ exports[`Storyshots Components|Dotcom Shell With short footer (language only) 1`
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -82434,6 +82450,7 @@ exports[`Storyshots Components|Dotcom Shell With short footer 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -82465,6 +82482,7 @@ exports[`Storyshots Components|Dotcom Shell With short footer 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -100553,6 +100571,7 @@ exports[`Storyshots Components|Masthead Default 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -100584,6 +100603,7 @@ exports[`Storyshots Components|Masthead Default 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -101029,6 +101049,7 @@ exports[`Storyshots Components|Masthead Search open by default 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -101060,6 +101081,7 @@ exports[`Storyshots Components|Masthead Search open by default 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -104420,6 +104442,7 @@ exports[`Storyshots Components|Masthead With L 1 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -104451,6 +104474,7 @@ exports[`Storyshots Components|Masthead With L 1 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down Expand Up @@ -104734,6 +104758,7 @@ exports[`Storyshots Components|Masthead With Platform 1`] = `
<button
aria-label="Masthead left caret"
className="bx--header__nav-caret-left"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretLeft20)>
Expand Down Expand Up @@ -104765,6 +104790,7 @@ exports[`Storyshots Components|Masthead With Platform 1`] = `
<button
aria-label="Masthead right caret"
className="bx--header__nav-caret-right"
hidden={true}
onClick={[Function]}
>
<ForwardRef(CaretRight20)>
Expand Down
77 changes: 67 additions & 10 deletions packages/react/src/components/Masthead/HeaderNavContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,75 @@ const HeaderNavContainer = ({ children }) => {
const caretRightRef = useRef(null);
const [io, setIO] = useState(null);
const [position, setPosition] = useState(0);
const buttonSize = 48; // 40px(width) + 8px(gradient)

const paginateLeft = useCallback(() => {
const moveLeft = Math.min(position + containerRef.current.offsetWidth, 0);
contentRef.current.style.left = String(moveLeft) + 'px';
setPosition(moveLeft);
let menuItems = contentRef.current.querySelectorAll(
'.bx--header__menu-bar li'
);
for (let i = 0; i < menuItems.length; i++) {
// checks if first visible item is partially hidden
if (
menuItems[i].offsetLeft + menuItems[i].offsetWidth + position >=
buttonSize
) {
// checks if there is space for remaining menuItems
if (
menuItems[i].offsetLeft + menuItems[i].offsetWidth >
containerRef.current.offsetWidth - buttonSize
) {
setPosition(
containerRef.current.offsetWidth -
menuItems[i].offsetLeft -
menuItems[i].offsetWidth -
buttonSize
);
contentRef.current.style.left =
String(
containerRef.current.offsetWidth -
menuItems[i].offsetLeft -
menuItems[i].offsetWidth -
buttonSize
) + 'px';
} else {
setPosition(0);
contentRef.current.style.left = '0px';
}
break;
}
}
}, [position]);

const paginateRight = useCallback(() => {
const moveRight = -Math.min(
-position + containerRef.current.offsetWidth,
contentRef.current.offsetWidth - containerRef.current.offsetWidth
let menuItems = contentRef.current.querySelectorAll(
'.bx--header__menu-bar li'
);
contentRef.current.style.left = String(moveRight) + 'px';
setPosition(moveRight);
for (let i = 0; i < menuItems.length; i++) {
// checks if the right most visible element is partially hidden
if (
menuItems[i].offsetLeft + menuItems[i].offsetWidth + position >
containerRef.current.offsetWidth - buttonSize
) {
// checks if there is space for remaining menuItems
if (
contentRef.current.offsetWidth - menuItems[i].offsetLeft <
containerRef.current.offsetWidth - buttonSize
) {
setPosition(
containerRef.current.offsetWidth - contentRef.current.offsetWidth
);
contentRef.current.style.left =
String(
containerRef.current.offsetWidth - contentRef.current.offsetWidth
) + 'px';
} else {
setPosition(buttonSize - menuItems[i].offsetLeft);
contentRef.current.style.left =
String(buttonSize - menuItems[i].offsetLeft) + 'px';
}
break;
}
}
}, [position]);

useEffect(() => {
Expand Down Expand Up @@ -100,14 +155,16 @@ const HeaderNavContainer = ({ children }) => {
className={`${prefix}--header__nav-caret-left`}
aria-label="Masthead left caret"
onClick={paginateLeft}
ref={caretLeftRef}>
ref={caretLeftRef}
hidden>
<CaretLeft20 />
</button>
<button
className={`${prefix}--header__nav-caret-right`}
aria-label="Masthead right caret"
onClick={paginateRight}
ref={caretRightRef}>
ref={caretRightRef}
hidden>
<CaretRight20 />
</button>
</div>
Expand Down