JGuang

Toggle Button

Toggle Button · Material UI

Preview
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

Toggle Button

Choose one or several pressed actions in a native ToggleButtonGroup.

This guide covers Material UI Core with the default Emotion engine; extensions and additional demos are explicitly classified.

Use case and business boundary

Exclusive selection can return null when deselected; rejecting empty selection is host policy, not a built-in required prop.

Native composition and scope

ToggleButton, ToggleButtonGroup

Preserve native Material UI composition and types; do not substitute JGuang UI/Base UI/Radix APIs. Example entries are not extra components.

Native installation and initialization

npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

Use native npm and Emotion, not an invented JGuang UI Registry URL. Place examples inside native ThemeProvider; choose CssBaseline or ScopedCssBaseline for the project. Inspect React, existing providers, lockfile and SSR first. Never overwrite files or conceal peer conflicts. This batch verified React 19.2.8; React 18 and below require matching react-is. See the included bilingual setup guide.

Practical native parameters

These selected parameters are not a flattened universal API. Preserve the native declaring owner, slot structure, nullable values and generics.

Owner.propPractical roleDeclaration source
ToggleButtonGroup.valueControlled native value; its scalar/array/nullable type is owner-specific.ToggleButtonGroup
ToggleButtonGroup.exclusiveUse single ToggleButtonGroup selection; deselection can return null.ToggleButtonGroup
ToggleButtonGroup.onChangeOwning control's native callback; preserve its exact event/value/nullable signature.ToggleButtonGroup
ToggleButton.selectedNative selected/pressed presentation; the host owns the selected value.ToggleButton

Runnable native examples

Machine keys select example entries, not universal variant props. Native demo JSX/data stay unchanged; JGuang-authored acceptance fixtures are explicitly labelled.

  • Exclusive selection · ToggleButtons · upstream native demo
  • Multiple selection · ToggleButtonsMultiple · upstream native demo
  • Size · ToggleButtonSizes · upstream native demo
  • Color · ColorToggleButton · upstream native demo
  • Vertical buttons · VerticalToggleButtons · upstream native demo
  • Enforce value set · ToggleButtonNotEmpty · upstream native demo
  • Standalone toggle button · StandaloneToggleButton · upstream native demo
  • Customization · CustomizedDividers · upstream native demo
  • Horizontal Spacing · HorizontalSpacingToggleButton · upstream native demo
  • Vertical Spacing · VerticalSpacingToggleButton · upstream native demo

Actual usage example

Unmodified pinned native demo; copy its required local helper files together.

import * as React from 'react';
import FormatAlignLeftIcon from '@mui/icons-material/FormatAlignLeft';
import FormatAlignCenterIcon from '@mui/icons-material/FormatAlignCenter';
import FormatAlignRightIcon from '@mui/icons-material/FormatAlignRight';
import FormatAlignJustifyIcon from '@mui/icons-material/FormatAlignJustify';
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';

export default function ToggleButtons() {
  const [alignment, setAlignment] = React.useState<string | null>('left');

  const handleAlignment = (
    event: React.MouseEvent<HTMLElement>,
    newAlignment: string | null,
  ) => {
    setAlignment(newAlignment);
  };

  return (
    <ToggleButtonGroup
      value={alignment}
      exclusive
      onChange={handleAlignment}
      aria-label="text alignment"
    >
      <ToggleButton value="left" aria-label="left aligned">
        <FormatAlignLeftIcon />
      </ToggleButton>
      <ToggleButton value="center" aria-label="centered">
        <FormatAlignCenterIcon />
      </ToggleButton>
      <ToggleButton value="right" aria-label="right aligned">
        <FormatAlignRightIcon />
      </ToggleButton>
      <ToggleButton value="justify" aria-label="justified" disabled>
        <FormatAlignJustifyIcon />
      </ToggleButton>
    </ToggleButtonGroup>
  );
}

Example dependency boundaries

