User Agents

Web browsers and everyday web tech, explained

How to Add a Close Button to a Material-UI Tab

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to add a close (X) button to each tab in a Material-UI (now MUI) Tabs component. This is a common pattern in dashboards, editors, and browser-like interfaces where users need to manage multiple open views. You'll end up with tabs that have a small icon on the right that, when clicked, removes that tab from the list.

What You'll Need

  • A React project with @mui/material and @emotion/react installed.
  • Basic familiarity with React hooks (useState) and Material-UI components.
  • An icon library – Material-UI includes @mui/icons-material; if you haven't installed it, run npm install @mui/icons-material.

Step-by-Step

  1. Set up the Tabs state. Create a state array that holds your tab labels (or objects with label and value). For example: const [tabs, setTabs] = useState(['Tab 1', 'Tab 2', 'Tab 3']); and a state for the active tab index: const [activeTab, setActiveTab] = useState(0);
  2. Render the Tabs component. Use <Tabs value={activeTab} newValue) => setActiveTab(newValue)}> and map over your tabs array to create <Tab> components.
  3. Add the close icon inside each Tab label. The label prop of Tab accepts a React node. So instead of a plain string, render a <span> with the tab text and a <CloseIcon /> from @mui/icons-material.
  4. Style the label container. Use a flex container with display: 'flex', alignItems: 'center', gap: '6px' so the icon sits nicely to the right of the text.
  5. Make the icon clickable. Wrap the icon in a <Box component="span" or use on the icon. The click handler should call a function that removes that tab from the array.
  6. Prevent the tab from switching when closing. The click event on the icon will also trigger the Tab's onClick. You must call event.stopPropagation() inside the icon's onClick to prevent the parent Tab from receiving the click.
  7. Handle closing the active tab. When you remove a tab, if it was the active tab, you need to adjust the active index. A common approach: after removal, set the active tab to the previous tab (or the first tab if none left).
  8. Update the tabs state. Use setTabs(prevTabs => prevTabs.filter((_, i) => i !== index)) to remove the tab. Also update activeTab accordingly.
  9. Edge case: closing the last tab. If there are no tabs left, you may want to show a placeholder or disable the close button. Decide what makes sense for your app.

Common Mistakes

  • Forgetting stopPropagation. Without it, clicking the close icon will also switch to that tab, which is usually not desired.
  • Not updating activeTab after removal. If you remove the active tab and don't change the index, the Tabs component will show an out-of-range value, causing errors or a blank tab.
  • Using a plain string as label. If you try to append an icon to a string label, it won't render; you must use a React element.
  • Icon not aligned. Without flex styling, the icon may appear below the text or with odd spacing. Always set display:flex and alignItems:center.

Tips

  • Use a separate component for the tab label. This keeps your code clean and allows you to pass the close handler as a prop.
  • Add a tooltip to the close icon. Use <Tooltip title="Close tab"> for better UX.
  • Consider using a context menu for more options. If you need more than close, you can extend the pattern.
  • Test with keyboard navigation. Ensure the close icon is focusable and can be activated with Enter/Space for accessibility.

Troubleshooting

  • The close icon doesn't appear. Check that you imported CloseIcon from @mui/icons-material and that it's inside the label prop.
  • Clicking close also switches tabs. Add event.stopPropagation() in the icon's onClick handler.
  • After closing, the active tab index is out of range. Always recalculate the active index after removal. For example: const newActive = activeTab === index ? Math.max(0, index - 1) : activeTab; and then set it.
  • Tab label is not vertically centered. Ensure the parent span has display:inline-flex and alignItems:center.
  • Icon is too large or too small. Adjust the font size via on the icon.

Frequently Asked Questions

Can I use a custom icon instead of the default CloseIcon?

Yes, any icon component from MUI or your own SVG works. Just replace <CloseIcon /> with your chosen icon.

How do I add a close button to only some tabs?

Conditionally render the icon based on a property in your tab data, e.g., tab.closable !== false.

What if I want to confirm before closing?

Inside the onClick handler, call window.confirm('Close this tab?') and only proceed if the user confirms.

Can I animate the removal of a tab?

Yes, you can use <Collapse> or <Grow> from MUI to animate the tab's exit, but you'll need to manage the removal timing carefully.

How do I make the close button accessible?

Add aria-label="Close tab" to the clickable element and ensure it's focusable (e.g., using a <IconButton> with size small).

Why does my Tabs component throw a warning about value being out of bounds?

This happens when activeTab is greater than or equal to the length of tabs. Always update activeTab when removing a tab.