Skip to content

Commit a2b42a0

Browse files
committed
Add context menu example
1 parent 577fbdc commit a2b42a0

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@using AstroPanda.Blazor.Toolkit.Services
2+
<MudPaper>
3+
<MudButton OnClick="Close">Close</MudButton>
4+
</MudPaper>
5+
6+
@code {
7+
[Inject]
8+
private IContextMenuService _contextService { get; set; }
9+
10+
[Inject]
11+
private ISnackbar _snackbar { get; set; }
12+
13+
private void Close()
14+
{
15+
_snackbar.Add("Close Pressed");
16+
_contextService.Close();
17+
}
18+
}

src/BlazorToolkit.Wasm/Layout/MainLayout.razor

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
@inherits LayoutComponentBase
22
@using AstroPanda.Blazor.Toolkit
3+
@using AstroPanda.Blazor.Toolkit.Services
34
@using BlazorToolkit.Wasm.Components
5+
@using AstroPanda.Blazor.Toolkit.Components
46
@using MudBlazor
57

68
<MyMudThemeProvider />
79
<MyMudProviders />
8-
10+
<ContextMenuProvider @ref="cxt"/>
11+
<div class="page" @onclick="CloseContextMenus">
912
<MudLayout>
1013
<MudAppBar Elevation="0">
1114
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((e) => DrawerToggle())" />
@@ -24,18 +27,32 @@
2427
</MudDrawerHeader>
2528
<NavMenu />
2629
</MudDrawer>
27-
<MudMainContent>
30+
<MudMainContent >
31+
<div @oncontextmenu="OpenContextMenu" @oncontextmenu:preventDefault @oncontextmenu:stopPropagation>
2832
<MudContainer MaxWidth="MaxWidth.Large" Class="my-16 pt-16">
2933
@Body
3034
</MudContainer>
35+
</div>
3136
</MudMainContent>
3237
</MudLayout>
38+
</div>
3339

3440
@code {
41+
[Inject]
42+
private IContextMenuService _contextService { get; set; }
43+
3544
bool _drawerOpen = true;
45+
private ContextMenuProvider cxt { get; set; }
3646

3747
void DrawerToggle()
3848
{
3949
_drawerOpen = !_drawerOpen;
4050
}
51+
52+
private void CloseContextMenus() => cxt.CloseAll();
53+
54+
private void OpenContextMenu(MouseEventArgs e)
55+
{
56+
_contextService.Open<ExampleContextMenu>(e.ClientX, e.ClientY);
57+
}
4158
}

0 commit comments

Comments
 (0)