Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stable-svg-siblings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/charts': patch
---

Avoid rebuilding child identity maps during non-animated SVG updates when sibling order is unchanged.
22 changes: 22 additions & 0 deletions packages/charts-core/src/reconcile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@ import { describe, expect, it, vi } from 'vitest'
import { reconcileChartSvg, reconcileChartSvgFragment } from './reconcile'

describe('keyed SVG reconciliation', () => {
it('preserves mixed keyed and unkeyed siblings through stable updates and reordering', () => {
const container = document.createElement('div')
const group = (value: number) =>
`<svg><g><circle data-ts-key="a" cx="${value}"/><rect width="${value}"/><circle cx="${value}"/><circle data-ts-key="" cx="${value}"/></g></svg>`
reconcileChartSvg(container, group(1))
const children = [...container.querySelector('g')!.children]
reconcileChartSvg(container, group(2))
expect([...container.querySelector('g')!.children]).toEqual(children)
expect(children[0].getAttribute('cx')).toBe('2')
expect(children[1].getAttribute('width')).toBe('2')
reconcileChartSvg(
container,
'<svg><g><rect width="3"/><circle data-ts-key="a" cx="3"/><circle cx="3"/><circle data-ts-key="" cx="3"/></g></svg>',
)
expect([...container.querySelector('g')!.children]).toEqual([
children[1],
children[0],
children[2],
children[3],
])
})

it.each([
['<tspan data-ts-key="a">old</tspan>', 'new'],
['old', '<tspan data-ts-key="a">new</tspan>'],
Expand Down
32 changes: 32 additions & 0 deletions packages/charts-core/src/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,38 @@ function reconcileElement(

const currentChildren = [...current.children]
const nextChildren = [...next.children]
// Stable sibling order needs no identity maps. Keep ambiguous keys, text,
// and animated updates on the general reconciliation path.
if (
!tweens &&
currentChildren.length === nextChildren.length &&
current.childNodes.length === currentChildren.length &&
next.childNodes.length === nextChildren.length
) {
const seen = new Set<string>()
let same = true
for (let index = 0; index < currentChildren.length; index++) {
const previous = currentChildren[index]
const following = nextChildren[index]
const key = previous.getAttribute('data-ts-key')
if (
previous.namespaceURI !== following.namespaceURI ||
previous.localName !== following.localName ||
key !== following.getAttribute('data-ts-key') ||
(key && seen.has(key))
) {
same = false
break
}
if (key) seen.add(key)
}
if (same) {
for (let index = 0; index < currentChildren.length; index++) {
reconcileElement(currentChildren[index], nextChildren[index], tweens)
}
return
}
}
const currentByIdentity = indexChildren(currentChildren)
const nextIdentities = identities(nextChildren)
const unmatched = new Set(currentChildren)
Expand Down
Loading