Skip to content

KeyInterceptorService: Allow omitting TargetClass to attach event handlers to element itself#12377

Merged
ScarletKuro merged 2 commits intoMudBlazor:devfrom
JMolenkamp:feature/allow-attaching-key-event-handlers-to-element-itself
Jan 2, 2026
Merged

KeyInterceptorService: Allow omitting TargetClass to attach event handlers to element itself#12377
ScarletKuro merged 2 commits intoMudBlazor:devfrom
JMolenkamp:feature/allow-attaching-key-event-handlers-to-element-itself

Conversation

@JMolenkamp
Copy link
Contributor

@JMolenkamp JMolenkamp commented Jan 1, 2026

Accept KeyInterceptorOptions.TargetClass being null to allow for attaching the key event handlers to the subscribed element itself instead of having to attach it to child elements having some class.

For context, I want to stop propagation for the keydowns that I'm interested in, but let events for other keys bubble up. As far as i know, this is not possible without JS interop. This service provides exactly that functionality, except for it not being possible to simply attach to the subscribed element itself.

Checklist:

  • I've read the contribution guidelines
  • My code follows the style of this project
  • I've added or updated relevant unit tests

@JMolenkamp JMolenkamp changed the title KeyInterceptorService: Allow omitting KeyInterceptorOptions.TargetClass to attach event handlers to element itself KeyInterceptorService: Allow omitting KeyInterceptorOptions.TargetClass to attach event handlers to element itself Jan 1, 2026
@mudbot mudbot bot added the enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library label Jan 1, 2026
@ScarletKuro ScarletKuro changed the title KeyInterceptorService: Allow omitting KeyInterceptorOptions.TargetClass to attach event handlers to element itself KeyInterceptorService: Allow omitting TargetClass to attach event handlers to element itself Jan 1, 2026
@ScarletKuro ScarletKuro requested a review from Copilot January 1, 2026 21:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the KeyInterceptorService to allow attaching event handlers directly to the subscribed element itself, rather than requiring attachment to child elements with a specific CSS class. This is achieved by making the TargetClass property nullable and handling the null case appropriately.

Key Changes:

  • Made KeyInterceptorOptions.TargetClass nullable to support attaching handlers to the element itself
  • Updated JavaScript logic to conditionally attach handlers based on whether TargetClass is null or defined
  • Added a new constructor overload for KeyInterceptorOptions that accepts only key options (defaulting TargetClass to null)

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/MudBlazor/TScripts/mudKeyInterceptor.js Removed validation requiring targetClass and added conditional logic to attach handlers to the element itself when targetClass is null
src/MudBlazor/Services/KeyInterceptor/KeyInterceptorOptions.cs Made TargetClass nullable, added sealed modifier to class, created new constructor overload accepting only key options, and updated documentation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ScarletKuro
Copy link
Member

Looks good, just one question, we won't have any problems here when it's null?

var targetClass = self._options.targetClass;
for (const mutation of mutationsList) {
//self.logger('[MudBlazor | KeyInterceptor] Subtree mutation: ', { mutation });
for (const element of mutation.addedNodes) {
if (element.classList && element.classList.contains(targetClass))
self.attachHandlers(element);
}
for (const element of mutation.removedNodes) {
if (element.classList && element.classList.contains(targetClass))
self.detachHandlers(element);
}
}

@JMolenkamp
Copy link
Contributor Author

Guess I was cutting corners a bit with my testing.
I'll provide an example in MudBlazor.UnitTests.Viewer, like I did for #12376, and make sure to test this.

Joost Molenkamp and others added 2 commits January 2, 2026 13:23
@JMolenkamp JMolenkamp force-pushed the feature/allow-attaching-key-event-handlers-to-element-itself branch from 1fb2190 to fd50146 Compare January 2, 2026 12:24
@ScarletKuro ScarletKuro merged commit 55c5457 into MudBlazor:dev Jan 2, 2026
5 of 6 checks passed
@JMolenkamp
Copy link
Contributor Author

Looks good, just one question, we won't have any problems here when it's null?

Will not be a problem, but could be improved upon, see #12380.

This was referenced Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants