Introduce KeyInterceptorService withs sync/async support to replace KeyInterceptor #9896
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #9896 +/- ##
==========================================
+ Coverage 89.82% 90.93% +1.10%
==========================================
Files 412 411 -1
Lines 11878 12863 +985
Branches 2364 2486 +122
==========================================
+ Hits 10670 11697 +1027
+ Misses 681 618 -63
- Partials 527 548 +21 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
551c154 to
36b4166
Compare
henon
left a comment
There was a problem hiding this comment.
Awesome. I'll add the removal of KeyInterceptor to the v8 project.
var keyInterceptorOptions = KeyInterceptorOptions.Create(
targetClass: "mud-input-control",
enableLogging: true,
keys:
[
// prevent scrolling page, toggle open/close
KeyOptions.Of(key: " ", preventDown: "key+none"),
// prevent scrolling page, instead highlight previous item
KeyOptions.Of(key: "ArrowUp", preventDown: "key+none"),
// prevent scrolling page, instead highlight next item
KeyOptions.Of(key: "ArrowDown", preventDown: "key+none"),
KeyOptions.Of(key: "Home", preventDown: "key+none"),
KeyOptions.Of(key: "End", preventDown: "key+none"),
KeyOptions.Of(key: "Escape"),
KeyOptions.Of(key: "Enter", preventDown: "key+none"),
KeyOptions.Of(key: "NumpadEnter", preventDown: "key+none"),
// select all items instead of all page text
KeyOptions.Of(key: "a", preventDown: "key+ctrl"),
// select all items instead of all page text
KeyOptions.Of(key: "A", preventDown: "key+ctrl"),
// for our users
KeyOptions.Of(key: "/./", subscribeDown: true, subscribeUp: true)
]);into: var keyInterceptorOptions = new KeyInterceptorOptions(
targetClass: "mud-input-control",
enableLogging: true,
// prevent scrolling page, toggle open/close
new(" ", preventDown: "key+none"),
// prevent scrolling page, instead highlight previous item
new("ArrowUp", preventDown: "key+none"),
// prevent scrolling page, instead highlight next item
new("ArrowDown", preventDown: "key+none"),
new("Home", preventDown: "key+none"),
new("End", preventDown: "key+none"),
new("Escape"),
new("Enter", preventDown: "key+none"),
new("NumpadEnter", preventDown: "key+none"),
// select all items instead of all page text
new("a", preventDown: "key+ctrl"),
// select all items instead of all page text
new("A", preventDown: "key+ctrl"),
// for our users
new("/./", subscribeDown: true, subscribeUp: true)
);Is this feasible in reality? |
I made |
I don't think I understand this one. var keyInterceptorOptions = new KeyInterceptorOptions(
targetClass: "mud-input-control",
enableLogging: true,
new(" ", preventDown: "key+none"),
new("ArrowUp", preventDown: "key+none"),
);without the Or you just want to add two constructors to |
I remember a case where we needed to change the interceptor options at runtime. It was the PR #3393. |
Currently it's not supported, not in the old and new implementation, I event explicitly mentioned that in remarks. If there is no urgent need for it I wouldn't implement it, it's possible to do it but keeping track of settings is not trivial code and you can see that in the browserviewportservice. |
|
Hmm, strange. I thought the Close on ESC needed that update feature because it was added for it. Edit: If you look at the #3393 description you see that it modifies the options because otherwise the ESC on an open Select would also close the dialog. |
You are mixing the interceptor settings that were moved to separate But the KeyOptions still can be immutable in this case since you just create a new instance with same key but different options. We just need to wait for a major version to do so when old interceptor is removed.
|
Right, I see what you mean about the optional parameters. Maybe making public KeyOptions(
string? key,
bool subscribeDown = false,
bool subscribeUp = false,
string preventDown = "none",
string preventUp = "none",
string stopDown = "none",
string stopUp = "none")
{
Key = key;
PreventDown = preventDown;
PreventUp = preventUp;
SubscribeDown = subscribeDown;
SubscribeUp = subscribeUp;
StopDown = stopDown;
StopUp = stopUp;
}/// <summary>
/// Configuration options for key interception.
/// </summary>
public class KeyInterceptorOptions
{
/// <summary>
/// The CSS class of the target HTML element that should be observed for keyboard events.
/// </summary>
/// <remarks>
/// Note: This must be a single class name.
/// </remarks>
public string TargetClass { get; }
/// <summary>
/// Specifies whether resize events should be logged in the browser's console.
/// </summary>
public bool EnableLogging { get; }
/// <summary>
/// A list of key options that define the keys to intercept and their respective configurations.
/// </summary>
public IReadOnlyList<KeyOptions> Keys { get; }
/// <summary>
/// Creates a new instance of <see cref="KeyInterceptorOptions"/> with the specified parameters.
/// </summary>
/// <param name="targetClass">The CSS class of the target HTML element.</param>
/// <param name="keys">An array of <see cref="KeyOptions"/> defining the keys to intercept and their configurations.</param>
/// <param name="enableLogging">Specifies whether to enable logging of resize events in the browser's console.</param>
/// <returns>A new instance of <see cref="KeyInterceptorOptions"/>.</returns>
public KeyInterceptorOptions(
string targetClass,
KeyOptions[] keys,
bool enableLogging = false)
{
TargetClass = targetClass;
Keys = keys;
EnableLogging = enableLogging;
}
}-protected override async Task OnAfterRenderAsync(bool firstRender)
-{
- if (firstRender)
- {
- var keyInterceptorOptions = KeyInterceptorOptions.Create(
- targetClass: "mud-switch-base",
- keys:
- [
- // prevent scrolling page, instead increment
- KeyOptions.Of(key: "ArrowUp", preventDown: "key+none"),
- // prevent scrolling page, instead decrement
- KeyOptions.Of(key: "ArrowDown", preventDown: "key+none"),
- KeyOptions.Of(key: " ", preventDown: "key+none", preventUp: "key+none")
- ]);
- await KeyInterceptorService.SubscribeAsync(_elementId, keyInterceptorOptions, keyDown: HandleKeyDown);
- }
- await base.OnAfterRenderAsync(firstRender);
-}
+protected override async Task OnAfterRenderAsync(bool firstRender)
+{
+ if (firstRender)
+ {
+ var keyInterceptorOptions = new KeyInterceptorOptions(
+ "mud-switch-base",
+ [
+ // prevent scrolling page, instead increment
+ new("ArrowUp", preventDown: "key+none"),
+ // prevent scrolling page, instead decrement
+ new("ArrowDown", preventDown: "key+none"),
+ new(" ", preventDown: "key+none", preventUp: "key+none")
+ ]
+ );
+ await KeyInterceptorService.SubscribeAsync(_elementId, keyInterceptorOptions, keyDown: HandleKeyDown);
+ }
+ await base.OnAfterRenderAsync(firstRender);
+}See previous edit for optional targetClass |
|
@danielchalmers done, tho I ended up adding several constructors to var keyInterceptorOptions = new KeyInterceptorOptions(
targetClass: "mud-input-control",
new(" ", preventDown: "key+none"),
new("ArrowUp", preventDown: "key+none"),
);and var keyInterceptorOptions = new KeyInterceptorOptions(
targetClass: "mud-input-control",
enableLogging: true,
new(" ", preventDown: "key+none"),
new("ArrowUp", preventDown: "key+none"),
);also If there are no other suggestion then this can be merged. |
danielchalmers
left a comment
There was a problem hiding this comment.
If there are no other suggestion then this can be merged.
Not able to do a deep dive but it looks great! So glad to see async handlers along with a streamlined and opinionated API that can move accessibility implementations forward. Great work as always
|
Added to v8.0.0 Migration Guide #9953 |
|
Ah, I bamboozled myself. |
Description
Fixes: #8992
Fixes: #8583
NB! The old service is not removed, just slightly refactored. I don't think we should remove it, just obsolete and remove in next major release.
The API is quite flexible, that's how you can use it:
View
MudDialogInstance.razor.csas example:MudBlazor/src/MudBlazor/Components/Dialog/MudDialogInstance.razor.cs
Lines 83 to 86 in ba830b1
How Has This Been Tested?
Fully tested with unit tests
Type of Changes
Checklist
dev).