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/ui

Usage

Import the component

import '@dile/ui/components/toast/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 property Description Default
--dile-toast-persistent-background-color menu overlay layer background color #0e6ff6
--dile-toast-persistent-color Overlay text color #fff
--dile-toast-persistent-z-index menu overlay layer z-index 999
--dile-toast-persistent-width Menu overlay layer width 300px
--dile-toast-persistent-max-width Menu overlay layer max width 300px
--dile-toast-persistent-border-radius Menu overlay layer border radius 0
--dile-toast-persistent-box-shadow Menu overlay box shadow 0 0 2px rgba(200, 200, 200, 0.5)
--dile-toast-persistent-padding Menu layer padding 1px
--dile-toast-persistent-transition Open / Close transition configuration ease 0.5s

dile-toast-persistent demos

Default toast

<script type="module">
import { LitElement, html, css } from 'lit';
import '@dile/ui/components/toast/toast-persistent';

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);
</script>
<my-component></my-component>

Open the toast to the right

<script type="module">
import { LitElement, html, css } from 'lit';
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);
</script>
<second-component></second-component>