[Blazor] Add focus and blur events to handle all child elements.
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
I’m trying to create a context menu that automatically hides once the menu has lost focus. Currently Blazor only exposes onblur
and onfocus
events which are bound to the element but not its children. This means that if an item within the menu is clicked, the onblur
event is called for the parent menu which is not intended behaviour. The only way I can achieve this currently, is to use JSInterop
which is undesirable.
Describe the solution you’d like
Two new events introduced: @OnFocusWithin
& @OnBlurWithin
.
OnFocusWithin
firing if any element in the element tree (the parent or any nested children) is focused.
OnBlurWithin
firing if all elements in the element tree (the parent or any nested children) are unfocused and any element within the tree is blurred.
Additional context
I don’t feel strongly about the event names.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:8 (3 by maintainers)
Top GitHub Comments
Hey @KieranDevvs,
You can achieve this using the
@onfocusin
and@onfocusout
events as they bubble up.@onfocus
and@onblur
specifically doesn’t bubble up.Example:
I used this method prior to posting this issue and found that it was quite janky. It would be much nicer to have Blazor support more DOM events, but with the
JSModule
feature in the later versions of .NET, it has made working around these kinds of problems far simpler.