-
Notifications
You must be signed in to change notification settings - Fork 8
/
AriaPopupExtensions.cs
63 lines (57 loc) · 2.13 KB
/
AriaPopupExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Microsoft.AspNetCore.Components.Web;
namespace Ignis.Components.HeadlessUI.Aria;
internal static class AriaPopupExtensions
{
public static void SetActiveDescendant(this IAriaPopup popup, IAriaDescendant descendant, bool isActive)
{
if (descendant == null) throw new ArgumentNullException(nameof(descendant));
if (isActive)
{
popup.ActiveDescendant = descendant;
}
else if (popup.ActiveDescendant == descendant)
{
popup.ActiveDescendant = null;
}
}
public static void OnKeyDown(this IAriaPopup popup, KeyboardEventArgs eventArgs)
{
var descendants = popup.Descendants.ToArray();
switch (eventArgs.Code)
{
case "Escape":
popup.Close();
break;
case "Space" or "Enter":
if (popup.IsOpen)
{
if (popup.ActiveDescendant != null) popup.ActiveDescendant.Click();
else popup.Close();
}
else
{
popup.Open();
}
break;
case "ArrowUp" when popup.ActiveDescendant == null:
case "ArrowDown" when popup.ActiveDescendant == null:
if (descendants.Any()) popup.SetActiveDescendant(descendants[0], isActive: true);
else if (!popup.IsOpen) popup.Open();
break;
case "ArrowDown":
{
var index = Array.IndexOf(descendants, popup.ActiveDescendant) + 1;
if (index < descendants.Length) popup.SetActiveDescendant(descendants[index], isActive: true);
else if (!popup.IsOpen) popup.Open();
break;
}
case "ArrowUp":
{
var index = Array.IndexOf(descendants, popup.ActiveDescendant) - 1;
if (index >= 0) popup.SetActiveDescendant(descendants[index], isActive: true);
else if (!popup.IsOpen) popup.Open();
break;
}
}
}
}