react, @mui/icons-material/FormatAlignLeft, @mui/icons-material/FormatAlignCenter, @mui/icons-material/FormatAlignRight, @mui/icons-material/FormatAlignJustify, @mui/material/ToggleButton, @mui/material/ToggleButtonGroup, @mui/icons-material/FormatBold, @mui/icons-material/FormatItalic, @mui/icons-material/FormatUnderlined, @mui/icons-material/FormatColorFill, @mui/icons-material/ArrowDropDown, @mui/material/Stack, @mui/icons-material/ViewList, @mui/icons-material/ViewModule, @mui/icons-material/ViewQuilt, @mui/icons-material/Laptop, @mui/icons-material/Tv, @mui/icons-material/PhoneAndroid, @mui/icons-material/Check, @mui/material/styles, @mui/material/Divider, @mui/material/Paper

Direct Icons/System/Utils example imports require their matching 9.4.0 packages. Relative data/helpers are part of the example closure, not extra components. Remote images and simulated operations do not supply a backend.

Pinned upstream API metadata reference

These tables retain the upstream metadata, not a newly inferred generic type system. Conditional generics, inherited props and callback details are defined by the source declarations below. Theme defaults may override metadata defaults.

ToggleButton

import ToggleButton from '@mui/material/ToggleButton';
import { ToggleButton } from '@mui/material';

Native inheritance: ButtonBase. Parent/DOM props are not fabricated as additional own props.

PropRequiredUpstream metadata type / constraintMetadata default
valueyesany—
childrennonode—
classesnoobject—
colorno'standard' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' | string'standard'
disablednoboolfalse
disableFocusRipplenoboolfalse
disableRipplenoboolfalse
fullWidthnoboolfalse
onChangenofunc—
onClicknofunc—
selectednobool—
sizeno'small' | 'medium' | 'large' | string'medium'
sxnoArray<func | object | bool> | func | object—

ToggleButtonGroup

import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import { ToggleButtonGroup } from '@mui/material';
PropRequiredUpstream metadata type / constraintMetadata default
childrennonode—
classesnoobject—
colorno'standard' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' | string'standard'
disablednoboolfalse
exclusivenoboolfalse
fullWidthnoboolfalse
onChangenofunc—
orientationno'horizontal' | 'vertical''horizontal'
sizeno'small' | 'medium' | 'large' | string'medium'
sxnoArray<func | object | bool> | func | object—
valuenoany—

Native source declarations

Declarations/imports are extracted from pinned source without flattening unions or external inheritance. Original English comments/types are source reference, not a claim of translated upstream prose. Shared types stay in their native packages.

vendor/src/ToggleButton/ToggleButton.d.ts

import { SxProps } from '@mui/system';

import { OverridableStringUnion } from '@mui/types';

import * as React from 'react';

import { Theme } from '../styles';

import { ExtendButtonBase, ExtendButtonBaseTypeMap } from '../ButtonBase';

import { OverrideProps } from '../OverridableComponent';

import { ToggleButtonClasses } from './toggleButtonClasses';

export interface ToggleButtonPropsSizeOverrides {}

export interface ToggleButtonPropsColorOverrides {}

export interface ToggleButtonOwnProps {
  /**
   * The content of the component.
   */
  children?: React.ReactNode;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<ToggleButtonClasses> | undefined;
  /**
   * The color of the button when it is in an active state.
   * It supports both default and custom theme colors, which can be added as shown in the
   * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
   * @default 'standard'
   */
  color?:
    | OverridableStringUnion<
        'standard' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning',
        ToggleButtonPropsColorOverrides
      >
    | undefined;
  /**
   * If `true`, the component is disabled.
   * @default false
   */
  disabled?: boolean | undefined;
  /**
   * If `true`, the  keyboard focus ripple is disabled.
   * @default false
   */
  disableFocusRipple?: boolean | undefined;
  /**
   * If `true`, the button will take up the full width of its container.
   * @default false
   */
  fullWidth?: boolean | undefined;
  /**
   * Callback fired when the state changes.
   *
   * @param {React.MouseEvent<HTMLElement>} event The event source of the callback.
   * @param {any} value of the selected button.
   */
  onChange?: ((event: React.MouseEvent<HTMLElement>, value: any) => void) | undefined;
  /**
   * Callback fired when the button is clicked.
   *
   * @param {React.MouseEvent<HTMLElement>} event The event source of the callback.
   * @param {any} value of the selected button.
   */
  onClick?: ((event: React.MouseEvent<HTMLElement>, value: any) => void) | undefined;
  /**
   * If `true`, the button is rendered in an active state.
   */
  selected?: boolean | undefined;
  /**
   * The size of the component.
   * The prop defaults to the value inherited from the parent ToggleButtonGroup component.
   * @default 'medium'
   */
  size?:
    | OverridableStringUnion<'small' | 'medium' | 'large', ToggleButtonPropsSizeOverrides>
    | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   * The value to associate with the button when selected in a
   * ToggleButtonGroup.
   */
  value: NonNullable<unknown>;
}

export type ToggleButtonTypeMap<
  AdditionalProps = {},
  RootComponent extends React.ElementType = 'button',
> = ExtendButtonBaseTypeMap<{
  props: AdditionalProps & ToggleButtonOwnProps;
  defaultComponent: RootComponent;
}>;

declare const ToggleButton: ExtendButtonBase<ToggleButtonTypeMap>;

export type ToggleButtonProps<
  RootComponent extends React.ElementType = ToggleButtonTypeMap['defaultComponent'],
  AdditionalProps = {},
> = OverrideProps<ToggleButtonTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
  component?: React.ElementType | undefined;
};

vendor/src/ToggleButtonGroup/ToggleButtonGroup.d.ts

import * as React from 'react';

import { SxProps } from '@mui/system';

import { OverridableStringUnion } from '@mui/types';

import { Theme } from '../styles';

import { InternalStandardProps as StandardProps } from '../internal';

import { ToggleButtonGroupClasses } from './toggleButtonGroupClasses';

export interface ToggleButtonGroupPropsSizeOverrides {}

export interface ToggleButtonGroupPropsColorOverrides {}

export interface ToggleButtonGroupProps extends StandardProps<
  React.HTMLAttributes<HTMLDivElement>,
  'onChange' | 'children'
> {
  /**
   * The content of the component.
   */
  children?: React.ReactNode;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<ToggleButtonGroupClasses> | undefined;
  /**
   * The color of the button when it is selected.
   * It supports both default and custom theme colors, which can be added as shown in the
   * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
   * @default 'standard'
   */
  color?:
    | OverridableStringUnion<
        'standard' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning',
        ToggleButtonGroupPropsColorOverrides
      >
    | undefined;
  /**
   * If `true`, only allow one of the child ToggleButton values to be selected.
   * @default false
   */
  exclusive?: boolean | undefined;
  /**
   * If `true`, the component is disabled. This implies that all ToggleButton children will be disabled.
   * @default false
   */
  disabled?: boolean | undefined;
  /**
   * If `true`, the button group will take up the full width of its container.
   * @default false
   */
  fullWidth?: boolean | undefined;
  /**
   * Callback fired when the value changes.
   *
   * @param {React.MouseEvent<HTMLElement>} event The event source of the callback.
   * @param {any} value of the selected buttons. When `exclusive` is true
   * this is a single value; when false an array of selected values. If no value
   * is selected and `exclusive` is true the value is null; when false an empty array.
   */
  onChange?: ((event: React.MouseEvent<HTMLElement>, value: any) => void) | undefined;
  /**
   * The component orientation (layout flow direction).
   * @default 'horizontal'
   */
  orientation?: 'horizontal' | 'vertical' | undefined;
  /**
   * The size of the component.
   * @default 'medium'
   */
  size?:
    | OverridableStringUnion<'small' | 'medium' | 'large', ToggleButtonGroupPropsSizeOverrides>
    | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   * The currently selected value within the group or an array of selected
   * values when `exclusive` is false.
   *
   * The value must have reference equality with the option in order to be selected.
   */
  value?: any;
}

export default function ToggleButtonGroup(props: ToggleButtonGroupProps): React.JSX.Element;

Post-install acceptance

Run strict types, production build and real interactions. Check keyboard labels/focus, disabled/error/read-only semantics where natively supported, both themes, narrow screens and reduced motion. Verify callbacks against the real host data; do not declare installation complete from copying a prompt. Initialization fixtures also require server HTML and warning-free hydration.

Source and license

https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e

MIT · @mui/material@9.4.0. Vendor originals and license are preserved. Full original upstream prose remains in vendor/docs and is not represented as fully translated.