@agney/react-loading

中文文档
0.1.2发布于 2020/10/05MITTypeScriptESM + CJS近 30 天下载 6977

React Loading

Simple and Accessible loading indicators with React.

原始使用文档
文档目录9 节
  1. React Loading
  2. Installation
  3. Features
  4. Usage
  5. Loaders
  6. Context
  7. Extra Props on Loader
  8. Bring your own loader
  9. Development

React Loading

Simple and Accessible loading indicators with React.


code style: prettier

Comes bundled with React components of Sam Herbert's animated SVG loaders in a tree shakeable package.

Example of loading indicator SVGs

Installation

npm i @agney/react-loading
# OR
yarn add @agney/react-loading

Demo

Features

  • Small Size

    The whole library is about 20kB minified. But you would never need the whole bundle.

    The library is build to be treeshakeable that when you use one or two of the bundled loaders, you would have less than 1kB in your bundle.

  • Accessibility

    Provides accessibility attributes on your loading components and containers.

    aria-busy is set to true on container on loading and progress indicators have role=progressbar.

  • Specify a Global loader

    You probably don't want loader components mixed everywhere, so you can specify a LoaderContext that can be overridden later if necessary.

  • Bring your own loader

    If you decide to bring your own loading indicator, library would support that as well, keeping all your logic the same.

  • TypeScript support. Zero extra CSS.

Usage

import { useLoading, Audio } from '@agney/react-loading';

function Content() {
  const { containerProps, indicatorEl } = useLoading({
    loading: true,
    indicator: <Audio width="50" />,
  });

  return (
    {/* Accessibility props injected to container */}
    <section {...containerProps}>
      {indicatorEl} {/* renders only while loading */}
    </section>
  );
}

Loaders

This library comes bundled with React components of Sam Herbert's animated SVG loaders in a tree shakeable package.

Each loader is an SVG and all props passed shall be applied to the top SVG element. All SVGs are set to inherit currentColor from it's parents for fill/stroke.

Available loaders are:

import {
  Audio,
  BallTriangle,
  Bars,
  Circles,
  Grid,
  Hearts,
  Oval,
  Puff,
  Rings,
  SpinningCircles,
  TailSpin,
  ThreeDots,
} from '@agney/react-loading';

Only the ones you use will be included in your bundle when you use a bundler like Webpack/Rollup.

Context

You can specify a single loading indicator reused across hooks with the LoaderProvider.

import { LoaderProvider, useLoading, BallTriangle } from '@agney/react-loading';

function App() {
  const { containerProps, indicatorEl } = useLoading({
    loading: true,
  });
  return <section {...containerProps}>{indicatorEl}</section>;
}

ReactDOM.render(
  <LoaderProvider indicator={<BallTriangle width="50" />}>
    <App />
  </LoaderProvider>
);

You can use as many LoaderProvider provider elements as you like and React will pick the one closest to the hook you are rendering.

More on React Context

Extra Props on Loader

If you want to provide specific props on a loader specifically when you use the hook:

useLoading({
  loading: true,
  loaderProps: {
    // Any props here would be spread on to the indicator element.
    style: {{ margin: '0 auto' }}
  }
});

We also a provide a special key for valueText, that will be used as description for indicator:

useLoading({
  loading: true,
  loaderProps: {
    valueText: 'Fetching video from the Great Internet',
  },
});

// now this will generate:
/* <svg role="progressbar" aria-valuetext="Fetching video from the Great Internet"></svg>*/

aria-valuetext will be read by screenreaders.

You could also provide aria-valuenow for indicators that display progress (but the prebundled ones are best for indeterminate progress indicators)

MDN for Reference on progressbar

Bring your own loader

Just switch the import to your own loading indicator (just make sure that it accepts props)

import { LoaderProvider, useLoading } from '@agney/react-loading';

const Loader = ({ ...rest }) => <p {...rest}>Loading...</p>;

function App() {
  const { containerProps, indicatorEl } = useLoading({
    loading: true,
  });
  return <section {...containerProps}>{indicatorEl}</section>;
}

ReactDOM.render(
  <LoaderProvider indicator={<Loader />}>
    <App />
  </LoaderProvider>
);

Contributing

All PRs welcome.

Development

We use tsdx for generating boilerplate.

Install:

# Library
npm i

# Example
cd example && npm i

Development:

# Running library dev
npm start

# Running example
cd example && npm start

Testing:

Testing with react-testing-library and jest

npm test

Commands are available in detail on tsdx repository.

版本与发布记录

@agney/react-loading 共发布 2 个版本,首次发布于 2020/10/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2020/10/056 年前
0.1.12020/10/046 年前

关于 @agney/react-loading 的常见问题

@agney/react-loading 是什么?

React Loading Simple and Accessible loading indicators with React.

如何安装 @agney/react-loading?

在项目目录运行 npm install @agney/react-loading,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@agney/react-loading 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@agney/react-loading 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@agney/react-loading 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@agney/react-loading 可以商用吗?

@agney/react-loading 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@agney/react-loading 的下载量有多少?

根据 npm 官方下载统计,@agney/react-loading 在 2026/10/05 – 2026/11/03 这 30 天内被下载 6,977 次,日均约 233 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@agney/react-loading 还在维护吗?

最新版本 0.1.2 发布于 2020/10/05(6 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/10/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。