Creating a dual style name within a single component using Styled Components

Need assistance with implementing this code using styled components or CSS for transitions.

The code from style.css:

.slide {
  opacity: 0;
  transition-duration: 1s ease;
}

.slide.active {
  opacity: 1;
  transition-duration: 1s;
  transform: scale(1.08);
}

And the JSX code:

<div
  className={index === current ? 'slide active' : 'slide'}
  key={index}
>
  {index === current && (
    <img src={slide.image} alt='travel image' className='image' />
  )}
</div>

Would appreciate any guidance on how to implement this in styled components or utilize the CSS transitions effectively.

Answer №1

If you want to make the component dynamic, consider using styled-components where the styles change based on an isActive prop.

const StyledComponent = styled.div`
  opacity: ${(props) => (props.$isActive ? 1 : 0)};
  transition-duration: ${(props) => (props.$isActive ? "1s" : "1s ease")};
  transform: ${(props) => (props.$isActive ? "scale(1.08)" : "inherit")};
`;
<StyledComponent $isActive>Hello World!</StyledComponent>
// or
// <StyledComponent $isActive={true|false}>Hello World!</StyledComponent>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Downloading files with React and Redux

How can I initiate a file download from the server when a button is clicked? I have implemented a MaterialUI button with an onClick callback that triggers an action in the connected container component. The action is asynchronous and involves making an a ...

Providing the type for an abstract class implementation: A guide

Is there a way to define a type for "any class that implements this abstract class"? For instance: // LIBRARY CODE abstract class Table { static readonly tableName: string; } type TableConstructor = typeof Table; // type TableConstructor = (new (...args ...

What might be causing the onclick action in a ReactJS app using Redux to not work as

Below is the simplified code snippet: -action.js export const removeVisitedUser = id => async dispatch => { try { const visited = JSON.parse(localStorage.getItem(`visited_`)) || []; const filtered = visited.filter(i => ...

Is it possible for Express JS to receive incoming requests from external sources and then transmit the retrieved data to the front end of

I am currently developing a React JS frontend for my application, but I have an interesting idea to incorporate an external mobile app that can control certain elements on the screen, such as font styles. To achieve this functionality, I require the web ap ...

Passing a click event to a reusable component in Angular 2 for enhanced functionality

I am currently working on abstracting out a table that is used by several components. While most of my dynamic table population needs have been met, I am facing a challenge with making the rows clickable in one instance of the table. Previously, I simply ...

Caution: flattenKids(): Two children with identical keys, `false`, have been detected in the ReactJS environment

Currently, I am working on creating a clickable list where each item redirects me to another page upon clicking. Below is the render method: render() { const quesItems = this.state.questions.map((question, i) => { return ( <li key={this.prop ...

What is preventing me from accessing my session array in this.state.props from my mapStateToProps in React-Native Redux?

I am currently facing an issue with my Redux store setup. I am attempting to store an array of Session objects, where each Session object contains an array of Hand objects. However, when trying to access my store using `mapStateToProps`, none of the option ...

What is the best way to display json data in a react component?

I currently have a JSON file that I am importing into my component with the following code: import { beer, wine, spirits, alcopop } from '../../config/calculator.json'; I’m trying to figure out how to utilize this JSON data within my render m ...

Using Checkboxes in React with Material-UI

I am currently facing an issue with my 2 checkboxes. Whenever I select one, both of them get selected automatically. I want the user to be able to choose one, both, or none at all. It's puzzling why they are both getting selected simultaneously. Here ...

The boundary of embedded components

I am looking for CSS code that will allow me to arrange all elements with specific indents, such as placing the first element on the left side of the page, followed by an image with some indentation, and so on. <div class="container-fluid"> &l ...

Discovering whether a link has been clicked on in a Gmail email can be done by following these steps

I am currently creating an email for a marketing campaign. In this email, there will be a button for users to "save the date" of the upcoming event. I would like to implement a feature that can detect if the email was opened in Gmail after the button is cl ...

Are you looking for a demonstration of "Creative Loading Effects" that triggers when the page is loaded?

I came across this demo for a preloader on my website called Creative Loading Effects, specifically the "3D Bar Bottom" effect, which I find very exciting. However, I noticed that it only loads when we press the button, and not automatically when the page ...

Setting a maximum quantity in React

I'm struggling with limiting the number of items a user can add to their cart in a React function called handleNumberSum. I attempted using an if statement, but it doesn't seem to work as expected. Here's my code: const ItemCount = () => ...

How can I stop the body from scrolling to 100% height when the virtual keyboard is displayed?

My chat app has sticky header and footer elements. To handle the mobile virtual keyboard opening, I adjust the document's height using window.visualViewport.height. For example, if the browser's height is 1000px and the virtual keyboard takes up ...

Setting up a specific print media query for the body element through JavaScript

I'm facing an issue with my web page that has a bootstrap modal which pops up when a button is clicked. I've added a print media query to print the content of the modal, but it's causing a problem. When I include the media query in the CSS, ...

How come the client directory from my project didn't get sent to GitHub?

When attempting to upload my project to a Github repository for the first time, I noticed that all folders' content was successfully uploaded except for the client folder. Strangely, the icon for this folder looks different compared to the others. Can ...

Visual Studio Code is encountering issues when trying to start a Node application

I am in the process of setting up a workflow for an express app using TypeScript, Visual Studio Code, and gulp. Here is the structure of my project: src/ <-- source files Start.ts Server.ts models/ Contact.ts Organization.ts bin/ <- ...

Issues with rendering Google Maps on google-maps-react persists, stuck endlessly in loading phase

After following the tutorial for google-maps-react, I attempted to display a Google Map in my app using the same structure as the example. However, the map is not rendering. Link to Tutorial There are no errors showing up in my console. Here is the dire ...

Resolving Issues with Text Overlaid on an Image Hyperlink

Alright, so this problem might be a bit too complex for CSS/HTML to handle, but I'll give it a shot anyway. I have some images that are dynamically placed using the code from this website. What I want to do with these images is add a text overlay w ...

Use the IONIC CAPACITOR application to send a 20-byte command to a BLE device

Hello everyone, I am currently working on an application that is designed to connect to a BLE device. I have a requirement to write 20 Bytes to the device using BleClient.write function. 34h 01h 50h 4Fh 4Ch 49h 54h 45h 43h 00 00 00h 00h 00h 00h 00h 00h 00h ...