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

Caution in React: Utilizing functions with Object.assign() may not be a valid approach when dealing with React children

I have a setup using react for front-end and node for back-end. My goal is to retrieve data from the server to update the user entries on the front-end. I came across using Object.assign() as a potential solution to re-render user entries, but encountered ...

Combine both typescript and javascript files within a single Angular project

Is it feasible to include both TypeScript and JavaScript files within the same Angular project? I am working on a significant Angular project and considering migrating it to TypeScript without having to rename all files to .ts and address any resulting er ...

Issue retrieving nested object in NextJS

view-component.js const ViewComponent = () => { const route = useRoute(); //All good here const data = fetchData(); console.log(data); //Issue arises with this line const profileData = data.profile; console.log(profileData) ...

The disappearance of the element is too swift for Jest to catch

During my testing, I encountered an issue with a loading spinner not being displayed when fetching data. The error message SyntaxError: Unexpected token < in JSON at position 0 is returned because of the json() function used on the awaited response from ...

Discover the steps to deploy NextJS on AWS with the same efficiency as Vercel

For my POC, I am looking to deploy NextJS on AWS using AWS CDK and exploring different options. The NextJS docs suggest running npm run build && npm start to start the service, but this may not be the most optimized way. Vercel offers a highly opt ...

Establish pathways based on a collection of values

I am seeking to establish language routes for my webpage, similar to this: import Component from '../src/component/component'; <Switch> <Route exact path="/es" component={(props) => <Component language="es" />}/> < ...

Display a separate component within a primary component upon clicking a button

Looking to display data from a placeholder module upon component click. As a beginner with React, my attempts have been unsuccessful so far. I have a component that lists some information for each element in the module as a list, and I would like to be ab ...

Implementing varied components within a single React route depending on user roles

I need to dynamically set different components on the same React route depending on user roles. Here is my approach: // AppRouter.js const AppRouter = () => { const {loading, data, error} = useQuery(GET_CURRENT_USER) if (loading) return < ...

Moving the marker does not update the address

When the dragend event is triggered, the Getaddress function will be called in the following code: Getaddress(LastLat, LastLng , marker,source){ this.http.get('https://maps.googleapis.com/maps/api/geocode/json?latlng='+LastLat+ &apos ...

How can I use JavaScript to modify the style of the first unordered list (ul) element without affecting the

function displayMenu(){ var parentElement = document.getElementById("menuItem1"); var lis = parentElement.getElementsByTagName("ul"); for (var i = 0; i < lis.length; i++) { lis[i].setAttribute("style","display: block"); } } When the button is clicke ...

error message indicating that a static file could not be found following deployment as it is not located within the root directory

When deploying my Next.js web app in a directory other than the root, such as '/next' instead of '/', the static files in the public folder cannot be found. This is because the requested URL is not http://example.com/next/a.png, but rat ...

Issue concerning props and renderItem within react native's flatlist encountered

I am currently working on a basic project in react native to get familiar with everything before I start my main project. The main objective of this project is to display an array using a flatlist, but the challenge is to modularize it gradually (e.g. sto ...

Retrieve HTML form data or an object within the getServerSideProps() function in NEXTJS

How can I submit form data from a NextJS page to another page with server-side rendering? The function getServerSideProps() on the receiving page requires these form data to make an API call. I considered using localStorage/sessionStorage to store the da ...

I am attempting to create a footer with a set size, but encountering an issue

I'm in the process of developing a responsive website using Vue.js. One aspect I am working on is the footer container, which looks fine on the full screen but shrinks when the screen resolution is reduced below 1100 pixels. As shown in the images li ...

Display a preview of a React component

Currently facing a challenge where I need to create a form for users to adjust the title, background color, button background, text color, and other settings of a component. I want to provide a live preview of the component as the user makes changes. I en ...

Issue Encountered during Git Bash Installation of React-Scripts: "UNRECOGNIZED ERROR: unknowingly, scanning directory 'E:... ode_modules@babel.helper-annotate-as-pure.DELETE'"

ERROR: An unknown error occurred while trying to scan directory 'E:\Sorted\Capstone\WOO-WOO.net\WOO-WOO.net\project\FrontEnd\frontendapp\node_modules\@babel\.helper-annotate-as-pure.DELETE' Encou ...

Formatting Outlook HTML Signatures

I'm currently designing an HTML email signature for Outlook, but I'm having trouble with formatting and ensuring it looks consistent across different devices. When I preview the HTML in my browser, it appears exactly as I intended. However, I am ...

Dividing Trapezoid Shapes, Cropping Background Images

I am attempting to design a website layout with trapezoid shapes similar to the attached image. Each section should have a background image that fills the background with cover or a similar effect. I successfully created the first section (dark blue) usin ...

How to center text both horizontally and vertically in HTML

I am struggling with centering a background image along with a 1-line text vertically and horizontally inside a div. Although I have successfully centered the image, the text remains misaligned. I attempted using vertical-align: middle without success. Be ...

Discover the step-by-step guide to creating a dynamic and adaptable gallery layout using

I have encountered an issue with my gallery where the images stack once the window is resized. Is there a way to ensure that the layout remains consistent across all screen sizes? <div class="container"> <div class="gallery"> &l ...