Custom Collapse Component in Css and React Js

  • To create a custom Collapse component that using only React and CSS for styling, follow these steps. This component will smoothly transition content visibility on click.
Step 1: The Collapse Component
  • This Collapse component will use state to manage whether its content is shown or hidden and CSS for the transition effects.


    import React, { useState, useRef } from 'react';
    import './Collapse.css'; // Import the CSS file for styling

    const Collapse = ({ children, isOpen }) => {
        const [height, setHeight] = useState(isOpen ? 'auto' : '0px');
        const contentRef = useRef();

        // Adjust the height dynamically for the collapse animation
        const toggleCollapse = () => {
            setHeight(isOpen ? '0px' : `${contentRef.current.scrollHeight}px`);
        };

        return (
            <>
                <button onClick={toggleCollapse} className="collapse-toggle-button">
                    {isOpen ? 'Hide' : 'Show'}
                </button>
                <div
                    ref={contentRef}
                    style={{ maxHeight: `${height}` }}
                    className="collapse-content"
                >
                    <div className="collapse-inner">
                        {children}
                    </div>
                </div>
            </>
        );
    };

    export default Collapse;

Step 2: The CSS Styling

  • Next, create the `Collapse.css` file to style the collapse component. We'll use CSS transitions for the smooth opening and closing effect.


    .collapse-content {
        overflow: hidden;
        transition: max-height 0.5s ease-out;
    }

    .collapse-toggle-button {
        margin-bottom: 10px;
        cursor: pointer;
    }

    /* Add more styling as needed */

Step 3: Usage Example

  • Here's an example of how to use the Collapse component. It demonstrates a simple case where the component's open state is managed by a parent component, allowing for a toggle button to control the visibility of the collapsible content.


    import React, { useState } from 'react';
    import Collapse from './Collapse'; // Import the Collapse component

    const App = () => {
        const [isOpen, setIsOpen] = useState(false);

        const toggle = () => setIsOpen(!isOpen);

        return (
            <div className="App">
                <button onClick={toggle} style={{ marginBottom: '10px' }}>
                    Toggle Collapse
                </button>
                <Collapse isOpen={isOpen}>
                    <div style={{ padding: '20px', border: '1px solid #ddd' }}>
                        <p>This is the collapsible content!</p>
                    </div>
                </Collapse>
            </div>
        );
    };

    export default App;

  • This setup provides a basic collapse functionality that can be expanded with additional props and styles for more complex layouts or effects. The example above demonstrates a simple collapsible block of content with a button to toggle its visibility.

Custom Tooltip Component in Css and React Js

  • Creating a custom tooltip component in React with pure CSS styling, including CSS variables for easy theme management, involves handling hover events and positioning the tooltip relative to its parent element. Let's get started.
Step 1: The Tooltip Component
  • Here's a simple tooltip component that you can customize further as needed.

    import React, { useState, useRef } from 'react';
    import './Tooltip.css'; // Import the CSS file for styling

    const Tooltip = ({ children, text }) => {
        const [isVisible, setIsVisible] = useState(false);
        const tooltipRef = useRef();

        // Function to handle mouse hover
        const handleMouseOver = () => setIsVisible(true);
        const handleMouseOut = () => setIsVisible(false);

        return (
            <div className="tooltip-container" onMouseOver={handleMouseOver} onMouseOut={handleMouseOut}>
                {children}
                {isVisible && (
                    <div className="tooltip-box" ref={tooltipRef}>
                        {text}
                    </div>
                )}
            </div>
        );
    };

    export default Tooltip;


Step 2: The CSS Styling

  • Save this as `Tooltip.css`. We'll use CSS variables for easy customization and add a simple fade-in animation for the tooltip appearance.

    :root {
        --tooltip-background-color: black;
        --tooltip-color: white;
        --tooltip-font-size: 14px;
        --tooltip-border-radius: 4px;
        --tooltip-padding: 8px;
        --tooltip-margin: 10px;
        --tooltip-transition: opacity 0.3s ease;
    }

    .tooltip-container {
        position: relative;
        display: inline-block;
    }

    .tooltip-box {
        position: absolute;
        bottom: 100%;
        left: 50%;
        transform: translateX(-50%);
        background-color: var(--tooltip-background-color);
        color: var(--tooltip-color);
        text-align: center;
        border-radius: var(--tooltip-border-radius);
        padding: var(--tooltip-padding);
        margin-bottom: var(--tooltip-margin);
        font-size: var(--tooltip-font-size);
        white-space: nowrap;
        opacity: 0;
        visibility: hidden;
        transition: var(--tooltip-transition);
    }

    .tooltip-container:hover .tooltip-box {
        opacity: 1;
        visibility: visible;
    }


Step 3: Usage Example

  • To use the tooltip, wrap any element that you want to trigger the tooltip with the `Tooltip` component and pass the tooltip text via the `text` prop.

    import React from 'react';
    import Tooltip from './Tooltip'; // Import the Tooltip component

    const App = () => {
        return (
            <div className="App" style={{ marginTop: '20px', textAlign: 'center' }}>
                Hover over me:
                <Tooltip text="This is the tooltip content!">
                    <button>Hover Me</button>
                </Tooltip>
            </div>
        );
    };

    export default App;

  • This setup provides a basic, customizable tooltip that can be enhanced with additional props for positioning, delays, or animations based on your needs. The tooltip appears above the child element and can be easily styled through the CSS variables.

Custom Modal Component in Css and React Js

  • I'll guide you through creating a custom modal component in React and we will style it purely with CSS, including the use of CSS variables for easier theme management.
