• This repository has been archived on 15/Mar/2020
  • Stars
    star
    649
  • Rank 69,373 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Alerts / Notifications for React with rich configuration options

This lib is no longer maintained. It needs major adjustments and rewrites. I hope I'll find some time for it in the future. Sorry.

React sAlert component

sAlert is a React component which provides alerts or notifications with rich configuration possibilities. This is a rewritten version of Meteor/Blaze sAlert package which you can find here: s-alert.meteor.com.

Demo

The demo website and provided source code are the best learning resources.

Usage with React

Here is what you need to do to make it work. Of course you need to have React and ReactDOM installed in your project.

1. Install the package

npm install react-s-alert --save

2. Import component

With ES2015:

import Alert from 'react-s-alert';

With ES5:

var Alert = require('react-s-alert').default;

3. Import (or copy) CSS files

All you need to do is to import (or copy) a default CSS file and some or all CSS files with effects which you want to use. A default CSS file is mandatory. With Webpack you could do something like:

With ES2015:

// mandatory
import 'react-s-alert/dist/s-alert-default.css';

// optional - you can choose the effect you want
import 'react-s-alert/dist/s-alert-css-effects/slide.css';
import 'react-s-alert/dist/s-alert-css-effects/scale.css';
import 'react-s-alert/dist/s-alert-css-effects/bouncyflip.css';
import 'react-s-alert/dist/s-alert-css-effects/flip.css';
import 'react-s-alert/dist/s-alert-css-effects/genie.css';
import 'react-s-alert/dist/s-alert-css-effects/jelly.css';
import 'react-s-alert/dist/s-alert-css-effects/stackslide.css';

With ES5:

// mandatory
require('react-s-alert/dist/s-alert-default.css');

// optional - you can choose the effect you want
require('react-s-alert/dist/s-alert-css-effects/slide.css');
require('react-s-alert/dist/s-alert-css-effects/scale.css');
require('react-s-alert/dist/s-alert-css-effects/bouncyflip.css');
require('react-s-alert/dist/s-alert-css-effects/flip.css');
require('react-s-alert/dist/s-alert-css-effects/genie.css');
require('react-s-alert/dist/s-alert-css-effects/jelly.css');
require('react-s-alert/dist/s-alert-css-effects/stackslide.css');

CDN:

(you can change versions in URL if needed)

You can also copy the files and include it another way in your app. It depends on your workflow.

If you are using CSS Modules for now you need to import these files globally. (You can check the demo website Webpack config file).

4. Place sAlert component in your main app component

You need to place the main sAlert container. The best place for it is at the end of your main app component. For Example:

import React from 'react';
import {Router} from 'react-router';
import Alert from 'react-s-alert';

import 'react-s-alert/dist/s-alert-default.css';
import 'react-s-alert/dist/s-alert-css-effects/slide.css';

class Main extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (
            <div>
                <span>
                    {this.props.children}
                </span>
                <Alert stack={{limit: 3}} />
            </div>
        )
    }
}

export default Main;

5. Make calls to activate alerts

You can activate your alerts in many different places in the app. You need to call proper methods. For Example:

Methods which you can use:

  • Alert.warning(message, configObj)
  • Alert.error(message, configObj)
  • Alert.info(message, configObj)
  • Alert.success(message, configObj)
  • Alert.close(alertId)
  • Alert.closeAll()

sAlert methods will return the already created alertId.

Example usage:

import React from 'react';
import Alert from 'react-s-alert';

