Skip to content

[context menu] FocusGuard creates <span> elements in table body if used on table rows #3166

Closed

Description

@ilovemesomeramen

Bug report

Current behavior

When a ContextMenu is triggered it created <span> elements in the container where it was defined. This creates an issue when used inside Tables since they are not allowed to be inside the table structure.

Expected behavior

Being able to use the ContextMenu inside tables and table rows

Reproducible example

Link to CodeSandbox

Github Repo

Base UI version

1.0.0-beta.4

Which browser are you using?

Chrome

Which OS are you using?

Linux

Which assistive tech are you using (if applicable)?

None

Additional context

The issue was already briefly discussed here:
https://discord.com/channels/1287292451308048406/1436013951367254137

Activity

  1. added
    status: waiting for maintainer
    on Nov 7, 2025
  2. eckmLJE commented on Nov 7, 2025

    @eckmLJE

    Here is my (rushed) implementation of the hacky approach to this.

    • Wraps each row in the context (context doesn't render an html element, so no breaking the table)
    • Renders the menu itself as a sibling of the row in the react component hierarchy (it's portalled so it's not an actual dom sibling)
    • Within each <td>, wraps the cell content with a zero padding trigger container -- altogether this results in a continuous click area in the whole row
    • Inside of that trigger container is the cell content container with padding

    https://codesandbox.io/p/sandbox/morning-lake-m6s24z

    The benefit of this approach is you're not violating the table row role or breaking assistive technologies by making the <tr> itself interactive.

    Edit: I'm noticing that it does still render spans inside the <tbody> while the menu is open, which appears not to break the table layout, but does temporarily make the table markup invalid. I wonder though if that is acceptable because the focus will be locked into the popup, and by the time the focus there is removed, the table will be returned to its valid state? I'm curious too why those spans are rendered as children of the root and not in the portal

    From the discord thread, here is some reading on the issue:
    https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/row_role
    https://adrianroselli.com/2023/11/dont-turn-a-table-into-an-aria-grid-just-for-a-clickable-row.html

  3. added
    component: context menu
    has workaround
    and removed
    status: waiting for maintainer
    on Nov 7, 2025
  4. mj12albert commented on Nov 7, 2025

    @mj12albert
    Member

    Here is my (rushed) implementation of the hacky approach to this.

    Thanks for the sandbox ~ for the focus guard spans, they are also transient in OPs sandbox, I wonder if it has any impact on actual usage

    Though even if you don't care about invalid HTML, Next.js will throw:

    Image
  5. removed
    has workaround
    on Nov 7, 2025
  6. eckmLJE commented on Nov 7, 2025

    @eckmLJE

    Well I guess if you're using next, and next relies on valid html, then you care about valid html 😄

    You could possibly rework my workaround to wrap the whole <table> with the context menu, and use the click target or its ancestor to get up to the row you need with some data-rowid attribute, and populate the menu with what you need that way.

    Although I'm still wondering why the focus guard spans render as children of the provider and not into the portal. I suspect it's a requirement for how returning focus works or something. (I haven't investigated yet.)

  7. eckmLJE commented on Nov 11, 2025

    @eckmLJE

    Here is a new example that places the context menu outside of the table, so we don't render any invalid html elements inside the table. We read the rowid data- attribute off the event target.

    https://codesandbox.io/p/sandbox/morning-lake-m6s24z

  8. added
    has workaround
    on Nov 12, 2025
  9. andresmarpz commented on Dec 26, 2025

    @andresmarpz

    Hopefully this use-case can be looked at. Linear style UIs where you want to render a context menu wrapping a <tr> element are pretty common these days.

  10. atomiks commented on Dec 27, 2025

    @atomiks
    Contributor

    We can change Context Menu specifically to fully trap focus inside modally, without letting you tab out of the context menu (which is the reason the visually-hidden invisible elements get rendered, to preserve the tabbing order).

  11. github-actions commented on Dec 30, 2025

    @github-actions

    This fix will be available in the next npm release of Base UI.

    In the meantime, you can try it out on our Canary release channel:

    npm i https://pkg.pr.new/@base-ui/react@3645
  12. changed the title [-][ContextMenu] FocusGuard creates <span> elements in table body if used on table rows[/-] [+][context menu] FocusGuard creates <span> elements in table body if used on table rows[/+] on May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: context menuhas workaround

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions