ui5-webcomponents icon indicating copy to clipboard operation
ui5-webcomponents copied to clipboard

[ui5-tab-container]: Null pointer exception in _getFocusableRefs during connectedCallback

Open mastanshaikk opened this issue 6 months ago • 7 comments

Bug Description

`TabContainer._getFocusableRefs()` throws a `TypeError: Cannot read properties of null (reading 'hasAttribute')` during the component's `connectedCallback` phase. `_getStartOverflow()` and/or `_getEndOverflow()` return `null` at this lifecycle stage because the Shadow DOM elements (`.ui5-tc__overflow--start` / `.ui5-tc__overflow--end`) have not been rendered yet when `ItemNavigation._init()` fires its first event.

Affected Component

TabContainer

Expected Behaviour

`_getFocusableRefs()` should handle the case where overflow elements are not yet in the DOM and return an empty or partial list instead of throwing.

Steps to Reproduce

  1. Mount a `` into the DOM.
  2. Observe the `connectedCallback` lifecycle phase.
  3. `ItemNavigation._init()` calls `getItemsCallback` → `_getFocusableRefs()` before the Shadow DOM is fully populated.

Log Output, Stack Trace or Screenshots

``` TabContainer.ts:1308 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'hasAttribute') at iU._getFocusableRefs (TabContainer.ts:1308:13) at c.getItemsCallback [as _getItems] (TabContainer.ts:373:33) at c._init (ItemNavigation.ts:153:8) at EventProvider.ts:56:14 at Array.map () at i.fireEvent (EventProvider.ts:55:25) at iU._render (UI5Element.ts:845:47) at v (Render.ts:44:15) at iU.connectedCallback (UI5Element.ts:337:3) ```

Root Cause

`_getStartOverflow()` and `_getEndOverflow()` use `shadowRoot.querySelector()` with a non-null assertion (`!`). During `connectedCallback`, the first render may not yet have written the overflow wrapper elements to the Shadow DOM, so both methods return `null`.

The crash occurs at `TabContainer.ts#L1320` when `.hasAttribute("hidden")` is called on the `null` result.

Suggested Fix

Add optional chaining in `_getFocusableRefs()` to guard against `null` overflow elements:

```ts _getFocusableRefs() { if (!this.getDomRef()) { return []; }

const focusableRefs = [];

if (!this._getStartOverflow()?.hasAttribute("hidden")) {
    const startBtn = this.startOverflowButton[0] || this._getStartOverflowBtnDOM();
    if (startBtn) focusableRefs.push(startBtn);
}

this._getTabs().forEach(tab => {
    const ref = tab.getDomRefInStrip();
    if (ref && !ref.hasAttribute("hidden")) {
        focusableRefs.push(tab);
    }
});

if (!this._getEndOverflow()?.hasAttribute("hidden")) {
    const endBtn = this.overflowButton[0] || this._getEndOverflowBtnDOM();
    if (endBtn) focusableRefs.push(endBtn);
}

return focusableRefs;

} ```

Priority

High Prio

UI5 Web Components Version

To be confirmed

Browser

All

Operating System

All

Additional Context

Reported by SAP SuccessFactors (SAP SF). The existing `getDomRef()` guard at the top of `_getFocusableRefs()` does not cover this case because `getDomRef()` returns the host element (which is connected), while the Shadow DOM overflow wrappers are not yet rendered.

Declaration

  • [x] I'm not disclosing any internal or sensitive information.

mastanshaikk avatar May 16 '26 16:05 mastanshaikk

Hello @mastanshaikk ,

Thanks for reporting this issue. Before I can route this to the owning team, I need a couple of clarifications:

  1. Exact @ui5/webcomponents version where the crash occurs. I tried reproducing on 2.22.0 and the described steps do not trigger the error.
  2. Minimal reproduction sample (StackBlitz, standalone HTML, etc.). Based on the framework's render lifecycle, simply mounting a should not hit the reported code path — the existing getDomRef() guard in _getFocusableRefs() returns early on the first render. So there must be something in the SuccessFactors hosting setup that I am missing.

Once I have a runnable repro, I can redirect the incident to the responsible team.

Thanks!

IlianaB avatar May 18 '26 08:05 IlianaB

Hi @IlianaB, thanks for the quick follow-up!

Version confirmed: @ui5/webcomponents 2.18.1

