logo polydile dile-components

dile-toast-persistent

Web Component to implement an always visible toast message that opens and closes with a smooth animation.

Install

npm install @dile/dile-toast-persistent

Usage

Import the component

import '@dile/dile-toast-persistent/dile-toast-persistent';

Use the component

This component needs to configure the toast content as slot

<dile-toast-persistent>
  This is the toast content!!
</dile-toast-persistent>

Once you have the toast, you can use the open() and close() component methods to show or hide the toast message.

Properties

The component offers several properties to configure the way it works.

Methods

Custom events

Style customization

You can customize the menu box interface by using the CSS custom properties bellow.

Custom propertyDescriptionDefault
--dile-toast-persistent-background-colormenu overlay layer background color#0e6ff6
--dile-toast-persistent-colorOverlay text color#fff
--dile-toast-persistent-z-indexmenu overlay layer z-index999
--dile-toast-persistent-widthMenu overlay layer width300px
--dile-toast-persistent-max-widthMenu overlay layer max width300px
--dile-toast-persistent-border-radiusMenu overlay layer border radius0
--dile-toast-persistent-box-shadowMenu overlay box shadow0 0 2px rgba(200, 200, 200, 0.5)
--dile-toast-persistent-paddingMenu layer padding1px
--dile-toast-persistent-transitionOpen / Close transition configurationease 0.5s

dile-toast-persistent demos

Default toast

class MyComponent extends LitElement {
  
  static get styles() {
    return css`
      :host {
        position: relative;
        z-index: 1000;
      }
      dile-toast-persistent {
        --dile-toast-persistent-padding: 0 10px;
      }
    `
  }

  render() {
    return html`
      <dile-toast-persistent id="toast">
        <p>
          I am a toast message
        </p>
      </dile-toast-persistent>
      <button id="open">Show default toast</button>
      <button id="close">Close default toast</button>
    `
  }
  firstUpdated() {
    this.shadowRoot.getElementById('open').addEventListener('click', () => {
      this.shadowRoot.getElementById('toast').open();
    });
    this.shadowRoot.getElementById('close').addEventListener('click', () => {
      this.shadowRoot.getElementById('toast').close();
    });
  }
}
customElements.define('my-component', MyComponent);
export const JsStory = () => html`<my-component></my-component>`;

Open the toast to the right

class SecondComponent extends LitElement {
  
  static get styles() {
    return css`
      :host {
        position: relative;
        z-index: 1000;
      }
      dile-toast-persistent {
        --dile-toast-persistent-padding: 0 10px;
        --dile-toast-persistent-background-color: #923;
      }
    `
  }

  render() {
    return html`
      <dile-toast-persistent id="toast" right>
        <p>
          I am a toast message
        </p>
      </dile-toast-persistent>
      <button id="open">Show right toast</button>
      <button id="close">Close right toast</button>
    `
  }
  firstUpdated() {
    this.shadowRoot.getElementById('open').addEventListener('click', () => {
      this.shadowRoot.getElementById('toast').open();
    });
    this.shadowRoot.getElementById('close').addEventListener('click', () => {
      this.shadowRoot.getElementById('toast').close();
    });
  }
}
customElements.define('second-component', SecondComponent);
export const JsStory2 = () => html`<second-component></second-component>`;