All Projects → poteto → Ember Cli Flash

poteto / Ember Cli Flash

Licence: mit
Simple, highly configurable flash messages for ember-cli

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Ember Cli Flash

ember-gridstack
Ember components to build drag-and-drop multi-column grids powered by gridstack.js
Stars: ✭ 31 (-91.34%)
Mutual labels:  ember, ember-addon
Ember Metrics
Send data to multiple analytics integrations without re-implementing new API
Stars: ✭ 356 (-0.56%)
Mutual labels:  ember, ember-addon
ember-credit-card
"make your credit card form dreamy in one line of code"
Stars: ✭ 89 (-75.14%)
Mutual labels:  ember, ember-addon
ember-headlessui
gavinjoyce.github.io/ember-headlessui/
Stars: ✭ 76 (-78.77%)
Mutual labels:  ember, ember-addon
Ember Simple Auth Token
Ember Simple Auth extension that is compatible with token-based authentication like JWT.
Stars: ✭ 356 (-0.56%)
Mutual labels:  ember, ember-addon
Ember Burger Menu
An off-canvas sidebar component with a collection of animations and styles using CSS transitions
Stars: ✭ 280 (-21.79%)
Mutual labels:  ember, ember-addon
ember-stripe-elements
A simple Ember wrapper for Stripe Elements.
Stars: ✭ 64 (-82.12%)
Mutual labels:  ember, ember-addon
ember-changeset-conditional-validations
Conditional validations for ember-changeset-validations
Stars: ✭ 26 (-92.74%)
Mutual labels:  ember, ember-addon
ember-key-manager
A service for (un)binding keyboard up and down events.
Stars: ✭ 39 (-89.11%)
Mutual labels:  ember, ember-addon
ember-deep-tracked
Deep auto-tracking for when you just don't care, and want things to work (at the cost of performance in some situtations)
Stars: ✭ 20 (-94.41%)
Mutual labels:  ember, ember-addon
ember-render-helpers
Complimentary render template helpers to the render modifiers
Stars: ✭ 19 (-94.69%)
Mutual labels:  ember, ember-addon
Ember Apollo Client
🚀 An ember-cli addon for Apollo Client and GraphQL
Stars: ✭ 257 (-28.21%)
Mutual labels:  ember, ember-addon
glimmer-apollo
Ember and Glimmer integration for Apollo Client.
Stars: ✭ 32 (-91.06%)
Mutual labels:  ember, ember-addon
ember-local-storage-decorator
Decorator for Ember.js to read and persist data in localStorage
Stars: ✭ 13 (-96.37%)
Mutual labels:  ember, ember-addon
ember-simple-auth-token
Ember Simple Auth extension that is compatible with token-based authentication like JWT.
Stars: ✭ 356 (-0.56%)
Mutual labels:  ember, ember-addon
ember-rapid-forms
Smart, Intuitive forms for Ember.js styled with Bootstrap, Multi layouts and Validation support.
Stars: ✭ 58 (-83.8%)
Mutual labels:  ember, ember-addon
els-addon-typed-templates
Ember templates linting / autocomplete, based on Typescript language server
Stars: ✭ 27 (-92.46%)
Mutual labels:  ember, ember-addon
ember-luxon
🕐 🌐 [deprecated] Addon thats brings Luxon to Ember applications.
Stars: ✭ 20 (-94.41%)
Mutual labels:  ember, ember-addon
ember-new-relic
Adds New Relic to your Ember CLI app based on the app's environment
Stars: ✭ 29 (-91.9%)
Mutual labels:  ember, ember-addon
Ember Websockets
Ember.js websockets and socket.io addon
Stars: ✭ 336 (-6.15%)
Mutual labels:  ember, ember-addon

ember-cli-flash

Download count all time npm version CircleCI Ember Observer Score Code Climate

Simple, highly configurable flash messages for ember.

This ember addon adds a flash message service and component to your app.

Table of Contents

Installation

ember install ember-cli-flash

Compatibility

This addon is tested against the release, beta and canary channels, ~1.11.0, and 1.12.1. Because this addon makes use of attribute bindings, which were introduced in ember 1.11.0, earlier versions of ember are not compatible with the latest version.

Usage

Usage is very simple. First, add one of the template examples to your app. Then, inject the flashMessages service and use one of its convenience methods:

import Component from '@ember/component';
import { inject } from '@ember/service';

export default Component.extend({
  flashMessages: inject()
});

Convenience methods (Bootstrap / Foundation alerts)

You can quickly add flash messages using these methods from the service:

Bootstrap

  • .success
  • .warning
  • .info
  • .danger

Foundation

  • .success
  • .warning
  • .info
  • .alert
  • .secondary

These will add the appropriate classes to the flash message component for styling in Bootstrap or Foundation. For example:

// Bootstrap: the flash message component will have 'alert alert-success' classes
// Foundation: the flash message component will have 'alert-box success' classes
this.flashMessages.success('Success!');

You can take advantage of Promises, and their .then and .catch methods. To add a flash message after saving a model (or when it fails):

