use Interlude
Read the curtain’s state or bring it back from anywhere inside `Interlude`.
Usage
Call useInterlude in any component inside Interlude to read its state or control its curtain:
import { useInterlude } from "react-interlude";
const { status, ready, first, meta, cover, uncover } = useInterlude();Bring the curtain back
cover() brings the curtain in and resolves when it reaches covered. uncover() asks it to leave and resolves when it reaches idle:
const { cover, uncover } = useInterlude();
const save = async () => {
await cover();
await saveEverything();
await uncover();
};Before leaving, uncover() still waits for until and hold. If you call cover() while the curtain is leaving, it turns around from where it is. Both promises resolve if Interlude unmounts, so an await won’t be left waiting forever.
Reading the current phase
status is the curtain’s current phase, or idle when it is hidden. Use it to coordinate the rest of the page with the curtain:
const { status } = useInterlude();
<main data-hidden={status === "covered"} />;Checking whether the page is ready
ready becomes true as soon as until resolves. The curtain may stay up while hold finishes, so a spinner inside it can stop as soon as the page is ready:
const { ready } = useInterlude();
{!ready && <Spinner />}Typing `meta`
Declare the meta type once and TypeScript will use it for both useInterlude and InterludeProps:
declare module "react-interlude" {
interface InterludeRegister {
meta: { duration: number };
}
}You can also skip the declaration and provide a type argument where you call the hook. This is useful when you have multiple interludes with different meta values:
const { meta } = useInterlude<{ duration: number }>();Reference
Returns
| prop | type | default | |
|---|---|---|---|
status | "idle" | "entering" | "covered" | "leaving" | — | the curtain’s current phase, or idle when it is hidden |
ready | boolean | — | whether until has resolved |
first | boolean | — | true until another curtain enters after the initial one |
meta | TMeta | — | the meta you passed to Interlude |
cover | () => Promise<void> | — | brings the curtain in and resolves when it is covered |
uncover | () => Promise<void> | — | lets the curtain leave and resolves when it is hidden |