Skip to content

Add shouldStopAtNode callback to closestDataStack#4734

Closed
joshhanley wants to merge 1 commit intoalpinejs:mainfrom
joshhanley:josh/shortcircuit-closestdatastack
Closed

Add shouldStopAtNode callback to closestDataStack#4734
joshhanley wants to merge 1 commit intoalpinejs:mainfrom
joshhanley:josh/shortcircuit-closestdatastack

Conversation

@joshhanley
Copy link
Collaborator

The Scenario

When Livewire contextualises wire:click expressions, it prefixes bare identifiers with $wire. so they resolve against the component. For example, wire:click="$js.select(user)" becomes $wire.$js.select($wire.user).

To support Alpine-scoped variables (like x-for iteration items), Livewire needs to check Alpine's scope chain before prefixing. However, closestDataStack traverses all the way up the DOM tree, which means Alpine scope from outside the Livewire component can incorrectly override $wire properties.

The Problem

There's currently no way to tell closestDataStack where to stop collecting scope. It always traverses to the root of the document.

The Solution

Add an optional shouldStopAtNode callback parameter to closestDataStack. When provided, the function stops traversing and returns an empty array if shouldStopAtNode(node) returns true.

// Default behaviour (unchanged)
Alpine.closestDataStack(el)

// Stop at a specific boundary
Alpine.closestDataStack(el, node => {
    return node.hasAttribute && node.hasAttribute('wire:id')
})

This allows Livewire to collect Alpine scope only within the component boundary.

Allows callers to stop scope collection at a specific node when
traversing up the DOM tree.
@calebporzio
Copy link
Collaborator

change this to undefined callback plus conditional

@calebporzio calebporzio closed this Feb 5, 2026
@joshhanley
Copy link
Collaborator Author

Closed as we decided not to add the change to Livewire for now, that required this PR.

@joshhanley joshhanley deleted the josh/shortcircuit-closestdatastack branch February 6, 2026 00:15
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.

2 participants