Skip to content

BearStudio/ui-state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@bearstudio/ui-state

A lightweight utility to handle dynamic UI display states like loading, error, or any custom-defined state β€” based on your app logic.

✨ Features

  • 🧠 Declarative and expressive API
  • πŸ” Dynamic state evaluation with conditions
  • βœ… Fully customizable states and payloads

πŸ“¦ Installation

pnpm install @bearstudio/ui-state
npm install @bearstudio/ui-state
yarn add @bearstudio/ui-state

πŸš€ Basic Usage

import { getUiState } from "@bearstudio/ui-state";

const ui = getUiState((set) => {
  if (bookQuery.status === "pending") return set("pending");
  if (bookQuery.status === "error") return set("error");
  return set("default", { book: bookQuery.data });
});

Rendering in React

ui.match("pending", () => <>Loading...</>)
  .match("error", () => <>Error</>)
  .match("default", ({ book }) => <>{book.title}</>)
  .exhaustive();

πŸͺ„ Fully customizable and type-safe

const ui = getUiState((set) => {
  if (booksToImport === null) return set("show-input");
  if (booksToImport.length === 0) return set("empty");
  return set("listing", { booksToImport });
});

ui.is("show-input"); // valid
ui.is("default"); // invalid

πŸ“– APIs

is

ui.is("pending"); // Return true if state is `pending`, false otherwise

when

ui.when("pending", () => <>Loading...</>); // Render only when state is `pending`

match + exhaustive

ui
  .match("pending", () => <>Loading...</>); // Render when state is `pending`
  .match("error", () => <>Error...</>); // Render when state is `error`
  .match("default", () => <>Error...</>); // Render when state is `default`
  .exhaustive(); // Ensures all possible states are matched and rendered

match + nonExhaustive

ui
  .match("pending", () => <>Loading...</>); // Render when state is `pending`
  .match("default", () => <>Error...</>); // Render when state is `default`
  .nonExhaustive(); // Allows rendering without matching all possible states

πŸ“ƒ License MIT

Made with ❀️ by BearStudio Team

About

A lightweight utility to handle dynamic UI display states like loading, error, or any custom-defined state, based on your app logic.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

 
 
 

Contributors