2021-04-03 01:17:44 +00:00
|
|
|
import React, { MouseEvent, useContext } from "react";
|
Improve layout (#74)
This improves the layout of all our pages to look more like the mandala page.
Additionally, some form widgets now have better layout, and the header takes up less vertical space.
At an implementation level, the component hierarchy of pages has been inverted to make this kind of layout easier. Now fully laid-out pages are contained within `<Page>` components that are at the top of the component hierarchy, and which are defined by each specific page (mandala, creature, vocabulary, etc).
I had to do a few architectural things to avoid circular imports, though; most notably, this involved the creation of a new React context called a `PageContext`.
It uses CSS grid, which should be pretty well-supported amongst recent browsers.
2021-04-02 23:00:29 +00:00
|
|
|
import type { PageName } from "./pages";
|
|
|
|
|
|
|
|
export type PageContext = {
|
|
|
|
currPage: PageName;
|
|
|
|
allPages: PageName[];
|
2021-04-03 01:17:44 +00:00
|
|
|
pushState: (href: string) => void;
|
Improve layout (#74)
This improves the layout of all our pages to look more like the mandala page.
Additionally, some form widgets now have better layout, and the header takes up less vertical space.
At an implementation level, the component hierarchy of pages has been inverted to make this kind of layout easier. Now fully laid-out pages are contained within `<Page>` components that are at the top of the component hierarchy, and which are defined by each specific page (mandala, creature, vocabulary, etc).
I had to do a few architectural things to avoid circular imports, though; most notably, this involved the creation of a new React context called a `PageContext`.
It uses CSS grid, which should be pretty well-supported amongst recent browsers.
2021-04-02 23:00:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const PageContext = React.createContext<PageContext>({
|
|
|
|
currPage: "vocabulary",
|
|
|
|
allPages: [],
|
2021-04-03 01:17:44 +00:00
|
|
|
pushState: () => {
|
|
|
|
throw new Error("No page context is defined!");
|
|
|
|
},
|
Improve layout (#74)
This improves the layout of all our pages to look more like the mandala page.
Additionally, some form widgets now have better layout, and the header takes up less vertical space.
At an implementation level, the component hierarchy of pages has been inverted to make this kind of layout easier. Now fully laid-out pages are contained within `<Page>` components that are at the top of the component hierarchy, and which are defined by each specific page (mandala, creature, vocabulary, etc).
I had to do a few architectural things to avoid circular imports, though; most notably, this involved the creation of a new React context called a `PageContext`.
It uses CSS grid, which should be pretty well-supported amongst recent browsers.
2021-04-02 23:00:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export const PAGE_QUERY_ARG = "p";
|
|
|
|
|
2021-04-03 01:17:44 +00:00
|
|
|
function isNormalLinkClick(e: MouseEvent): boolean {
|
|
|
|
return !e.shiftKey && !e.altKey && !e.metaKey && !e.ctrlKey && e.button === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PageLink: React.FC<{ page: PageName }> = ({ page }) => {
|
|
|
|
const href = `?${PAGE_QUERY_ARG}=${encodeURIComponent(page)}`;
|
|
|
|
const { pushState } = useContext(PageContext);
|
|
|
|
const handleClick = (e: MouseEvent) => {
|
|
|
|
if (isNormalLinkClick(e)) {
|
|
|
|
pushState(href);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<a href={href} onClick={handleClick}>
|
|
|
|
{page}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
};
|
Improve layout (#74)
This improves the layout of all our pages to look more like the mandala page.
Additionally, some form widgets now have better layout, and the header takes up less vertical space.
At an implementation level, the component hierarchy of pages has been inverted to make this kind of layout easier. Now fully laid-out pages are contained within `<Page>` components that are at the top of the component hierarchy, and which are defined by each specific page (mandala, creature, vocabulary, etc).
I had to do a few architectural things to avoid circular imports, though; most notably, this involved the creation of a new React context called a `PageContext`.
It uses CSS grid, which should be pretty well-supported amongst recent browsers.
2021-04-02 23:00:29 +00:00
|
|
|
|
|
|
|
const Navbar: React.FC<{}> = (props) => {
|
|
|
|
const pc = useContext(PageContext);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<nav>
|
|
|
|
<ul className="navbar">
|
|
|
|
{pc.allPages.map((pageName) => (
|
|
|
|
<li key={pageName}>
|
|
|
|
{pc.currPage === pageName ? pageName : <PageLink page={pageName} />}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export type PageProps = {
|
|
|
|
title: string;
|
|
|
|
children?: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Page: React.FC<PageProps> = ({ title, children }) => {
|
|
|
|
return (
|
|
|
|
<div className="page">
|
|
|
|
<header>
|
2021-04-02 23:02:40 +00:00
|
|
|
<h1>Mystic Symbolic {title}</h1>
|
Improve layout (#74)
This improves the layout of all our pages to look more like the mandala page.
Additionally, some form widgets now have better layout, and the header takes up less vertical space.
At an implementation level, the component hierarchy of pages has been inverted to make this kind of layout easier. Now fully laid-out pages are contained within `<Page>` components that are at the top of the component hierarchy, and which are defined by each specific page (mandala, creature, vocabulary, etc).
I had to do a few architectural things to avoid circular imports, though; most notably, this involved the creation of a new React context called a `PageContext`.
It uses CSS grid, which should be pretty well-supported amongst recent browsers.
2021-04-02 23:00:29 +00:00
|
|
|
<Navbar />
|
|
|
|
</header>
|
|
|
|
{children}
|
|
|
|
<footer>
|
|
|
|
<p>
|
|
|
|
For more details about this project, see its{" "}
|
|
|
|
<a href="https://github.com/toolness/mystic-symbolic" target="_blank">
|
|
|
|
GitHub repository
|
|
|
|
</a>
|
|
|
|
.
|
|
|
|
</p>
|
|
|
|
</footer>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|