/ Toast / Positioning
Search results

Positioning in Angular Toast component

21 Dec 2022 / 3 minutes to read

Toast position can be updated based on predefined positions or user customizable positions. Predefined position combinations are updated in X and Y position properties.

Predefined

X Positions

  • Left
  • Center
  • Right

Y Positions

  • Top
  • Bottom

In the case of multiple Toast display, new Toast position will not update on dynamic change of property values, until the old Toast messages removed. Toast occupies full width when we set width as ‘100%’, so X positions won’t affect changes when ‘100%’ width.

Custom

Custom X and Y Position can be given as pixels/numbers/percentage. The number value is considered as pixels. based value top and left value updated in the toast.

Copied to clipboard
import { Component, ViewChild } from '@angular/core';

@Component({
selector: 'app-root',
template: `
    <div id="toast_target"></div>
    <div id='sample_container'>
    <div id='container'>
      <div class='row' style="padding-top: 20px" id= "toast_pos_target">
        <div id="toast_pos"> </div>
        <div id="toast_pos_property">
            <table style="width: 100%">
                <tr>
                    <td>
                        <div style='padding:25px 0 0 0;'>
                            <ejs-radiobutton (change)="positionChange($event)" label="Position" name="position" value="position" checked="true"></ejs-radiobutton>
                        </div>
                    </td>
                    <td>
                        <div style='padding:25px 0 0 0;'>
                            <ejs-radiobutton (change)="customePosition($event)" label="Custom" name="position" value="position"></ejs-radiobutton>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td id="dropdownChoose" colspan="2">
                        <div id="dropdown" style='padding-top:25px;'>
<ejs-dropdownlist #dropDown (change)="dropDownChange($event)"  [dataSource]='dropdownDB' index='4'>
        </ejs-dropdownlist>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" id="customChoose" style="display: none">
                        <form id="formId" class="form-horizontal">
                            <div class='e-row'>
                                <div class="e-float-input">
                                    <input class="e-input" id="xPos" name="Digits" value=50 digits="true" data-digits-message="Please enter digits only." required="">
                                    <span class="e-float-line"></span>
                                    <label class="e-float-text" for="Digits">X Position</label>
                                </div>
                            </div>
                            <div class='e-row'>
                                <div class="e-float-input">
                                    <input class="e-input" id="yPos" name="Digits" value=50 digits="true" data-digits-message="Please enter digits only." required="">
                                    <span class="e-float-line"></span>
                                    <label class="e-float-text" for="Digits">Y Position</label>
                                </div>
                            </div>
                        </form>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div style='padding:25px 0 0 0;'>
                            <ejs-radiobutton (change)="targetChange($event)" label="Target" name="target" value="Bottom"></ejs-radiobutton>
                        </div>
                    </td>
                    <td>
                        <div style='padding:25px 0 0 0;'>
                            <ejs-radiobutton (change)="globalTargetChange($event)" label="Global" name="target" value="Bottom" checked="true"></ejs-radiobutton>
                        </div>
                    </td>
                </tr>
            </table>
            <div id="toast_btn" style="padding-top: 25px">
              <button ejs-button [isPrimary]="true" (click)="btnClick($event)">Show Toast</button>
            </div>
        </div>
    </div>
    </div></div>

    <ejs-toast #element (created)="onCreate($event)"  [position] = 'position' >
          <ng-template #title>
              <div>Matt sent you a friend request</div>
          </ng-template>
          <ng-template #content>
              <div>Hey, wanna dress up as wizards and ride our hoverboards?</div>
          </ng-template>
        </ejs-toast>
    `
})

export class AppComponent {
@ViewChild('element') toastObj;
@ViewChild('dropDown') dropDownList;

public position = { X: 'Right', Y: 'Bottom' };
public dropdownDB = ['Top Left', 'Top Right', 'Top Center', 'Bottom Left', 'Bottom Right', 'Bottom Center' ];
public customFlag = false;

onCreate() {
  this.toastShow();
}
btnClick() {
if (this.customFlag) {
    this.setcustomPosValue();
}
this.toastObj.hide('All');
this.toastShow();
}

positionChange(e) {
  if (e.event.target.checked) {
    this.toastObj.hide('All');
    document.getElementById('dropdownChoose').style.display = 'table-cell';
    document.getElementById('customChoose').style.display = 'none';
    this.setToastPosValue(this.dropDownList.value.toString());
    this.customFlag = false;
    this.toastShow();
  }
}

customePosition(e) {
  if (e.event.target.checked) {
    this.toastObj.hide('All');
    document.getElementById('dropdownChoose').style.display = 'none';
    document.getElementById('customChoose').style.display = 'table-cell';
    this.setcustomPosValue();
    this.customFlag = true;
    this.toastShow();
  }
}

dropDownChange(e) {
this.toastObj.hide('All');
this.setToastPosValue(e.value);
this.toastShow();
}
globalTargetChange(e) {
  if (e.event.target.checked) {
    this.toastObj.hide('All');
    this.toastObj.target = document.body;
    this.toastShow();
  }
}

targetChange(e) {
  if (e.event.target.checked) {
    this.toastObj.hide('All');
    this.toastObj.target = '#toast_pos_target';
    this.toastShow();
  }
}

