1 Star 0 Fork 0

heerzhang / react-toastnotes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载

A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.

Permissions
  • Commercial use
  • Modification
  • Distribution
  • Private use
Limitations
  • Liability
  • Warranty
Conditions
  • License and copyright notice
README.md 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
herzhang 提交于 2020-12-18 05:42 . 确定发行版

Toasted-notes

npm package Tweet Follow on Twitter

A simple but flexible implementation of toast style notifications for React extracted from customize-easy-ui-component.

  • 原版的包"toasted-notes"无人维护更新,无法适应react-spring v9,依赖有问题,故而只好自建新的包并改名。

View the demo and documentation.

Features

  • An imperative API. This means that you don't need to set component state or render elements to trigger notifications. Instead, just call a function.
  • Render whatever you want. Utilize the render callback to create entirely custom notifications.
  • Functional default styles. Import the provided css for some nice styling defaults or write your own styles.

Install

Install react-toastnotes , using yarn or npm.

yarn add react-toastnotes

Example

import toaster from "react-toastnotes";
//import "react-toastnotes/src/styles.css"; // optional styles

const HelloWorld = () => (
  <button
    onClick={() => {
      toaster.notify("Hello world", {
        duration: 2000
      });
    }}
  >
    Say hello
  </button>
);

API

The notify function accepts either a string, a react node, or a render callback.

// using a string
toaster.notify("With a simple string");

// using jsx
toaster.notify(<div>Hi there</div>);

// using a render callback
toaster.notify(({ onClose }) => (
  <div>
    <span>My custom toaster</span>
    <button onClick={onClose}>Close me please</button>
  </div>
));

It also accepts options.

toaster.notify("Hello world", {
  position: "bottom-left", // top-left, top, top-right, bottom-left, bottom, bottom-right
  duration: null // This notification will not automatically close
});

Using Context

One downside to the current API is that render callbacks and custom nodes won't get access to any application context, such as theming variables provided by styled-components. To ensure that render callbacks have access to the necessary context, you'll need to supply that context to the callback.

const CustomNotification = ({ title }) => {
  const theme = useTheme();
  return <div style={{ color: theme.primary }}>{title}</div>;
};

const CustomNotificationWithTheme = withTheme(CustomNotification);

toaster.notify(() => <CustomNotificationWithTheme title="I am pretty" />);

Contributors

License

MIT

Prior art

Way back, this was originally based on the wonderful implementation of notifications in evergreen.

TypeScript
1
https://gitee.com/heerzhang/react-toastnotes.git
git@gitee.com:heerzhang/react-toastnotes.git
heerzhang
react-toastnotes
react-toastnotes
master

搜索帮助