Solution 1 :

@Andrewnosov
I have made what I think is a simpler code to implement your version of the gif.

CSS:

h2 {
  display: flex;
  position: absolute;
  top: 0;
  left: 0;
  background-color: white;
  width: 100%;
  height: 40px;
}

JS:

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

class App extends React.Component {
    constructor() {
        super();
        this.state = {
            isHovered: false,
            showing: 'Email'
        };
    }

    setShowing = showing => {
        this.setState({ isHovered: true, showing });
    };

    render() {
        return (
            <div onMouseLeave={() => this.setState({ ...this.state, isHovered: false })}>
                {this.state.showing === 'Phone' ? <h2>+7 992 020 1025</h2> : null}
                {this.state.showing === 'Email' ? <h2>[email protected]</h2> : null}
                {!this.state.isHovered ? (
                    <h2>
                        <span onMouseEnter={() => this.setShowing('Phone')}> Phone </span>
                        /
                        <span onMouseEnter={() => this.setShowing('Email')}> Email </span>
                    </h2>
                ) : null}
            </div>
        );
    }
}

export default App;

and Hope it will be helpful 🙂

Problem :

I’m trying to make something like this in my ReactJS website:

Work solution example

I’ve tried to make it myself and got the working result, but in my opinion implementation is very complex and cumbersome. Below I’ve placed the code of my implementation.

JS:

import React from "react"

import switchStyles from "./switch.module.scss"

class LinkSwitch extends React.Component {
  constructor() {
    super();
    this.state = {
      isHover: false,
      email: false,
      phone: false
    };
  }
  handleHoverEmail = () => this.setState({ isHover: !this.state.isHover, email: !this.state.email })
  handleHoverPhone = () => this.setState({ isHover: !this.state.isHover, phone: !this.state.phone })
  render() {
    return (
      <div 
        className={`${switchStyles.hoverContainer} ${this.state.email ? switchStyles.activeEmail : ""}${this.state.phone ? switchStyles.activePhone : ""}`}
      >
        <a 
          className={switchStyles.ctaButton}
          onMouseEnter={this.handleHoverEmail}
        >
          почта&nbsp;
        </a>
        <a className={switchStyles.ctaButtonEmail__hover} onMouseLeave={this.handleHoverEmail}>[email protected]</a>
        <a 
          className={switchStyles.ctaButton}
          onMouseEnter={this.handleHoverPhone}
        >
          &nbsp;телефон
        </a>
        <a className={switchStyles.ctaButtonPhone__hover} onMouseLeave={this.handleHoverPhone}>+7 992 020 1025</a>
      </div>
    )
  }
}

export default LinkSwitch

SCSS:

.hover-container {
    width: fit-content;
}

.hover-container > a:first-child:after {
    content: '/';
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    display: inline-block;
    margin: 0 0;
    font-weight: 500;
}

.cta-button-email__hover {
    display: block;
    opacity: 0;
    visibility: hidden;
    position: absolute;
    white-space: nowrap;
    top: 0;
    left: 0;
    z-index: 1;
}

.cta-button-phone__hover {
    display: block;
    opacity: 0;
    visibility: hidden;
    position: absolute;
    white-space: nowrap;
    top: 0;
    left: 0;
    z-index: 1;
}

.active-email .cta-button-email__hover {
    opacity: 1;
    visibility: visible;
}

.active-phone .cta-button-phone__hover {
    opacity: 1;
    visibility: visible;
}

.active-email .cta-button {
    opacity: 0;
}

.active-phone .cta-button {
    opacity: 0;
}

Using two states, specific classNames for phone number and email is bad code, I think. I will be glad of any help! Maybe someone can offer simpler and more optimal solutions.

Comments

Comment posted by andrewnosov

Thank you! Yes, it’s more simple approach, but this one does not fulfill the task. It just happens alternating mail and phone on hover over the block, for example if i hover on phone — i got the phone, and second time i hover on phone, but i’ll got email etc. :(((

Comment posted by RedJanvier

Thank you very much for pointing it out. I am really sorry I didn’t notice that before but I edited the answer’s JavaScript to implement that. Still hope it i helps you enough and thank you for your help.

Comment posted by andrewnosov

It works very good! And your code is very simple! Thanks a lot, my friend!

Comment posted by RedJanvier

Glad it helped and please feel free to accept my answer and vote for it. Thank you too. I really appreciated your question keep up the good work.

By