 setcustomPosValue(): void {
this.toastObj.position.X = parseInt((<any>document.getElementById('xPos')).value, 10);
this.toastObj.position.Y = parseInt((<any>document.getElementById('yPos')).value, 10);
 }

 setToastPosValue(value: string): void {
value = value.toLowerCase().replace(' ', '');
let toastObj = this.toastObj;
switch (value) {
    case 'topleft':
        toastObj.position.X = 'Left'; toastObj.position.Y = 'Top'; break;
    case 'topright':
        toastObj.position.X = 'Right'; toastObj.position.Y = 'Top'; break;
    case 'topcenter':
        toastObj.position.X = 'Center'; toastObj.position.Y = 'Top'; break;
    case 'topfullwidth':
        toastObj.width = '100%'; toastObj.position.X = 'Center'; toastObj.position.Y = 'Top'; break;
    case 'bottomleft':
        toastObj.position.X = 'Left'; toastObj.position.Y = 'Bottom'; break;
    case 'bottomright':
        toastObj.position.X = 'Right'; toastObj.position.Y = 'Bottom'; break;
    case 'bottomcenter':
        toastObj.position.X = 'Center'; toastObj.position.Y = 'Bottom'; break;
    case 'bottomfullwidth':
        toastObj.width = '100%'; toastObj.position.X = 'Center'; toastObj.position.Y = 'Bottom'; break;
}
}

toastShow() {
        setTimeout(
    () => {
        this.toastObj.show();
    }, 700);
}
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ToastModule } from '@syncfusion/ej2-angular-notifications';
import { ButtonModule, CheckBoxModule , RadioButtonModule } from '@syncfusion/ej2-angular-buttons';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
import { DatePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, ToastModule, ButtonModule, CheckBoxModule , RadioButtonModule, DropDownListModule, DatePickerModule
    ],
    declarations: [AppComponent ],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
            <script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js"></script>
    <title>Syncfusion Angular Toast Sample</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/20.4.38/material.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link href="index.css" rel="stylesheet" />
    <script src="https://unpkg.com/core-js/client/shim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/core.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/zone.js/0.9.0/zone.min.js"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.3"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>
<body>
    <app-root>
        <div id='loader'>LOADING....</div>
    </app-root>
    <script id="template_toast_ele" type="text/x-template">
        <div id='template_toast' style="display: none">
            <div class="horizontal-align">
                <div class='toast-content'>
                    <div class='toast-title'>
                        Weekend Alarm
                    </div>
                    <div class='toast-message'> With traffic, its likely to take 45 minutes to get to jenny's 24th Birthday Bash at Hillside Bar, 454 E.
                        Olive Way by 10:00PM </div>
                </div>
            </div>
            <img src="https://ej2.syncfusion.com/demos/src/toast/resource/map.jpg" width="100%" height="70%">
        </div>
    </script>
</body>
</html>
Copied to clipboard
/* csslint ignore:start */
#container {
    margin: 25px;
}

#elementToastTime .e-toast {
    width: 500px;
    height: 147px;
}

#elementToastTime {
    text-align: center;
}

#elementToastTime img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
}

#elementToastTime .e-toast-message {
    width: inherit;
}

#sample_container {
    margin: 10px;
}

body>#element .e-toast {
    width: 400px !important;
}

#template_toast .horizontal-align {
    margin: 10px 0;
}

#template_toast .horizontal-align .toast-content .toast-message {
    opacity: 0.4;
}

#template_toast .horizontal-align .toast-content {
    display: inline-flex;
    flex: 1;
    flex-direction: column;
    margin-left: 10px;
}

#loader {
    color: #008cff;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
}

.progress {
    height: 20px;
    position: relative;
    margin: 20px 0 20px 0;
    background: #555;
    -moz-border-radius: 25px;
    -webkit-border-radius: 25px;
    border-radius: 25px;
    box-shadow: inset 0 -1px 1px rgba(255, 255, 255, 0.3);
}

.progress span {
    background-color: #f0a3a3;
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #f0a3a3), color-stop(1, #f42323));
    display: block;
    height: 100%;
    border-radius: 10px;
    width: 50%;
    position: relative;
    overflow: hidden;
}

.progress span::after {
    background-image: -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.25, rgba(255, 255, 255, .2)), color-stop(.25, transparent), color-stop(.5, transparent), color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.75, rgba(255, 255, 255, .2)), color-stop(.75, transparent), to(transparent));
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-size: 50px 50px;
    -webkit-animation: moveAnimate 2s linear infinite;
    overflow: hidden;
}

@-webkit-keyframes moveAnimate {
    0% {
        background-position: 0 0;
    }
    100% {
        background-position: 50px 50px;
    }
}
/* csslint ignore:end */

See Also