Skip to content

[Notification] shortcut methods for showing notifications with preset theme variants and closing time #8400

@taefi

Description

@taefi

Describe your motivation

Similar to what is described in vaadin/web-components#5769, I would like to have utility/shortcut methods that enables showing success, warn, error, and info notifications with some defaults without writing 10 lines of code.

Describe the solution you'd like

The Notification component already has enough API to make this happen, just by configuring a Notification instance with some defaults for the icon, location, duration, and theme variant.

My desired solution is to be able to show a notification with one-liner call to the Notification API.

Static shortcut methods

One way to achieve this is through introducing a set of overloads like these:

String message = "This is the message.";
Notification.info(message);
Notification.success(message);
Notification.warn(message);
Notification.error(message);

// and to show a component as the message:
UnorderedList ul = new UnorderedList();
ul.add("First thing to fix.");
ul.add("Second thing to fix.");
Notification.error(ul)

Apart from correspondent default theme variants and icon for each type, we can assume some defaults for the followings:

The default position could be Notification.Position.TOP_END.
The default duration could be 5 seconds.

Then, some limited set of overloads such as the followings, it would be awesome:

Notification.info(String message, int duration);
Notification.info(String message, Position position);
// other overloads

Builder Pattern

Instead of a lot of shortcut overloads we can employ a builder pattern to configure the preset defaults after calling the base method:

Notification.success("Transaction was successful") // this has all the defaults but doesn't show
        .position(Notification.Position.TOP_START) // optionally overriding the defaults
        .duration(10) // optional
        .icon(...) // optional
        .show();

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions