|
| 1 | +--- |
| 2 | +title: Show and Hide Behavior |
| 3 | +page_title: Menu - Show and Hide Behavior |
| 4 | +description: Configure the Menu's sub-items appearance by using the ShowOn parameter and control how they disappear through the HideOn option. |
| 5 | +slug: menu-show-hide-behavior |
| 6 | +tags: telerik,blazor,menu,show,hide,events,hover,click,mouseleave,mouseenter |
| 7 | +published: true |
| 8 | +position: 3 |
| 9 | +--- |
| 10 | + |
| 11 | +# Show and Hide Behavior |
| 12 | + |
| 13 | +You can control the user interaction with the Menu items by defining how the child items show and hide. |
| 14 | + |
| 15 | +The `ShownOn` and `HideOn` parameters allow you to set the event that will show and hide the child Menu items. |
| 16 | + |
| 17 | +* The `ShowOn` parameter accepts a value from the `Telerik.Blazor.MenuShowEvent` enum: |
| 18 | + |
| 19 | + * `MouseEnter`—Child Menu items will display when the mouse cursor enters a parent Menu item. |
| 20 | + * `Click`—Child Menu items will display when the user clicks or taps on a parent Menu item. |
| 21 | + |
| 22 | +* The `HideOn` parameter accepts a value from the `Telerik.Blazor.MenuHideEvent` enum: |
| 23 | + |
| 24 | + * `MouseLeave`—Child Menu items will disappear when the mouse cursor leaves the child item group and their parent. |
| 25 | + * `Click`—Child Menu items will disappear when the user clicks or taps on their parent or on another parent, or outside the Menu. Clicking a child item will not close the currently open child item group. To change this behavior, use the [`CloseOnClick`]({%slug components/menu/overview%}#menu-parameters) parameter. |
| 26 | + |
| 27 | +By default, the Menu items are shown on hover (mouse enter) over the Menu and hidden on mouse leave. |
| 28 | + |
| 29 | +> Changing the `ShowOn` & `HideOn` values dynamically at runtime is not supported at this stage. |
| 30 | +> |
| 31 | +> Mixing the two behaviors is likely to produce undesired UX and is not recommended. |
| 32 | +
|
| 33 | +>caption Explore the show and hide behavior of the Menu items |
| 34 | +
|
| 35 | +````CSHTML |
| 36 | +@* Setting `ShowOn` and `HideOn` is not mandatory. The default values are `MenuShowEvent.MouseEnter` & `MenuHideEvent.MouseLeave`. *@ |
| 37 | +
|
| 38 | +<div> |
| 39 | + <div style="padding: 20px; margin:2rem"> |
| 40 | + <p>Menu <strong>ShowOn: Click</strong> : <strong>HideOn: Click</strong></p> |
| 41 | + <TelerikMenu Data="@MenuItems" |
| 42 | + ShowOn="@MenuShowEvent.Click" |
| 43 | + HideOn="@MenuHideEvent.Click" /> |
| 44 | + </div> |
| 45 | + <div style="padding: 20px; margin:2rem"> |
| 46 | + <p>Menu <strong>ShowOn: MouseEnter</strong> : <strong>HideOn: MouseLeave</strong></p> |
| 47 | + <TelerikMenu Data="@MenuItems" |
| 48 | + ShowOn="@MenuShowEvent.MouseEnter" |
| 49 | + HideOn="@MenuHideEvent.MouseLeave" /> |
| 50 | + </div> |
| 51 | +</div> |
| 52 | +
|
| 53 | +@code { |
| 54 | + private List<MenuItem> MenuItems { get; set; } |
| 55 | +
|
| 56 | + protected override void OnInitialized() |
| 57 | + { |
| 58 | + MenuItems = new List<MenuItem>() |
| 59 | + { |
| 60 | + new MenuItem() |
| 61 | + { |
| 62 | + Text = "item 1", |
| 63 | + Items = new List<MenuItem>() |
| 64 | + { |
| 65 | + new MenuItem() |
| 66 | + { |
| 67 | + Text = "item 1.1", |
| 68 | + }, |
| 69 | + new MenuItem() |
| 70 | + { |
| 71 | + Text = "item 1.2", |
| 72 | + Items = new List<MenuItem>() |
| 73 | + { |
| 74 | + new MenuItem() |
| 75 | + { |
| 76 | + Text = "item 1.2.1", |
| 77 | + }, |
| 78 | + new MenuItem() |
| 79 | + { |
| 80 | + Text = "item 1.2.2", |
| 81 | + } |
| 82 | + } |
| 83 | + }, |
| 84 | + new MenuItem() |
| 85 | + { |
| 86 | + Text ="item 1.3", |
| 87 | + Items = new List<MenuItem>() |
| 88 | + { |
| 89 | + new MenuItem() |
| 90 | + { |
| 91 | + Text = "item 1.3.1", |
| 92 | + }, |
| 93 | + new MenuItem() |
| 94 | + { |
| 95 | + Text = "item 1.3.2", |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + new MenuItem() |
| 102 | + { |
| 103 | + Text = "item 2", |
| 104 | + Items = new List<MenuItem>() |
| 105 | + { |
| 106 | + new MenuItem() |
| 107 | + { |
| 108 | + Text = "item 2.1", |
| 109 | + }, |
| 110 | + new MenuItem() |
| 111 | + { |
| 112 | + Text = "item 2.2", |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + }; |
| 117 | +
|
| 118 | + base.OnInitialized(); |
| 119 | + } |
| 120 | +
|
| 121 | + public class MenuItem |
| 122 | + { |
| 123 | + public string Text { get; set; } |
| 124 | + public string Url { get; set; } |
| 125 | + public List<MenuItem> Items { get; set; } |
| 126 | + } |
| 127 | +} |
| 128 | +```` |
| 129 | + |
| 130 | +## Next Steps |
| 131 | + |
| 132 | +* [Explore the Menu Templates]({%slug components/menu/templates%}) |
| 133 | + |
| 134 | +## See Also |
| 135 | + |
| 136 | +* [Menu Overview]({%slug components/menu/overview%}) |
0 commit comments