"use client";
import { type ButtonHTMLAttributes, type ReactElement, type Ref } from "react";
import { type ComponentWithRippleProps } from "../interaction/types.js";
import { useElementInteraction } from "../interaction/useElementInteraction.js";
import { useHigherContrastChildren } from "../interaction/useHigherContrastChildren.js";
import { type PropsWithRef } from "../types.js";
import {
FloatingActionButton,
type FloatingActionButtonPosition,
type FloatingActionButtonProps,
} from "./FloatingActionButton.js";
import { type ButtonClassNameThemeOptions, button } from "./styles.js";
export interface ButtonProps
extends
ButtonHTMLAttributes,
ButtonClassNameThemeOptions,
ComponentWithRippleProps {
ref?: Ref;
/** @defaultValue `"button"` */
type?: "button" | "reset" | "submit";
/**
* The position within the viewport to display the button as a floating action
* button.
*/
floating?: FloatingActionButtonPosition;
/**
* Any additional props to provide the to `FAB` container element when the
* `floating` prop is provided
*/
floatingProps?: PropsWithRef;
}
/**
* **Client Component**
*
* @example Simple Example
* ```tsx
* import { Button } from "@react-md/core/button/Button";
* import type { ReactElement } from "react";
*
* function Example(): ReactElement {
* return (
*
* );
* }
* ```
*
* @example Theme Example
* ```tsx
* import { Button } from "@react-md/core/button/Button";
* import type { ReactElement } from "react";
*
* function Example(): ReactElement {
* return (
*
* );
* }
* ```
*
* @example Icon Button Example
* ```tsx
* import { Button } from "@react-md/core/button/Button";
* import FavoriteIcon from "@react-md/material-icons/FavoriteIcon";
* import type { ReactElement } from "react";
*
* function Example(): ReactElement {
* return (
*
* );
* }
* ```
*
* @example Text Button with icons
* ```tsx
* import { Button } from "@react-md/core/button/Button";
* import FavoriteIcon from "@react-md/material-icons/FavoriteIcon";
* import type { ReactElement } from "react";
*
* function Example(): ReactElement {
* return (
*
* );
* }
* ```
*
* @see {@link https://react-md.dev/components/button | Button Demos}
*/
export function Button(props: ButtonProps): ReactElement {
const {
ref,
type = "button",
disabled = false,
floating = null,
floatingProps,
theme = floating ? "secondary" : "clear",
themeType = floating ? "contained" : "flat",
iconSize,
buttonType = floating || iconSize ? "icon" : "text",
className,
responsive,
children: propChildren,
onBlur,
onClick,
onKeyDown,
onKeyUp,
onMouseDown,
onMouseUp,
onMouseLeave,
onDragStart,
onTouchStart,
onTouchEnd,
onTouchMove,
disableRipple,
...remaining
} = props;
const isThemeDisabled = theme === "disabled";
const ariaDisabled = props["aria-disabled"];
const { pressed, pressedClassName, ripples, handlers } =
useElementInteraction({
mode: disableRipple ? "press" : undefined,
onBlur,
onClick,
onKeyDown,
onKeyUp,
onMouseDown,
onMouseUp,
onMouseLeave,
onDragStart,
onTouchStart,
onTouchEnd,
onTouchMove,
disabled:
disabled ||
isThemeDisabled ||
(ariaDisabled && ariaDisabled !== "false"),
});
const children = useHigherContrastChildren(propChildren);
return (
);
}