Utilities for using hyperscript
with React. This library is thin wrapper
around react-hyperscript
with some nice additions. It is used in most of Macrostrat's
modern web applications instead of JSX.
import { render } from "react-dom";
import h from "@macrostrat/hyper";
const MyDiv = () =>
h("div", [h("h1", "Hello, world!"), h("p", "This is a paragraph.")]);
const el = document.body.appendChild(document.createElement("div"));
render(h(MyDiv), el);
The Mithril framework is one of the few widely-used frameworks to
recommend hyperscript syntax over JSX. They have a nice explainer
on the differences between the syntaxes in their docs. @macrostrat/hyper
has some of the
Mithril template engine's flexibility, allowing templates to cross-compile. This means that
tooling for converting HTML and JSX to hyperscript, such as this
Mithril template converter,
can be used to migrate components.
The hyperStyled
function makes working with CSS modules a bit easier.
Finds class names in components and replaces with namespaced values in all components
created. This adds a bit of runtime overhead, but is worth it, since you can transparently refer to styles by their simple names. Accessible as h.styled
.
import { render } from "react-dom";
import styles from "./test.css";
import hyper from "@macrostrat/hyper";
const h = hyper.styled(styles);
// or equivalently
import { hyperStyled } from "@macrostrat/hyper";
const h = hyperStyled(styles);
const el = document.body.appendChild(document.createElement("div"));
render(h("div.styled"), el);
If you have a lot of styles, you can apply them all:
import hyper from "@macrostrat/hyper";
import styles from "./test.css";
import styles1 from "./test1.styl";
const h = hyper.styled({ ...styles, ...styles1 });
h("div#pos-1.warning.major-caveat");
The hyperIf
function renders a component only if a boolean argument returns true. Accessible as h.if
. To wit:
h.if(shouldRender)("div", "I only render when I should");
This construction can make conditional template logic easier to read.
A wrapper around the classnames module,
which extends the className
string for a props
object.
Adds a class to a React component.
Compose React components.
An aggressive shorthand to create a React component that passes through children.
const C =
(c, props = {}) =>
({ children }) =>
h(c, { ...props, children });
Use compose
and C
together to improve deeply nested provider chains.
import { compose, C } from "@macrostrat/hyper";
const DataProvider = compose(
C(PublicURLProvider, { publicURL }),
C(APIProvider, { baseURL }),
C(ImageStoreProvider, { baseURL: imageBaseURL }),
C(TaggingApplication, props)
);
Note: Running compose
within the render function of another component
is a bad idea, since components will be regenerated each time the function
is run. Prefer to run this outside of the render loop where possible.
- Export Hyper type by default.
- Fix a small bug with types for React fragments
- Add a note about Mithril JS compatibility to documentation.
- Integrate
react-hyperscript
and@types/react-hyperscript
directly into codebase to fix typing errors. - Substantially rework typings to be simpler and more consistent.
- The
hyper.styled
function was changed to be stateful, allowing an easier API for CSS styling. - The
applyIf
function was removed from the public API. - Note: In
2.0.1
, we had to roll back some of our "improvements" because they were unworkable with ES Modules.