Coder Social home page Coder Social logo

react-alerts-plus's Introduction

WIP

react-alerts-plus

Demo

Shows an alert in the position and for the duration specified. You will most likely want to pass your own custom alert component to use with the library, see below.

This library was influenced by schiehlls react-alert library. I liked what he had done but the project seems to no longer be supported, and I wanted something just a little different. Feel free to check out his library, maybe it will work better for your needs. Otherwise keep reading...

Installation:

yarn add react-alerts-plus

or

npm install react-alerts-plus

Usage: Wrap your app in the provider:

import { AlertProvider } from 'react-alerts-plus';

class MyApp extends React.Component {
  render() {
    return (
      <AlertProvider>
        <App />
      </AlertProvider>
    );
  }
}

In your app where you would like to show alerts

import { AlertWrapper } from 'react-alerts-plus';
...
...
render() {
  const options = {
        message: 'My alert message',
        style: {
          backgroundColor: 'cornflowerblue',
          borderRadius: 0,
        },
        offset: '50px',
        position: 'top right',
        duration: 0,
      }
  }

  return (
    <div>
      <button>
        <AlertWrapper>
          {({show, close}) => {
            <button type="button" onClick={() => const alertId = show(options)}>Show Alert</button>
          }}
          <button type="button" onClick={() => close(alertId)}>
        </AlertWrapper>
    </div>
  )
}

The AlertWrapper returns a show and close function.

show(options, AlertComponent): The show function will display the alert using the options object passed. Returns the ID of the alert.

close(alertId): The close function will close the alert with the corresponding ID.

Alert Options:

All three methods of using react-alerts-plus accept the same options, though some maybe ignored with different alerts.

Options are passed as an object, as the first argument to the show function. While z-index is not an option, you can pass a custom z-index in your styles and it will be applied. See below.

Option Type Description
message String / Number message displayed in the alert
id String / Number id for the alert
style Object defining javascript styles
offset String defining the offset of the alert from position
duration Number time in milliseconds for the alert to be shown
position String placement of the alert
theme String default alert theme colors
showProgessBar Bool show auto close progress bar
progressBarColor String progress bar color
AlertComponent PureComponent pure components recommended

Option Examples:

  message:          'Hi alert here!'
  id:               'my-alert'
  style:            style: {
                      backgroundColor: 'cornflowerblue',
                      borderRadius: 0,
                    }
  offset:           '50px'
  duration:         2000 (use 0 to never auto close the alert)
  position:         'top left'
                    'top center'
                    'top right'
                    'bottom left'
                    'bottom center'
                    'bottom right'
  theme:            'light' or 'dark' (light is default)
  showProgressBar   false
  progressBarColor '#666', 'cornflowerblue', 'red'
  AlertComponent:   Default alert will be totally replaced by your custom alert.
                    Only offset, duration, id, showProgressBar, progressBarColor,
                    and position are used when passing a custom AlertComponent.
                    See below for specifics about using
                    your own custom alert component. **While custom alert
                    components can be functional stateless components, Components,
                    we recommend using PureComponents.**

  const optionsExample = {
        message: 'Hi alert here!',
        id: 'my-alert',
        style: {
          backgroundColor: 'cornflowerblue',
          borderRadius: 0,
          zIndex: 1000,
        },
        offset: '50px',
        duration: 2000,
        position: 'top right',
        theme: 'dark',
        progressBarColor: 'cornflowerblue',
      }

react-alerts-plus provides three different types of alerts.

  • Default alerts
  • Base alert components
  • Custom alerts

Default Alerts

Default alerts are nothing fancy, just basic alerts that display using the options you pass to the show function. (See AlertWrapper example above).

Base Alert Components

To use the base alert components you still need to wrap your App in our AlertProvider and AlertWrapper, but you pass our CardAlert component as the second AlertComponent arguement to show. CardAlert will return any of the base alert components you wish to use.

  • AlertContainer
  • AlertHeader
  • AlertBody
  • AlertImage
  • AlertProgressBar

As long as you pass props to this function you can make use of react-alerts-plus transitions (transitionStyle) and close function (see below).

AlertContainer

A container to wrap your other alert components.

Special props:

  • N/A.
  • All your props are spread across this component for you to style your alert.

AlertHeader

Alert header.

