Git Product home page Git Product logo

bulma-toast's Introduction

bulma-toast

npm version Publish

Bulma's pure JavaScript extension to display toasts. Basically a Bulma's notification implemented as a toast plugin.

Demo

Bulma Toast

Options

The plugin comes with the following options:

  • message: The actual message to be displayed. It can be a string, a template string, or a DOM node. See examples. This is required.
  • type: Essentially a Bulma's css class. It can be is-primary, is-link, is-info, is-success, is-warning, is-danger, or any other custom class. Default is a whitesmoke background with dark text as shown here.
  • duration: Duration of the notification in milliseconds. Default is 2000 milliseconds.
  • position: Position where the notification will be shown. The default is top-right, so if you want it to be on the top-left just add top-left to this option. The available options are: top-left, top-center, top-right, center, bottom-left, bottom-center, and bottom-right.
  • dismissible: Whether the notification will have a close button or not. Default is false.
  • pauseOnHover: Pauses delay when hovering the notification. Default is false.
  • single: Only show a single toast in appendTo. Default is false.
  • closeOnClick: Dismisses the notification when clicked. Default is true.
  • opacity: The notification's container opacity. Default is 1.
  • animate: See here. Default is no animations.
  • appendTo: By default, the notification will be appended to document.body. Pass a different Node to have it appended else where in the DOM.
  • extraClasses: Adds classes for styling the toast notification.

Install

  npm install bulma-toast

or

  yarn add bulma-toast

Quick Start

  1. Link to bulma-toast.min.js
<script src="bulma-toast.min.js"></script>
  1. Use bulma-toast to display a toast
bulmaToast.toast({ message: 'Hello There' })
bulmaToast.toast({ message: 'General Kenobi', type: 'is-danger' })

ES Modules

// Import the toast function
import * as bulmaToast from 'bulma-toast'
// Or use
// import { toast as superToast } from 'bulma-toast'
// to rename your import

toast({
  message: 'Hello There',
  type: 'is-success',
  dismissible: true,
  animate: { in: 'fadeIn', out: 'fadeOut' },
})

Default config

A simple default object to prevent errors. Your options will be merged with these and the defaults will be used if the fields are not provided.

{
  "duration": 2000,
  "position": "top-right",
  "closeOnClick": true,
  "opacity": 1,
  "single": false,
  "offsetTop": 0,
  "offsetBottom": 0,
  "offsetLeft": 0,
  "offsetRight": 0
}

The default config can be updated using the funcion setDefaults. Also, it's possible to reset to the default config using resetDefaults

bulmaToast.setDefaults({
  duration: 1000,
  position: 'top-left',
  closeOnClick: false,
})

Change document context

bulmaToast.setDoc(window.document)

This can be changed before each toast call and can be set to eny element.

Animate

Bulma Toast supports animate.css (and maybe others?). You MUST include animate.css on your document's <head>

<head>
  <link rel="stylesheet" href="animate.min.css" />
  <!-- or -->
  <link
    rel="stylesheet"
    href="https://cdn.jsdelivr.net/npm/[email protected]/animate.min.css"
  />
  <!-- or -->
  <link
    rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.0.0/animate.min.css"
  />
</head>

Accepts a object with in and out with css classes to add animations. Using Animate.css you would pass a object like this:

  {
    message: "I'm animated! Yay!",
    duration: 2000,
    position: "top-right",
    animate: { in: 'fadeIn', out: 'fadeOut' }
  }

Warning: Don't use opacity when using animations. Some of them use the opacity property like fade in and fade out.

Examples

import { toast } from 'bulma-toast'

toast({
  message: 'Hello There',
  type: 'is-success',
  dismissible: true,
  pauseOnHover: true,
})

toast({
  message: '<h1>LOOK HERE</h1>',
  type: 'is-danger',
  dismissible: true,
  pauseOnHover: true,
  animate: { in: 'fadeIn', out: 'fadeOut' },
})

const myMessage = `It's ${new Date().toDateString()}`

toast({
  message: myMessage,
  type: 'is-primary',
  position: 'center',
  closeOnClick: true,
  pauseOnHover: true,
  opacity: 0.8,
})

const elm = document.createElement('a')
elm.text = 'Visit my website!'
elm.href = 'https://rfoel.com'

toast({
  message: elm,
  type: 'is-warning',
  position: 'center',
  closeOnClick: true,
  pauseOnHover: true,
  animate: { in: 'fadeIn', out: 'fadeOut' },
})

Contributing

Issues and pull requests are welcome.

License

MIT

bulma-toast's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bulma-toast's Issues

Allow overriding position

Please allow passing the position value. The the current value anchors the initial positions to 0.

Toast opacity

Let opacity as an option, please!

toast({
  message: 'Your message here',
  duration: 2000,
  position: 'top-right',
  closeOnClick: true,
  opacity: 1
})

No way to style toasts

Is there any way to add classes or styling options to the created toasts?