class Home extends React.Component {
    handleClick1(e) {
        e.preventDefault();
        Alert.warning('<h1>Test message 1</h1>', {
            position: 'top-right',
            effect: 'scale',
            onShow: function () {
                console.log('aye!')
            },
            beep: false,
            timeout: 'none',
            offset: 100
        });
    }
    handleClick2(e) {
        e.preventDefault();
        Alert.info('Test message 2', {
            position: 'bottom-left',
            effect: 'bouncyflip',
            timeout: 'none'
        });
    }
    handleClick3(e) {
        e.preventDefault();
        Alert.error('Test message 3', {
            position: 'bottom-right',
            effect: 'slide',
            timeout: 'none'
        });
    }
    handleCloseAll(e) {
        e.preventDefault();
        Alert.closeAll();
    }
    render() {
        return (
            <div>
                <div>
                    <a href="#" onClick={this.handleClick1}>Click 1</a> |
                    <a href="#" onClick={this.handleClick2}>Click 2</a> |
                    <a href="#" onClick={this.handleClick3}>Click 3</a> |
                    <a href="#" onClick={this.handleCloseAll}>Close All</a>
                </div>
            </div>
        )
    }
}

export default Home;

You always need to provide a message. For example

Alert.error('Test message 3');

You can also provide a react component:

Alert.error(<MyComponent props1={props1} props2={props2}/>);

You don't need to provide the configuration object here, just remember to provide it globally.

Configuration details

With sAlert you can place your configuration as a global config in the main sAlert container, for example:

<Alert stack={{limit: 3}} html={true} />

You can also overwrite these global settings in the particular method call. For example, here we will overwrite the global settings for 'html' in our error alert call:

Alert.error('Error message...', {
    position: 'bottom-right',
    effect: 'slide',
    html: false
});

All possible configuration options:

First - only global configuration options

You can set it up only in the main sAlert component props

1. stack

You can stack your alerts or just display them in the same place.

Possible stack values:

  • true or false
  • object with:
    • limit (you can limit your alerts displayed on screen)
    • spacing (you can change the space around your alerts)

Examples:

<Alert stack={{limit: 3, spacing: 50}} />

or

<Alert stack={true} />
2. contentTemplate

You can prepare your own content template even with additional fields (More details can be found later on in this README.)

Examples:

<Alert contentTemplate={MyContentTemplate} />

Here you can also use all configuration options listed below.

Configuration options with a single sAlert method call

1. effect

You can provide the name of the animations effect, you also need to import proper CSS file.

Example:

In method call:

Alert.success('Message...', {
    effect: 'genie'
});

In global config:

<Alert effect='genie' />

Possible effects names:

  • slide
  • scale
  • bouncyflip
  • flip
  • genie
  • jelly
  • stackslide

Remember that you need to import the CSS files for the effects. See above.

2. position

Where the alert should appear.

Example:

In method call:

Alert.success('Message...', {
    position: 'top-right'
});

In global config:

<Alert position='top-right' />

Possible positions:

  • top (full width)
  • bottom (full width)
  • top-right
  • top-left
  • bottom-right
  • bottom-left
3. timeout

You can set up the timeout in ms.

Example:

In method call:

Alert.success('Message...', {
    timeout: 5000
});

In global config:

<Alert timeout={5000} />

Possible timeout values:

  • (Number - ms)
  • 'none'
4. html

You can configure if your alert should display HTML formated messages.

Example:

In method call:

Alert.success('<h1>Message...</h1>', {
    html: true
});

In global config:

<Alert html={true} />

Possible 'html' values:

  • true
  • false
5. offset

In px. Will be added to first alert (bottom or top - depends on the position in config).

Example:

In method call:

Alert.success('Message...', {
    offset: 150
});

In global config:

<Alert offset={150} />

Possible offset values:

  • (Number - px)
6. beep

You can set up your audio 'beeps'. Just configure your audio file path. (.mp3 files should work in every browser.) You can also configure 4 paths for 4 conditions.

There is no default audio sample in the package. You should use sound samples which you know that you have the right to use it.

Example:

In method call:

Alert.success('Message...', {
    beep: '/path-to-audio/file.mp3'
});

In global config:

<Alert beep={{
    info: '/path-to-audio/file-info.mp3',
    error: '/path-to-audio/file-error.mp3',
    warning: '/path-to-audio/file-warning.mp3',
    success: '/path-to-audio/file-success.mp3'}} />

