Table of contents
1.
Introduction
2.
Basic Notifications
2.1.
Theme Variants
2.2.
Duration
2.3.
Persistent Notifications
2.4.
Position
2.5.
Size
2.6.
Stacking
2.7.
Use Notifications efficiently
3.
Interactive Notifications
4.
Icons in notifications
5.
Frequently Asked Questions
5.1.
What is Vaadin used for?
5.2.
Is Vaadin easy to learn?
5.3.
How does vaadin flow work?
6.
Conclusion
Last Updated: Mar 27, 2024

Vaadin-Notification

Author Harsh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Vaadin is an open-source web development framework with inbuilt support for both javascript and AJAX. External functionality can also be included into it using the google web toolkit.

Vaadin has many components which can be used to create the web app and in this blog, we are going to see Vaadin-Notification which is used to communicate different information about activities, processes, and events throughout the application.

Vaadin-notifications logo

Basic Notifications

Vaadin-Notification is used to provide some feedback or information to the user. A very simple vaadin-notification component can be created using the below code.

const notification = Notification.show(‘Welcome ninja’, {
  position: 'middle',
});
You can also try this code with Online Javascript Compiler
Run Code

Output of the above program

Theme Variants

Different theme variants can also be used like success, error, primary, and contrast. To switch between different themes you can use.

notification.setAttribute('theme', ‘THEME_NAME’);
You can also try this code with Online Javascript Compiler
Run Code

 

For example, if you want to set the theme to success or primary you can set the attribute theme to that particular theme name.

notification.setAttribute('theme', ‘success’);
You can also try this code with Online Javascript Compiler
Run Code

 

1. Success: To display the vaadin-notification after an operation is successfully completed we can use this theme variant. The below code snippet can be used to change the theme variant of vaadin-notification to success.

const notification = Notification.show('Welcome ninja!', {
  position: 'middle',
});
notification.setAttribute('theme', ‘success’);
You can also try this code with Online Javascript Compiler
Run Code

Output of the above program

2. Error: This theme variant can be used when we want to notify the user that some error has occurred. The below code snippet can be used to change the theme variant of vaadin-notification to error.

const notification = Notification.show(‘Registration Failed!’, {
  position: 'middle',
});
notification.setAttribute('theme', ‘error’);
You can also try this code with Online Javascript Compiler
Run Code

 

Output of the above program

 

3. Primary: when we want to grab the attention of the user at that time we can use this theme variant. The below code snippet can be used to change the theme variant of vaadin-notification to primary.

const notification = Notification.show('Hello Ninja!’, {
  position: 'middle',
});
notification.setAttribute('theme', ‘primary’);
You can also try this code with Online Javascript Compiler
Run Code

Output of the above program

 

4. Contrast: High-contrast version of vaadin-notification. The below code snippet can be used to change the theme variant of vaadin-notification to contrast.

const notification = Notification.show('High-Contrast Version’, {
  position: 'middle',
});
notification.setAttribute('theme', ‘contrast’);
You can also try this code with Online Javascript Compiler
Run Code

High-Contrast Output
 

Duration

Users can control how long a vaadin-notification is shown on screen by changing its duration. By default, 5 seconds is the on-time for the vaadin-notification.

Persistent Notifications

Some notifications need more attention than others and need explicit user permission to close. We can set the duration to 0 to get this functionality. There should have a button that the user can press to close the notification or take other suitable action.

Position

There are 7 different positions available in the viewport where we can place vaadin-notification.

Positions in vaadin

Position Explained

Size

The notification card's size is determined by its content automatically. The card's maximum width is one-third of the viewport if the viewport is large; otherwise, the card always occupies the entire viewport.

Stacking

Depending on where they are placed, multiple simultaneously displayed notifications are stacked vertically. A fresh notification appears below the more recent ones when the bottom half of the screen is used as the position.

A new notification shows above the already-present notifications when the position is set to the top half.

Use Notifications efficiently

Use notifications efficiently as they are disruptive in nature. Reserving alerts for more crucial information that the user might otherwise miss will help.

Instead of sending out immediate notifications, less urgent notifications can be sent via a link or dropdown in the application's header or footer.

Interactive Notifications

Vaadin also provides interactive notifications that allow interactive elements to be inserted in the vaadin-notifications such as buttons or links so that users can perform relevant actions. Below code snippet can be used to create an interactive vaadin-notification.

<vaadin-notification
  theme="contrast"
  duration="10000"
  position="middle"
  .opened="${this.notificationOpened}"
  @opened-changed="${(e: NotificationOpenedChangedEvent) => {
    this.notificationOpened = e.detail.value;
  }}"
  ${notificationRenderer(this.renderer, [])}
></vaadin-notification>

...

renderer: NotificationLitRenderer = () => {
  return html`
    <vaadin-horizontal-layout theme="spacing" style="align-items: center;">
      <div>Task Deleted!!</div>
      <vaadin-button
        theme="tertiary-inline"
        style="margin-left: var(--lumo-space-xl);"
        @click="${this.close}"
      >
        Undo
      </vaadin-button>
      <vaadin-button theme="tertiary-inline" aria-label="Close" @click="${this.close}">
        <vaadin-icon icon="lumo:cross"></vaadin-icon>
      </vaadin-button>
    </vaadin-horizontal-layout>
  `;
};
You can also try this code with Online Javascript Compiler
Run Code

Output of above program

Icons in notifications

Icons and other visual effects can also be added into vaadin-notifications to make it more appealing. The usage of icons and other content formatting can be used to convey information and useful visual cues, such as making it simpler for color-blind users to discern between success and error notices.

Notification Icons

 

Frequently Asked Questions

What is Vaadin used for?

A Java web framework called Vaadin Flow (formerly known as Vaadin Framework) is used to create web pages and web applications. With the help of the Vaadin Flow programming model, developers may create User Interfaces (UIs) using Java without directly utilising HTML or JavaScript.
 

Is Vaadin easy to learn?

Vaadin Flow's Java API is easy to learn if you are familiar with Java. Each component is a Java class and you can create more complex components by combining them with layouts.
 

How does vaadin flow work?

You construct the UI from components, link it to a data source, and respond to user events much like you would when creating a conventional desktop application. Without having to offer REST services or find alternative methods of transferring data to the browser, the UI operates on the JVM. In the browser, Flow apps are displayed as typical HTML.

Conclusion

In this article, we have extensively discussed vaadin-notifications. We have also seen how different theme variants are implemented in the vaadin-notifications.
 

If you think this blog has helped you enhance your knowledge about Vaadin-notitication and if you would like to learn more, check out our articles Overloading and Overriding static Methods in javaUnderstanding Association, Aggregation, and Composition in Javajava interview questionsMultiple Inheritance in JavaMultilevel Inheritance in Javaand many more on our Website.
 

Visit our website to read more such blogs. Make sure that you enroll in the courses provided by us, take mock tests and solve problems available and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.

 

Nevertheless, you may consider our paid courses to give your career an edge over others!

Thank You

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass