All Projects → f → React Wait

f / React Wait

Licence: mit
Complex Loader Management Hook for React Applications

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Wait

Smart Webcomponents
Web Components & Custom Elements for Professional Web Applications
Stars: ✭ 110 (-58.96%)
Mutual labels:  ux, ui-components
Time Capsule
Write a letter to your future self - receive it in one year.
Stars: ✭ 48 (-82.09%)
Mutual labels:  hooks, ux
Nativebase Sketch Template
Sketch Template for NativeBase components
Stars: ✭ 157 (-41.42%)
Mutual labels:  ux, ui-components
Vue Content Loading
Vue component to easily build (or use presets) SVG loading cards Facebook like.
Stars: ✭ 729 (+172.01%)
Mutual labels:  ux, loading
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (-8.21%)
Mutual labels:  hooks, ux
Patternfly Design
Use this repo to file all new feature or design change requests for the PatternFly project
Stars: ✭ 82 (-69.4%)
Mutual labels:  ux, ui-components
React Components By Ruvkr
A collection of Responsive Animated Mobile friendly Lightweight React Components
Stars: ✭ 319 (+19.03%)
Mutual labels:  hooks, ui-components
Swiftui Drawer
A SwiftUI bottom-up controller, like in the Maps app. Drag to expand or minimize.
Stars: ✭ 280 (+4.48%)
Mutual labels:  ux, ui-components
React Nprogress
⌛️ A React primitive for building slim progress bars.
Stars: ✭ 173 (-35.45%)
Mutual labels:  hooks, loading
Zent
A collection of essential UI components written with React.
Stars: ✭ 2,133 (+695.9%)
Mutual labels:  hooks, ui-components
Circlebar
A fun, easy-to-use tab bar navigation controller for iOS.
Stars: ✭ 513 (+91.42%)
Mutual labels:  ux, ui-components
Bulma.io-axure
AxureRP Library with Bulma.io components
Stars: ✭ 90 (-66.42%)
Mutual labels:  ux, ui-components
React Native Blurhash
🖼️ A library to show colorful blurry placeholders while your content loads.
Stars: ✭ 430 (+60.45%)
Mutual labels:  ux, loading
Web Client
Generic Linked Data browser and UX component framework. Apache license.
Stars: ✭ 105 (-60.82%)
Mutual labels:  ux, ui-components
Oruga
🐛 Oruga is a lightweight library of UI components without CSS framework dependency
Stars: ✭ 297 (+10.82%)
Mutual labels:  ux, ui-components
Osiris
An Electron based desktop application for generating components, building pages, and storing them in a UI library.
Stars: ✭ 175 (-34.7%)
Mutual labels:  ux, ui-components
Pro Components
🏆 Use Ant Design like a Pro!
Stars: ✭ 1,039 (+287.69%)
Mutual labels:  hooks, ui-components
react-native-imaged-card-view
Rising Imaged Card View with Awesome Shadows and Fully Customizable Library for React Native
Stars: ✭ 16 (-94.03%)
Mutual labels:  ux, ui-components
svelteit
Svelteit is a minimalistic UI/UX component library for Svelte and Sapper projects
Stars: ✭ 64 (-76.12%)
Mutual labels:  ux, ui-components
mantine
React components library with native dark theme support
Stars: ✭ 4,390 (+1538.06%)
Mutual labels:  hooks

Complex Loader Management Hook for React.

Read the Medium post "Managing Complex Waiting Experiences on Web UIs".

npm version build codecov


Edit useWait

react-wait is a React Hook helps to manage multiple loading states on the page without any conflict. It's based on a very simple idea that manages an Array of multiple loading states. The built-in loader component listens its registered loader and immediately become loading state.

Why not React.Suspense?:

React has its own Suspense feature to manage all the async works. For now it only supports code-splitting (not data-fetching).

useWait allows you to manage waiting experiences much more explicitly and not only for Promised/async patterns but also complete loading management.

Overview

Here's a quick overview that what's useWait for:

import { useWait, Waiter } from "react-wait";

function A() {
  const { isWaiting } = useWait();
  return (
    <div>
      {isWaiting("creating user") ? "Creating User..." : "Nothing happens"}
    </div>
  );
}

