1- @inherits MenuTreeComponent
2- @inject BlazorContextMenuSettings settings
3- @inject IContextMenuStorage contextMenuStorage
4- @inject IMenuTreeTraverser menuTreeTraverser
5- @inject IInternalContextMenuHandler contextMenuHandler
6- @inject IJSRuntime jsRuntime
1+ @inherits ContextMenuBase
72@namespace BlazorContextMenu
83
94<div @attributes =" @Attributes" class =" @($" {BaseClass} {ClassCalc} {DisplayClassCalc} " )" id =" @Id" style =" @($ " left:{X}px;top:{Y}px; " )" data-autohide =" @AutoHide.ToString().ToLower()" >
138 <CascadingValue Value =" (MenuTreeComponent)this" Name =" ParentComponent" >
149 <CascadingValue Value =" @(Template != null ? Template : CascadingTemplate)" Name =" CascadingTemplate" >
1510 <CascadingValue Value =" @(Animation.HasValue ? Animation.Value : CascadingAnimation)" Name =" CascadingAnimation" >
16- @ChildContent
11+ @{
12+ var context = new MenuRenderingContext (Id , TargetId , X , Y , Trigger , Data );
13+ @ChildContent(context)
14+ }
1715 </CascadingValue >
1816 </CascadingValue >
1917 </CascadingValue >
2018 </ul >
2119 }
2220</div >
2321
24- @code {
25-
26- [Parameter (CaptureUnmatchedValues = true )]
27- public Dictionary <string , object > Attributes { get ; set ; }
28-
29- protected virtual string BaseClass => " blazor-context-menu blazor-context-menu__wrapper" ;
30-
31- /// <summary >
32- /// The id that the <see cref =" ContextMenuTrigger" /> will use to bind to. This parameter is required
33- /// </summary >
34- [Parameter ]
35- public string Id { get ; set ; }
36-
37- /// <summary >
38- /// The name of the template to use for this <see cref =" ContextMenu" /> and all its <see cref =" SubMenu" />.
39- /// </summary >
40- [Parameter ]
41- public string Template { get ; set ; }
42-
43- [CascadingParameter (Name = " CascadingTemplate" )]
44- protected string CascadingTemplate { get ; set ; }
45-
46- /// <summary >
47- /// Allows you to override the default css class of the <see cref =" ContextMenu" />'s div element, for full customization.
48- /// </summary >
49- [Parameter ]
50- public string OverrideDefaultCssClass { get ; set ; }
51-
52- /// <summary >
53- /// Allows you to override the default css class of the <see cref =" ContextMenu" />'s div element while it's shown, for full customization.
54- /// </summary >
55- [Parameter ]
56- public string OverrideDefaultShownCssClass { get ; set ; }
57-
58- /// <summary >
59- /// Allows you to override the default css class of the <see cref =" ContextMenu" />'s div element while it's hidden, for full customization.
60- /// </summary >
61- [Parameter ]
62- public string OverrideDefaultHiddenCssClass { get ; set ; }
63-
64- /// <summary >
65- /// Allows you to override the default css class of the <see cref =" ContextMenu" />'s ul element, for full customization.
66- /// </summary >
67- [Parameter ]
68- public string OverrideDefaultListCssClass { get ; set ; }
69-
70- /// <summary >
71- /// Additional css class that is applied to the <see cref =" ContextMenu" />'s div element. Use this to extend the default css.
72- /// </summary >
73- [Parameter ]
74- public string CssClass { get ; set ; }
75-
76- /// <summary >
77- /// Additional css class that is applied to the <see cref =" ContextMenu" />'s div element while is shown. Use this to extend the default css.
78- /// </summary >
79- [Parameter ]
80- public string ShownCssClass { get ; set ; }
81-
82- /// <summary >
83- /// Additional css class that is applied to the <see cref =" ContextMenu" />'s div element while is hidden. Use this to extend the default css.
84- /// </summary >
85- [Parameter ]
86- public string HiddenCssClass { get ; set ; }
87-
88- /// <summary >
89- /// Additional css class that is applied to the <see cref =" ContextMenu" />'s ul element. Use this to extend the default css.
90- /// </summary >
91- [Parameter ]
92- public string ListCssClass { get ; set ; }
93-
94- /// <summary >
95- /// Allows you to set the <see cref =" BlazorContextMenu.Animation" /> used by this <see cref =" ContextMenu" /> and all its <see cref =" SubMenu" />
96- /// </summary >
97- [Parameter ]
98- public Animation ? Animation { get ; set ; }
99-
100- /// <summary >
101- /// A handler that is triggered before the menu appears. Can be used to prevent the menu from showing.
102- /// </summary >
103- [Parameter ]
104- public EventCallback <MenuAppearingEventArgs > OnAppearing { get ; set ; }
105-
106- /// <summary >
107- /// A handler that is triggered before the menu is hidden. Can be used to prevent the menu from hiding.
108- /// </summary >
109- [Parameter ]
110- public EventCallback <MenuHidingEventArgs > OnHiding { get ; set ; }
111-
112- /// <summary >
113- /// Set to false if you want to close the menu programmatically. Default: true
114- /// </summary >
115- [Parameter ]
116- public bool AutoHide { get ; set ; } = true ;
117-
118- [CascadingParameter (Name = " CascadingAnimation" )]
119- protected Animation ? CascadingAnimation { get ; set ; }
12022
23+ @code {
12124 [Parameter ]
122- public RenderFragment ChildContent { get ; set ; }
123-
124-
125- protected bool IsShowing ;
126- protected string X { get ; set ; }
127- protected string Y { get ; set ; }
128- protected string TargetId { get ; set ; }
129- protected ContextMenuTrigger Trigger { get ; set ; }
130- internal object Data { get ; set ; }
131-
132- protected string ClassCalc
133- {
134- get
135- {
136- var template = settings .GetTemplate (GetActiveTemplate ());
137- return Helpers .AppendCssClasses ((OverrideDefaultCssClass ?? template .DefaultCssOverrides .MenuCssClass ),
138- (CssClass ?? template .MenuCssClass ));
139- }
140- }
141-
142- protected Animation GetActiveAnimation ()
143- {
144- var animation = CascadingAnimation ;
145- if (this .Animation != null )
146- {
147- animation = this .Animation ;
148- }
149- if (animation == null )
150- {
151- animation = settings .GetTemplate (GetActiveTemplate ()).Animation ;
152- }
153-
154- return animation .Value ;
155- }
156-
157- internal string GetActiveTemplate ()
158- {
159- var templateName = CascadingTemplate ;
160- if (Template != null )
161- {
162- templateName = Template ;
163- }
164- if (templateName == null )
165- {
166- templateName = BlazorContextMenuSettings .DefaultTemplateName ;
167- }
168-
169- return templateName ;
170- }
171-
172- protected string DisplayClassCalc
173- {
174- get
175- {
176- var template = settings .GetTemplate (GetActiveTemplate ());
177- var (showingAnimationClass , hiddenAnimationClass ) = GetAnimationClasses (GetActiveAnimation ());
178- return IsShowing ?
179- Helpers .AppendCssClasses (OverrideDefaultShownCssClass ?? template .DefaultCssOverrides .MenuShownCssClass ,
180- showingAnimationClass ,
181- ShownCssClass ?? settings .GetTemplate (GetActiveTemplate ()).MenuShownCssClass ) :
182- Helpers .AppendCssClasses (OverrideDefaultHiddenCssClass ?? template .DefaultCssOverrides .MenuHiddenCssClass ,
183- hiddenAnimationClass ,
184- HiddenCssClass ?? settings .GetTemplate (GetActiveTemplate ()).MenuHiddenCssClass );
185- }
186- }
187- protected string ListClassCalc
188- {
189- get
190- {
191- var template = settings .GetTemplate (GetActiveTemplate ());
192- return Helpers .AppendCssClasses ((OverrideDefaultListCssClass ?? template .DefaultCssOverrides .MenuListCssClass ),
193- (ListCssClass ?? settings .GetTemplate (GetActiveTemplate ()).MenuListCssClass ));
194- }
195- }
196-
197- protected (string showingClass , string hiddenClass ) GetAnimationClasses (Animation animation )
198- {
199- switch (animation )
200- {
201- case BlazorContextMenu .Animation .None :
202- return (" " , " " );
203- case BlazorContextMenu .Animation .FadeIn :
204- return (" blazor-context-menu__animations--fadeIn-shown" , " blazor-context-menu__animations--fadeIn" );
205- case BlazorContextMenu .Animation .Grow :
206- return (" blazor-context-menu__animations--grow-shown" , " blazor-context-menu__animations--grow" );
207- case BlazorContextMenu .Animation .Slide :
208- return (" blazor-context-menu__animations--slide-shown" , " blazor-context-menu__animations--slide" );
209- case BlazorContextMenu .Animation .Zoom :
210- return (" blazor-context-menu__animations--zoom-shown" , " blazor-context-menu__animations--zoom" );
211- default :
212- throw new Exception (" Animation not supported" );
213- }
214- }
215-
216- protected override void OnInitialized ()
217- {
218- base .OnInitialized ();
219- if (string .IsNullOrEmpty (Id ))
220- {
221- throw new ArgumentNullException (nameof (Id ));
222- }
223- contextMenuStorage .Register ((ContextMenu )(object )this );
224- }
225-
226- protected override async Task OnAfterRenderAsync (bool firstRender )
227- {
228- if (! contextMenuHandler .ReferencePassedToJs )
229- {
230- await jsRuntime .InvokeAsync <object >(" blazorContextMenu.SetMenuHandlerReference" , DotNetObjectReference .Create (contextMenuHandler ));
231- contextMenuHandler .ReferencePassedToJs = true ;
232- }
233- }
234-
235- public override void Dispose ()
236- {
237- base .Dispose ();
238- contextMenuStorage .Unregister ((ContextMenu )(object )this );
239- }
240-
241- internal async Task Show (string x , string y , string targetId = null , ContextMenuTrigger trigger = null )
242- {
243-
244- if (trigger is null )
245- {
246- var rootMenu = menuTreeTraverser .GetRootContextMenu (this );
247- trigger = rootMenu ? .GetTrigger ();
248- }
249-
250- if (trigger != null )
251- {
252- Data = trigger .Data ;
253- }
254-
255- if (OnAppearing .HasDelegate )
256- {
257- var eventArgs = new MenuAppearingEventArgs (Id , targetId , x , y , trigger , Data );
258- await OnAppearing .InvokeAsync (eventArgs );
259- x = eventArgs .X ;
260- y = eventArgs .Y ;
261- if (eventArgs .PreventShow )
262- {
263- return ;
264- }
265- }
266-
267- IsShowing = true ;
268- X = x ;
269- Y = y ;
270- TargetId = targetId ;
271- Trigger = trigger ;
272- await InvokeAsync (() => StateHasChanged ());
273- }
274-
275- internal async Task <bool > Hide ()
276- {
277- if (OnHiding .HasDelegate )
278- {
279- var eventArgs = new MenuHidingEventArgs (Id , TargetId , X , Y , Trigger , Data );
280- await OnHiding .InvokeAsync (eventArgs );
281- if (eventArgs .PreventHide )
282- {
283- return false ;
284- }
285- }
286-
287- IsShowing = false ;
288- await InvokeAsync (() => StateHasChanged ());
289- return true ;
290- }
291-
292- internal string GetTarget ()
293- {
294- return TargetId ;
295- }
296-
297- internal ContextMenuTrigger GetTrigger ()
298- {
299- return Trigger ;
300- }
301-
25+ public RenderFragment <MenuRenderingContext > ChildContent { get ; set ; }
30226}
0 commit comments