or just one for all:

<Alert beep='/path-to-audio/file.mp3' />

Possible 'beep' values:

  • (String - audio file path) (one audio file for all alerts)
  • (Object)
    • {info: '/path/file.mp3', error: '/path/file.mp3', warning: '/path/file.mp3', success: '/path/file.mp3'}
7. preserveContext

Makes sure that your Alert always has the parent's context. It is needed because the Alert's height, which is needed for calculating the position of each element in the stack, is measured by directly mounting an Alert into DOM by using ReactDOM.render. If you want to include any custom JSX inside your Alert, e.g. for Material UI, which uses context for passing theme configuration, you will need this option to be set to true.

This options enables the usage of the new ReactDOM.unstable_renderSubtreeIntoContainer function, which works exactly the same as ReactDOM.render, but keeps the context from the parent's component. Even though this option is named as "unstable", it works perfectly fine.

Possible preserveContext values:

  • true or false (Defaults to false)

Example:

In method call:

Alert.success('Message...', {
    preserveContext: true
});

In global config:

<Alert preserveContext />
8. onShow

Execute a callback function. onShow will fire the function when the alert appears.

Example:

In method call:

Alert.success('Message...', {
    onShow: function () {
        console.log('onShow Fired!');
    }
});

In global config:

<Alert onShow={this.handleOnShow} />

Possible 'onShow' values:

  • (Function)
9. onClose

Execute a callback function. Will fire the function when the alert is closed.

Example:

In method call:

Alert.success('Message...', {
    onClose: function () {
        console.log('onClose Fired!');
    }
});

In global config:

<Alert onClose={this.handleOnClose} />

Possible 'onClose' values:

  • (Function)
10. customFields

You can pass a customFields object for your custom content template component. You need to prepare the component to be able to display customFields values. You'll read more about it below.

Example:

In global config you need to provide custom content template component:

<Alert contentTemplate={MyContentTemplate} />

In method call you can provide custom fields used in your custom template:

Alert.success('Message...', {
    customFields: {
        specialInfo: this.getSpecialInfo();
    }
});

Possible 'customFields' values:

  • (Object)

Overwrite content template component

With sAlert you have the possibility to overwrite the SAlertContentTmpl core component. This is useful when you want to provide more dynamic data in your alerts or just when you want to rebuild the HTML structure of the alert. This is very useful, but might not be trivial. Standard sAlerts will take only the message you want to display and some configuration. There will be use cases when you want to display some more dynamic data or just some more HTML structures.

I'll try to explain it by example:

Let's say that we want to have an alert with the additional dynamic data. We want the name of the customer, and a confirmation button which will close the alert.

Here is what we could do.

In our main app component we will use sAlert component (see above) with a custom content component:

import MyCustomContentTemplate from './MyCustomContentTemplate';
(...)
<Alert contentTemplate={MyCustomContentTemplate} />
(...)

We have just told our sAlert component that we will use a custom content component instead the core one which is called SAlertContentTmpl. (You should copy the content of the SAlertContentTmpl in your custom one and add your own modifications to it).

For example our MyCustomContentTemplate component could look like:

import React from 'react';
import Alert from 'react-s-alert';

class MyCustomContentTemplate extends React.Component {
    constructor(props) {
        super(props);
    }
    handleConfirm() {
        console.log('Customer confirmation!');
        Alert.close(this.props.id);
    }
    render() {
        return (
            <div className={this.props.classNames} id={this.props.id} style={this.props.styles}>
                <div className='s-alert-box-inner'>
                    {this.props.message}

                    {/* use this api to customize alert style */}
                    {this.props.condition}
                </div>
                <h3>{this.props.customFields.customerName}</h3>
                <button onClick={this.handleConfirm.bind(this)}>Confirm</button>
                <span className='s-alert-close' onClick={this.props.handleClose}></span>
            </div>
        )
    }
}

export default MyCustomContentTemplate;

Then you just need to pass the customerName value somewhere in your app. For example:

