Skip to content

Commit e932a8a

Browse files
Fixes that Blazor focus extension could not focus SVG elements (#35080)
1 parent 0c14ece commit e932a8a

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

src/Components/Web.JS/dist/Release/blazor.server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/dist/Release/blazor.webview.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/src/DomWrapper.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ export const domFunctions = {
55
focusBySelector,
66
};
77

8-
function focus(element: HTMLElement, preventScroll: boolean): void {
8+
function focus(element: HTMLOrSVGElement, preventScroll: boolean): void {
99
if (element instanceof HTMLElement) {
1010
element.focus({ preventScroll });
11+
} else if (element instanceof SVGElement) {
12+
if (element.hasAttribute('tabindex')) {
13+
element.focus({ preventScroll });
14+
} else {
15+
throw new Error('Unable to focus an SVG element that does not have a tabindex.');
16+
}
1117
} else {
1218
throw new Error('Unable to focus an invalid element.');
1319
}

src/Components/test/E2ETest/Tests/ComponentRenderingTestBase.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,28 @@ public void CanUseFocusExtensionToFocusElement()
410410
long getPageYOffset() => (long)((IJavaScriptExecutor)Browser).ExecuteScript("return window.pageYOffset");
411411
}
412412

413+
[Fact]
414+
public void CanUseFocusExtensionToFocusSvgElement()
415+
{
416+
Browser.Manage().Window.Size = new System.Drawing.Size(100, 300);
417+
var appElement = Browser.MountTestComponent<SvgFocusComponent>();
418+
419+
var buttonElement = appElement.FindElement(By.Id("focus-button"));
420+
421+
// Make sure the circle isn't focused when the test begins; we don't want
422+
// the test to pass just because the circle started as the focused element
423+
Browser.NotEqual("focus-circle", getFocusedElementId);
424+
425+
// Click the button whose callback focuses the SVG element
426+
buttonElement.Click();
427+
428+
// Verify that the circle is focused
429+
Browser.Equal("focus-circle", getFocusedElementId);
430+
431+
// A local helper that gets the ID of the focused element.
432+
string getFocusedElementId() => Browser.SwitchTo().ActiveElement().GetAttribute("id");
433+
}
434+
413435
[Fact]
414436
public void CanUseFocusExtensionToFocusElementPreventScroll()
415437
{

src/Components/test/testassets/BasicTestApp/Index.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<option value="BasicTestApp.SignalRClientComponent">SignalR client</option>
9292
<option value="BasicTestApp.StringComparisonComponent">StringComparison</option>
9393
<option value="BasicTestApp.SvgComponent">SVG</option>
94+
<option value="BasicTestApp.SvgFocusComponent">SVG Focus component</option>
9495
<option value="BasicTestApp.TextOnlyComponent">Plain text</option>
9596
<option value="BasicTestApp.ToggleEventComponent">Toggle Event</option>
9697
<option value="BasicTestApp.TouchEventComponent">Touch events</option>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<svg width="100" heigth="100">
2+
<circle tabindex="-1" cx="50" cy="50" r="20" id="focus-circle" @ref="circleReference" @onfocusin="@(() => { didReceiveFocusIn = true; })"></circle>
3+
</svg>
4+
5+
<button id="focus-button" @onclick="@(() => FocusCircle(false))">Click to focus!</button>
6+
<hr />
7+
<p>Received focus in: <span id="focus-event-received">@didReceiveFocusIn</span></p>
8+
9+
@code {
10+
private ElementReference circleReference;
11+
private bool didReceiveFocusIn;
12+
13+
private async Task FocusCircle(bool preventScroll)
14+
{
15+
await circleReference.FocusAsync(preventScroll);
16+
}
17+
}

0 commit comments

Comments
 (0)