import { createChartRuntime } from './runtime' import { resolveChartHostTabIndex } from './adapter-shared' import { createDomTextMeasurer } from './dom-text' import { findNearestPoint, viewportInteractionPoints } from './scene' import { focusDisabled } from './focus-disabled' import { nearestPoint } from './nearest' import { chartPointFromNavigationOrder, chartPointFromSceneOrder, resolveChartFocusStrategy, resolveChartPointerFocus, restoreChartFocusPoint, sameChartPointIdentity, } from './interaction' import { createChartCursorHostSession, type ChartCursorHostSession, } from './cursor-host-contract' import type { ChartInteractionController, ChartLayerRenderer, ChartPointerResolution, ChartRenderer, ChartRendererHost, ChartRendererHostOptions, ChartHostControlExtension, ChartHostControlInstance, ChartSurface, ChartTooltipExtension, ChartTooltipExtensionInstance, } from './dom-types' import type { ChartAnimationOptions, ChartCursorBinding, ChartCursorPresentation, ChartFocusMode, ChartFocusSource, ChartFocusState, ChartFocusStrategy, ChartHostControl, ChartPoint, ChartRuntime, ChartScene, ChartSpatialIndex, ChartTooltipInput, ChartTooltipOptions, ChartTooltipPosition, ChartMotionTransition, ChartValue, SceneNode, ChartMarkRenderer, } from './types' export function resolveChartRenderer( scene: ChartScene, defaultRenderer: ChartRenderer, ): ChartRenderer { const renderer = findLayerRenderer(scene.nodes, defaultRenderer) ?? findGuideLayerRenderer(scene.focusGuides, defaultRenderer) return renderer?.compose(defaultRenderer) ?? defaultRenderer } function findGuideLayerRenderer( guides: ChartScene['focusGuides'], defaultRenderer: ChartRenderer, ): ChartLayerRenderer | undefined { for (const guide of guides ?? []) { if ( guide.renderer !== undefined && guide.renderer !== (defaultRenderer as unknown) ) { return requiredCompositor(guide.renderer) } } return undefined } function findLayerRenderer( nodes: readonly SceneNode[], defaultRenderer: ChartRenderer, ): ChartLayerRenderer | undefined { for (const node of nodes) { if ( node.renderer !== undefined && node.renderer !== (defaultRenderer as unknown) ) { return requiredCompositor(node.renderer) } if (node.kind === 'group') { const renderer = findLayerRenderer(node.children, defaultRenderer) if (renderer) return renderer } } return undefined } function requiredCompositor( renderer: ChartMarkRenderer, ): ChartLayerRenderer { const candidate = renderer as unknown as Partial if (typeof candidate.compose !== 'function') { throw new TypeError( `Mark renderer "${renderer.id}" cannot compose chart layers`, ) } return renderer as unknown as ChartLayerRenderer } type HostRenderReason = 'update' | 'resize' | 'layout' type FocusOwner = 'pointer' | 'keyboard' | 'controlled' /** * Mounts a chart and owns the runtime until the returned host is destroyed. * Pass a runtime that already rendered initial markup to preserve renderer * handoff state across adapter prerender and DOM mounting. */ export function mountChartRenderer< TDatum, TXValue extends ChartValue = ChartValue, TYValue extends ChartValue = ChartValue, >( container: HTMLElement, initialOptions: ChartRendererHostOptions, runtime: ChartRuntime = createChartRuntime< TDatum, TXValue, TYValue >(), ): ChartRendererHost { resolveTooltipInput(initialOptions.definition.tooltip) let options = initialOptions let scene!: ChartScene let interactionScene!: ChartScene let focusedPoint: ChartPoint | null = null let notifiedPoints: readonly ChartPoint[] = [] let focusSource: ChartFocusSource = 'pointer' let emphasisFocus: ChartFocusState | undefined let focusOwner: FocusOwner | null = null let pointerPosition: ChartTooltipPosition | null = null let pinnedKey: string | null = null let observer: ResizeObserver | undefined let observedContentSize: { width: number | undefined; height: number | undefined } | undefined let measuredBounds: DOMRect | undefined let renderedWidth: number let renderedHeight: number let renderFrame: number | undefined let forceScheduledRender = false let scheduledRenderReason: Exclude | undefined let destroyed = false let hasRendered = false let surface: ChartSurface | undefined let unsubscribePresentation: (() => void) | undefined let renderingSurface = false let paintingFocus = false let tooltipExtension: ChartTooltipExtension | undefined let tooltipInstance: ChartTooltipExtensionInstance | undefined let suppressNextSurfaceFocus = false let spatialIndex: ChartSpatialIndex | undefined const controlInstances = new Map< string, { extension: ChartHostControlExtension instance: ChartHostControlInstance } >() let cursorSession: ChartCursorHostSession | undefined let cursorMode: ChartCursorBinding['mode'] | undefined let cursorMatch: 'x' | 'y' | 'xy' | undefined let cursorExtension: ChartCursorBinding['use'] | undefined let renderedCursorBinding: ChartCursorBinding | undefined let unsubscribeCursor: (() => void) | undefined let cursorPresentation: ChartCursorPresentation | null = null let hasCursorPresentation = false const previousPosition = container.style.position const view = container.ownerDocument.defaultView const computedPosition = view?.getComputedStyle(container).position const ownsPosition = !computedPosition || computedPosition === 'static' const domText = createDomTextMeasurer(container) const fontSet = container.ownerDocument.fonts if (ownsPosition) container.style.position = 'relative' const render = (refreshText = false, reason: HostRenderReason = 'update') => { if (destroyed) return if (refreshText && !options.measureText) domText.refresh() const previousFocusedPoint = focusedPoint const previousCursorPresentation = cursorPresentation const previousCursorBinding = renderedCursorBinding scene = createHostedScene(createScene()) renderedWidth = scene.width renderedHeight = scene.height interactionScene = scene const renderer = resolveChartRenderer(scene, options.renderer) if (!surface) { surface = renderer.mount(container, scheduleRender) subscribeToPresentation() } else if (surface.renderer !== renderer) { unsubscribePresentation?.() unsubscribePresentation = undefined destroyTooltip() destroyHostControls() surface.destroy() container.replaceChildren() surface = renderer.mount(container, scheduleRender) subscribeToPresentation() hasRendered = false } renderingSurface = true try { surface.render(scene, { ariaLabel: options.ariaLabel, ariaDescription: options.ariaDescription, className: options.className, tabIndex: resolveChartHostTabIndex( options.definition, options.tabIndex, ), idPrefix: options.idPrefix, animation: hasRendered ? resolveAnimation(options.definition.svgAnimation, container, reason) : undefined, }) } finally { renderingSurface = false } syncHostControls() hasRendered = true const presentedPoints = interactionPoints() spatialIndex = options.definition.focus === false ? undefined : options.definition.spatialIndex?.( viewportInteractionPoints(scene, scene.points), { scene }, ) const nextCursorBinding = cursorBinding() renderedCursorBinding = nextCursorBinding if (nextCursorBinding) { applyCursorState(true) } else if (previousCursorBinding) { cursorPresentation = null focusedPoint = null pinnedKey = null pointerPosition = null focusOwner = null paintFocus(null, []) if (previousFocusedPoint) { notifyFocus(null, []) } } else { cursorPresentation = null const trackedPointer = (focusOwner === 'pointer' || (focusOwner === 'controlled' && focusSource === 'pointer')) && pinnedKey === null ? pointerPosition : null const nextFocusedPoints = trackedPointer ? resolvePointerFocus(trackedPointer.x, trackedPointer.y, maxDistance()) : previousFocusedPoint ? (() => { const restored = restoreChartFocusPoint( presentedPoints, previousFocusedPoint, ) return restored ? focusPointsForPoint(restored, presentedPoints) : [] })() : [] const nextFocusedPoint = nextFocusedPoints[0] ?? null focusedPoint = nextFocusedPoint if (!nextFocusedPoint) pinnedKey = null if ( previousFocusedPoint || nextFocusedPoint || previousCursorPresentation ) { if (!trackedPointer) focusSource = 'restored' paintFocus(nextFocusedPoint, nextFocusedPoints) notifyFocus(nextFocusedPoint, nextFocusedPoints) } } const onRender = options.onRender if (onRender) { onRender({ container, scene, surface, interaction }) } } const observesContainerWidth = () => options.width === undefined const observesContainerHeight = () => options.height === undefined && !isPositiveFiniteNumber(options.aspectRatio) const currentContainerContentSize = (bounds: DOMRect | undefined) => { const styles = view?.getComputedStyle(container) const paddingWidth = cssPixelLength(styles?.paddingLeft) + cssPixelLength(styles?.paddingRight) const paddingHeight = cssPixelLength(styles?.paddingTop) + cssPixelLength(styles?.paddingBottom) const borderWidth = cssPixelLength(styles?.borderLeftWidth) + cssPixelLength(styles?.borderRightWidth) const borderHeight = cssPixelLength(styles?.borderTopWidth) + cssPixelLength(styles?.borderBottomWidth) return { width: contentBoxDimension( styles?.width, styles?.boxSizing, bounds?.width, paddingWidth, borderWidth, ), height: contentBoxDimension( styles?.height, styles?.boxSizing, bounds?.height, paddingHeight, borderHeight, ), } } const currentSize = () => { const measureWidth = observesContainerWidth() const measureHeight = observesContainerHeight() const bounds = (measureWidth || measureHeight) && !observedContentSize ? container.getBoundingClientRect() : undefined if (bounds) measuredBounds = bounds const contentSize = measureWidth || measureHeight ? (observedContentSize ?? currentContainerContentSize(bounds)) : undefined const width = options.width ?? contentSize?.width return { width, height: measureHeight ? contentSize?.height : undefined, } } const responsiveSizeChanged = () => { const next = currentSize() return ( (observesContainerWidth() && next.width !== undefined && next.width !== renderedWidth) || (observesContainerHeight() && next.height !== undefined && next.height !== renderedHeight) ) } const configureObserver = () => { observer?.disconnect() observer = undefined if (observedContentSize) measuredBounds = container.getBoundingClientRect() observedContentSize = undefined if (!observesContainerWidth() && !observesContainerHeight()) return const ResizeObserverConstructor = view?.ResizeObserver if (!ResizeObserverConstructor) return observer = new ResizeObserverConstructor((entries) => { const entry = entries.find((candidate) => candidate.target === container) ?? entries[0] if (entry && !observedContentSize && measuredBounds) { const bounds = container.getBoundingClientRect() const content = currentContainerContentSize(bounds) // The first observer report can be more precise than CSSOM. Adopt // that precision only when both layout and CSS content size stayed // unchanged since rendering. Later resizes are compared exactly. if ( bounds.width === measuredBounds.width && content.width === scene.width ) { renderedWidth = positiveFiniteNumber(entry.contentRect.width) ?? renderedWidth } if ( bounds.height === measuredBounds.height && content.height === scene.height ) { renderedHeight = positiveFiniteNumber(entry.contentRect.height) ?? renderedHeight } } observedContentSize = entry ? { width: positiveFiniteNumber(entry.contentRect.width), height: positiveFiniteNumber(entry.contentRect.height), } : undefined if (!responsiveSizeChanged()) return scheduleRender(false, 'resize') }) observer.observe(container) } const scheduleRender = ( force = false, reason: Exclude = 'layout', ) => { forceScheduledRender ||= force scheduledRenderReason = scheduledRenderReason === 'layout' || reason === 'layout' ? 'layout' : 'resize' if (renderFrame !== undefined) return if (!view?.requestAnimationFrame) { const shouldRender = forceScheduledRender || responsiveSizeChanged() forceScheduledRender = false const nextReason = scheduledRenderReason ?? 'layout' scheduledRenderReason = undefined if (shouldRender) render(true, nextReason) return } renderFrame = view.requestAnimationFrame(() => { renderFrame = undefined const shouldRender = forceScheduledRender || responsiveSizeChanged() forceScheduledRender = false const nextReason = scheduledRenderReason ?? 'layout' scheduledRenderReason = undefined if (shouldRender) render(true, nextReason) }) } const handleFontLoad = () => { if (destroyed || options.measureText) return domText.invalidate() scheduleRender(true) } const cursorBinding = (): ChartCursorBinding | undefined => options.definition.cursor const cursorIsPinned = () => cursorSession?.getState()?.pinned === true const interactionIsPinned = () => pinnedKey !== null || cursorIsPinned() const configureCursorController = () => { const nextBinding = cursorBinding() if (nextBinding) hasCursorPresentation = true const nextController = nextBinding?.controller const nextMode = nextBinding?.mode const nextMatch = nextBinding?.mode === 'focus' ? (nextBinding.match ?? 'xy') : undefined if ( nextController === cursorSession?.controller && nextMode === cursorMode && nextMatch === cursorMatch && nextBinding?.use === cursorExtension ) { return } unsubscribeCursor?.() unsubscribeCursor = undefined cursorSession?.destroy() cursorSession = nextBinding ? createChartCursorHostSession(nextBinding) : undefined cursorMode = nextMode cursorMatch = nextMatch cursorExtension = nextBinding?.use unsubscribeCursor = cursorSession?.subscribe(() => { if (!destroyed && hasRendered) applyCursorState(false) }) } const applyCursorState = (notifyRestored: boolean) => { const binding = cursorBinding() if (!binding) { cursorPresentation = null return } const session = cursorSession if (!session) return const state = session.getState() if (state?.source !== 'pointer' || !session.owns(state)) { pointerPosition = null } cursorPresentation = session.resolvePresentation(scene, binding, state) const previous = focusedPoint if (binding.mode === 'focus') { const focus = resolveChartFocusStrategy(options.definition.focus) const points = session.resolveFocus( interactionPoints(), binding, state, focus, ) const point = points[0] ?? null if (state) focusSource = state.source pinnedKey = state?.pinned && point ? point.key : null focusedPoint = point paintFocus(point, points) if ( !sameChartPointIdentity(previous, point) || (notifyRestored && (previous !== null || point !== null)) ) { notifyFocus(point, points) } return } pinnedKey = null focusedPoint = null paintFocus(null, []) if (previous) { notifyFocus(null, []) } } const publishFocusCursor = ( points: readonly ChartPoint[], pinned = interactionIsPinned(), ) => { const binding = cursorBinding() if (binding?.mode !== 'focus') return false const session = cursorSession if (!session) return false const point = points[0] if (!point) { session.clearOwnedTransient() return true } session.publish( session.createFocusState(scene, binding, { primary: point, group: points, source: focusSource, pinned, }), ) return true } const updateFocus = ( points: readonly ChartPoint[], forcePaint = false, ) => { const point = points[0] ?? null if (publishFocusCursor(points)) return if (sameChartPointIdentity(point, focusedPoint)) { focusedPoint = point if (forcePaint) paintFocus(point, points) return } focusedPoint = point paintFocus(point, points) notifyFocus(point, points) } const notifyFocus = ( point: ChartPoint | null, points: readonly ChartPoint[], ) => { const previous = notifiedPoints let groupChanged = previous.length !== points.length if (options.onFocusGroupChange && !groupChanged) { for (let index = 0; index < points.length; index++) { if (!sameFocusObservation(previous[index]!, points[index]!)) { groupChanged = true break } } } // Store the observation before callbacks, which can synchronously update us. notifiedPoints = points if (!sameFocusObservation(previous[0] ?? null, point)) { options.onFocusChange?.(point) } if (groupChanged) options.onFocusGroupChange?.(points) } const dismissTooltip = () => { const binding = cursorBinding() if (!focusedPoint && !pinnedKey && !cursorSession?.getState()) return const restoreFocus = Boolean( tooltipInstance?.contains(container.ownerDocument.activeElement), ) pinnedKey = null pointerPosition = null focusOwner = null if (binding) cursorSession?.clear() else updateFocus([]) const element = surface?.element if ( restoreFocus && element && 'focus' in element && typeof element.focus === 'function' ) { suppressNextSurfaceFocus = true element.focus() } } const paintFocus = ( point: ChartPoint | null, points: readonly ChartPoint[], updateTooltip = true, ) => { paintingFocus = true let paintedScene: ChartScene | void try { const focus = point ? { primary: point, group: points, source: focusSource, pinned: interactionIsPinned(), } : null paintedScene = emphasisFocus ? surface?.paintFocus(focus, pointerPosition, cursorPresentation, { stateFocus: emphasisFocus, }) : hasCursorPresentation ? surface?.paintFocus(focus, pointerPosition, cursorPresentation) : surface?.paintFocus(focus, pointerPosition) } finally { paintingFocus = false } interactionScene = paintedScene ?? scene if (updateTooltip) paintTooltip(point, points) } const resolveClientPointer = (clientX: number, clientY: number) => { const position = surface?.clientToScene?.(scene, clientX, clientY) if (!position) return null return { position, points: resolvePointerFocus(position.x, position.y, maxDistance()), } } const pointsAtPointer = (clientX: number, clientY: number) => { const resolved = resolveClientPointer(clientX, clientY) pointerPosition = resolved?.position ?? null return resolved?.points ?? [] } const interaction: ChartInteractionController = { clientToScene(clientX, clientY) { if (destroyed) return null return surface?.clientToScene?.(scene, clientX, clientY) ?? null }, resolvePointer(clientX, clientY) { if (destroyed) return null const resolved = resolveClientPointer(clientX, clientY) const point = resolved?.points[0] return resolved && point ? { position: resolved.position, point, points: resolved.points, } : null }, setControlledFocus(target, controlledOptions = {}) { if (destroyed) return focusOwner = 'controlled' if (!target) { focusSource = controlledOptions.source ?? 'programmatic' pointerPosition = null pinnedKey = null updateFocus([]) focusOwner = null return } let resolution: ChartPointerResolution | null let targetPoint: ChartPoint if (isPointerResolution(target)) { resolution = target targetPoint = target.point } else { resolution = null targetPoint = target } focusSource = controlledOptions.source ?? (resolution === null ? 'programmatic' : 'pointer') const points = interactionPoints() const point = restoreChartFocusPoint(points, targetPoint) pointerPosition = resolution?.position ?? null if (!point) { pinnedKey = null updateFocus([]) focusOwner = null return } const focusPoints = focusPointsForPoint(point, points) pinnedKey = controlledOptions.pinned && (tooltipIsSticky() || cursorBinding()?.pin === true) ? point.key : null if (sameChartPointIdentity(point, focusedPoint)) { focusedPoint = point paintFocus(point, focusPoints) return } updateFocus(focusPoints) }, } const scenePositionAtPointer = (clientX: number, clientY: number) => { const position = surface?.clientToScene?.(scene, clientX, clientY) pointerPosition = position ?? null return position } const updateFreeCursorAtPointer = (clientX: number, clientY: number) => { const binding = cursorBinding() if (binding?.mode !== 'free') return false if (cursorIsPinned()) return true const position = scenePositionAtPointer(clientX, clientY) if (!position || !plotContains(scene, position)) { pointerPosition = null cursorSession?.clearOwnedTransient() return true } const session = cursorSession if (!session) return false session.publish( session.createFreeState(scene, binding, position, 'pointer', false), ) return true } const handlePointerMove = (event: PointerEvent) => { if (controlContains(event.target)) { if (!interactionIsPinned()) { pointerPosition = null updateFocus([]) } return } if (options.definition.pointer === false || interactionIsPinned()) return focusOwner = 'pointer' focusSource = 'pointer' if (updateFreeCursorAtPointer(event.clientX, event.clientY)) return updateFocus( pointsAtPointer(event.clientX, event.clientY), tooltipTracksPointer(), ) } const handlePointerDown = (event: PointerEvent) => { if (options.definition.pointer === false || interactionIsPinned()) return focusOwner = 'pointer' focusSource = 'pointer' updateFreeCursorAtPointer(event.clientX, event.clientY) } const clearPointerFocus = ({ relatedTarget }: MouseEvent | PointerEvent) => { if ( options.definition.pointer !== false && focusOwner === 'pointer' && !interactionIsPinned() && !( view && relatedTarget instanceof view.Node && container.contains(relatedTarget) ) ) { pointerPosition = null const binding = cursorBinding() if (binding) cursorSession?.clearOwnedTransient() else updateFocus([]) focusOwner = null } } const clearKeyboardFocus = ({ relatedTarget }: FocusEvent) => { if ( focusOwner === 'keyboard' && !pinnedKey && !( view && relatedTarget instanceof view.Node && container.contains(relatedTarget) ) ) { pointerPosition = null const binding = cursorBinding() if (binding) cursorSession?.clearOwnedTransient() else updateFocus([]) focusOwner = null } } const dismissPinnedInteractionOutside = (event: PointerEvent) => { if ( options.definition.pointer === false || pinnedKey === null || cursorIsPinned() ) { return } const path = event.composedPath() if ( path.includes(container) || path.some( (target) => view && target instanceof view.Node && tooltipInstance?.contains(target), ) ) { return } dismissTooltip() } const handleClick = (event: MouseEvent) => { if (controlContains(event.target)) return if (options.definition.pointer === false) return const activeTooltip = tooltipInstance const NodeConstructor = container.ownerDocument.defaultView?.Node const originatedInTooltip = NodeConstructor ? event .composedPath() .some( (target) => target instanceof NodeConstructor && activeTooltip?.contains(target), ) : activeTooltip?.contains(event.target) if (activeTooltip && originatedInTooltip) { return } focusOwner = 'pointer' const binding = cursorBinding() if (binding?.mode === 'free') { const state = cursorSession?.getState() if (!state) { updateFreeCursorAtPointer(event.clientX, event.clientY) } const current = cursorSession?.getState() if (binding.pin && current) { if (current.pinned) { cursorSession?.clear() } else { cursorSession?.publish({ ...current, pinned: true }) } } options.onSelect?.(null) return } const points = pointsAtPointer(event.clientX, event.clientY) focusSource = 'pointer' const point = points[0] ?? null let pinChanged = false const canPin = tooltipIsSticky() || binding?.pin === true if (canPin) { if (interactionIsPinned()) { pinnedKey = null pinChanged = true if (binding) cursorSession?.clear() } else if (point) { pinnedKey = point.key pinChanged = true } } if (!(binding && canPin && pinChanged && !pinnedKey)) { updateFocus(points, pinChanged) } options.definition.selection?.change(point, 'pointer') options.onSelect?.(point) } const handleKeyDown = (event: KeyboardEvent) => { if (controlContains(event.target)) return const binding = cursorBinding() if (event.key === 'Escape' && cursorSession?.getState()) { event.preventDefault() dismissTooltip() return } if (event.key === 'Escape' && pinnedKey) { event.preventDefault() dismissTooltip() return } if (options.definition.keyboard === false || binding?.mode === 'free') { return } const points = interactionPoints() if (!points.length) return if (event.key === 'Enter' || event.key === ' ') { if (!focusedPoint) return event.preventDefault() const point = focusedPoint const canPin = tooltipIsSticky() || binding?.pin === true if (binding?.mode === 'focus' && canPin) { if (interactionIsPinned()) { pinnedKey = null cursorSession?.clear() } else { pinnedKey = point.key publishFocusCursor(focusPointsForPoint(point), true) } } else if (tooltipIsSticky()) { pinnedKey = pinnedKey ? null : point.key paintFocus(point, focusPointsForPoint(point)) } options.definition.selection?.change(point, 'keyboard') options.onSelect?.(point) return } const focus = resolveRendererFocusStrategy(options.definition.focus) const point = focus ? chartPointFromNavigationOrder( focus.navigation(points), focusedPoint, event.key, ) : chartPointFromSceneOrder(points, focusedPoint, event.key) if (point === undefined) return event.preventDefault() pointerPosition = null focusOwner = 'keyboard' focusSource = 'keyboard' updateFocus(point ? focusPointsForPoint(point) : []) } const handleFocus = (event: FocusEvent) => { if (controlContains(event.target)) { if (!pinnedKey) { pointerPosition = null updateFocus([]) } return } if (event.target === surface?.element && suppressNextSurfaceFocus) { suppressNextSurfaceFocus = false return } if ( options.definition.keyboard !== false && cursorBinding()?.mode !== 'free' && event.target === surface?.element && !focusedPoint ) { const focus = resolveRendererFocusStrategy(options.definition.focus) const points = interactionPoints() const point = focus ? focus.navigation(points)[0] : chartPointFromSceneOrder(points, null, 'Home') pointerPosition = null focusOwner = 'keyboard' focusSource = 'keyboard' updateFocus(point ? focusPointsForPoint(point) : []) } } container.addEventListener('pointermove', handlePointerMove) container.addEventListener('pointerdown', handlePointerDown) container.addEventListener('pointercancel', clearPointerFocus) container.addEventListener('mouseleave', clearPointerFocus) container.addEventListener('click', handleClick) container.addEventListener('keydown', handleKeyDown) container.addEventListener('focusin', handleFocus) container.addEventListener('focusout', clearKeyboardFocus) fontSet?.addEventListener?.('loadingdone', handleFontLoad) configureCursorController() render() configureObserver() container.ownerDocument.addEventListener( 'pointerdown', dismissPinnedInteractionOutside, true, ) return { interaction, update(nextOptions) { if (destroyed) return resolveTooltipInput(nextOptions.definition.tooltip) const fontChanged = nextOptions.measureText === undefined && domText.refresh() const definitionChanged = options.definition !== nextOptions.definition const sizeChanged = options.height !== nextOptions.height || options.aspectRatio !== nextOptions.aspectRatio || options.width !== nextOptions.width || options.initialWidth !== nextOptions.initialWidth const layoutChanged = options.idPrefix !== nextOptions.idPrefix || options.renderer !== nextOptions.renderer || options.measureText !== nextOptions.measureText || fontChanged const needsRender = definitionChanged || sizeChanged || options.ariaLabel !== nextOptions.ariaLabel || options.ariaDescription !== nextOptions.ariaDescription || options.className !== nextOptions.className || options.tabIndex !== nextOptions.tabIndex || options.idPrefix !== nextOptions.idPrefix || options.renderer !== nextOptions.renderer || options.measureText !== nextOptions.measureText || fontChanged const observerChanged = options.width !== nextOptions.width || options.height !== nextOptions.height || options.aspectRatio !== nextOptions.aspectRatio const pointerDisabled = options.definition.pointer !== false && nextOptions.definition.pointer === false && focusOwner === 'pointer' options = nextOptions configureCursorController() syncTooltip() if (pointerDisabled) { pointerPosition = null pinnedKey = null focusOwner = null updateFocus([]) } if (!tooltipIsSticky()) pinnedKey = null if (needsRender) { render( false, layoutChanged ? 'layout' : sizeChanged ? 'resize' : 'update', ) } else { if (cursorBinding()) { applyCursorState(false) } else if (focusedPoint) { paintFocus(focusedPoint, focusPointsForPoint(focusedPoint)) } } if (observerChanged) configureObserver() }, getScene: () => scene, destroy() { if (destroyed) return destroyed = true observer?.disconnect() unsubscribeCursor?.() unsubscribeCursor = undefined cursorSession?.destroy() cursorSession = undefined cursorMode = undefined cursorMatch = undefined cursorExtension = undefined fontSet?.removeEventListener?.('loadingdone', handleFontLoad) if (renderFrame !== undefined) { view?.cancelAnimationFrame?.(renderFrame) } destroyTooltip() destroyHostControls() unsubscribePresentation?.() unsubscribePresentation = undefined surface?.destroy() runtime.destroy() container.removeEventListener('pointermove', handlePointerMove) container.removeEventListener('pointerdown', handlePointerDown) container.removeEventListener('pointercancel', clearPointerFocus) container.removeEventListener('mouseleave', clearPointerFocus) container.removeEventListener('click', handleClick) container.removeEventListener('keydown', handleKeyDown) container.removeEventListener('focusin', handleFocus) container.removeEventListener('focusout', clearKeyboardFocus) container.ownerDocument.removeEventListener( 'pointerdown', dismissPinnedInteractionOutside, true, ) container.replaceChildren() if (ownsPosition && container.style.position === 'relative') { container.style.position = previousPosition } }, } function createScene(): ChartScene { const size = currentSize() const width = size.width ?? (hasRendered ? scene.width : (options.initialWidth ?? 640)) return runtime.render( options.definition, { width, height: options.height ?? (isPositiveFiniteNumber(options.aspectRatio) ? width / options.aspectRatio : (size.height ?? (hasRendered ? scene.height : 320))), }, { measureText: options.measureText ?? domText.measureText, typography: domText.typography(), }, ) } function syncHostControls() { const retained = new Set() for (const control of scene.controls ?? []) { const extension = control.extension as ChartHostControlExtension< TDatum, TXValue, TYValue > const identity = `${extension.id}:${control.key}` retained.add(identity) let current = controlInstances.get(identity) if (current && current.extension !== extension) { current.instance.destroy() controlInstances.delete(identity) current = undefined } if (!current) { current = { extension, instance: extension.create({ container, surface: surface!, setStateFocus, }), } controlInstances.set(identity, current) } current.instance.update(control, scene) } for (const [identity, current] of controlInstances) { if (retained.has(identity)) continue current.instance.destroy() controlInstances.delete(identity) } } function setStateFocus( focus: ChartFocusState | null, ) { if (destroyed) return if (focus !== null && !surface?.supportsStateFocus) { throw new TypeError('Renderer does not support independent state focus') } emphasisFocus = focus ?? undefined paintFocus( focusedPoint, focusedPoint ? focusPointsForPoint(focusedPoint) : [], false, ) } function destroyHostControls() { for (const current of controlInstances.values()) { current.instance.destroy() } controlInstances.clear() } function controlContains(target: EventTarget | null) { for (const current of controlInstances.values()) { if (current.instance.contains?.(target)) return true } return false } function resolvePointerFocus( x: number, y: number, maxDistance: number, ): readonly ChartPoint[] { const points = interactionPoints() const focus = resolveRendererFocusStrategy(options.definition.focus) const focused = resolveChartPointerFocus( interactionScene, focus, x, y, maxDistance, points, ) if (focused) return focused const presentationPoints = surface?.getPresentationPoints?.() const candidate = presentationPoints !== undefined ? nearestPoint(points, x, y, maxDistance) : spatialIndex && interactionScene === scene ? spatialIndex.findNearest(x, y, maxDistance) : findNearestPoint(interactionScene, x, y, maxDistance, points) const point = candidate ? restoreChartFocusPoint(points, candidate) : null return point ? [point] : [] } function interactionPoints() { const points = (surface?.getPresentationPoints?.() ?? interactionScene.points) as readonly ChartPoint< TDatum, TXValue, TYValue >[] return viewportInteractionPoints(scene, points) } function focusPointsForPoint( point: ChartPoint, points = interactionPoints(), ): readonly ChartPoint[] { return ( resolveRendererFocusStrategy(options.definition.focus)?.group(points, { point, }) ?? [point] ) } function subscribeToPresentation() { unsubscribePresentation = surface?.subscribePresentationPoints?.( handlePresentationPoints, ) } function handlePresentationPoints( points: readonly ChartPoint[], ) { if (destroyed || renderingSurface || paintingFocus) return if (cursorBinding()) { applyCursorState(false) return } const visiblePoints = viewportInteractionPoints(scene, points) if (pointerPosition && pinnedKey === null) { updateFocus( resolvePointerFocus( pointerPosition.x, pointerPosition.y, maxDistance(), ), true, ) return } if (!focusedPoint) return const point = restoreChartFocusPoint(visiblePoints, focusedPoint) updateFocus(point ? focusPointsForPoint(point, visiblePoints) : [], true) } function maxDistance() { return options.definition.maxFocusDistance ?? 48 } function paintTooltip( point: ChartPoint | null, points: readonly ChartPoint[], ) { const input = resolveTooltipInput(options.definition.tooltip) if (!input || !point || !surface) { tooltipInstance?.hide() return } if (tooltipExtension !== input.extension || !tooltipInstance) { destroyTooltip() tooltipExtension = input.extension const tooltipMotionCapability = surface.renderer.capabilities?.tooltipMotion tooltipInstance = input.extension.create({ container, motion: tooltipMotionCapability?.protocol === 1 ? tooltipMotionCapability.createController({ container, transition: resolveTooltipMotion, }) : undefined, dismiss: dismissTooltip, bodyChange: () => options.onTooltipBodyChange, }) } const instance = tooltipInstance instance.update(input.options) instance.paint({ point, points, scene, surface, pointer: pointerPosition, focus: { primary: point, group: points, source: focusSource, pinned: interactionIsPinned(), }, pinned: interactionIsPinned(), }) } function resolveTooltipMotion(): false | ChartMotionTransition | undefined { const definition = options.definition.motion if (definition === false) return false return typeof definition === 'function' ? undefined : definition?.transition } function syncTooltip() { const input = resolveTooltipInput(options.definition.tooltip) if (!input) { tooltipInstance?.update(emptyTooltipOptions) tooltipInstance?.hide() } else if (input.extension !== tooltipExtension) { destroyTooltip() } else { tooltipInstance?.update(input.options) } } function destroyTooltip() { tooltipInstance?.destroy() tooltipInstance = undefined tooltipExtension = undefined } function tooltipIsSticky() { const input = resolveTooltipInput(options.definition.tooltip) return Boolean(input && input.options.sticky !== false) } function tooltipTracksPointer() { const input = resolveTooltipInput(options.definition.tooltip) const anchor = input?.options.anchor return ( anchor === 'pointer' || typeof anchor === 'function' || (typeof anchor === 'object' && (anchor.x === 'pointer' || anchor.y === 'pointer')) ) } } const emptyTooltipOptions: ChartTooltipOptions = {} function createHostedScene< TDatum, TXValue extends ChartValue, TYValue extends ChartValue, >( scene: ChartScene, ): ChartScene { const fallbackKeys = new Set( (scene.controls ?? []).flatMap((control: ChartHostControl) => control.fallbackNodeKey ? [control.fallbackNodeKey] : [], ), ) if (!fallbackKeys.size) return scene return { ...scene, nodes: scene.nodes.filter((node) => !fallbackKeys.has(node.key)), } } function isPointerResolution< TDatum, TXValue extends ChartValue, TYValue extends ChartValue, >( target: | ChartPointerResolution | ChartPoint, ): target is ChartPointerResolution { return 'position' in target && 'point' in target && 'points' in target } function resolveTooltipInput< TDatum, TXValue extends ChartValue, TYValue extends ChartValue, >( input: false | ChartTooltipInput | undefined, ): { extension: ChartTooltipExtension options: ChartTooltipOptions } | null { if (!input) return null const extension = 'create' in input ? input : input.use if (extension.__chartTooltipHost !== 'dom') { throw new TypeError( 'DOM chart hosts require a tooltip extension from @tanstack/charts/tooltip.', ) } const domExtension = extension as ChartTooltipExtension return 'create' in input ? { extension: domExtension, options: emptyTooltipOptions, } : { extension: domExtension, options: input, } } function isPositiveFiniteNumber(value: unknown): value is number { return typeof value === 'number' && Number.isFinite(value) && value > 0 } function sameFocusObservation< TDatum, TXValue extends ChartValue, TYValue extends ChartValue, >( first: ChartPoint | null, second: ChartPoint | null, ) { return ( sameChartPointIdentity(first, second) && Object.is(first?.datum, second?.datum) ) } function cssPixelLength(value: string | undefined) { return cssPixelValue(value) ?? 0 } function contentBoxDimension( computedSize: string | undefined, boxSizing: string | undefined, boundingSize: number | undefined, padding: number, border: number, ) { const resolvedSize = isPositiveFiniteNumber(boundingSize) ? cssPixelValue(computedSize) : undefined const size = resolvedSize === undefined ? (boundingSize ?? 0) - padding - border : resolvedSize - (boxSizing === 'border-box' ? padding + border : 0) return isPositiveFiniteNumber(size) ? size : undefined } function cssPixelValue(value: string | undefined) { const match = /^(-?(?:\d+(?:\.\d*)?|\.\d+))px$/i.exec(value?.trim() ?? '') if (!match) return undefined const length = Number(match[1]) return Number.isFinite(length) ? length : undefined } function positiveFiniteNumber(value: number) { return isPositiveFiniteNumber(value) ? value : undefined } function resolveRendererFocusStrategy< TDatum, TXValue extends ChartValue, TYValue extends ChartValue, >( focus: ChartFocusMode | undefined, ): ChartFocusStrategy | undefined { if (focus === false) return focusDisabled return resolveChartFocusStrategy(focus) } function plotContains( scene: ChartScene, position: Readonly<{ x: number; y: number }>, ) { return ( position.x >= scene.chart.x && position.x <= scene.chart.x + scene.chart.width && position.y >= scene.chart.y && position.y <= scene.chart.y + scene.chart.height ) } function resolveAnimation( animation: boolean | ChartAnimationOptions | undefined, container: HTMLElement, reason: HostRenderReason, ) { const configured = animation === true ? {} : animation || undefined if (!configured) return undefined if (reason === 'layout') return undefined if (reason === 'resize' && configured.resize !== true) return undefined if ( (configured.respectReducedMotion ?? true) && container.ownerDocument.defaultView?.matchMedia?.( '(prefers-reduced-motion: reduce)', ).matches ) { return undefined } const { resize: _resize, ...resolved } = configured return resolved }