Implementing a Custom Button Component with CSS Styling in React.js

  • Creating a dynamic and fully customizable button component in React that includes various states (active, disabled, loading, etc.) with a clickable transition effect involves defining state-specific styles controlled by CSS variables. Here's a comprehensive guide to creating such a component.
Step 1: Define CSS Variables for Button States
  • In your global CSS file (e.g., `App.css`), define CSS variables for the different states of the button:

  :root {
    --button-bg-color: #007bff;
    --button-text-color: #ffffff;
    --button-hover-bg-color: #0056b3;
    --button-active-bg-color: #004094;
    --button-disabled-bg-color: #cccccc;
    --button-disabled-text-color: #666666;
    --button-loading-bg-color: #007bff;
    --button-loading-text-color: #ffffff;
    --button-border-radius: 4px;
    --button-padding: 10px 20px;
    --button-transition: background-color 0.3s, color 0.3s;
  }


Step 2: Create the Button Component with States

  • Create a React component (`DynamicButton.js`) that can handle different button states (normal, hover, active, disabled, loading):

    import React from 'react';
    import './App.css'; // Make sure your CSS variables are defined here

    const DynamicButton = ({ isLoading, isDisabled, children, onClick }) => {
        const handleClick = () => {
            if (!isLoading && !isDisabled && onClick) {
                onClick();
            }
        };

        let buttonClassName = 'dynamic-button';
        if (isLoading) {
            buttonClassName += ' loading';
        } else if (isDisabled) {
            buttonClassName += ' disabled';
        }

        return (
            <button
                className={buttonClassName}
                onClick={handleClick}
                disabled={isDisabled || isLoading}
            >
                {isLoading ? 'Loading...' : children}
            </button>
        );
    };

    export default DynamicButton;


Step 3: Style the Button with Dynamic States

  • Now, add the necessary CSS to handle the different states of the button. This includes the use of CSS variables for easy customization:

  .dynamic-button {
    background-color: var(--button-bg-color);
    color: var(--button-text-color);
    padding: var(--button-padding);
    border: none;
    border-radius: var(--button-border-radius);
    cursor: pointer;
    transition: var(--button-transition);
  }

  .dynamic-button:hover {
    background-color: var(--button-hover-bg-color);
  }

  .dynamic-button:active {
    background-color: var(--button-active-bg-color);
  }

  .dynamic-button.disabled {
    background-color: var(--button-disabled-bg-color);
    color: var(--button-disabled-text-color);
    cursor: not-allowed;
  }

  .dynamic-button.loading {
    background-color: var(--button-loading-bg-color);
    color: var(--button-loading-text-color);
    cursor: wait;
  }


Step 4: Use the Button Component

  • You can now use the `DynamicButton` component within your app. Here's how you might use it, including handlers for different states:

  import React, { useState } from 'react';
  import DynamicButton from './DynamicButton';

  function App() {
    const [isLoading, setIsLoading] = useState(false);

    const handleButtonClick = () => {
      setIsLoading(true);
      // Simulate an async operation
      setTimeout(() => setIsLoading(false), 2000);
    };

    return (
      <div className="App">
        <DynamicButton
          isLoading={isLoading}
          onClick={handleButtonClick}
        >
          Click Me
        </DynamicButton>
        <DynamicButton
          isDisabled={true}
        >
          Disabled Button
        </DynamicButton>
      </div>
    );
  }

  export default App;


Customizing Button States

  • To customize the button for different states, users can simply override the CSS variables:

  :root {
    --button-bg-color: #28a745;
    /* Primary background color */
    --button-hover-bg-color: #218838;
    /* Hover state background color */
    --button-active-bg-color: #1e7e34;
    /* Active state background color */
    --button-disabled-bg-color: #e0e0e0;
    /* Disabled state background color */
  }

  • This approach provides a highly customizable button component in React that supports dynamic states and styles controlled through CSS variables, offering flexibility in UI design and user interaction.

No comments:

Post a Comment

Primitive Types in TypeScript

In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple ...