JGuang

Button

Button · Mantine Core

Preview
npm install @mantine/core@9.7.1 @mantine/hooks@9.7.1

Button

Install: npm install @mantine/core@9.7.1 @mantine/hooks@9.7.1

Version and compatibility

Pinned Mantine Core 9.7.1 requires React and React DOM ^19.2.0 and matching @mantine/hooks@9.7.1, from the installed npm package's peerDependencies. Do not force it into React 18 or another incompatible version. Explain upgrade impact and agree on a plan with the project owner; do not hide conflicts with --force/--legacy-peer-deps. The native Provider enables theme.respectReducedMotion and uses forceColorScheme for light/dark.

Use case

Trigger a user action with native button semantics.

Native props used by this example

These props are extracted from the local typechecked JSX and grouped by their owning native component. This is not an exhaustive prop list; see pinned source signatures and inheritance below.

Component propExplanation
Button.onClickLocal click handler; replace demo logic with an explicit business action.
Button.variantNative visual variant; not the gallery's scenario identifier.
Button.disabledDisable user input/action; not equivalent to readOnly.

Interaction and state checks

Current scenarios: Default. Check actual behavior after selecting supported disabled/error/read-only scenarios; components without those native states do not receive invented universal props. Verify keyboard focus and exits, then connect changed values through the native callback signature.

Business integration boundary

Demo content/state is local, without server persistence, authorization or fetching. A static display component is not a completed business workflow.

Host setup

Install pinned @mantine/core and @mantine/hooks, import @mantine/core/styles.css at the entry and use MantineProvider. The example's theme/variant are JGuang-authored host props, not universal native component APIs; forceColorScheme synchronizes the app theme. Native implementation source is a reading reference: use npm exports rather than copying internal engines. This batch covers Core, not dates/charts/forms extension packages. Detailed upstream prose remains English.

Runnable usage

import { useState } from 'react';
import * as M from '@mantine/core';
import '@mantine/core/styles.css';


function ComponentPreview({ variant = 'Default' }: { variant?: string }) {
const [open, setOpen] = useState(false);
const disabled = variant === "Disabled";
return <M.Group><M.Button onClick={() => setOpen(!open)}>{open ? "Clicked / 已点击" : "Click me / 点击"}</M.Button><M.Button variant="outline">Outline</M.Button><M.Button disabled>Disabled</M.Button></M.Group>;
}
export default function Example({ variant = 'Default', theme = 'light' }: { variant?: string; theme?: 'light' | 'dark' }) {
return <M.MantineProvider forceColorScheme={theme} theme={{ respectReducedMotion: true }}><ComponentPreview variant={variant} /></M.MantineProvider>;
}

Preview states

Default

The example uses the native Mantine API. For supported input demos, change the local variant to Disabled, Invalid or Read only; these are example labels, not new component props. Keep the documented data, controlled values, stores and compound children when adapting the example.

Usage

Full width

If the fullWidth prop is set, the Button will take 100% of the parent width:

Left and right sections

leftSection and rightSection allow adding icons or any other element to the left and right sides of the button. When a section is added, padding on the corresponding side is reduced.

Note that leftSection and rightSection are flipped in RTL mode (leftSection is displayed on the right and rightSection is displayed on the left).

Sections position

The justify prop sets the justify-content of the inner element. You can use it to change the alignment of left and right sections. For example, to spread them across the button, set justify="space-between".

If you need to align just one section to the side of the button, set justify to space-between and add an empty <span /> to the opposite section.

Compact size

Button supports xs – xl and compact-xs – compact-xl sizes. compact sizes have the same font size as xs – xl but with reduced padding and height.

Disabled state

To make a Button disabled, set the disabled prop. This will prevent any interactions with the button and add disabled styles. If you want the button to just look disabled but still be interactive, set the data-disabled prop instead. Note that disabled styles are the same for all variants.

The <a /> element does not support the disabled attribute. To make a Button disabled when it is rendered as a link, set the data-disabled attribute instead and prevent default behavior in the onClick event handler.

Customize disabled styles

To customize disabled styles, it is recommended to use both &:disabled and &[data-disabled] selectors:

  • &:disabled is used to style the button when the disabled prop is set and also when the button is disabled by the parent component (for example, when the disabled prop is set on a <fieldset /> element which contains the Button).
  • &[data-disabled] is used to style the button when it is not actually disabled but should look like it is (for example, data-disabled should be used if you need to use Tooltip with a disabled Button or when the Button is used as a link)

Disabled button with Tooltip

The onMouseLeave event is not triggered when a Button is disabled, so if you need to use a Tooltip with a disabled Button, you need to set the data-disabled prop on the Button instead of disabled. Note that it is also required to change the onClick event handler to (event) => event.preventDefault() as the Button is not actually disabled and will still trigger the onClick event.

Loading state

When the loading prop is set, the Button will be disabled and a Loader with overlay will be rendered in the center of the button. Loader color depends on the Button variant.

Loader props

You can customize the Loader with the loaderProps prop, which accepts all props that the Loader component has:

Example of customizing Button with Styles API and data-* attributes:

Custom variants

To add new Button variants, use the data-variant attribute. Usually new variants are added to the theme, this way they are available in all Button components in your application.

Customize variant colors

You can customize colors for Button and other component variants by adding variantColorResolver to your theme.

Button.Group

Note that you must not wrap child Button components with any additional elements:

import { Button } from '@mantine/core';

function Demo() {
  return (
    <Button.Group>
      <div>
        <Button>This will not work</Button>
      </div>
      <Button>Buttons will have incorrect borders</Button>
    </Button.Group>
  );
}

Button.GroupSection

Use Button.GroupSection component to render sections that are not buttons inside Button.Group:

Source-backed parameter reference

Implementation signatures and types are generated from the pinned local source. Optional props, native inherited types, union branches and never exclusions are preserved. A forwardRef implementation's ref is a React reference, not an additional business prop.

Button(_props)
export type ButtonSize = MantineSize | `compact-${MantineSize}` | (string & {});

export type ButtonStylesNames = 'root' | 'inner' | 'loader' | 'section' | 'label';

export type ButtonVariant =
  | 'filled'
  | 'light'
  | 'outline'
  | 'transparent'
  | 'white'
  | 'subtle'
  | 'default'
  | 'gradient';

export type ButtonCssVariables = {
  root:
    | '--button-justify'
    | '--button-height'
    | '--button-padding-x'
    | '--button-fz'
    | '--button-radius'
    | '--button-bg'
    | '--button-hover'
    | '--button-hover-color'
    | '--button-color'
    | '--button-bd';
};

export interface ButtonProps extends BoxProps, StylesApiProps<ButtonFactory> {
  'data-disabled'?: boolean;

  /** Controls button `height`, `font-size` and horizontal `padding` @default 'sm' */
  size?: ButtonSize;

  /** Key of `theme.colors` or any valid CSS color @default theme.primaryColor */
  color?: MantineColor;

  /** Sets `justify-content` of `inner` element, can be used to change distribution of sections and label @default 'center' */
  justify?: React.CSSProperties['justifyContent'];

  /** Content on the left side of the button label */
  leftSection?: React.ReactNode;

  /** Content on the right side of the button label */
  rightSection?: React.ReactNode;

  /** Sets `width: 100%` @default false */
  fullWidth?: boolean;

  /** Key of `theme.radius` or any valid CSS value to set `border-radius` @default theme.defaultRadius */
  radius?: MantineRadius;

  /** Gradient configuration used for `variant="gradient"` @default theme.defaultGradient */
  gradient?: MantineGradient;

  /** Sets `disabled` attribute, applies disabled styles */
  disabled?: boolean;

  /** Button content */
  children?: React.ReactNode;

  /** If set, the `Loader` component is displayed over the button */
  loading?: boolean;

  /** Props added to the `Loader` component (only visible when `loading` prop is set) */
  loaderProps?: LoaderProps;

  /** If set, adjusts text color based on background color for `filled` variant */
  autoContrast?: boolean;
}

export type ButtonFactory = PolymorphicFactory<{
  props: ButtonProps;
  defaultRef: HTMLButtonElement;
  defaultComponent: 'button';
  stylesNames: ButtonStylesNames;
  vars: ButtonCssVariables;
  variant: ButtonVariant;
  staticComponents: {
    Group: typeof ButtonGroup;
    GroupSection: typeof ButtonGroupSection;
  };
}>;

export type Mod = Record<string, any> | string;

export type BoxMod = Mod | Mod[] | BoxMod[];

export interface BoxProps extends MantineStyleProps {
  /** Class added to the root element, if applicable */
  className?: string | undefined;

  /** Inline style added to root component element, can subscribe to theme defined on MantineProvider */
  style?: MantineStyleProp;

  /** CSS variables defined on root component element */
  __vars?: CssVarsProp;

  /** `size` property passed down the HTML element */
  __size?: string;

  /** Breakpoint above which the component is hidden with `display: none` */
  hiddenFrom?: MantineBreakpoint;

  /** Breakpoint below which the component is hidden with `display: none` */
  visibleFrom?: MantineBreakpoint;

  /** Determines whether component should be hidden in light color scheme with `display: none` */
  lightHidden?: boolean;

  /** Determines whether component should be hidden in dark color scheme with `display: none` */
  darkHidden?: boolean;

  /** Element modifiers transformed into `data-` attributes, for example, `{ 'data-size': 'xl' }`, falsy values are removed */
  mod?: BoxMod;
}

export interface CSSProperties extends React.CSSProperties {
  [key: string]: any;
}

type MantineStyle = CSSProperties | ((theme: MantineTheme) => CSSProperties);

export type MantineStyleProp = MantineStyle | MantineStyle[] | MantineStyleProp[] | undefined;

export type CssVariable = `--${string}`;

export type CssVariables<Variable extends string = CssVariable> = Partial<Record<Variable, string>>;

export type CssVars<Variable extends string = CssVariable> =
  | CssVariables<Variable>
  | ((theme: MantineTheme) => CssVariables<Variable>)
  | CssVars<Variable>[];

export type CssVarsProp<Variable extends string = CssVariable> =
  | CssVars<Variable>
  | CssVars<Variable>[];

Native and external types

External types remain defined by React or native dependencies, not manually expanded into guessed props. The installed version's TypeScript definitions are authoritative.

PolymorphicFactory<{
  props: ButtonProps;
  defaultRef: HTMLButtonElement;
  defaultComponent: 'button';
  stylesNames: ButtonStylesNames;
  vars: ButtonCssVariables;
  variant: ButtonVariant;
  staticComponents: {
    Group: typeof ButtonGroup;
    GroupSection: typeof ButtonGroupSection;
  };
}>
MantineStyleProps
React.CSSProperties
MantineTheme
Partial<Record<Variable, string>>
Record<Variable, string>
Variable
MantineBreakpoint
Record<string, any>
StylesApiProps<ButtonFactory>
MantineSize
MantineColor
React.ReactNode
MantineRadius
MantineGradient
LoaderProps
HTMLButtonElement