function B() {
  const { anyWaiting } = useWait();
  return (
    <div>
      {anyWaiting() ? "Something happening on app..." : "Nothing happens"}
    </div>
  );
}

function C() {
  const { startWaiting, endWaiting, isWaiting } = useWait();

  function createUser() {
    startWaiting("creating user");
    // Faking the async work:
    setTimeout(() => {
      endWaiting("creating user");
    }, 1000);
  }

  return (
    <button disabled={isWaiting("creating user")} onClick={createUser}>
      <Wait on="creating user" fallback={<Spinner />}>
        Create User
      </Wait>
    </button>
  );
}

ReactDOM.render(
  <Waiter>
    <C />
  </Waiter>,
  document.getElementById("root")
);

Quick Start

If you are a try and learn developer, you can start trying the react-wait now using codesandbox.io.

Quick start on CodeSandbox

1. Install:

yarn add react-wait

2. Require:

import { Waiter, useWait } from "react-wait";

function UserCreateButton() {
  const { startWaiting, endWaiting, isWaiting, Wait } = useWait();

  return (
    <button
      onClick={() => startWaiting("creating user")}
      disabled={isWaiting("creating user")}
    >
      <Wait on="creating user" fallback={<div>Creating user!</div>}>
        Create User
      </Wait>
    </button>
  );
}

3. Wrap with the Waiter Context Provider

And you should wrap your App with Waiter component. It's actually a Context.Provider that provides a loading context to the component tree.

const rootElement = document.getElementById("root");
ReactDOM.render(
  <Waiter>
    <App />
  </Waiter>,
  rootElement
);

Installation

$ yarn add react-wait
# or if you using npm
$ npm install react-wait

The API

react-wait provides some helpers to you to use in your templates.

anyWaiting()

Returns boolean value if any loader exists in context.

const { anyWaiting } = useWait();

return <button disabled={anyWaiting()}>Disabled while waiting</button>;

isWaiting(waiter String)

Returns boolean value if given loader exists in context.

const { isWaiting } = useWait();

return (
  <button disabled={isWaiting("creating user")}>
    Disabled while creating user
  </button>
);

startWaiting(waiter String)

Starts the given waiter.

const { startWaiting } = useWait();

return <button onClick={() => startWaiting("message")}>Start</button>;

endWaiting(waiter String)

Stops the given waiter.

const { end } = useWait();

return <button onClick={() => endWaiting("message")}>Stop</button>;

Using Wait Component

function Component() {
  const { Wait } = useWait();
  return (
    <Wait on="the waiting message" fallback={<div>Waiting...</div>}>
      The content after waiting done
    </Wait>
  );
}

Better example for a button with loading state:

<button disabled={isWaiting("creating user")}>
  <Wait on="creating user" fallback={<div>Creating User...</div>}>
    Create User
  </Wait>
</button>

Making Reusable Loader Components

With reusable loader components, you will be able to use custom loader components as example below. This will allow you to create better user loading experience.

function Spinner() {
  return <img src="spinner.gif" />;
}

Now you can use your spinner everywhere using waiting attribute:

<button disabled={isWaiting("creating user")}>
  <Wait on="creating user" fallback={<Spinner />}>
    Create User
  </Wait>
</button>

Creating Waiting Contexts using createWaitingContext(context String)

To keep your code DRY you can create a Waiting Context using createWaitingContext.

function CreateUserButton() {
  const { createWaitingContext } = useWait();

  // All methods will be curried with "creating user" on.
  const { startWaiting, endWaiting, isWaiting, Wait } = createWaitingContext(
    "creating user"
  );

  function createUser() {
    startWaiting();
    setTimeout(endWaiting, 1000);
  }

  return (
    <Button disabled={isWaiting()} onClick={createUser}>
      <Wait fallback="Creating User...">Create User</Wait>
    </Button>
  );
}

Contributors

  • Fatih Kadir Akın, (creator)

Other Implementations

Since react-wait based on a very simple idea, it can be implemented on other frameworks.

  • vue-wait: Multiple Process Loader Management for Vue.
  • dom-wait: Multiple Process Loader Management for vanilla JavaScript.

License

MIT © Fatih Kadir Akın

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].