Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Development

    Enhancing Software Development Efficiency: The Role of Version Control

    Comics

    Outdated MVP

    CSS

    How to build complex layouts with CSS Grid

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Sunday, September 28
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / React / How to Creact a React Component Library – Using a Modal Example
    JavaScript

    How to Creact a React Component Library – Using a Modal Example

    dillionmegidaBy dillionmegidaAugust 31, 2020Updated:August 31, 2020No Comments7 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    How to Creact a React Component Library – Using a Modal Example
    How To Careate a React Ccomponent Library - Using a Modal Example
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    How To Careate a React Ccomponent Library - Using a Modal Example
    How To Careate a React Ccomponent Library – Using a Modal Example

    Components allow us to break applications into smaller pieces that can be worked on independently. Another good thing about them is that they allow us to reuse logic. Such components will be able to be used in (and with) several other components across the application.

    When working on a project, you can simply store the components in a components folder where all pages and components within your project can access it.

    But, have you ever found yourself needing a very component you use all the time across different projects? Where you’d have to copy and paste the component’s file or folder into another codebase?

    Instead of doing this, you can have your own React library. Tools like Chakra UI and Material UI are React component libraries that can be adopted by any react project.

    However, creating a react library is different from how you normally would for a component within a project.

    Here’s an example of creating a component in a project:

    import React from 'react'
    
    const Input = props => {
      const {label, placeholder} = props;
    
      return <div>
        <label htmlFor={label}>{label}</label>
        <input id={label} placeholder={placeholder} />
      </div>
    }
    
    export default Input

    This is just okay for a project-based component. The above is code for development and cannot be used directly in production.

    For a library, the components created would have to be processed into production code which can be then adopted by other applications.

    As we continue in this article, we’ll learn how to create a component library using a Modal example.

    How to create a Modal component Library

    For this example, we are going to create a modal component that would be exported from the library.

    Read More:  React Hooks + RxJS or How React Is Meant to Be

    Firstly, we would need to set up our environment. There are few ways to create a react library but I’ve found create-react-library to be seamless and easily customizable.

    create-react-library supports typescript, CSS modules, Babel for transpiling, and Jest to name a few.

    Let’s get started building our library.

    Install Library

    npm i -g create-react-library

    This command would install the library globally in your device. If you do not want it this way, here’s an alternative:

    npx create-react-library
    

    The above would be executed when you want to start the project. With npx, the library would not be installed globally but used directly (from the source) to create your project folder.

    Create the project folder

    For this example, we’ll stick with react-modal as the name of our project.

    You may want to change the name to something else if you intend on publishing it on npm because npm has unique names for packages.

    If you installed the library globally, create a project by doing so:

    create-react-library react-modal
    

    Else,

    npx create-react-library react-modal

    The above command would display a list of options which are:

    * The template can be default (which is JavaScript), typescript or custom. For this example, we’ll use default.

    After filling in your answers, the library would create your folder, install the required dependencies set up default files for your library. Here’s the tree structure:

    react-modal/
    ├── dist
    ├── example
    ├── node_modules
    ├── package.json
    ├── package-lock.json
    ├── README.md
    └── src

    On install, the library creates an src directory which contains the files for the library, a dist directory which contains the production code of the library and an example directory which contains codes that use the library we are making. This way, we can make changes to the library and simultaneously visualize the result when in use.

    Read More:  Introduction to the Best Code Playgrounds: JSFiddle, Codepen, and CodeSandbox

    See it this way: src directory is the library in development and example directory is the library in production (which uses the codes in the dist directory).

    Start server

    The only directory that would require a server is the example directory because that is where we preview the result. There’s nothing to preview in the src directory as the library on its own is a utility and not an application.

    However, the src/package.json file also has a start script which watches for change in the library and automatically builds the production code of the library in dist.

    The following are commands to run to get started:

    npm start # start script for the library
    cd example # change directory
    npm start # start server for example application

    Here’s is the webpage on localhost:3000:

    Screenshot of the default webpage created using create-react-library
    Screenshot of the default webpage created using create-react-library

    The example directory uses the default component created by the library.

    Here’s the default component:

    import React from 'react'
    import styles from './styles.module.css'
    
    export const ExampleComponent = ({ text }) => {
       return <div className={styles.test}>Example Component: {text}</div>
    }

    Here’s how it is used in example/src/App.js:

    import React from 'react'
    
    import { ExampleComponent } from 'react-modal'
    import 'react-modal/dist/index.css'
    
    const App = () => {
       return <ExampleComponent text="Create React Library Example :)" />
    }
    
    export default App

    If you notice in the image above, the component and its stylesheet had to be imported together. That may appear ugly as you would always need those two imports for just one component.

    An alternative to this is having inline styles. That is what we would use in the component. Also, you can use libraries like styled-components which support CSS in JS.

    Let’s create the Modal component

    The features of the modal component are very simple:

    • it can contain anything
    • it has a close button
    • it shows a backdrop overlay when open

    Firstly, let’s create the Backdrop component:

    // src/backdrop.js
    import React from 'react'
    
    const backdropStyles = {
      position: 'fixed',
      left: 0,
      top: 0,
      width: '100%',
      height: '100vh',
      backgroundColor: 'rgba(0, 0, 0, 0.472)'
    }
    
    const Backdrop = ({ onClick }) => {
      return <div onClick={onClick} style={backdropStyles} />
    }
    
    export default Backdrop

    This component receives an onClick prop which we would use to close the modal.

    The code for the modal component is as follows:

    // src/index.js
    import React, { Fragment } from 'react'
    import Backdrop from './backdrop'
    
    const wrapperStyles = {
      width: '100%',
      position: 'absolute',
      left: 0,
      top: 0
    }
    
    const modalStyles = {
      maxWidth: '500px',
      width: '100%',
      border: '1px solid #ddd',
      backgroundColor: 'white',
      margin: '100px auto 0',
      zIndex: 1,
      position: 'relative',
      padding: '10px'
    }
    
    const closeBtnStyles = {
      position: 'absolute',
      right: '20px',
      top: '20px',
      background: 'none',
      border: 'none',
      fontWeight: 'bold',
      fontSize: '20px',
      cursor: 'pointer'
    }
    
    const Modal = ({ children, show, onClickCloseBtn }) => {
      return (
        <Fragment>
          {show && (
            <div style={wrapperStyles}>
              <Backdrop onClick={onClickCloseBtn} />
              <div style={modalStyles}>
                <button onClick={onClickCloseBtn} style={closeBtnStyles}>
                  X
                </button>
                {children}
              </div>
            </div>
          )}
        </Fragment>
      )
    }
    
    export default Modal

    The modal component receives three props: children, show and onClickCloseBtn.

    * children is the content that appears between `<Modal>` and `</Modal>`.
    * show is a boolean which we use to determine if the modal should show or not. Alternatively, this can be handled from the parent component.
    * onClickCloseBtn is a function that the parent component handles when the backdrop or close button of the modal is clicked. As you would have guessed, this would be used to close the modal.

    Now, let’s use the component in the example directory:

    // example/src/App.js
    import React, { useState } from 'react'
    import Modal from 'react-modal'
    
    const App = () => {
      const [showModal, setShowModal] = useState(false)
    
      const hideModal = () => showModal && setShowModal(false)
    
      return (
        <div>
          <h1 style={{ textAlign: 'center', padding: '10px' }}>react-modal</h1>
          <button
            style={{
              width: '200px',
              display: 'block',
              margin: '0 auto',
              padding: '10px',
              background: 'none',
              border: 'none',
              backgroundColor: '#eee',
              fontSize: '20px',
              cursor: 'pointer'
            }}
            onClick={() => setShowModal(true)}
          >
            Show Modal
          </button>
          <Modal show={showModal} onClickCloseBtn={hideModal}>
            <h1>I am a modal</h1>
          </Modal>
        </div>
      )
    }
    
    export default App

    Here’s the result:

    Preview of the example application using the Modal component
    Preview of the example application using the Modal component

    When the button is clicked, here’s the result:

    Preview of the example application using the Modal component when the show button is clicked
    Preview of the example application using the Modal component when the show button is clicked

    You’ll notice the faded backdrop. On clicking it or the close button of the modal, the modal closes.

    Here’s the source code on GitHub

    Wrap up

    This article explains how to create a react component library using create-react-library and using a Modal example.

    Although the example used here was minimal, I hope this gives you a full understanding of how it works and how you can make your own complex and reusable components.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    dillionmegida

      Related Posts

      Streamlining Resource Allocation for Enhanced Project Success

      December 18, 2024

      Conducting Effective Post-Project Evaluations: A Guide

      December 16, 2024

      Strategies for Keeping Projects on Track and Meeting Deadlines

      December 10, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Tips December 7, 2019

      Top 9 Web Components UI Libraries

      Best web component libraries that are available today and that will be prominent in 2020 and after.

      Essential Modernising our Talent Programme

      January 22, 2019

      Create simple POS with React.js, Node.js, and MongoDB #16: Order Screen

      October 14, 2020

      Express.js Lessons. Express: Basics and Middleware. Part 2.

      November 24, 2016

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Express.js Lessons. Express: Basics and Middleware. Part 2.

      Programming November 24, 2016

      5 Best Tools for Managing Your Web Developer’s Salary

      Remote Job January 30, 2019

      Balancing Value-Driven Content and Promotional Messaging Strategies

      Content & Leadership August 27, 2025

      Leveraging Lean Startup Methodology for Business Growth Success

      Startups December 1, 2024

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Startups

      Startup Spotlight: Five Companies That Revolutionize Healthcare & Wellness

      SaaS & Tech

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      Job

      Soshace is looking for Independent IT Agents

      Most Popular

      JAMstack Architecture with Next.js

      JavaScript

      Introduction to WebAssembly: The Magic of Native Code in Web Apps

      JavaScript

      Essential Steps for Beginners in Mobile App Development

      Programming
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.