Alert.warning('Customer confirmation needed.', {
    customFields: {
        customerName: 'Stefan Kowalski'
    }
});

As you can see you should keep the other props here. These are the props which are needed to provide proper behaviour of your alerts. You need to be careful with custom content components.

Testing and Development

Clone and install it first:

git clone https://github.com/juliancwirko/react-s-alert.git
cd react-s-alert
npm install

If you want to test (Node.js 4.0 or newer):

npm test

or

npm run testonly

If you want to transpile from ES2015 to ES5:

npm run prepublish

Also check out

License

MIT

More Repositories

1

react-npm-boilerplate

Boilerplate for creating React Npm packages with ES2015
JavaScript
235
star
2

s-chat-app

SimpleChat.Support - Open Source Live Chat App
HTML
231
star
3

generator-zf5

Yeoman generator for Zurb Foundation 5
CSS
204
star
4

meteor-s-alert

Simple and fancy notifications for Meteor
JavaScript
190
star
5

abc

Free theme for your Ghost blog
SCSS
172
star
6

scotty

Meteor React Redux boilerplate with Server-Side Rendering
JavaScript
151
star
7

react-boilerplate

React Boilerplate
JavaScript
125
star
8

nft-art-maker

NFT Art Maker - generates images and metadata files, packs them into IPFS CAR files, and uploads them using nft.storage. All from provided PNG layers.
TypeScript
98
star
9

react-redux-webpack-meteor

React demo app with Webpack, Redux and Meteor as a backend only
JavaScript
89
star
10

s-grid

Flexbox grid system for Stylus
CSS
64
star
11

react-s-alert-demo

Demo website for react-s-alert component
JavaScript
38
star
12

horizon-react-webpack-boilerplate

Horizon, React, Webpack boilerplate with demo app
JavaScript
35
star
13

create-harold-app

Static blog/site generator
JavaScript
35
star
14

meteor-s-grid

Stylus Flexbox grid system for Meteor with PostCSS and Autoprefixer included
JavaScript
28
star
15

s-grid-main-website

stylusgrid.com - source code
HTML
24
star
16

meteor-s-id

Simple accounts for Meteor.
JavaScript
19
star
17

meteor-bootstrap-postcss-test

Demo Meteor project which uses Bootstrap 4, Scss, PostCSS, Npm
JavaScript
15
star
18

elven.js

Authenticate, sign and send transactions on the MultiverseX (previously Elrond) blockchain in the browser. No need for bundlers, frameworks, etc. Just import from the static script source, and you are ready to go. One static file to rule it all on the MultiversX blockchain!
TypeScript
13
star
19

generator-jeet

Yeoman generator for Jeet Grid System
JavaScript
13
star
20

elrond-nft-minting-platform-poc

Elrond NFT minting platform POC (Also check out: www.elven.tools)
TypeScript
12
star
21

elven-mint

Script for automating minting NFTs on the Elrond blockchain (Also check out: www.elven.tools)
TypeScript
11
star
22

moralis-playground

Moralis playground - Moralis is a service that helps to build serverless web3 dApps.
JavaScript
10
star
23

meteor-zf5

Foundation 5 for Meteor
CSS
10
star
24

caprica-demo

Caprica - Meteor and Arduino - demo app
JavaScript
9
star
25

meteor-logo-website

Download customized Meteor logo
HTML
9
star
26

meteor-pretty-diff

Prettify and export your raw git diff output
JavaScript
8
star
27

meteor-npm-foundation-test

Foundation 6 from Npm with Meteor
CSS
8
star
28

generator-ziom

Angular + Bootstrap + Scss (Libsass) simple and custom Yeoman generator
JavaScript
7
star
29

meteor-bootstrap-npm-test

Bootstrap 4 from Npm with Meteor
JavaScript
7
star
30

meteor-s-jeet

Jeet (Stylus) grid system for Meteor. Especially for Scotty boilerplate.
JavaScript
6
star
31