Step 1: The Modal Component
  • We'll start with the React component. This example assumes you're using functional components and hooks.


    import React, { useState, useEffect } from 'react';
    import './Modal.css'; // Import the CSS file for styling

    const Modal = ({ isOpen, toggle, children }) => {
        // Listen for escape key press to close the modal
        useEffect(() => {
            const closeOnEscapeKey = (e) => e.key === 'Escape' ? toggle() : null;
            document.body.addEventListener('keydown', closeOnEscapeKey);
            return () => document.body.removeEventListener('keydown', closeOnEscapeKey);
        }, [toggle]);

        if (!isOpen) {
            return null;
        }

        return (
            <div className="modal" onClick={toggle}>
                <div className="modal-content" onClick={(e) => e.stopPropagation()}>
                    <span className="close-button" onClick={toggle}>&times;</span>
                    {children}
                </div>
            </div>
        );
    };

    export default Modal;

Step 2: The CSS Styling

  • Now, let's add some CSS for the modal. We'll include CSS variables for easy customization. Save this as `Modal.css`.


    :root {
        --modal-background-color: rgba(0, 0, 0, 0.5);
        --modal-content-background-color: white;
        --modal-content-border-radius: 10px;
        --modal-transition: all 0.3s ease;
    }

    .modal {
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: var(--modal-background-color);
        display: flex;
        align-items: center;
        justify-content: center;
        animation: fadeIn var(--modal-transition);
    }

    .modal-content {
        background-color: var(--modal-content-background-color);
        margin: auto;
        padding: 20px;
        border: 1px solid #888;
        width: 80%;
        max-width: 500px;
        border-radius: var(--modal-content-border-radius);
        transition: var(--modal-transition);
    }

    .close-button {
        color: #aaaaaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
    }

    .close-button:hover,
    .close-button:focus {
        color: #000;
        text-decoration: none;
        cursor: pointer;
    }

    @keyframes fadeIn {
        from {
            opacity: 0;
        }

        to {
            opacity: 1;
        }
    }

Step 3: Usage Example

  • Finally, here's how you could use this modal component within another component.


    import React, { useState } from 'react';
    import Modal from './Modal'; // Import the Modal component

    const App = () => {
        const [isModalOpen, setIsModalOpen] = useState(false);

        const toggleModal = () => setIsModalOpen(!isModalOpen);

        return (
            <div className="App">
                <button onClick={toggleModal}>Toggle Modal</button>
                <Modal isOpen={isModalOpen} toggle={toggleModal}>
                    <h2>Modal Title</h2>
                    <p>This is modal content</p>
                </Modal>
            </div>
        );
    };

    export default App;

  • This setup gives you a customizable modal component with basic styling and transitions that can be easily adjusted using CSS variables. You can expand upon this by adding more props for further customization, such as modal size or animations.

Custom Progress Bar in React Js

  • To create a custom progress bar component in React with dynamic styling using CSS variables, you can follow the steps below. This approach involves setting CSS variables directly on the component's style attribute, allowing you to modify these variables later, which will be reflected in the progress bar's appearance.
Step 1: Set Up Your React Component
  • First, create a new React component named `ProgressBar.js`. In this component, you will accept `progress` (to indicate the progress percentage) and `styleVars` (an object containing CSS variable values) as props.

  import React from 'react';
  import './ProgressBar.css';

  const ProgressBar = ({ progress, styleVars }) => {
    const progressBarStyle = {
      ...styleVars,
      width: `${progress}%`,
    };

    return (
      <div className="progress-bar-container" style={{ '--progress-bar-height': '20px', ...styleVars }}>
        <div className="progress-bar" style={progressBarStyle}></div>
      </div>
    );
  };

  export default ProgressBar;


Step 2: Style Your Component with CSS

  • Next, you need to define some basic styles for your progress bar. You can do this in a CSS file associated with your component. Here's an example you can include in `ProgressBar.css`:

    .progress-bar-container {
        width: 100%;
        background-color: #eee;
        border-radius: 8px;
        overflow: hidden;
        height: var(--progress-bar-height, 20px);
    }

    .progress-bar {
        height: 100%;
        background-color: var(--progress-bar-color, #4caf50);
        transition: width 0.4s ease;
        border-radius: 8px 0 0 8px;
    }

  • Make sure to import this CSS file into your `ProgressBar.js` component:
Step 3: Use Your Component
  • Now, you can use your `ProgressBar` component within your application and dynamically change its style by passing a `styleVars` prop. Here's an example of how to use it in an `App.js` component:

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

  const App = () => {
    const [progress, setProgress] = useState(50);
    const styleVars = {
      '--progress-bar-color': '#ff4500',
      '--progress-bar-height': '30px',
    };

    // Function to simulate progress
    const increaseProgress = () => {
      setProgress((prevProgress) => (prevProgress >= 100 ? 0 : prevProgress + 10));
    };

    return (
      <div>
        <ProgressBar progress={progress} styleVars={styleVars} />
        <button onClick={increaseProgress}>Increase Progress</button>
      </div>
    );
  };

  export default App;


Explanation

  • CSS Variables: In this setup, the `styleVars` prop allows you to pass CSS variable values dynamically to your `ProgressBar` component. You can define any number of CSS variables within this prop to control various aspects of the progress bar's styling, such as its color or height.
  • Dynamic Styles: The progress bar's width is controlled by the `progress` prop, and its appearance (color, height, etc.) can be dynamically adjusted via the `styleVars` prop containing CSS variable values.
  • This approach offers a flexible way to create and manipulate a progress bar's appearance in your React applications.

Phase 2 — Angular Fundamentals

Chapter 1 — What is Angular and How Does it Think? 1.1 — What is Angular? Angular is a framework for building web applications. A framewo...