Skip to main content

Button Component

The <Button> component is a versatile React component that provides various styling and functional options. It supports icons, images, loading states, and different button styles.

Basic Usage​

All of the React.CSSProperties are valid prop with the benefit of passing just the number without unit (px are used by default). The library extents the CSS properties and adds all of them with Hover suffix that will take affect only when the component is hovered by the device pointer.

import { Button } from "react-better-html";

function App() {
return (
<Button
text="Click me"
onClick={(event) => {
console.log("Button was clicked. Event:", event);
}}
/>
);
}

Common Props​

All standard <button> attributes are valid props for this component.

import { Button } from "react-better-html";

function App() {
return (
<Button
text="Click me"
backgroundColor="#ff0000"
title="A `click me` button"
onClick={(event) => {
console.log("Button was clicked. Event:", event);
}}
/>
);
}

Value click​

The library provides a new click event called onClickWithValue that is of type (value: Value) => void and is used together with the value prop. The Value type is generic and will automatically inherit the value prop type.

import { Button } from "react-better-html";

function App() {
return (
<Button
text="Click me"
value={12}
onClickWithValue={(value) => {
console.log(value); // Will log to the console "12"
}}
/>
);
}
note

Keep in mind that onClick and onClickWithValue will both fire if passed.

When the href prop is passed the button renders as an <a> tag (or the linkComponent from the components configuration, e.g. the Link from react-router-dom). The target and download props are passed to the link.

import { Button } from "react-better-html";

function App() {
return (
<Button
text="Open docs"
href="https://github.com/krissvv/react-better-html"
target="_blank"
/>
);
}

Small and Submit​

  • isSmall - renders the button with smaller padding
  • isSubmit - sets the button type to submit so it can submit the parent <form>
import { Button } from "react-better-html";

function App() {
return (
<form>
<Button text="Save" isSmall isSubmit />
</form>
);
}

Icon​

The icon prop allows you to specify an icon to display on the button.

import { Button } from "react-better-html";

function App() {
return <Button text="Close" icon="XMark" />;
}

There are props to control the icon style:

  • icon - the name of the icon of type IconName
  • iconPosition - weather to put the icon on the left or on the right side of the text (defaults to left)
  • iconColor - the color of the icon (defaults to the color of the text)
  • iconSize - the size of the icon (default to the font-side of the text)

Image​

The image prop allows you to specify an image to display on the button.

import { Button } from "react-better-html";

function App() {
return <Button text="react-better-html" image="logo" />;
}

There are props to control the image style:

  • image - the name of the image of type AssetName
  • imagePosition - weather to put the image on the left or on the right side of the text (defaults to left)
  • imageWidth - the width of the image (default to the font-side of the text)
  • imageHeight - the height of the image

Loading State​

The isLoading prop allows you to display a loading indicator on the button. You can also pass a loaderName prop and the button will be in loading state while the global loader with that name is true. The size of the loader can be changed with loaderSize. Clicks are ignored while the button is loading.

import { Button } from "react-better-html";

function App() {
return (
<Button
text="Loading"
isLoading
/>
);
}

Disabled State​

The disabled prop allows you to disable the button.

import { Button } from "react-better-html";

function App() {
return (
<Button
text="Click me"
disabled
onClick={(event) => {
console.log("Button was clicked. Event:", event);
}}
/>
);
}

Subcomponents​

A number of components in the library have a subcomponent feature witch is like a preset of the same component that is frequently used.

Button.secondary​

This component is styles like a secondary button.

import { Div, Button } from "react-better-html";

function App() {
return (
<Div.row gap={10}>
<Button text="Start now" />
<Button.secondary text="Reed more" />
</Div.row>
);
}

Button.destructive​

This component is with a destructive style (red background).

import { Div, Button } from "react-better-html";

function App() {
return (
<Div.row gap={10}>
<Button text="Keep reading" />
<Button.destructive text="Close" />
</Div.row>
);
}

Button.text​

This component renders like a bold text link - no background, no padding and a lighter color on hover.

import { Div, Button } from "react-better-html";

function App() {
return (
<Div.row gap={10}>
<Button text="Sign up" />
<Button.text text="I already have an account" />
</Div.row>
);
}

Button.icon​

This component is with an icon only and has a circular shape.

import { Button } from "react-better-html";

function App() {
return <Button.icon icon="XMark" />;
}

There are props to control the icon button style:

  • size - the size of the icon (defaults to 16)
  • buttonSize - the size of the circular button (defaults to size + theme.styles.space)
  • backgroundButtonColor - the color of the circle on hover (defaults to textPrimary with transparency)

Button.upload​

This component triggers a file upload and has an upload icon. It also handles the files with the onUpload prop.

import { Button } from "react-better-html";

function App() {
return (
<Button.upload
text="Pick images"
accept="image/*"
multiple
onUpload={(files) => {
console.log("Selected files:", files);
}}
/>
);
}
  • accept - the file types the picker allows (same as the accept attribute of <input type="file">)
  • multiple - whether multiple files can be selected