Skip to content

[button] Remove discriminated props union - #3643

Merged
atomiks merged 2 commits into
mui:masterfrom
atomiks:fix/button-props
Jan 14, 2026
Merged

atomiks merged 2 commits into
mui:masterfrom
atomiks:fix/button-props

Conversation

@atomiks

@atomiks atomiks commented Dec 30, 2025

Copy link
Copy Markdown
Contributor

Trying to use Omit, & {...}, extends etc. breaks the Button.Props type. All other button components (e.g. Popover.Trigger) do not yet handle this either. It can be solved with overloads, but this also requires wrappers to handle overloads as well to retain the typesafety. The types for all button components can be changed in a separate PR.

Fixes #3641

@atomiks atomiks added component: button Changes related to the button component. type: bug It doesn't behave as expected. typescript labels Dec 30, 2025
@pkg-pr-new

pkg-pr-new Bot commented Dec 30, 2025 •

Copy link
Copy Markdown
  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@3643
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@3643
    

commit: ddd9a44

@mui-bot

mui-bot commented Dec 30, 2025 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Dec 30, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit ddd9a44
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/696757d6c38017000858cec2
😎 Deploy Preview https://deploy-preview-3643--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks marked this pull request as ready for review December 30, 2025 02:06

@battaglr battaglr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I cannot think of any other way of typing this so TS can properly infer types for native and non-native buttons. Maybe there's some TS dark magic that we don't know about? Even then, I think this is a good enough. 🙌

"type": "boolean",
"detailedType": "boolean | undefined"
},
"disabled": {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The disabled prop is already a native prop part of HTMLButtonProps, so it's redundant here (at least with the current solution)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add dev-time warnings if native button props are used alongside nativeButton={false}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runtime warnings are better than nothing sure, but this applies to every button component, so it's probably better done in a separate PR to make it cohesive across the library

@LukasTy LukasTy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried defining the discriminating union differently?
I don't remember having problems with this in MUI X. 🤔

diff --git a/packages/react/src/button/Button.spec.tsx b/packages/react/src/button/Button.spec.tsx
index 19f4258ea..be337d0b0 100644
--- a/packages/react/src/button/Button.spec.tsx
+++ b/packages/react/src/button/Button.spec.tsx
@@ -7,4 +7,5 @@ import { Button } from '@base-ui/react/button';
 <Button nativeButton={false} render={(props) => <div {...props} />} />;
 <Button nativeButton={false} disabled render={<span />} />;
 
+// @ts-expect-error -- 'type' is not a valid prop when 'nativeButton' is false
 <Button nativeButton={false} type="submit" />;
diff --git a/packages/react/src/button/Button.tsx b/packages/react/src/button/Button.tsx
index 72eab0875..be1f67317 100644
--- a/packages/react/src/button/Button.tsx
+++ b/packages/react/src/button/Button.tsx
@@ -10,8 +10,11 @@ import type { BaseUIComponentProps, NativeButtonProps } from '../utils/types';
  *
  * Documentation: [Base UI Button](https://base-ui.com/react/components/button)
  */
-export const Button = React.forwardRef(function Button(
-  componentProps: Button.Props,
+export const Button = React.forwardRef(function Button<
+  TNativeButton extends boolean = true,
+  TElement extends React.ElementType = 'button',
+>(
+  componentProps: Button.Props<TNativeButton, TElement>,
   forwardedRef: React.ForwardedRef<HTMLElement>,
 ) {
   const {
@@ -43,7 +46,7 @@ export const Button = React.forwardRef(function Button(
     ref: [forwardedRef, buttonRef],
     props: [elementProps, getButtonProps],
   });
-});
+}) as ButtonComponent;
 
 export interface ButtonState {
   /**
@@ -52,16 +55,44 @@ export interface ButtonState {
   disabled: boolean;
 }
 
-export interface ButtonProps
-  extends NativeButtonProps, BaseUIComponentProps<'button', ButtonState> {
-  /**
-   * Whether the button should be focusable when disabled.
-   * @default false
-   */
-  focusableWhenDisabled?: boolean;
-}
+type NonNativeAttributeKeys =
+  | 'form'
+  | 'formAction'
+  | 'formEncType'
+  | 'formMethod'
+  | 'formNoValidate'
+  | 'formTarget'
+  | 'name'
+  | 'type'
+  | 'value';
+
+export type ButtonProps<
+  TNativeButton extends boolean,
+  TElement extends React.ElementType,
+> = NativeButtonProps<TNativeButton> &
+  (TNativeButton extends true
+    ? Omit<BaseUIComponentProps<TElement, ButtonState>, 'disabled'>
+    : Omit<BaseUIComponentProps<TElement, ButtonState>, NonNativeAttributeKeys>) & {
+    /**
+     * Whether the button should be focusable when disabled.
+     * @default false
+     */
+    focusableWhenDisabled?: boolean;
+    // Whether the button should ignore user interaction.
+    disabled?: boolean;
+  };
 
 export namespace Button {
   export type State = ButtonState;
-  export type Props = ButtonProps;
+  export type Props<
+    TNativeButton extends boolean,
+    TElement extends React.ElementType,
+  > = ButtonProps<TNativeButton, TElement>;
 }
+
+type ButtonComponent = <
+  TNativeButton extends boolean = true,
+  TElement extends React.ElementType = 'button',
+>(
+  props: Button.Props<TNativeButton, TElement> & { ref?: React.Ref<HTMLElement> },
+) => React.ReactElement | null;
diff --git a/packages/react/src/utils/types.ts b/packages/react/src/utils/types.ts
index 16ffd8155..e418cd2af 100644
--- a/packages/react/src/utils/types.ts
+++ b/packages/react/src/utils/types.ts
@@ -74,14 +74,14 @@ export type BaseUIComponentProps<
   style?: React.CSSProperties | ((state: State) => React.CSSProperties | undefined);
 };
 
-export interface NativeButtonProps {
+export interface NativeButtonProps<TNativeButton extends boolean> {
   /**
    * Whether the component renders a native `<button>` element when replacing it
    * via the `render` prop.
    * Set to `false` if the rendered element is not a button (e.g. `<div>`).
    * @default true
    */
-  nativeButton?: boolean;
+  nativeButton?: TNativeButton;
 }
 
 export interface NonNativeButtonProps {

@atomiks

atomiks commented Jan 12, 2026

Copy link
Copy Markdown
Contributor Author

@LukasTy that definitely looks promising~ I think it's better to change this for all <button> components simultaneously though, in a separate PR

@LukasTy

LukasTy commented Jan 12, 2026

Copy link
Copy Markdown
Member

Sounds nice. 👍
I take it if the separate PR works out, this one won't be needed anymore?

@atomiks

atomiks commented Jan 12, 2026

Copy link
Copy Markdown
Contributor Author

I think it's worth merging this in the meantime to fix the linked issue (and make it consistent with other components' props currently)

@atomiks atomiks mentioned this pull request Jan 13, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: button Changes related to the button component. type: bug It doesn't behave as expected. typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot set Button type prop

5 participants