Skip to content

Commit 90113df

Browse files
authored
Merge pull request #402 from telerik/notification-center
docs: Improve Notification custom position KB
2 parents 008ceab + 950bdd5 commit 90113df

File tree

1 file changed

+80
-37
lines changed

1 file changed

+80
-37
lines changed
Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
title: Custom Notification Position
3-
description: How to position the notifications more precisely
2+
title: Custom or Centered Notification Position
3+
description: How to position the notifications more precisely at a specific custom location? How to center notifications on the screen?
44
type: how-to
5-
page_title: Custom Notification Position
5+
page_title: Custom or Centered Notification Position
66
slug: notification-kb-custom-position
77
position:
8-
tags:
9-
ticketid: 1504012
8+
tags: notification, center, centre, custom, position
9+
ticketid: 1504012, 1526682
1010
res_type: kb
1111
---
1212

@@ -20,14 +20,28 @@ res_type: kb
2020
</tbody>
2121
</table>
2222

23-
2423
## Description
25-
I have a top-menu and would like to be able to adjust the notification so it is displayed below the menu, like setting margin-top to -18px.
2624

27-
I want to customize the position of the notification to be different from the default options that are relative to the viewport. I want the notifications in an area of the page I choose.
25+
I want to customize the position of the notification to be different from the default options that are relative to the viewport boundaries. I want the notifications in a page area I choose.
26+
27+
I'd like to vertically center the notifications and display them at the center of the screen.
28+
29+
I would like to adjust the notification, so it is displayed below the top menu.
2830

2931
## Solution
30-
You can wrap the notification in an element from your app (such as a `<div>`) and, through the `Class` the notification exposes, you can define CSS rules that put the notifications in that container so they respect its own offsets and position in your layout.
32+
33+
To display notifications at a custom position, follow these steps. The examples below demonstrate them in action.
34+
35+
* Place the `TelerikNotification` component in two nested `<div>` elements.
36+
* The outer `<div>` should have a `position:relative` style.
37+
* The inner `<div>` should have a `position:absolute` style.
38+
* The **absolute** positioning ensures that the notifications don't push other content on the page. In some cases the outer `<div>` may not be needed.
39+
* Adjust the position of one or both `<div>`s. The exact approach and CSS styles will depend on the specific case.
40+
* Set a `Class` to the `TelerikNotification` component, which applies `position:relative` and `flex-wrap: nowrap !important` CSS styles.
41+
42+
### Custom Notifications near the center-top of the page
43+
44+
Here is where the notifications will appear if the outer relative `<div>` is placed right below the top bar of a Blazor app.
3145

3246
![Custom positions of the notifications that respect their parent element](images/notifications-with-custom-position.png)
3347

@@ -42,17 +56,16 @@ You can wrap the notification in an element from your app (such as a `<div>`) an
4256
left: 50%;
4357
top: 50%;
4458
transform: translate(-50%, -50%);
45-
z-index: 1234;
46-
/* you may also want to set z-index or other rules here depending on your needs
47-
This example centers the notification container according to its own parent which is
48-
between the button and the tile layout in this snippet
49-
Centering approach taken from https://css-tricks.com/centering-css-complete-guide/
59+
z-index: 1234; /* depends on other z-indexes on the page */
60+
/* This example centers the notification container according to its own parent which is
61+
below the button.
62+
Centering approach based on https://css-tricks.com/centering-css-complete-guide/
5063
*/
5164
}
5265
5366
.custom-positioned-notifications {
54-
position: relative; /*make the container for the individual notifications respect the position of its parent*/
55-
flex-wrap: nowrap !important; /*override the default setting from the component - this lets the individual notifications stay in a column instead of breaking out into two columns*/
67+
position: relative; /* force the individual notifications respect the position of their parent */
68+
flex-wrap: nowrap !important; /* display the individual notifications in a single column */
5669
}
5770
</style>
5871
@@ -68,26 +81,7 @@ You can wrap the notification in an element from your app (such as a `<div>`) an
6881
</div>
6982
</div>
7083
71-
<TelerikTileLayout Columns="3"
72-
ColumnWidth="200px"
73-
RowHeight="150px"
74-
Resizable="true"
75-
Reorderable="true">
76-
<TileLayoutItems>
77-
<TileLayoutItem HeaderText="Panel 1">
78-
<Content>Regular sized first panel.</Content>
79-
</TileLayoutItem>
80-
<TileLayoutItem HeaderText="Panel 2">
81-
<Content>You can put components in the tiles too.</Content>
82-
</TileLayoutItem>
83-
<TileLayoutItem HeaderText="Panel 3" RowSpan="3">
84-
<Content>This tile is three rows tall.</Content>
85-
</TileLayoutItem>
86-
<TileLayoutItem HeaderText="Panel 4" RowSpan="2" ColSpan="2">
87-
<Content>This tile is two rows tall and two columns wide</Content>
88-
</TileLayoutItem>
89-
</TileLayoutItems>
90-
</TelerikTileLayout>
84+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam lacinia suscipit velit, ut lobortis lorem egestas a. Mauris lobortis imperdiet lacus, quis dictum lacus. Integer suscipit ultrices velit nec malesuada. Vestibulum sodales tellus in nibh feugiat maximus non a augue. Nunc eu nisl arcu. Donec vitae justo felis. Maecenas condimentum, risus quis vehicula cursus, elit nisi posuere dui, eget feugiat sapien lacus in leo. Morbi molestie et velit at egestas.</p>
9185
9286
@code {
9387
TelerikNotification NotificationReference { get; set; }
@@ -105,9 +99,58 @@ You can wrap the notification in an element from your app (such as a `<div>`) an
10599
}
106100
````
107101

102+
### Center Notifications Vertically and Horizontally on the Screen
103+
104+
In this case, the `TelerikNotification` is wrapped in a single `<div>` with a `position:fixed` style. This will center the notifications on the screen, no matter the page scroll offset.
105+
106+
````CSHTML
107+
<style>
108+
.centered-notification-parent {
109+
/* center this element on the screen, no matter the page scroll offset */
110+
position: fixed;
111+
left: 50%;
112+
top: 50%;
113+
transform: translate(-50%, -50%);
114+
z-index: 1234; /* depends on other z-indexes on the page */
115+
}
116+
117+
.custom-positioned-notifications {
118+
position: relative; /* force the individual notifications respect the position of their parent */
119+
flex-wrap: nowrap !important; /* display the individual notifications in a single column */
120+
}
121+
</style>
122+
123+
<TelerikButton OnClick="@AddNotification">Display a vertically centered notification</TelerikButton>
124+
125+
<div class="custom-notification-parent">
126+
<TelerikNotification Class="custom-positioned-notifications"
127+
@ref="@NotificationReference"
128+
HorizontalPosition="@NotificationHorizontalPosition.Center"
129+
VerticalPosition="@NotificationVerticalPosition.Top">
130+
</TelerikNotification>
131+
</div>
132+
133+
@code {
134+
TelerikNotification NotificationReference { get; set; }
135+
int counter { get; set; }
136+
public void AddNotification()
137+
{
138+
var notificationText = counter % 5 == 0 ? "A Lot Longer Notification Content Here" : "Foo";
139+
NotificationReference.Show(new NotificationModel()
140+
{
141+
Text = $"{notificationText} {++counter}",
142+
ThemeColor = "primary",
143+
Closable = true,
144+
CloseAfter = 0
145+
});
146+
}
147+
}
148+
````
149+
108150
## Notes
151+
109152
* Keep in mind that the container for the notifications might consume mouse clicks, selection or otherwise interfere with the UX of your app. Thus, style and size it accordingly.
110153

111154
* You can inspect the rendered content to see what the built-in CSS rules and HTML structure are, so you can tweak this example further to fit your needs.
112155

113-
* @[template](/_contentTemplates/notification/templates.md#one-instance-per-app-link)
156+
* @[template](/_contentTemplates/notification/templates.md#one-instance-per-app-link)

0 commit comments

Comments
 (0)