Skip to content

Commit 199e68b

Browse files
committed
fix svg links in solid router
1 parent 2852acb commit 199e68b

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

e2e/solid-router/basic/src/main.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Link,
66
Outlet,
77
RouterProvider,
8+
createLink,
89
createRootRoute,
910
createRoute,
1011
createRouter,
@@ -28,6 +29,7 @@ const rootRoute = createRootRoute({
2829
})
2930

3031
function RootComponent() {
32+
const SvgLink = createLink('svg')
3133
return (
3234
<>
3335
<HeadContent />
@@ -75,7 +77,7 @@ function RootComponent() {
7577
<div class="flex items-center">
7678
<svg width="20" height="20" viewBox="0 0 20 20" role="img">
7779
<title id="rectTitle">Link in SVG</title>
78-
<Link to="/posts" aria-label="Open posts from SVG">
80+
<SvgLink to="/posts" aria-label="Open posts from SVG">
7981
<rect
8082
x="0"
8183
y="0"
@@ -85,7 +87,7 @@ function RootComponent() {
8587
fill="blue"
8688
stroke-width="2"
8789
/>
88-
</Link>
90+
</SvgLink>
8991
</svg>
9092
</div>
9193
</div>

e2e/solid-router/basic/tests/app.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ test('Link in SVG does not trigger a full page reload', async ({ page }) => {
6161
await page.waitForURL(url)
6262

6363
expect(fullPageLoad).toBeFalsy()
64-
})
64+
})

packages/solid-router/src/link.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ export function useLinkProps<
281281
// The click handler
282282
const handleClick = (e: MouseEvent) => {
283283
// Check actual element's target attribute as fallback
284-
const elementTarget = (e.currentTarget as HTMLAnchorElement).target
284+
const elementTarget = (
285+
e.currentTarget as HTMLAnchorElement | SVGAElement
286+
).getAttribute('target')
285287
const effectiveTarget =
286288
local.target !== undefined ? local.target : elementTarget
287289

@@ -587,6 +589,15 @@ export const Link: LinkComponent<'a'> = (props) => {
587589
return ch satisfies Solid.JSX.Element
588590
})
589591

592+
if (local._asChild === 'svg') {
593+
const [_, svgLinkProps] = Solid.splitProps(linkProps, ['class'])
594+
return (
595+
<svg>
596+
<a {...svgLinkProps}>{children()}</a>
597+
</svg>
598+
)
599+
}
600+
590601
return (
591602
<Dynamic component={local._asChild ? local._asChild : 'a'} {...linkProps}>
592603
{children()}

0 commit comments

Comments
 (0)