Trigger scenario: The crash occurs in the SAP SuccessFactors application when the user clicks a tab inside a <ui5-tabcontainer> while navigating between pages — the navigation causes a full page reload, and on the new page the TabContainer is mounted fresh. It is during this re-mount (the connectedCallback of the newly inserted TabContainer) that the crash fires. This is why a simple static mount may not reproduce it — the reload-and-remount cycle in a routed SPA is what triggers the exact timing window.

Screenshot from the live environment:

The screenshot below was captured directly from the SuccessFactors app with DevTools open. The right-hand call stack panel shows the exact crash path — connectedCallback → _render → fireEvent → _init → getItemsCallback → _getFocusableRefs — confirming this is a real production occurrence on every tab-triggered navigation:

Image

On the getDomRef() guard — why it does not protect here:

I understand the reasoning, but there is a genuine timing gap on the first render that the guard does not cover. Here is the exact sequence from the source:

// UI5Element.ts — _render() method

// line 884: fires BEFORE the shadow DOM template is written
this._componentStateFinalizedEventProvider.fireEvent("componentStateFinalized");
//  ↑ ItemNavigation._init() is hooked here (ItemNavigation.ts:108)
//    → _getItems() → getItemsCallback() → _getFocusableRefs()

// line 890+: shadow DOM template is actually rendered HERE
updateShadowRoot(this, ...);

Why getDomRef() still returns non-null at that moment:

getDomRef() (UI5Element.ts:956-966) returns this.shadowRoot.children[0] — the wrapper div that exists as soon as the custom element is defined, not as a result of template rendering. So on the very first render:

Check Result Reason
getDomRef() ✅ non-null Shadow root wrapper already exists
_getStartOverflow() ❌ null .ui5-tc__overflow--start not yet in DOM
_getEndOverflow() ❌ null .ui5-tc__overflow--end not yet in DOM

This leads directly to the crash at TabContainer.ts:1320 — calling .hasAttribute() on null.

The framework comment at line 883 even describes this intentional ordering:

"Intended for framework usage only. Currently ItemNavigation updates tab indexes after the component has updated its state but before the template is rendered"

From a code perspective, _getFocusableRefs() must explicitly verify that the overflow DOM elements exist before accessing their attributes — the getDomRef() guard alone is not sufficient for this case.


Proposed fix — optional chaining in _getFocusableRefs():

_getFocusableRefs() {
    if (!this.getDomRef()) {
        return [];
    }

    const focusableRefs = [];

    if (!this._getStartOverflow()?.hasAttribute("hidden")) {
        const startBtn = this.startOverflowButton[0] || this._getStartOverflowBtnDOM();
        if (startBtn) focusableRefs.push(startBtn);
    }

    this._getTabs().forEach(tab => {
        const ref = tab.getDomRefInStrip();
        if (ref && !ref.hasAttribute("hidden")) {
            focusableRefs.push(tab);
        }
    });

    if (!this._getEndOverflow()?.hasAttribute("hidden")) {
        const endBtn = this.overflowButton[0] || this._getEndOverflowBtnDOM();
        if (endBtn) focusableRefs.push(endBtn);
    }

    return focusableRefs;
}

Could you please route this to the owning team? Thank you!

mastanshaikk avatar May 18 '26 21:05 mastanshaikk

Hello @UI5/ui5-team-ctr-rod , The exact production timing of this issue seems to be specific to the SuccessFactors React/SPA reload cycle and we couldn't reconstruct it standalone. This small sample simulates the end state (shadow root present, overflow divs absent) and produces the identical TypeError on 2.22.0. Combined with the production stack trace in the reporter's screenshot, this confirms _getFocusableRefs() has an unsafe code path: _getStartOverflow() and _getEndOverflow() use querySelector(...)! (non-null assertion) but querySelector can return null. The reporter's suggested fix (optional chaining on .hasAttribute("hidden")) is the minimal correct hardening.

Regards, Iliana

IlianaB avatar May 19 '26 07:05 IlianaB

This issue has been closed. To reopen, just leave a comment!

github-actions[bot] avatar May 21 '26 10:05 github-actions[bot]

:tada: This issue has been resolved in version v2.23.0-rc.0 :tada:

The release is available on v2.23.0-rc.0

Your semantic-release bot :package::rocket:

ui5-webcomponents-bot avatar May 23 '26 04:05 ui5-webcomponents-bot

:tada: This issue has been resolved in version v2.23.0 :tada:

The release is available on v2.23.0

Your semantic-release bot :package::rocket:

This issue has been closed. To reopen, just leave a comment!

github-actions[bot] avatar Jun 05 '26 12:06 github-actions[bot]