actions: {
  saveFoo() {
    const flashMessages = this.flashMessages;

    this.model
      .save()
      .then((res) => {
        flashMessages.success('Successfully saved!');
        doSomething(res);
      })
      .catch((err) => {
        flashMessages.danger('Something went wrong!');
        handleError(err);
      });
  }
}

Custom messages

If the convenience methods don't fit your needs, you can add custom messages with add:

this.flashMessages.add({
  message: 'Custom message'
});

Custom messages API

You can also pass in options to custom messages:

this.flashMessages.add({
  message: 'I like alpacas',
  type: 'alpaca',
  timeout: 500,
  priority: 200,
  sticky: true,
  showProgress: true,
  extendedTimeout: 500,
  destroyOnClick: false,
  onDestroy() {
    // behavior triggered when flash is destroyed
  }
});

this.flashMessages.success('This is amazing', {
  timeout: 100,
  priority: 100,
  sticky: false,
  showProgress: true
});
  • message: string

    Required when preventDuplicates is enabled. The message that the flash message displays.

  • type?: string

    Default: info

    This is mainly used for styling. The flash message's type is set as a class name on the rendered component, together with a prefix. The rendered class name depends on the message type that was passed into the component.

  • timeout?: number

    Default: 3000

    Number of milliseconds before a flash message is automatically removed.

  • priority?: number

    Default: 100

    Higher priority messages appear before low priority messages. The best practise is to use priority values in multiples of 100 (100 being the lowest priority). Note that you will need modify your template for this work.

  • sticky?: boolean

    Default: false

    By default, flash messages disappear after a certain amount of time. To disable this and make flash messages permanent (they can still be dismissed by click), set sticky to true.

  • showProgress?: boolean

    Default: false

    To show a progress bar in the flash message, set this to true.

  • extendedTimeout?: number

    Default: 0

    Number of milliseconds before a flash message is removed to add the class 'exiting' to the element. This can be used to animate the removal of messages with a transition.

  • destroyOnClick?: boolean

    Default: true

    By default, flash messages will be destroyed on click. Disabling this can be useful if the message supports user interaction.

  • onDestroy: function

    Default: undefined

    A function to be called when the flash message is destroyed.

Animated example

To animate messages, set extendedTimeout to something higher than zero. Here we've chosen 500ms.

module.exports = function(environment) {
  var ENV = {
    flashMessageDefaults: {
      extendedTimeout: 500
    }
  }
}

Then animate using CSS transitions, using the .active and .active.exiting classes.

.alert {
  opacity: 0;
  position: relative;
  left: 100px;

  transition: all 700ms cubic-bezier(0.68, -0.55, 0.265, 1.55);

  &.active {
    opacity: 1;
    left: 0px;

    &.exiting {
      opacity: 0;
      left: 100px;
    }
  }
}

Arbitrary options

You can also add arbitrary options to messages:

this.flashMessages.success('Cool story bro', {
  someOption: 'hello'
});

this.flashMessages.add({
  message: 'hello',
  type: 'foo',
  componentName: 'some-component',
  content: customContent
});

Example use case

This makes use of the component helper, allowing the template that ultimately renders the flash to be dynamic:

{{#each flashMessages.queue as |flash|}}
  <FlashMessage @flash={{flash}} as |component flash|>
    {{#if flash.componentName}}
      {{component flash.componentName content=flash.content}}
    {{else}}
      <h6>{{component.flashType}}</h6>
      <p>{{flash.message}}</p>
    {{/if}}
  </FlashMessage>
{{/each}}

Clearing all messages on screen

It's best practice to use flash messages sparingly, only when you need to notify the user of something. If you're sending too many messages, and need a way for your users to clear all messages from screen, you can use this method:

this.flashMessages.clearMessages();

Returning flash object

The flash message service is designed to be Fluent, allowing you to chain methods on the service easily. The service should handle most cases but if you want to access the flash object directly, you can use the getFlashObject method:

const flashObject = this.flashMessages.add({
  message: 'hola',
  type: 'foo'
}).getFlashObject();

You can then manipulate the flashObject directly. Note that getFlashObject must be the last method in your chain as it returns the flash object directly.

Service defaults

In config/environment.js, you can override service defaults in the flashMessageDefaults object:

module.exports = function(environment) {
  var ENV = {
    flashMessageDefaults: {
      // flash message defaults
      timeout: 5000,
      extendedTimeout: 0,
      priority: 200,
      sticky: true,
      showProgress: true,

      // service defaults
      type: 'alpaca',
      types: [ 'alpaca', 'notice', 'foobar' ],
      preventDuplicates: false
    }
  }
}

See the options section for information about flash message specific options.

  • type?: string

    Default: info

    When adding a custom message with add, if no type is specified, this default is used.

  • types?: array

    Default: [ 'success', 'info', 'warning', 'danger', 'alert', 'secondary' ]

    This option lets you specify exactly what types you need, which means in the above example, you can do this.flashMessages.{alpaca,notice,foobar}.

  • preventDuplicates?: boolean

    Default: false

    If true, only 1 instance of a flash message (based on its message) can be added at a time. For example, adding two flash messages with the message "Great success!" would only add the first instance into the queue, and the second is ignored.

Displaying flash messages

Then, to display somewhere in your app, add this to your template:

{{#each flashMessages.queue as |flash|}}
  <FlashMessage @flash={{flash}} />
{{/each}}

It also accepts your own template:

{{#each flashMessages.queue as |flash|}}
  <FlashMessage @flash={{flash}} as |component flash|>
    <h6>{{component.flashType}}</h6>
    <p>{{flash.message}}</p>
    {{#if component.showProgressBar}}
      <div class="alert-progress">
        <div class="alert-progressBar" style="{{component.progressDuration}}"></div>
      </div>
    {{/if}}
  </FlashMessage>
{{/each}}

Custom close action

The close action is always passed to the component whether it is used or not. It can be used to implement your own close button, such as an x in the top-right corner.

When using a custom close action, you will want to set destroyOnClick=false to override the default (destroyOnClick=true). You could do this globally in flashMessageDefaults.

{{#each flashMessages.queue as |flash|}}
  <FlashMessage @flash={{flash}} as |component flash close|>
    {{flash.message}}
    <span role="button" {{on "click" (action close)}}>x</span>
  </FlashMessage>
{{/each}}

Styling with Foundation or Bootstrap

By default, flash messages will have Bootstrap style class names. If you want to use Foundation, simply specify the messageStyle on the component:

{{#each flashMessages.queue as |flash|}}
  <FlashMessage @flash={{flash}} @messageStyle='foundation' />
{{/each}}

Styling with user-specified message type class prefix

If you don't wish to use the class names associated with Bootstrap / Foundation, specify the messageStylePrefix on the component. This will override the class name prefixes with your own. For example, messageStylePrefix='special-alert-' would create flash messages with the class special-alert-succcess

{{#each flashMessages.queue as |flash|}}
  <FlashMessage @flash={{flash}} @messageStylePrefix='special-alert-' />
{{/each}}

Sort messages by priority

To display messages sorted by priority, add this to your template:

{{#each flashMessages.arrangedQueue as |flash|}}
  <FlashMessage @flash={{flash}} />
{{/each}}

Rounded corners (Foundation)

To add radius or round type corners in Foundation:

{{#each flashMessages.arrangedQueue as |flash|}}
  <FlashMessage @flash={{flash}} @messageStyle='foundation' class='radius' />
{{/each}}
{{#each flashMessages.arrangedQueue as |flash|}}
  <FlashMessage @flash={{flash}} @messageStyle='foundation' class='round' />
{{/each}}

Custom flash message component

If the provided component isn't to your liking, you can easily create your own. All you need to do is pass in the flash object to that component:

{{#each flashMessages.queue as |flash|}}
  <CustomComponent @flash={{flash}} />
{{/each}}

Acceptance / Integration tests

When you install the addon, it should automatically generate a helper located at tests/helpers/flash-message.js. You can do this manually as well:

$ ember generate ember-cli-flash

This also adds the helper to tests/test-helper.js. You won't actually need to import this into your tests, but it's good to know what the blueprint does. Basically, the helper overrides a method used to initialise the flash-message's class, so that it behaves intuitively in a testing environment.

Some example tests below, based on qunit.

An example acceptance test:

// tests/acceptance/foo-page-test.js

import { module, test } from 'qunit'
import { setupApplicationTest } from 'ember-qunit'
import { click, visit } from '@ember/test-helpers'

module('Application | Component | foo-page', function (hooks) {
  setupApplicationTest(hooks)

  test('flash message is rendered', async function(assert) {
    assert.expect(1);

    await visit('/');

    await click('.button-that-opens-alert')

    assert.dom('.alert.alert-success').exists({ count: 1 });
  });
});

An example integration test:

// tests/integration/components/x-foo-test.js

import { module, test } from 'qunit'
import { setupRenderingTest } from 'ember-qunit'
import { render } from '@ember/test-helpers'
import { hbs } from 'ember-cli-htmlbars'

module('Integration | Component | x-foo', function (hooks) {
  setupRenderingTest(hooks)

  hooks.beforeEach(function() {
    // We have to register any types we expect to use in this component
    const typesUsed = ['info', 'warning', 'success'];
    this.owner.lookup('service:flash-messages').registerTypes(typesUsed);
  })

  test('it renders', function(assert) {
    await render(hbs`<XFoo/>`)
    ...
  })
});

Unit testing

For unit tests that require the flashMessages service, you'll need to do a small bit of setup:

import { module, test } from 'qunit'
import { setupTest } from 'ember-qunit'

module('Container | Route | foo', function (hooks) {
  setupTest(hooks)

  hooks.beforeEach(function() {
    // We have to register any types we expect to use in this component
    const typesUsed = ['info', 'warning', 'success'];
    this.owner.lookup('service:flash-messages').registerTypes(typesUsed);
  })

  test('it does the thing it should do', function(assert) {
    const subject = this.owner.lookup('route:foo')
    ...
  })
});

Styling

This addon is minimal and does not currently ship with a stylesheet. You can style flash messages by targeting the appropriate alert classes in your CSS.

License

MIT

Contributors

We're grateful to these wonderful contributors who've contributed to ember-cli-flash:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].