I’m not able to find any.

add extraStyle option

Hello!
Can u add extraStyle option?
Append to end of existing styles on .notification div
That need for calculated styling on each notice

Thank you

Animation speed?

Is there a way to control animation speed, or where the animation ends? Animation opening is great but closing is a bit grainy and ends short on some, see here.

Bug - setDefaults and resetDefaults are not recognized as functions by typescript

With 2.2.0 bulma-toast introduced new functions called 'setDefaults' and 'resetDefaults'. While the functions are working properly, typescript cannot recognize the functions as they are missing from index.d.ts and are marking the usage of said functions as errors.

Upon request I am able to provide a simple repo which reproduces this issue.

Adding fade in / out as an option

Hi, excellent contribution. Perhaps you could add a fade in and fade out option? The vast majority of toasts I've seen around the web use the fade behavior.

creating a new instance of the toast class causes issues in SSR environments :(

Just upgraded to 1.0.0 from 0.1.0
The issue is that the default export in 1.0.0 is https://github.com/rfoel/bulma-toast/blob/master/src/js/index.js#L58 trying to access the document when bulma-toast is imported.

but in 0.1.0, just an function is exported: https://github.com/rfoel/bulma-toast/blob/0.1.0/src/extension.js#L7
so it's not evaluated until invocation time, allowing it to work in SSR environments. :-\

(for now, I'm downgrading back to 0.1.0 :-\ )

Ran in to this issue: ember-fastboot/ember-cli-fastboot#74 (for reference)

bulma-toast not supporting IE11

I have an angular project with bulma-toast configured.
When I try to start the project with ng serve in Internet Explorer 11, I have the following errors in the console:

SCRIPT1014: Invalid character
vendor.js (122891,459)

Which refer to the following:
screenshot

Does bulma-toast support IE?

Versions;
"bulma": "^0.7.2",
"bulma-toast": "^1.4.0"
Internet Explorer v11.523.17134.0

Can't set appendTo

I am not able to set appendTo within my code. I want to append the toast to the <section class="section">...</section> area.
However, this toast is overlapping the footer section of the page, which I want to prevent. I want to position the toast on the bottom-right corner of the <section class="section">...</section> area. It should not overlap with the footer area!

I tried to change the appendTo to things like document.body.section or document.section, but these are not working.

Am I doing something wrong or is there a bug?

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css">
  <script src="https://unpkg.com/bulma-toast"></script>
 
</head>
<body>

<nav class="navbar" role="navigation" aria-label="main navigation">
  <div class="navbar-brand">
    <a class="navbar-item" href="https://bulma.io">
      <img src="https://bulma.io/images/bulma-logo.png" width="112" height="28">
    </a>

    <a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
      <span aria-hidden="true"></span>
      <span aria-hidden="true"></span>
      <span aria-hidden="true"></span>
    </a>
  </div>

  <div id="navbarBasicExample" class="navbar-menu">
    <div class="navbar-start">
      <a class="navbar-item">
        Home
      </a>

      <a class="navbar-item">
        Documentation
      </a>

      <div class="navbar-item has-dropdown is-hoverable">
        <a class="navbar-link">
          More
        </a>

        <div class="navbar-dropdown">
          <a class="navbar-item">
            About
          </a>
          <a class="navbar-item">
            Jobs
          </a>
          <a class="navbar-item">
            Contact
          </a>
          <hr class="navbar-divider">
          <a class="navbar-item">
            Report an issue
          </a>
        </div>
      </div>
    </div>

    <div class="navbar-end">
      <div class="navbar-item">
        <div class="buttons">
          <a class="button is-primary">
            <strong>Sign up</strong>
          </a>
          <a class="button is-light">
            Log in
          </a>
        </div>
      </div>
    </div>
  </div>
</nav>


<section class="section">
  <h1 class="title">Lorem ipsum</h1>
  <h2 class="subtitle">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   

  </h2>

  <script>

bulmaToast.toast({
  message: 'Hello There' ,
  type: 'is-primary',
  duration: 100000,
  pauseOnHover: true,
  position: 'bottom-right',
  dismissible: true,
  appendTo: document.body.section
 })

</script>

</section>


<footer class="footer has-background-black">
  <div class="content has-text-centered">
    <p>
      <strong>Bulma</strong> by <a href="https://jgthms.com">Jeremy Thomas</a>. The source code is licensed
      <a href="http://opensource.org/licenses/mit-license.php">MIT</a>. The website content
      is licensed <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY NC SA 4.0</a>.
    </p>
  </div>
</footer>



 </body>
</html>

Toast is not automatically dismissed when "animate" is added

Normally -without "animate" option- toast is auto dismissed after 2 seconds, the default duration. But when I add "animate" option to my code, it is not working (I mean not animated) and it isn't automatically closed after the miliseconds that I gave in "duration" setting.

Old code is -which works normally-:

toast({
    message: "Hello There",
    type: "is-info",
    dismissible: true,
    position: "top-right"
});

New code is -which is not animated and not auto dismissed-:

toast({
    message: "Hello There",
    type: "is-info",
    dismissible: true,
    duration: 1000,
    position: "top-right",
    animate: { in: 'fadeIn', out: 'fadeOut' }
});

Feature requests: programmatic close, open & close events

Thanks for the useful project!

It would have a much wider applications if it includes the following options:

  • programmatic way to dismiss the notification
  • events/callbacks when various operations take place (notification opens/shows/closes).

Ability set a DOM node for the message

E.g.

setTimeout(() => {

  const elm = document.createElement('a')
  elm.text = "Click This"
  elm.href = "http://url.example.com"

  toast({
    message: elm,
    type: "is-info",
    dismissible: true,
    duration: 20000
  });
}, 2000);

It could be something like:

// UNTESTED

if (typeof this.message === 'string') {
  this.element.insertAdjacentText("beforeend", this.message);
} else {
  this.element.appendChild(this.message);
}

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot 📦🚀

SyntaxError: Unexpected token '<' in console?

Really enjoying bulma-toast and have it working with svelte, bulma, and animate.css quite well. Super nice and easy.

I do see 'SyntaxError: Unexpected token '<'' in the console pointing to 'anonymous function' at bulma-toast-min-js-1. Have you see this before?

PR #202 not merged properly

I don’t think #202 was merged properly. The changes to src/index.js for examples are not to be found. I think you created 188058a to squash my three commits, and meant to apply 4f08fdf on top of that, but 188058a is nowhere on main.

Container resets after dismissing a toast even if still containing other toasts

Steps to reproduce:

  1. Create a few (four) toasts with semi-long timer (10seconds) in the same position using either dismissible or closeOnClick set to true
  2. Dismiss any one of the toasts using either the "x" button or by clicking it (based on options given in step 1)
  3. Create a few (one-two) more toasts
  4. The new toasts now cover up the old toasts
    Expected behaivor: The new toasts would be added to the end of the list and not cover up other toasts

jsfiddle with example: https://jsfiddle.net/Aviv_Galmidi/6218y3t9/6/

create-react-app Test fail: this.element.insertAdjacentElement is not a function

"bulma-toast": "^1.5.4"
There is no error during normal using, only the Jest test will report the error.

image

import { toast } from 'bulma-toast'
export const error = (message: string, lasting?: boolean): void => {
  toast({
    message,
    type: 'is-danger',
    position: 'top-center',
    dismissible: true,
    duration: lasting? 4000*10: 4000,
    pauseOnHover: true,
    // animate: { in: 'fadeIn', out: 'fadeOut' },
  })
}

Jest tset code very simple:

import React from 'react';
import { render } from '@testing-library/react';
import App from './App';

test('renders Source Code', () => {
  const { getByText } = render(<App />);
  const linkElement = getByText(/Source Code/i);
  expect(linkElement).toBeInTheDocument();
});

Option to set defaults

It's a bit annoying to have to pass in the same options into every .toast() call. Would it be possible to set the defaults with something such as:

bulmaToast.setDefaults({
    ...
})

"Document is not defined" every time I refresh in the same page

image

I'm just wondering why I'm getting this every after I refresh at the same page that I'm working on.
Or, when nuxt.js hot-reloads the page whenever I change some code.

This is my current <script> in my page:

<script>
import { toast } from 'bulma-toast'

export default {
  methods: {
  async onUpload() {
    try {
      // ... send to axios and save to browser

      toast({
        message: 'Success! Your file is now saved.',
        position: 'top-center',
        type: 'is-success',
        dismissible: true,
        pauseOnHover: true,
        animate: { in: 'bounceInDown', out: 'bounceOutUp' }
      })

      console.log(response)
    } catch (e) {
      console.error(e)
      // TODO: create a variable for this "toast.message."
      toast({
        message: 'Something went wrong...',
        position: 'top-center',
        type: 'is-danger',
        dismissible: true,
        pauseOnHover: true,
        animate: { in: 'bounceInDown', out: 'bounceOutUp' }
      })
    } finally {
      this.isLoading = false
    }
}
</script>

Implementation in Angular

Hello,

is their a way to use this module in angular? I managed to set it up and the toasts appear, but they do not disappear after the time or when clicked.

Thanks in advance

Demo link no longer works

As the title states, the demo site doesn't seem to be active anymore. Any chance it can be updated?

Close button [objectHTMLButtonElement]

Close button not showing.

    bulmaToast.toast({
        message: "Seja bem vindo a Rádio Som do Mato!",
        type: "is-link",
        position: "bottom-left",
        dismissible: true,
        duration: 10000
    });

Captura de tela_2019-11-07_14-32-58

No SSR support

I used "bulma-toast" on a gatsby project where the build would fail with WebpackError: ReferenceError: document is not defined . Maybe add some some if(window.document...) for the plugin. Had to remove it, but was working great! Good work.

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.