Special props:

  • title: String. Alert title string
  • All your props are spread across this component for you to style your alert.

AlertBody

The body of your alert.

Special props:

  • message: String. Alert message string
  • All your props are spread across this component for you to style your alert.

AlertImage

Displays an image for your Alert.

Special props:

  • height: String. Height of image (ex: '200px')
  • width: String. Width of image (ex: '200px')
  • imageSrc: String. URL to image
  • alt: String. alt attribute for image
  • All your props are spread across this component for you to style your alert.

AlertProgress

A propress bar showing how long the alert will remain open before the duration runs out. Will not be shown if:

  • duration option is 0 (zero), never auto close
  • showProgresBar option is false

Special props: These props can be pulled from the props passed into the CardAlert (the options you sent to show, see below) or passed manually.

  • progressBarColor,
  • alertTimeout,
  • showProgressBar,
  • state,
/** @jsx jsx */
import React from 'react';
import { Icon } from 'react-icons-kit';
import { ic_close as closeIcon } from 'react-icons-kit/md/ic_close';
import { jsx, css } from '@emotion/core';
import { AlertProvider, AlertWrapper, CardAlert } from 'react-alerts-plus';
...
...
render() {
  const options = {
    message: 'My alert message',
    position: 'top left',
    offset: '20px',
    id: 'my-bottom-center-alert',
    duration: 2000,
    progressBarColor: 'linear-gradient(to right, yellow, orange, red)',
  }


  return (
    <div>
      <button>
        <AlertWrapper>
          {({show, close}) => (
            <button
              type="button"
              onClick={() =>
                show(options,
                  props => (
                    <CardAlert
                      render={({
                        AlertContainer,
                        AlertHeader,
                        AlertBody,
                        AlertImage,
                        AlertProgressBar,
                      }) => {
                        const {
                          transitionStyle,
                          close: cardAlertClose,
                        } = props;
                        return (
                          <AlertContainer
                            css={css`
                              display: grid;
                              grid-gap: 10px;
                              grid-template-rows: 40px 1fr 10px;
                              border: 1px solid lavenderblush;
                              justify-content: center;
                              padding: 20px;
                              width: 400px;
                              margin: 15px;
                              background-color: cadetblue;
                              box-shadow: 1px 1px 8px 1px #666;
                            `}
                            style={{ ...transitionStyle }}
                          >
                            <div
                              css={css`
                                display: grid;
                                grid-gap: 20px;
                                grid-template-columns: 1fr 20px;
                              `}
                            >
                              <AlertHeader
                                title="this is a title"
                                style={{ fontSize: '24pt' }}
                              />
                              <Icon
                                size={20}
                                icon={closeIcon}
                                onClick={cardAlertClose}
                              />
                            </div>
                            <div
                              css={css`
                                display: grid;
                                grid-gap: 15px;
                                grid-template-columns: 200px 1fr;
                              `}
                            >
                              <AlertImage
                                height={200}
                                width={200}
                                imageSrc={imageUri}
                                alt="My Alert Image"
                              />
                              <AlertBody message="this is a message to body" />
                            </div>
                            <AlertProgressBar {...props} />
                          </AlertContainer>
                        );
                      }}
                    />
                  ),
                )
              }
            >
              CardAlert Bottom Center
            </button>
          )}
        </AlertWrapper>
    </div>
  )
}

Custom Alert Component:

While custom alert components can be a functional stateless component, Reac.Component, etc. we recommend using React.PureComponents.

Passing a custom alert component will cause some options to be ignored (see Option Examples above). The close function will be added as a prop for you to consume in your custom alert component. If you are going to use the close prop you will need to supply the custom ID with your options. You can also pass any other props you might need in your custom alert component. See the codesandbox demo link at the top of this README.

/* eslint-disable import/no-extraneous-dependencies */
/** @jsx jsx */
import React from 'react';
import PropTypes from 'prop-types';
import { jsx, css } from '@emotion/core';
import { Icon } from 'react-icons-kit';
import { ic_close as closeIcon } from 'react-icons-kit/md/ic_close';

