npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-bootstrap-hooks-alert

v2.0.3

Published

Simple alerts/notifications built with react+react-bootstrap

Downloads

68

Readme

react-bootstrap-hooks-alert

Using React Bootstrap (Alerts) and React Hooks I built a simple way to generate, well, Alerts!

(Personally I think they are more like notifications than alerts, but maybe that's just me 🤔 )

Screenshot

Screenshot

Demo

A very basic demo: https://tofsjonas.github.io/react-bootstrap-hooks-alert-demo/

Installation

react-bootstrap@2 is in beta! (at the time I am writing this) Bootstrap 5! 🥳

[email protected]

yarn add react-bootstrap@next react-bootstrap-hooks-alert

(Once react-bootstrap leaves dev, "@next" will need to be replaced with something else...)

[email protected]

yarn add [email protected] [email protected]

How to use

import React, { useEffect } from 'react'
import { AlertProvider, AlertOutlet, useAlert } from 'react-bootstrap-hooks-alert'
import 'react-bootstrap-hooks-alert/dist/Alert.css'

const App = () => {
  return (
    <AlertProvider timeouts={{ warning: 2000, success: 1000 }}>
      <AlertOutlet />
      <DemoDiv />
    </AlertProvider>
  )
}

const DemoDiv = () => {
  const { warning, info } = useAlert()
  useEffect(() => {
    warning('This is a warning!')
  }, [warning])
  const infoClick = () => {
    info('This is information!')
  }
  return <div onClick={infoClick}>Demo</div>
}

export default App

AlertProvider

AlertProvider should be somewhere top-level, so the alerts don't go away if you change page.

By passing timeouts to AlertProvider you can set how long before they disappear by themselves.

<AlertProvider timeouts={{ info: 2000, dark: 3000, [etc...] }}>
...

By default there are no timeouts, so unless you pass them as a prop here or in the function calls as a parameter, the alerts will stay until the user clicks them away.

AlertOutlet

You can put AlertOutlet anywhere you like, so long as it's "inside" AlertProvider.

You can even have more than one of them if it tickles your fancy. Using className and styling they can appear different on different pages.

  <AlertOutlet className="do-be-do" />
  <AlertOutlet className="hubba-bubba" />

useAlert functions

The available useAlert functions are named after the Bootstrap Alert variants:

  • primary
  • secondary
  • success
  • danger
  • warning
  • info
  • light
  • dark
const { success, danger, [info, dark, etc...] } = useAlert()

If you for some reason wish to override the defaults, or lack thereof, you can set timeouts on this level as well.

success('Still alive!', { timeout: 2000 }) // two seconds
info('The earth is round!', { timeout: 1500 }) // 1.5 seconds
warning('Fear the reaper!', { timeout: null }) // no timeout

Customizing

The Alerts will follow the styling from react-bootstrap, so if you make global changes there, they will be reflected here.

CSS

The CSS in Alert.css is just to make the container fixed, and add some animation.

If you wish to alter it, the simplest way would be to not import 'react-bootstrap-hooks-alert/dist/Alert.css' and instead use your own css.

You can find the SCSS in the github package.

Custom message

If you wish to pass something other than plain text as a message, go right ahead. This would work fine, for instance:

import Alert from 'react-bootstrap/Alert'
...
...
success(
  <>
  <Alert.Heading>Hey, nice to see you</Alert.Heading>
  <p>
    Aww yeah, you successfully read this important alert message. This example
    text is going to run a bit longer so that you can see how spacing within an
    alert works with this kind of content.
  </p>
  <hr />
  <p className="mb-0">
    Whenever you need to, be sure to use margin utilities to keep things nice
    and tidy.
  </p>
  </>
  )

Bootstrap's CSS

You will need Bootstrap's CSS, but if you are using react-bootstrap, you most likely already have it. But just in case you don't, here's an easy way to get it.

Copy the <link /> below and paste it inside the <head>-tag of your public/index.html:

[email protected] stylesheet

<!-- COPY FROM HERE -->
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
  integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We"
  crossorigin="anonymous" />
<!-- TO HERE -->

[email protected] stylesheet

<!-- COPY FROM HERE -->
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
  integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l"
  crossorigin="anonymous" />
<!-- TO HERE -->

public/index.html

<head>
  <meta charset="utf-8" />
  <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
  ...
    <!-- PASTE IT SOMEWHERE HERE -->
  ...
  <title>React App</title>
</head>