Button E

Button with background color and highlighted icon.

Preview

Code

import React from "react"; import styles from "./button-e.module.css"; type Props = { className?: string; style?: React.CSSProperties; children?: React.ReactNode; type?: "button" | "submit" | "reset" | "link"; iconElement?: React.ReactNode; iconPosition?: "left" | "right"; size?: "x-small" | "small" | "medium" | "large"; width?: "auto" | "full"; height?: "auto" | "medium" | "large"; rounded?: "none" | "small" | "medium" | "large" | "full"; shadow?: "none" | "medium"; // Put additional variants here, then define them in the CSS variant?: "default" | "purple" | "light-blue" | "red" ; // Optional event handler onClick?: React.MouseEventHandler; }; export default function ButtonE({ className, style, children, type = "button", iconElement, iconPosition = "right", size = "medium", height = "auto", width = "auto", rounded = "full", shadow = "medium", variant = "default", onClick, }: Props) { const _classNames = [ styles["button"], styles[`s-${size}`], styles[`h-${height}`], styles[`w-${width}`], styles[`r-${rounded}`], styles[`sh-${shadow}`], styles[`variant-${variant}`], !children && styles[`icon-only`], className ].join(" "); const content = ( <> {iconElement && iconPosition === "left" && ( <span className={[styles["icon"], styles["icon-left"]].join(" ")} style={style}> {iconElement} </span> )} {children && <span className={styles["text"]}>{children}</span>} {iconElement && iconPosition === "right" && ( <span className={[styles["icon"], styles["icon-right"]].join(" ")} style={style}> {iconElement} </span> )} </> ); // type of the button if (type === "link") { // Link button should be embedded inside an <a href=... > tag or <Link href=...> if you use NextJs return ( <span onClick={onClick} className={_classNames}> {content} </span> ); } else { return ( <button onClick={onClick} className={_classNames} type={type}> {content} </button> ); } }

Design

Figma design file:

Documentation

Properties

Props of the component:

  • className (string): Specifies the CSS class of the component.

  • children (ReactNode): React children of the component. Specifies the ReactNode placed inside the component.

  • type ("button" | "submit" | "reset" | "link"): Specifies the type of the button.

  • iconElement (ReactNode): Specifies the icon component.

  • iconPosition ("left" | "right"): Specifies the position of the icon relative to the text.

  • size ("x-small" | "small" | "medium" | "large"): Specifies the size of the button.

  • width ("auto" | "full"): Specifies the width of the button. "full" will make the button occupy 100% of the width of the parent and "auto" will set the width of the button to the width of the content.

  • height ("auto" | "medium" | "large"): Specifies the height of the button.

  • rounded ("none" | "small" | "medium" | "large" | "full"): Specifies the border radius of the frame.

  • shadow ("none" | "medium"): Specifies the size of the shadow of the frame.

  • variant ("default" | "purple" | "light-blue" | "red" or a customized value): Specifies the color or theme variant of the component. See the "Sample CSS customization" below for an example of usage.

  • onClick (function): Fires when the button is clicked.

Sample CSS customization
.variant-red { --fg-color: white; --bg-color: rgb(240, 13, 62); --shadow-color: rgb(252, 57, 99); }