Create React Tooltip without External Library

Hello everyone, in this tutorial we are going to create 'Tooltip' component for React , but we will not use any external library, so that you get the flexibility to customise it according to your project need. So Let's start.

First Let's see have a look of the final outcome :


So we will start by initiating a project with create-react-app. And within src folder we will create a Tooltip.js file, Tooltip component will allow user to pass tooltip text.

import React from 'react'
import './style.css'
const Tooltip = ({text}) => {
    return(
        <div style={{
            textAlign:'center',
            margin:'10%'
        }}>
            <div class="custom-tooltip">Hover to see tooltip
                <span class="custom-tooltip-text">{text}</span>
            </div>
            <span style={{
                display:'block',
                marginTop:20,
                color:'blue'            
            }}>https://reactcodes.blogspot.com/</span>
        </div>
    )
}

export default Tooltip

I have created a style.css file which holds all the required styles for tooltip :


.custom-tooltip {
    position: relative;
    display: inline-block;
}
  
.custom-tooltip .custom-tooltip-text {
    visibility: hidden;
    width: 120px;
    background-color: gray;
    color: #fff;
    text-align: center;
    border-radius: 1px;
    padding: 5px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -60px;
    opacity: 0;
    transition: opacity 0.7s;
}

.custom-tooltip .custom-tooltip-text::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: gray transparent transparent transparent;
}

.custom-tooltip:hover .custom-tooltip-text {
    visibility: visible;
    opacity: 1;
}

Finally use Tooltip component in App component :


import React from 'react';
import Tooltip from './Tooltip'

const App = () => {
  return (
    <>
      <Tooltip text="Tooltip text content" />
    </>
  );
}
export default App;

Here is the result :


Thanks for your time, hope this helps :)

Comments

  1. You shared an excellent article with us. This article gives some unique knowledge to us. Keep sharing more posts.
    Best UPVC Windows and Doors Manufacturer in Chennai | Latest Upvc Doors in Chennai
    | Best UPVC Windows in Chennai

    ReplyDelete
  2. Can we create a custom library instead of the default library in our system and use it for Cheap Essay Writing Services with different apps at different times? If that happened, we would all be able to save a lot of time.

    ReplyDelete
  3. Nice blog.
    I've gone through all the details that are mentioned in the blog.
    The details for the frontend development using react is explained in a very proper manner and every early stage learner can understand the topic very easily.
    I was looking forward to hire dedicated react native developer and got your blog.
    Thanks for sharing such a great blog.
    Synsoft Global

    ReplyDelete
  4. React Native App Development:
    In today's highly competitive digital landscape, having a compelling and user-friendly mobile app is crucial for business success. React Native app development has gained immense popularity as it allows you to build native-like apps using a single codebase. With its extensive library of pre-built components and modules, React Native enables rapid development and easy maintenance of mobile applications. At Connect Infosoft Technologies, we specialize in React Native app development, offering end-to-end solutions tailored to your business needs. Our experienced team of React Native developers follows industry best practices to create scalable, intuitive, and visually appealing apps that engage users and drive business growth. Trust us to deliver a top-notch React Native app that exceeds your expectations.

    ReplyDelete
  5. Thank you for the information regarding web development company . These specifics some of the most important factors to consider when selecting a trustworthy partner in the digital space. Your advice on sifting through the various web development firms is priceless. I'm grateful.

    ReplyDelete

Post a Comment

Popular posts from this blog

Best coding practice and structure for redux saga with react hooks | Separate watcher and worker saga | React clean code

How to use redux with React Hooks - Creating TodoList

Setting up Redux Devtools for React applications