Skip to content

Fix x-sort crash when container has a <template> with no .content#4819

Open
joshhanley wants to merge 2 commits intomainfrom
josh/fix-sort-template-content-undefined
Open

Fix x-sort crash when container has a <template> with no .content#4819
joshhanley wants to merge 2 commits intomainfrom
josh/fix-sort-template-content-undefined

Conversation

@joshhanley
Copy link
Copy Markdown
Collaborator

@joshhanley joshhanley commented Apr 20, 2026

The Scenario

Placing a flux:chart inside a wire:sort:item (with no wire:sort:handle) breaks drag-to-reorder and throws in the console:

Screenshot 2026-04-21 at 08 49 37AM@2x
<div wire:sort="handleSort">
    @foreach ($tasks as $task)
        <flux:card wire:key="{{ $task['id'] }}" wire:sort:item="{{ $task['id'] }}">
            <flux:chart :value="[10, 12, 11, 13, 15]">
                <flux:chart.svg>
                    <flux:chart.line />
                </flux:chart.svg>
            </flux:chart>
        </flux:card>
    @endforeach
</div>

The Problem

x-sort auto-detects whether the sort container uses drag handles. If no handle element is found directly, it scans nested <template> elements for one:

useHandles: !! el.querySelector(handleSelector)
    || Array.from(el.querySelectorAll('template')).some(tmpl => tmpl.content.querySelector(handleSelector)),

querySelectorAll('template') matches every element with tag name template regardless of namespace, but .content only exists on HTMLTemplateElement. Flux's chart hydrates its SVG from an HTML <template> and the hydrated <svg> contains nested <template> elements that were parsed in the SVG namespace, so they're SVGElement instances with no .content property. The unconditional .querySelector call on tmpl.content throws.

The Solution

Guard the property access with optional chaining so non-HTMLTemplateElement matches are skipped:

|| Array.from(el.querySelectorAll('template')).some(tmpl => tmpl.content?.querySelector(handleSelector)),
Screenshot 2026-04-21 at 08 50 09AM

Fixes livewire/flux#2578

`x-sort` auto-detects drag handles by scanning top-level `<template>`
elements for `[x-sort:handle]` / `[wire:sort:handle]`. The scan
unconditionally reads `tmpl.content.querySelector(...)`, which throws
for any matched element that isn't an `HTMLTemplateElement`, e.g. a
`<template>` in the SVG namespace or a custom element named `template`.

Guarding with optional chaining on `.content` skips those entries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PRO] FluxUI chart breaks inside wire:sort without wire:sort:handle

1 participant