|
| 1 | +--- |
| 2 | +title: Open, Close and Hide |
| 3 | +page_title: Notification - Open, Close and Hide |
| 4 | +description: Open, Close and Hide the Notification component |
| 5 | +slug: notification-open-close-hide |
| 6 | +tags: telerik,blazor,notification,open,close,hide |
| 7 | +published: True |
| 8 | +position: 20 |
| 9 | +--- |
| 10 | + |
| 11 | +# Open, Close and Hide Notifications |
| 12 | + |
| 13 | + |
| 14 | +This article explains how to open, close and hide the Notification component. For brevity, it will be divided in the following sections: |
| 15 | + |
| 16 | +* [Open](#open) |
| 17 | + * [Use Only the Text and ThemeColor Properties](#use-only-the-text-and-themecolor-properties) |
| 18 | + * [Pass a NotificationModel to the Method](#pass-a-notificationmodel-to-the-method) |
| 19 | +* [Close and Hide](#close-and-hide) |
| 20 | + * [Automatically Closing Notification](#automatically-closing-notification) |
| 21 | + * [Manually Closing Notification](#manually-closing-notification) |
| 22 | + |
| 23 | +## Open |
| 24 | + |
| 25 | +You can open (show) the Notification component by using the [`Show`]({%slug notification-overview%}#show-method) method of its reference. |
| 26 | + |
| 27 | +You can use it in two ways: |
| 28 | + |
| 29 | +* [Use Only the Text and ThemeColor Properties](#use-only-the-text-and-themecolor-properties) |
| 30 | +* [Pass a NotificationModel to the Method](#pass-a-notificationmodel-to-the-method) |
| 31 | + |
| 32 | + |
| 33 | +### Use Only the Text and ThemeColor Properties |
| 34 | + |
| 35 | +If you do not need to customize the [closing](#close-and-hide) or the icon of the component you can quickly create them by passing only what text and [theme color]({%slug notification-appearance%}#themecolor) should the Notification have. |
| 36 | + |
| 37 | +````CSHTML |
| 38 | +@* At minimum, you can pass a text message and a color to the Show() method *@ |
| 39 | +
|
| 40 | +<TelerikButton OnClick="@OpenNotification">Open a notification</TelerikButton> |
| 41 | +
|
| 42 | +<TelerikNotification @ref="@NotificationReference" /> |
| 43 | +
|
| 44 | +@code { |
| 45 | + public TelerikNotification NotificationReference { get; set; } |
| 46 | +
|
| 47 | + public void OpenNotification() |
| 48 | + { |
| 49 | + NotificationReference.Show("My notification", "success"); |
| 50 | + } |
| 51 | +} |
| 52 | +```` |
| 53 | + |
| 54 | +### Pass a NotificationModel to the Method |
| 55 | + |
| 56 | +You can pass the entire [NotificationModel]({%slug notification-overview%}#notificationmodel-class) to provide detailed information for the component - whether it should be closable or specify the icon. |
| 57 | + |
| 58 | +````CSHTML |
| 59 | +@* You can pass the entire NotificationModel with all its features for complete control over the message settings *@ |
| 60 | +
|
| 61 | +<TelerikButton OnClick="@OpenNotification">Open a notification</TelerikButton> |
| 62 | +
|
| 63 | +<TelerikNotification @ref="@NotificationReference" /> |
| 64 | +
|
| 65 | +@code { |
| 66 | + public TelerikNotification NotificationReference { get; set; } |
| 67 | +
|
| 68 | + public void OpenNotification() |
| 69 | + { |
| 70 | + NotificationReference.Show(new NotificationModel() |
| 71 | + { |
| 72 | + Text = "My Notification", |
| 73 | + ThemeColor = "success", |
| 74 | + Icon = true, |
| 75 | + IconName = IconName.Star, |
| 76 | + Closable = false |
| 77 | + }); |
| 78 | + } |
| 79 | +} |
| 80 | +```` |
| 81 | + |
| 82 | +## Close and Hide |
| 83 | + |
| 84 | +There are two separate ways to close a notification: |
| 85 | + |
| 86 | +* [Automatically Closing Notification](#automatically-closing-notification) |
| 87 | +* [Manually Closing a Notification](#manually-closing-a-notification) |
| 88 | + |
| 89 | +### Automatically Closing Notification |
| 90 | + |
| 91 | +By default each notification is an automatically closing one. You can define the time it stays visible by adjusting the `CloseAfter` parameter of the [NotificationModel]({%slug notification-overview%}#notificationmodel-class). It defaults to `5000ms`. |
| 92 | + |
| 93 | +You can also let the user dismiss a notification message before that timer elapses through a closing button by setting the `Closable` parameter of the `NotificationModel` to `true` (its default value). |
| 94 | + |
| 95 | +>caption Automatically Closing Notification |
| 96 | +
|
| 97 | + |
| 98 | +````CSHTML |
| 99 | +@* By default, notification messages close on their own after 5 seconds *@ |
| 100 | +
|
| 101 | +<TelerikButton OnClick="@AddAutoClosingNotification">Add Auto closing notification</TelerikButton> |
| 102 | +
|
| 103 | +<TelerikNotification @ref="@NotificationReference" /> |
| 104 | +
|
| 105 | +@code { |
| 106 | + public TelerikNotification NotificationReference { get; set; } |
| 107 | +
|
| 108 | + public void AddAutoClosingNotification() |
| 109 | + { |
| 110 | + NotificationReference.Show(new NotificationModel() |
| 111 | + { |
| 112 | + Text = "My Notification", |
| 113 | + ThemeColor = "success", |
| 114 | + Closable = false, |
| 115 | + CloseAfter = 2000, |
| 116 | + IconName = IconName.Star |
| 117 | + }); |
| 118 | + } |
| 119 | +} |
| 120 | +```` |
| 121 | + |
| 122 | +### Manually Closing a Notification |
| 123 | + |
| 124 | +You can prevent the notification from closing automatically and let the user close it with the close button only. To do so, in the `NotificationModel` instance, set the `Closable` parameter to `true` and the `CloseAfter` parameter to `0`. |
| 125 | + |
| 126 | +>caption Manually Closing Notification |
| 127 | +
|
| 128 | +````CSHTML |
| 129 | +@* This notification will not disappear automatically, the user must close it on their own *@ |
| 130 | +
|
| 131 | +<TelerikButton OnClick="@AddManuallyClosingNotification">Add manually closing notification</TelerikButton> |
| 132 | +
|
| 133 | +<TelerikNotification @ref="@NotificationReference" /> |
| 134 | +
|
| 135 | +@code { |
| 136 | + public TelerikNotification NotificationReference { get; set; } |
| 137 | +
|
| 138 | + public void AddManuallyClosingNotification() |
| 139 | + { |
| 140 | + NotificationReference.Show(new NotificationModel() |
| 141 | + { |
| 142 | + Text = "My Notification", |
| 143 | + ThemeColor = "success", |
| 144 | + Closable = true, |
| 145 | + CloseAfter = 0, |
| 146 | + IconName = IconName.Star |
| 147 | + }); |
| 148 | + } |
| 149 | +} |
| 150 | +```` |
| 151 | + |
| 152 | + |
| 153 | +## See Also |
| 154 | + |
| 155 | + * [Notification Overview]({%slug notification-overview%}) |
0 commit comments