Skip to content

Fix x-id evaluation order so $id resolves consistently inside x-data#4821

Merged
calebporzio merged 1 commit intomainfrom
josh/fix-x-id-evaluation-order
Apr 30, 2026
Merged

Fix x-id evaluation order so $id resolves consistently inside x-data#4821
calebporzio merged 1 commit intomainfrom
josh/fix-x-id-evaluation-order

Conversation

@joshhanley
Copy link
Copy Markdown
Collaborator

The Scenario

When x-id and x-data are on the same element, $id('name') inside the x-data expression returns a different id than $id('name') in descendants — breaking ARIA wiring that relies on them matching.

<div
    x-id="['a']"
    x-data="{ idFromData: $id('a') }"
>
    <p>From <code>x-data</code>: <strong x-text="idFromData"></strong></p>   <!-- a-1 -->
    <p>From <code>x-text</code>: <strong x-text="$id('a')"></strong></p>     <!-- a-2 -->
</div>

The Problem

data runs before id in the directive order. So $id('a') inside x-data finds no _x_ids on its ancestor yet and falls back to the global counter (returns 1). Then x-id runs and calls findAndIncrementId again (returns 2), which becomes the actual id root. Descendants resolve a-2 while the x-data scope holds a-1.

The Solution

Swap id ahead of data in directiveOrder so the id root is registered first.

x-id only takes a static array of name strings, so it doesn't need x-data scope to evaluate. Docs, tests, and real-world usage all use this pattern.

Fixes #4817

…data`

When `x-id` and `x-data` were on the same element, `$id('name')` calls inside
the `x-data` expression resolved to a different id than calls in descendants,
because `x-data` ran before `x-id` had a chance to register the id root.
Move `id` ahead of `data` in the directive order so the id root is set up
before `x-data` evaluates.

Fixes #4817
@calebporzio calebporzio merged commit 7d3cb1c into main Apr 30, 2026
2 checks passed
@calebporzio calebporzio deleted the josh/fix-x-id-evaluation-order branch April 30, 2026 20:32
@calebporzio
Copy link
Copy Markdown
Collaborator

great!

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.

Order of execution on element with x-id and $id in x-data

2 participants