2021-04-03 01:17:44 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-02-04 00:11:34 +00:00
|
|
|
import ReactDOM from "react-dom";
|
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 { PageContext, PAGE_QUERY_ARG } from "./page";
|
|
|
|
import { pageNames, Pages, toPageName, DEFAULT_PAGE } from "./pages";
|
2021-02-04 00:36:29 +00:00
|
|
|
|
2021-02-04 00:11:34 +00:00
|
|
|
const APP_ID = "app";
|
|
|
|
|
|
|
|
const appEl = document.getElementById(APP_ID);
|
|
|
|
|
|
|
|
if (!appEl) {
|
|
|
|
throw new Error(`Unable to find #${APP_ID}!`);
|
|
|
|
}
|
|
|
|
|
2021-04-03 01:17:44 +00:00
|
|
|
function getWindowSearch(): URLSearchParams {
|
|
|
|
return new URLSearchParams(window.location.search);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the given handler whenever a `popstate` event
|
|
|
|
* occurs.
|
|
|
|
*
|
|
|
|
* Return a function that wraps `window.history.pushState()`;
|
|
|
|
* the given handler will be called immediately afterwards.
|
|
|
|
*/
|
|
|
|
function usePushState(onPushOrPopState: () => void) {
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener("popstate", onPushOrPopState);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("popstate", onPushOrPopState);
|
|
|
|
};
|
|
|
|
}, [onPushOrPopState]);
|
|
|
|
|
|
|
|
return function pushState(href: string) {
|
|
|
|
window.history.pushState(null, "", href);
|
|
|
|
onPushOrPopState();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:32:55 +00:00
|
|
|
const App: React.FC<{}> = (props) => {
|
2021-04-03 01:17:44 +00:00
|
|
|
const [search, setSearch] = useState(getWindowSearch());
|
|
|
|
const updateSearchFromWindow = () => setSearch(getWindowSearch());
|
|
|
|
const currPage = toPageName(search.get(PAGE_QUERY_ARG) || "", DEFAULT_PAGE);
|
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 PageComponent = Pages[currPage];
|
2021-04-03 01:17:44 +00:00
|
|
|
const pushState = usePushState(updateSearchFromWindow);
|
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 ctx: PageContext = {
|
|
|
|
currPage,
|
|
|
|
allPages: pageNames,
|
2021-04-03 01:17:44 +00:00
|
|
|
pushState,
|
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
|
|
|
};
|
2021-02-14 02:13:04 +00:00
|
|
|
|
|
|
|
return (
|
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
|
|
|
<PageContext.Provider value={ctx}>
|
|
|
|
<PageComponent />
|
|
|
|
</PageContext.Provider>
|
2021-02-14 02:13:04 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-02-14 17:32:55 +00:00
|
|
|
ReactDOM.render(<App />, appEl);
|