Skip to content

Commit

Permalink
Move Heading Check component to own file so I can add to the Page com…
Browse files Browse the repository at this point in the history
…ponent without making the file too cluttered
  • Loading branch information
lortimer committed Sep 2, 2023
1 parent 9902def commit a6d5af8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
29 changes: 29 additions & 0 deletions src/components/dev-only/HeadingCheck.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useEffect, useState } from "react";
import { ErrorMessageNotification } from "../userMessage/ErrorMessageNotification";

/**
* This is for development purposes. It displays an error message when a page is missing an h1 element or has
* one that is configured incorrectly. We will see the error if we create a page that has this issue.
*/
export const HeadingCheck = () => {
const [error, setError] = useState("");

useEffect(() => {
const heading1 = document.querySelector("h1");

if (heading1 === null) {
setError("This page is missing an h1 element - please add one with `tabindex=-1`");
} else {
const missingTabIndex = !(heading1.hasAttribute("tabindex"));
const wrongTabindex = heading1.tabIndex !== -1;

if (missingTabIndex || wrongTabindex) {
setError("This page's h1 element has a bad `tabindex` - please specify `tabindex=-1`");
}
}
}, []);

return (<>
{error && process.env.NODE_ENV !== "production" && <ErrorMessageNotification message={error}/>}
</>);
};
31 changes: 2 additions & 29 deletions src/pages/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useContext, useEffect, useState } from "react";
import React, { useContext, useEffect } from "react";
import { ChildrenProps } from "../utilities/children-props";
import { ErrorMessageNotification } from "../components/userMessage/ErrorMessageNotification";
import { DocumentStateContext } from "../contexts/DocumentStateContext";
import { useFocusHashOrMainHeading } from "../hooks/focus/useFocusHashOrMainHeading";
import "./Pages.css";
import { HeadingCheck } from "../components/dev-only/HeadingCheck";

type PageProps = {
title: string
Expand Down Expand Up @@ -32,30 +32,3 @@ export const Page = ({ title, children }: PageProps) => {
</div>
</>);
};

/**
* This is for development purposes. It displays an error message when a page is missing an h1 element or has
* one that is configured incorrectly. We will see the error if we create a page that has this issue.
*/
const HeadingCheck = () => {
const [error, setError] = useState("");

useEffect(() => {
const heading1 = document.querySelector("h1");

if (heading1 === null) {
setError("This page is missing an h1 element - please add one with `tabindex=-1`");
} else {
const missingTabIndex = !(heading1.hasAttribute("tabindex"));
const wrongTabindex = heading1.tabIndex !== -1;

if (missingTabIndex || wrongTabindex) {
setError("This page's h1 element has a bad `tabindex` - please specify `tabindex=-1`");
}
}
}, []);

return (<>
{error && process.env.NODE_ENV !== "production" && <ErrorMessageNotification message={error}/>}
</>);
};

0 comments on commit a6d5af8

Please sign in to comment.