Skip to main content

Button Component

This example wraps Infima button classes in a reusable component. It accepts a label, link, colour variant, and size.

Creating the Button component​

Create a Button component with these props:

  • label: The text to display on the button.
  • link: The URL to navigate to when the button is clicked.
  • variant (optional): The color variant to apply to the button. This will be used to apply different colors to the button based on the Infima CSS utility classes. The default value is primary.
  • size (optional): The size variant to apply to the button. This will be used to apply different sizes to the button based on the Infima CSS utility classes. The default value is null (normal size).
  • outline (optional): A boolean value that determines whether the button should have an outline style. The default value is false.
  • block (optional): A boolean value that determines whether the button should take up the full width of its container. The default value is false.
  • disabled (optional): A boolean value that determines whether the button should be disabled. The default value is false.

Create the component file and add the implementation:

src/components/Button/index.js
import React, { CSSProperties } from 'react';
import clsx from 'clsx';
import Link from '@docusaurus/Link';

// Build the Button component with the specified props
const Button = ({
size = null, // The size of the button (e.g., 'sm', 'lg', or null)
outline = false, // Whether the button should be an outline button
variant = 'primary', // The color variant of the button
block = false, // Whether the button should be a block-level button
disabled = false, // Whether the button should be disabled
className, // Custom classes for the button
style, // Custom styles for the button
link, // The URL the button should link to
label // The text of the button
}) => {
const sizeMap = {
sm: 'sm',
small: 'sm',
lg: 'lg',
large: 'lg',
medium: null,
};
const buttonSize = size ? sizeMap[size] : '';
const sizeClass = buttonSize ? `button--${buttonSize}` : '';
const outlineClass = outline ? 'button--outline' : '';
const variantClass = variant ? `button--${variant}` : '';
const blockClass = block ? 'button--block' : '';
const disabledClass = disabled ? 'disabled' : '';
// If the button is disabled, set the destination to null.
const destination = disabled ? null : link;
return (
<Link to={destination}>
<button
className={clsx('button', sizeClass, outlineClass, variantClass, blockClass, disabledClass, className)}
style={style}
role="button"
aria-disabled={disabled}
>
{label}
</button>
</Link>
);
};

export default Button;

MDX Component Scope​

To follow the Docusaurus documentation, we create a theme folder that will host the MDXComponents file. This gives us src\theme\MDXComponents.*. You may already have a src\theme folder or an MDXComponents file if so - merge the changes described here with yours.

src\theme\MDXComponents.js
import React from 'react';
// Importing the original mapper + our components according to the Docusaurus doc
import MDXComponents from '@theme-original/MDXComponents';
import Button from '@site/src/components/Button';
export default {
// Reusing the default mapping
...MDXComponents,
Button,
};

Using the Button Component in an MDX File​

Basic​

<Button label="Click me" link="#using" />

Customized​

<Button label="Click me" link="#using" variant="secondary" size="lg" outline />

Disabled​

<Button label="Click me" link="#using" disabled />

Block​

<Button label="Click me" link="#using" block />

Custom styles​

<Button label="Click me" link="#using" style={{ backgroundColor: 'red', color: 'white', border: 'none', }} />

Combined​


<Button label="Click me" link="#using" variant="danger" size="sm" outline block />

Conclusion​

The component uses Infima classes for colour, size, outline, and block layout. Add props or styles if your project needs other variants.