Kombai Logo

Mastering MUI ButtonGroup: Building Cohesive Action Controls in React Applications

As a front-end developer working with React, you've likely encountered scenarios where multiple related actions need to be presented together in a clean, organized interface. Whether it's a document editor's formatting toolbar, a data filtering panel, or a media player's control set, grouping related buttons is a common UI pattern that improves usability and visual cohesion.

Material UI's ButtonGroup component offers an elegant solution for this exact need. In this comprehensive guide, I'll walk you through everything you need to know about implementing ButtonGroup in your React applications—from basic usage to advanced customization techniques that I've refined over years of production development.

What You'll Learn

By the end of this article, you'll be able to:

  • Implement basic ButtonGroup components with various orientations and variants
  • Customize ButtonGroup appearance through theming and direct styling
  • Create split buttons and dropdown combinations
  • Build responsive toolbars with grouped actions
  • Handle complex interaction patterns and state management
  • Solve common implementation challenges and accessibility concerns

Let's dive into the details of this powerful yet often underutilized MUI component.

Understanding the ButtonGroup Component

The ButtonGroup component in Material UI serves a specific purpose: to group related buttons together in a visually cohesive unit. This grouping creates a stronger visual relationship between actions that are conceptually related, improving both the aesthetics and usability of your interface.

At its core, ButtonGroup is a wrapper that modifies how individual Button components render when placed adjacent to each other. It handles the styling of borders, spacing, and visual continuity between buttons, ensuring they appear as a unified control rather than separate elements.

Before we dive into implementation details, it's important to understand that ButtonGroup inherits many properties from the Button component. This means that props like variant, color, size, and disabled can be applied at the group level and will cascade to all child buttons (unless overridden at the individual button level).

Key Features and Props

ButtonGroup comes with several key props that control its appearance and behavior. Let's examine the most important ones:

PropTypeDefaultDescription
childrennode-The content of the component, typically Button elements
color

'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' | string

'primary'The color of the component
disabledboolfalseIf true, all buttons will be disabled
disableElevationboolfalseIf true, no elevation is applied to the buttons
disableRippleboolfalseIf true, the ripple effect is disabled for all buttons
fullWidthboolfalse

If true, the buttons will take up the full width of their container

orientation'horizontal' | 'vertical''horizontal'The orientation of the button group
size'small' | 'medium' | 'large''medium'The size of the component
variant'contained' | 'outlined' | 'text''outlined'The variant to use
sxobject-The system prop that allows defining system overrides

One key distinction to note is that unlike many other MUI components, ButtonGroup's default variant is 'outlined' rather than 'contained'. This is intentional, as outlined buttons often work better visually when grouped together.

Variants and Visual Options

ButtonGroup supports the same three variants as the standard Button component:

  1. Outlined (default): Buttons with a border outline and transparent background
  2. Contained: Solid buttons with background color and elevation
  3. Text: Buttons without a background or border, showing only text

The choice of variant affects not just the appearance of individual buttons but also how they're visually connected in the group. For example, contained buttons in a group will share borders and appear as a single unit, while text buttons will appear more subtly connected.

Getting Started with ButtonGroup

Let's start with the basics: implementing a simple horizontal ButtonGroup with three actions. First, ensure you have the necessary dependencies installed:

Now, let's create a basic ButtonGroup component:

This creates a simple horizontal group of three contained buttons. The aria-label provides accessibility context for screen readers.

Let's explore how different variants affect the appearance:

In this example, we're using MUI's Stack component to display three different ButtonGroup variants vertically. Each group demonstrates a different visual style while maintaining the grouped relationship between buttons.

Orientation Options

ButtonGroup can be oriented either horizontally (the default) or vertically, depending on your layout needs:

Vertical orientation is particularly useful for sidebar toolbars or when you need to conserve horizontal space. The visual connection between buttons is maintained, but the flow direction changes.

Creating a Practical Toolbar with ButtonGroup

Let's build something more practical: a text formatting toolbar that might be used in a rich text editor. We'll use icons from @mui/icons-material to create a more visually intuitive interface:

This example demonstrates several important concepts:

  1. Using ButtonGroup to organize related controls (text formatting in one group, alignment in another)
  2. Managing state to track which formatting options are active
  3. Visually indicating active state by changing button colors
  4. Using icons for better visual recognition
  5. Separating button groups with dividers for logical grouping
  6. Providing a live preview of the formatting options

The resulting toolbar is both functional and visually cohesive, with clear grouping of related actions.

Customizing ButtonGroup Appearance

While the default appearance of ButtonGroup works well in many cases, you'll often want to customize it to match your application's design system. MUI provides several approaches for customization.

Using the sx Prop for Direct Styling

The sx prop is the most direct way to apply custom styles to a ButtonGroup:

This example applies several custom styles:

  • A gradient background to the entire button group
  • Custom box shadow
  • Rounded edges on the first and last buttons
  • Custom spacing between buttons
  • Custom border styling

Theme Customization for Consistent Styling

For application-wide styling, it's better to customize the theme:

This approach uses MUI's theming system to define global styles for ButtonGroup components. The advantages include:

  1. Consistent styling across the application
  2. Separation of styling from component logic
  3. The ability to define variants that can be reused
  4. Easier maintenance when design changes are needed

Advanced ButtonGroup Patterns

Let's explore some more advanced patterns that solve common UI challenges.

Split Buttons with Dropdown Menus

A split button combines a primary action with a dropdown menu of related actions:

This pattern is useful when you have a primary action but want to offer variations of that action. The main button performs the currently selected action, while the dropdown button allows the user to select a different action variant.

Responsive ButtonGroup with Icon/Text Toggle

For responsive designs, you might want to show only icons on small screens and icons with text on larger screens:

This component uses MUI's useMediaQuery hook to detect screen size and render different button content accordingly. On mobile devices, only icons are shown to conserve space, while larger screens get the full text labels alongside icons.

Segmented Controls (Toggle Button Group)

For mutually exclusive options, MUI provides a related component called ToggleButtonGroup that works similarly to ButtonGroup but with built-in selection state:

While not technically a ButtonGroup, ToggleButtonGroup provides similar visual grouping with built-in state management for selection. The exclusive prop ensures that only one button can be selected at a time, making it perfect for mutually exclusive options.

Building a Complete Toolbar with ButtonGroup

Now let's combine several concepts to build a more complete document editing toolbar:

This comprehensive toolbar example demonstrates several important concepts:

  1. Responsive design: The toolbar adapts to screen size, showing fewer controls and more compact options on mobile devices.
  2. Mixed control types: It combines ButtonGroup, ToggleButtonGroup, and standalone buttons to create a cohesive interface.
  3. Overflow handling: On small screens, less important actions are moved to an overflow menu.
  4. Visual organization: Dividers separate logical groups of controls.
  5. State management: Multiple states are tracked for different aspects of the document formatting.
  6. Menus for complex options: The color selector uses a dropdown menu to provide more options without taking up toolbar space.

This pattern can be adapted for many different types of applications, from document editors to image manipulation tools to data analysis interfaces.

Accessibility Considerations

When implementing ButtonGroup components, accessibility should be a primary concern. Here are some key considerations:

Proper ARIA Attributes

Always include appropriate ARIA attributes to ensure screen readers can properly interpret your interface:

Notice how we provide aria-label attributes at both the group level (describing the overall purpose) and the individual button level (describing each specific action).

Keyboard Navigation

Ensure your ButtonGroup components are fully navigable using the keyboard:

This example enhances the default keyboard navigation by adding a more visible focus indicator, making it easier for keyboard users to see which button is currently focused.

Color Contrast

Ensure sufficient color contrast for all states of your buttons:

This example uses darker colors with better contrast ratios and enhances the visibility of interactive states like hover and focus.

Performance Optimization

When using ButtonGroup in larger applications, performance can become a concern. Here are some strategies to optimize performance:

Memoization for Stable Props

Use React's useMemo to prevent unnecessary re-renders:

By memoizing the props object, we prevent unnecessary prop comparisons and potential re-renders when the component updates for reasons unrelated to the ButtonGroup props.

Avoiding Inline Functions

Inline functions can cause unnecessary re-renders. Extract them to component-level functions:

By using useCallback, we ensure that the function references remain stable between renders, which can help prevent unnecessary re-renders of child components.

Common Issues and Solutions

Here are some common issues you might encounter when working with ButtonGroup, along with their solutions:

Inconsistent Button Sizes

Sometimes buttons in a group may have inconsistent widths, especially when they contain different amounts of text:

This solution uses the flex: 1 property to make all buttons take up equal space within the group, with a minimum width to ensure very short text doesn't result in too-narrow buttons.

Border Issues Between Buttons

Sometimes the borders between buttons in a group can appear doubled or inconsistent:

This solution uses theme overrides to ensure consistent border rendering between buttons in the group.

Accessibility with Icon-Only Buttons

Icon-only buttons can be problematic for accessibility if not properly labeled:

This solution combines aria-label attributes with tooltips to provide both screen reader support and visual cues for sighted users who may not recognize the icons.

Wrapping Up

The ButtonGroup component is a powerful tool in the MUI arsenal that helps create cohesive, visually connected action controls. When used effectively, it can significantly improve the user experience by clearly indicating related actions and providing a more organized interface.

Throughout this guide, we've explored everything from basic usage to advanced patterns, customization techniques, accessibility considerations, and performance optimizations. By applying these principles, you can create intuitive, accessible, and visually appealing action controls that enhance your React applications.

Remember that ButtonGroup works best when the grouped buttons are truly related actions—overusing it can lead to visual clutter and confusion. When in doubt, ask whether the actions you're grouping are conceptually related enough to warrant visual grouping. When they are, ButtonGroup provides an elegant, built-in solution that maintains Material Design principles while offering extensive customization options.