Repository navigation
[button] Remove discriminated props union - #3643
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
1229d0a to
81c0869
Compare
battaglr
left a comment
There was a problem hiding this comment.
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": { |
There was a problem hiding this comment.
The disabled prop is already a native prop part of HTMLButtonProps, so it's redundant here (at least with the current solution)
There was a problem hiding this comment.
We could add dev-time warnings if native button props are used alongside nativeButton={false}
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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 {|
@LukasTy that definitely looks promising~ I think it's better to change this for all |
|
Sounds nice. 👍 |
|
I think it's worth merging this in the meantime to fix the linked issue (and make it consistent with other components' props currently) |
Trying to use
Omit,& {...},extendsetc. breaks theButton.Propstype. 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