class MyAlert extends React.PureComponent {
  render() {
    const {
      close,
      title,
      message,
      imageUri,
      transitionStyle,
      showProgressBar,
      progressBarColor,
      alertTimeout,
      state,
    } = this.props;

    // console.log('rest = ', props);
    const progressStyle = {
      transition: `width ${alertTimeout}ms ease-in-out`,
      width: '0px',
    };

    const progressTransitionStyles = {
      entering: { width: '0px' },
      entered: { width: '100%' },
    };
    return (
      <div
        key="someRandomKey"
        css={css`
          display: grid;
          grid-gap: 10px;
          grid-template-rows: 40px 1fr 10px;
          border: 1px solid lavenderblush;
          justify-content: center;
          padding: 20px;
          width: 400px;
          margin: 15px;
          background-color: cadetblue;
          box-shadow: 1px 1px 8px 1px #666;
        `}
        style={{
          ...transitionStyle,
        }}
      >
        <div
          css={css`
            display: grid;
            grid-gap: 20px;
            grid-template-columns: 1fr 20px;
          `}
        >
          <header
            css={css`
              font-size: 18pt;
              color: white;
            `}
          >
            {title}
          </header>
          <div>
            <Icon size={20} icon={closeIcon} onClick={close} />
          </div>
        </div>
        <div
          css={css`
            display: grid;
            grid-gap: 15px;
            grid-template-columns: 200px 1fr;
          `}
        >
          <img height="200" width="200" src={imageUri} alt="pic" />
          <article
            css={css`
              color: #141414;
            `}
          >
            {message}
          </article>
        </div>
        {alertTimeout === 0
          ? null
          : showProgressBar && (
              <div
                style={{
                  height: '10px',
                  backgroundColor: `${progressBarColor}`,
                  ...progressStyle,
                  ...progressTransitionStyles[state],
                }}
              />
            )}
      </div>
    );
  }
}

MyAlert.propTypes = {
  close: PropTypes.func,
  title: PropTypes.string,
  message: PropTypes.string,
  imageUri: PropTypes.string,
};

MyAlert.defaultProps = {
  close: () => {},
  title: 'Default Title',
  message: 'Default Message',
  imageUri: '',
};

export default MyAlert;

react-alerts-plus's People

Contributors

dependabot[bot] avatar renovate-bot avatar renovate[bot] avatar sbardian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-alerts-plus's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Found renovate config warnings

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

circleci
.circleci/config.yml
  • cypress/base 10.22.0
  • circleci/node 10.24
  • circleci/node 10.24
npm
package.json
  • prop-types ^15.6.2
  • react-icons-kit ^1.2.1
  • react-sizes ^2.0.0
  • react-transition-group ^4.0.0
  • @babel/cli 7.22.6
  • @babel/core 7.22.6
  • @babel/plugin-proposal-class-properties 7.18.6
  • @babel/plugin-proposal-export-default-from 7.22.5
  • @babel/plugin-syntax-object-rest-spread 7.8.3
  • @babel/preset-env 7.22.6
  • @babel/preset-react 7.22.5
  • @commitlint/cli 11.0.0
  • @commitlint/config-conventional 11.0.0
  • @emotion/core 10.3.1
  • @testing-library/cypress 7.0.7
  • @testing-library/jest-dom 5.17.0
  • @testing-library/react 11.2.7
  • babel-core 7.0.0-bridge.0
  • @babel/eslint-parser 7.22.6
  • babel-loader 8.3.0
  • commitizen 4.2.4
  • coveralls 3.1.1
  • cypress 6.9.1
  • cz-conventional-changelog 3.3.0
  • eslint 7.32.0
  • eslint-config-airbnb 18.2.1
  • eslint-config-prettier 7.2.0
  • eslint-plugin-cypress 2.15.1
  • eslint-plugin-import 2.29.0
  • eslint-plugin-jsx-a11y 6.8.0
  • eslint-plugin-prettier 3.4.1
  • eslint-plugin-react 7.33.2
  • html-webpack-plugin 4.5.2
  • husky 4.3.8
  • jest 26.6.3
  • lint-staged 10.5.4
  • prettier 2.8.8
  • react 17.0.2
  • react-dom 17.0.2
  • semantic-release 17.4.7
  • style-loader 2.0.0
  • url-loader 4.1.1
  • webpack 5.89.0
  • webpack-cli 3.3.12
  • webpack-dev-server 3.11.3
  • react ^16.6.3 || ^17.0.0
  • react-dom ^16.6.3 || ^17.0.0
  • node >=10.18.0

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.