Skip to content

Commit

Permalink
chore(docs): Add onClick to SkipToContent in demos
Browse files Browse the repository at this point in the history
SkipToContent does not work in the context of the PatternFly page. Using document.getElementById sidesteps this issue and allows them to work as usual. I tried using refs, but because Page is an older class component, the refs were not compatible. We'd likely need to rewrite page as a function component. This is good enough for now, in my opinion. SkipToContentBasic works as advertised, so I am not touching that, and I also did not touch react-integration or unit tests.
  • Loading branch information
rebeccaalpert committed Dec 2, 2024
1 parent b0877b3 commit a574b5f
Show file tree
Hide file tree
Showing 21 changed files with 286 additions and 147 deletions.
15 changes: 13 additions & 2 deletions packages/react-core/src/demos/DashboardWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,19 @@ export const DashboardWrapper: React.FC<DashboardWrapperProps> = ({
</PageSidebar>
);

const defaultContainerId = 'main-content-page-layout-default-nav';

const handleClick = (event: React.MouseEvent) => {
event.preventDefault();

const mainContentElement = document.getElementById(mainContainerId ?? defaultContainerId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent href={`#${mainContainerId ?? 'main-content-page-layout-default-nav'}`}>
<SkipToContent onClick={handleClick} href={`#${mainContainerId ?? defaultContainerId}`}>
Skip to content
</SkipToContent>
);
Expand All @@ -112,7 +123,7 @@ export const DashboardWrapper: React.FC<DashboardWrapperProps> = ({
skipToContent={PageSkipToContent}
banner={banner}
breadcrumb={renderedBreadcrumb}
mainContainerId={mainContainerId ?? 'main-content-page-layout-default-nav'}
mainContainerId={mainContainerId ?? defaultContainerId}
notificationDrawer={notificationDrawer}
isNotificationDrawerExpanded={isNotificationDrawerExpanded}
{...(typeof onPageResize === 'function' && {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,21 @@ export const NotificationDrawerBasic: React.FunctionComponent = () => {
</PageSidebar>
);
const pageId = 'main-content-page-layout-default-nav';
const PageSkipToContent = <SkipToContent href={`#${pageId}`}>Skip to content</SkipToContent>;

const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(pageId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${pageId}`}>
Skip to content
</SkipToContent>
);

const PageBreadcrumb = (
<Breadcrumb>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,21 @@ export const NotificationDrawerGrouped: React.FunctionComponent = () => {
</PageSidebar>
);
const pageId = 'main-content-page-layout-default-nav';
const PageSkipToContent = <SkipToContent href={`#${pageId}`}>Skip to content</SkipToContent>;

const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(pageId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${pageId}`}>
Skip to content
</SkipToContent>
);

const PageBreadcrumb = (
<Breadcrumb>
Expand Down
126 changes: 0 additions & 126 deletions packages/react-core/src/demos/examples/DashboardWrapper.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,20 @@ export const MastheadWithHorizontalNav: React.FunctionComponent = () => {

const mainContainerId = 'main-content-page-layout-tertiary-nav';

const pageSkipToContent = <SkipToContent href={`#${mainContainerId}`}>Skip to content</SkipToContent>;
const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(mainContainerId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const pageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${mainContainerId}`}>
Skip to content
</SkipToContent>
);

return (
<Page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,20 @@ export const MastheadWithUtilitiesAndUserDropdownMenu: React.FunctionComponent =

const mainContainerId = 'main-content-page-layout-tertiary-nav';

const pageSkipToContent = <SkipToContent href={`#${mainContainerId}`}>Skip to content</SkipToContent>;
const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(mainContainerId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const pageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${mainContainerId}`}>
Skip to content
</SkipToContent>
);

return (
<Page
Expand Down
16 changes: 15 additions & 1 deletion packages/react-core/src/demos/examples/Nav/NavDefault.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,21 @@ export const NavDefault: React.FunctionComponent = () => {
</PageSidebar>
);
const pageId = 'main-content-page-layout-default-nav';
const PageSkipToContent = <SkipToContent href={`#${pageId}`}>Skip to content</SkipToContent>;

const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(pageId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${pageId}`}>
Skip to content
</SkipToContent>
);

return (
<>
Expand Down
16 changes: 15 additions & 1 deletion packages/react-core/src/demos/examples/Nav/NavExpandable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,21 @@ export const NavExpandableDemo: React.FunctionComponent = () => {
</PageSidebar>
);
const pageId = 'main-content-page-layout-expandable-nav';
const PageSkipToContent = <SkipToContent href={`#${pageId}`}>Skip to content</SkipToContent>;

const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(pageId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${pageId}`}>
Skip to content
</SkipToContent>
);

return (
<React.Fragment>
Expand Down
16 changes: 15 additions & 1 deletion packages/react-core/src/demos/examples/Nav/NavFlyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,21 @@ export const NavFlyout: React.FunctionComponent = () => {
);

const pageId = 'main-content-page-layout-flyout-nav';
const PageSkipToContent = <SkipToContent href={`#${pageId}`}>Skip to Content</SkipToContent>;

const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(pageId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${pageId}`}>
Skip to content
</SkipToContent>
);

return (
<Page
Expand Down
16 changes: 15 additions & 1 deletion packages/react-core/src/demos/examples/Nav/NavGrouped.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,21 @@ export const NavGrouped: React.FunctionComponent = () => {
</PageSidebar>
);
const pageId = 'main-content-page-layout-group-nav';
const PageSkipToContent = <SkipToContent href={`#${pageId}`}>Skip to Content</SkipToContent>;

const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(pageId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${pageId}`}>
Skip to content
</SkipToContent>
);

return (
<>
Expand Down
16 changes: 15 additions & 1 deletion packages/react-core/src/demos/examples/Nav/NavHorizontal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,21 @@ export const NavHorizontal: React.FunctionComponent = () => {
);

const pageId = 'main-content-page-layout-horizontal-nav';
const PageSkipToContent = <SkipToContent href={`#${pageId}`}>Skip to content</SkipToContent>;

const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(pageId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${pageId}`}>
Skip to content
</SkipToContent>
);

return (
<React.Fragment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,21 @@ export const NavHorizontalWithSubnav: React.FunctionComponent = () => {
);

const pageId = 'main-content-page-layout-horizontal-nav';
const PageSkipToContent = <SkipToContent href={`#${pageId}`}>Skip to content</SkipToContent>;

const handleClick = (event) => {
event.preventDefault();

const mainContentElement = document.getElementById(pageId);
if (mainContentElement) {
mainContentElement.focus();
}
};

const PageSkipToContent = (
<SkipToContent onClick={handleClick} href={`#${pageId}`}>
Skip to content
</SkipToContent>
);

return (
<React.Fragment>
Expand Down
Loading

0 comments on commit a574b5f

Please sign in to comment.