meteor-css-modules-test

CSS Modules in Meteor.
JavaScript
6
star
32

meteor-schat-client-blaze

sChat client app for Meteor - Blaze view layer
CSS
6
star
33

meteor-pretty-diff-app

Working demo app for Meteor pretty-diff package
HTML
6
star
34

cra-template-elrond-dapp

Custom Elrond Dapp template for Create React App
TypeScript
5
star
35

smooth-scroll-top

Smooth scroll to top of the page
TypeScript
4
star
36

meteor-caprica

Simple admin template for juliancwirko:arduino-cylon package
JavaScript
4
star
37

meteor-s-image-box

Simple image popup/lightbox for Meteor
JavaScript
4
star
38

elvenjs-website

Website and docs for elven.js Elrond SDK
SCSS
3
star
39

meteor-arduino-cylon

Meteor and Cylon.js for Arduino
JavaScript
3
star
40

meteor-postcss-test

Test project with Meteor PostCSS package
HTML
3
star
41

cssnext-grunt-boilerplate

Grunt project starter with cssnext, Assemble and BrowserSync
CSS
3
star
42

live-chat-app-demo-html-client

Live Chat App - Static HTML Client Demo (blog post purposes)
JavaScript
3
star
43

meteor-schat-client-core

sChat client app for Meteor - core package
JavaScript
3
star
44

s-grid-grunt

Flexbox grid system - project scaffold with Stylus, Assemble and many useful Grunt tasks
HTML
2
star
45

meteor-s-id-website

Homepage for meteor-s-id
CSS
2
star
46

meteor-s-alert-website

Homepage for meteor-s-alert
CSS
2
star
47

relecheck

Track your favourite app updates on GitHub
2
star
48

juliancwirko

GitHub Profile
2
star
49

elrond-simple-sc-frontend-app

Frontend app for PiggyBank Smart Contract
TypeScript
2
star
50

meteor-s-alert-flip

Flip effect for s-alert - simple and fancy notifications for Meteor
CSS
2
star
51

multiversx-donate-widget-demo

MultiversX donate widget demo use case with elven.js
JavaScript
2
star
52

meteor-zf5-demo

Foundation 5 for Meteor - package's demo website
HTML
2
star
53

meteor-s-alert-genie

Genie effect for s-alert - simple and fancy notifications for Meteor
CSS
1
star
54

react-redux-rtk-example

TypeScript
1
star
55

gatsby-starter-netlify-test

JavaScript
1
star
56

s-alert-react-demo

Simple demo for sAlert Meteor package with React
JavaScript
1
star
57

vite-erdjs-test

Just playing around
TypeScript
1
star
58

arduino-temperature-logger

Arduino + Johnny Five temperature logger JavaScript app
JavaScript
1
star
59

cli-tool-starter

Starter for CLI tools written in Node
JavaScript
1
star
60

harold-website

create-harold-app website and documentation
SCSS
1
star
61

harold-template-scaffold

Scaffold for Harold custom templates
Handlebars
1
star
62

live-chat-app-demo-meteor-client

Live Chat App - Meteor Client Demo (blog post purposes)
JavaScript
1
star
63

harold-template-default

Default template for create-harold-app
Handlebars
1
star
64

harold-template-docs

Docs template for create-harold-app
SCSS
1
star
65

multiversx-node-hw-provider-poc

TypeScript
1
star
66

live-chat-app-demo-host

Live Chat App - Host Demo (blog post purposes)
JavaScript
1
star
67

meteor-s-alert-stackslide

Stackslide effect for s-alert - simple and fancy notifications for Meteor
CSS
1
star
68

chakra-typescript-problem

reproduction repository
TypeScript
1
star
69

harold-template-bare

Bare template for create-harold-app
Handlebars
1
star
70

harold-scripts

Harold Scripts for Create Harold App
JavaScript
1
star
71

harold-docs-template-demo

Harold docs template demo
SCSS
1
star