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(docs): updated scroll selector for jumplinks, router links #4197

Merged
merged 2 commits into from
Aug 20, 2024
Merged
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
41 changes: 23 additions & 18 deletions packages/documentation-framework/components/link/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import React from 'react';
import { Link as ReachLink, navigate } from '@reach/router';
import { getAsyncComponent } from '../../routes';

const Promiseany = (Promise.any || function ($) {
return new Promise(function (D, E, A, L) {
A = [];
L = $.map(function ($, i) {
return Promise.resolve($).then(D, function (O) {
return ((A[i] = O), --L) || E({errors: A});
});
}).length;
});
}).bind(Promise);
const Promiseany = (
Promise.any ||
function ($) {
return new Promise(function (D, E, A, L) {
A = [];
L = $.map(function ($, i) {
return Promise.resolve($).then(D, function (O) {
return ((A[i] = O), --L) || E({ errors: A });
});
}).length;
});
}
).bind(Promise);

export const Link = ({
href,
Expand All @@ -23,7 +26,7 @@ export const Link = ({
let preloadPromise;
let url = href || to || '';
if (url.startsWith('#') && !onClick) {
onClick = ev => {
onClick = (ev) => {
ev.preventDefault(); // Don't use client-side routing
// Chrome does not jump until ALL network requests finish.
// We have to force it to...
Expand All @@ -37,8 +40,7 @@ export const Link = ({
}
if (url.includes('//') || url.startsWith('#')) {
return <a href={url} onClick={onClick} {...props} />;
}
else if (url.startsWith('/')) {
} else if (url.startsWith('/')) {
if (!process.env.PRERENDER) {
const Component = getAsyncComponent(url);
if (Component) {
Expand All @@ -48,14 +50,17 @@ export const Link = ({
onMouseOver();
};
// Wait up to an extra 500ms on click before showing 'Loading...'
props.onClick = ev => {
if (!(ev.ctrlKey || ev.metaKey)) { // avoid disallowing cmnd/ctrl+click opening in new tab
props.onClick = (ev) => {
if (!(ev.ctrlKey || ev.metaKey)) {
// avoid disallowing cmnd/ctrl+click opening in new tab
ev.preventDefault();
document.querySelector("#ws-page-main").scrollTo({top: 0}); // scroll to top of page
document
.querySelector('.pf-v6-c-page__main-container')
.scrollTo({ top: 0 }); // scroll to top of page
if (typeof window !== 'undefined' && url !== location.pathname) {
Promiseany([
preloadPromise,
new Promise(res => setTimeout(res, 500))
new Promise((res) => setTimeout(res, 500)),
]).then(() => navigate(url));
}
}
Expand All @@ -65,4 +70,4 @@ export const Link = ({
}

return <ReachLink to={url} {...props} />;
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import React from 'react';
import { JumpLinks, JumpLinksItem, JumpLinksList } from '@patternfly/react-core';
import {
JumpLinks,
JumpLinksItem,
JumpLinksList,
} from '@patternfly/react-core';
import { trackEvent } from '../../helpers';

export const TableOfContents = ({ items }) => {
Expand All @@ -9,15 +13,17 @@ export const TableOfContents = ({ items }) => {
const [stickyNavHeight, setStickyNavHeight] = React.useState(0);

React.useEffect(() => {
if (document.getElementById("ws-sticky-nav-tabs")) {
setStickyNavHeight(document.getElementById("ws-sticky-nav-tabs").offsetHeight);
if (document.getElementById('ws-sticky-nav-tabs')) {
setStickyNavHeight(
document.getElementById('ws-sticky-nav-tabs').offsetHeight
);
}
}, [])
}, []);

const updateWidth = () => {
const { innerWidth } = window;
innerWidth !== width && setWidth(innerWidth);
}
};
let jumpLinksItems = [];
let wasSublistRendered = false;

Expand All @@ -27,22 +33,28 @@ export const TableOfContents = ({ items }) => {
<>
{item.text}
<JumpLinksList>
{nextItemArr.map(curItem => (
{nextItemArr.map((curItem) => (
<JumpLinksItem
key={curItem.id}
href={`#${curItem.id}`}
className="ws-toc-item"
onKeyDown={updateWidth}
onMouseDown={updateWidth}
onClick={() => trackEvent('jump_link_click', 'click_event', curItem.id.toUpperCase())}
onClick={() =>
trackEvent(
'jump_link_click',
'click_event',
curItem.id.toUpperCase()
)
}
>
{curItem.text}
</JumpLinksItem>
))}
</JumpLinksList>
</>
);
}
};

const renderJumpLinksItems = () => {
items.forEach((item, index) => {
Expand All @@ -53,18 +65,28 @@ export const TableOfContents = ({ items }) => {
return;
}
if (!Array.isArray(nextItem) && Array.isArray(item)) {
{item.map(curItem => jumpLinksItems.push(
<JumpLinksItem
key={curItem.id}
href={`#${curItem.id}`}
className="ws-toc-item"
onKeyDown={updateWidth}
onMouseDown={updateWidth}
onClick={() => trackEvent('jump_link_click', 'click_event', curItem.id.toUpperCase())}
>
{curItem.text}
</JumpLinksItem>
))}
{
item.map((curItem) =>
jumpLinksItems.push(
<JumpLinksItem
key={curItem.id}
href={`#${curItem.id}`}
className="ws-toc-item"
onKeyDown={updateWidth}
onMouseDown={updateWidth}
onClick={() =>
trackEvent(
'jump_link_click',
'click_event',
curItem.id.toUpperCase()
)
}
>
{curItem.text}
</JumpLinksItem>
)
);
}
} else {
jumpLinksItems.push(
<JumpLinksItem
Expand All @@ -73,27 +95,35 @@ export const TableOfContents = ({ items }) => {
className="ws-toc-item"
onKeyDown={updateWidth}
onMouseDown={updateWidth}
onClick={() => trackEvent('jump_link_click', 'click_event', item.id.toUpperCase())}
onClick={() =>
trackEvent(
'jump_link_click',
'click_event',
item.id.toUpperCase()
)
}
>
{ Array.isArray(nextItem) ? renderSublist(item, nextItem) : item.text }
{Array.isArray(nextItem)
? renderSublist(item, nextItem)
: item.text}
</JumpLinksItem>
);
}
})
});
return jumpLinksItems;
}
};

return (
<JumpLinks
label="Table of contents"
isVertical
scrollableSelector="#ws-page-main"
scrollableSelector=".pf-v6-c-page__main-container"
className="ws-toc"
style={{ 'top': stickyNavHeight }}
style={{ top: stickyNavHeight }}
offset={width > 1450 ? 108 + stickyNavHeight : 148 + stickyNavHeight}
expandable={{ default: 'expandable', '2xl': 'nonExpandable' }}
>
{ renderJumpLinksItems() }
{renderJumpLinksItems()}
</JumpLinks>
);
}
};
Loading