Skip to content

JS operator-precedence bug in Section collapse/expand window listeners #19688

@webgas-tech

Description

@webgas-tech

Description

The expand-section, collapse-section, open-section, and toggle-section window event listeners in support/resources/views/components/section/index.blade.php have a JavaScript operator-precedence bug that prevents them from working when $collapseId is not set.

Affected lines

Lines 48, 50, 51, 52 in vendor/filament/support/resources/views/components/section/index.blade.php:

x-on:expand-section.window="if ($event.detail.id == @js($collapseId) ?? $el.id) isCollapsed = false"

Root cause

When $collapseId is null (the default — e.g. when using ->extraAttributes(['id' => 'section-foo']) instead of passing a collapseId), @js($collapseId) renders as JS null. The expression becomes:

if ($event.detail.id == null ?? $el.id) isCollapsed = false

JavaScript operator precedence: == (precedence 10) binds tighter than ?? (precedence 4), so this parses as:

if (($event.detail.id == null) ?? $el.id) isCollapsed = false

Since ?? only coalesces null/undefined (not false), and $event.detail.id == null evaluates to false for any real string id, the ?? never kicks in. The listener effectively only fires when detail.id is null/undefined — the opposite of the intended behavior.

Note: the x-data initializer on line 45 gets this right with template literals:

$persist(@js($collapsed)).as(`section-${@js($collapseId) ?? $el.id}-isCollapsed`)

Here there's no ==, so ?? works correctly as a fallback.

Fix

Wrap the fallback expression in parentheses so ?? resolves before ==:

x-on:expand-section.window="if ($event.detail.id == (@js($collapseId) ?? $el.id)) isCollapsed = false"

Same fix needed on all four affected lines (collapse-section, expand-section, open-section, toggle-section).

Steps to reproduce

  1. Create a Filament Section with ->collapsible()->collapsed() and ->extraAttributes(['id' => 'section-foo']) (no explicit collapseId)
  2. Dispatch from JS: window.dispatchEvent(new CustomEvent('expand-section', { detail: { id: 'section-foo' } }))
  3. Expected: section expands
  4. Actual: nothing happens

Filament version

v5 (tested on latest)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions