Fixing the slide bar in React using styled components

In the early stages of creating a slider where cards (divs) can be moved left and right with a click, I encountered an issue. The onClick handler is functioning properly. However, upon running the project, I noticed that the cards start 230px away from the left arrow. My goal is to have them positioned right next to the left arrow initially, with the 230px space being created (using transform: translateX) only when the left arrow is clicked.

Project available at https://codesandbox.io/s/jolly-cache-pk4j8

Parent Component (containers/card.js)

import React,{useRef,useState} from 'react';
import { Card } from '../components';
import { CardItemContainer } from './card-item';

export function CardContainer() 
 {

   const [scroll,setScroll]=useState('left')

    return(
        <Card>
            <Card.ArrowSliderLeft setScroll={setScroll}  />
                <Card.List scroll={scroll}>
                  <CardItemContainer/>
                  <CardItemContainer/>
                </Card.List>
            <Card.ArrowSliderRight setScroll={setScroll} />
        </Card>
    )}

Child Components(componets/card/index.js)

import {ArrowBackIosOutlined,ArrowForwardIosOutlined} from "@material-ui/icons";
import React from 'react';

import {Container,Wrapper,List,ArrowSliderLeft} from './styles/card';
 

export default function Card({ children, ...restProps }) {
   
       return <Container {...restProps}>{children}</Container>   
}


Card.List=function CardList({scroll,children,...restProps})
{
   return <List{...restProps} active={scroll}>
             {children}
         </List>
}

Card.ArrowSliderLeft = function HeaderArrowBackIosOutlinedSymbol({  setScroll,...restProps })
 {
    
const handleClick =e=>{
        setScroll(e)
     }

    return <ArrowSliderLeft  {...restProps } >
              <ArrowBackIosOutlined id="sliderLeft" onClick={()=>handleClick('left')}/>
           </ArrowSliderLeft>
}

Card.ArrowSliderRight = function HeaderArrowForwardIosOutlinedSymbol({setScroll,...restProps}) {
  const handleClick = (e) => {
    setScroll(e);
  };
  
  return (
    <ArrowSliderRight {...restProps}>
      <ArrowForwardIosOutlined 
        id="sliderRight" 
        onClick={() => handleClick("right")}
                               />
    </ArrowSliderRight>
  );
};

styled components (components/card/styles/card.js)

import styled from 'styled-components';

export const Container=styled.div`
width:100%;
margin-top:10px;
`

export const List=styled.div`

margin-left:50px;
display:flex;
width:max-content;
margin-top:10px;
transform:${({ active }) => (active==='left'  ? 'translateX(230px)' : 'translateX(0px)')}

`
export const ArrowSliderLeft=styled.div`
  
`

https://i.sstatic.net/RKTvq.png

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

Enhancing design precision from Figma to flawless pixels

I'm facing the challenge of converting a figma design into an HTML and CSS template with a fixed dimension of 1440px. I'm unsure about where to begin coding in terms of screen size - should I start at 1440px and scale down/up, or begin with mobil ...

iterate through all the items in the radioButton collection

Hello, I am looking to iterate through this object and display checkbox values along with their indices. data: [ { key1: 'last 6 months' }, { key2: 'last 30 days' } ] I would like to creat ...

Error: While constructing my React application, a TypeError occurs as it is unable to retrieve the property '0' of an undefined value

While trying to deploy my react application, I encountered the following error message: Error: Unable to read property '0' of undefined Oddly enough, this error only appears during the building process of the app, not when it's running on ...

Tips for ensuring an animation is triggered only after Angular has fully initialized

Within this demonstration, the use of the dashOffset property initiates the animation for the dash-offset. For instance, upon entering a new percentage in the input field, the animation is activated. The code responsible for updating the dashOffset state ...

Tips for getting free jqgrid to display frozen column borders in the search toolbar

If the actions column has a caption of "Activity" or custom settings and is frozen, the free jqgrid will not display column borders for this column in the search toolbar. Vertical lines do not appear in the search toolbar: Is there a way to resolve this ...

Angular array mapping techniques

My JSON Object $scope.selectedItems ={ "RECORDS": [ { "Id": 23040035705987, "arriveddate": "2015/04/24", "expirationDate": null, "replacedDate": null, "processDate": "2015/04/24" ...

Is there a way I can pass props to a different file?

Is there a way to pass props to another file? In my component file, I have an array where data is added. When the user clicks ok, I want to send the array to another file. For example: sizeComponent.js import React from 'react'; import { StyleS ...

Tips on using JQuery to extract form field information from a drop-down menu, display it in a div, and then compare it with the subsequently

In my HTML file, I am using two dropdown lists and JQuery for validation. First, I need to save the selected items from both dropdown lists in a variable and then compare them with the next selection. If the data from both dropdown lists match, an alert m ...

The issue of stuttering arises in Safari when using a combination of size and translate transitions

When applying transitions to an element and adjusting the width and/or height along with -webkit-transform:translate3d, there is a noticeable stutter in the transition animation. It seems like the width/height change is animated first, then partially trans ...

Right-click context menu not working properly with Internet Explorer

Seeking assistance with extracting the URL and title of a website using JavaScript. The script is stored in a .HTM file accessed through the registry editor at file://C:\Users\lala\script.htm Below is the script: <script type="text/java ...

Creating a multi-step progress bar in Yii2 that showcases completed steps using jQuery

Is there a way to automatically transition from one state to another once the order status changes in Yii2? var i = 1; $('.progress .circle').removeClass().addClass('circle'); $('.progress .bar').removeClass().addClass(&a ...

modify CSS style once the modal has been hidden

Is there a way to change the default background color of my table row back to normal after closing the modal window that appears when I click on it? <tr class='revisions'> <td>{{ $revision->date_of_revision->format(' ...

Instead of using a checkmark, consider using step numbers with Mui Stepper's Completed StepIcon

I'm currently utilizing Mui Stepper from [email protected] within a React project. Despite my efforts to search on platforms like Stackoverflow and Github, I have been unable to find a solution for displaying the step number upon completion inst ...

Tips for declaring the project npm registry within the package.json configuration file

Currently, I am juggling multiple projects simultaneously and facing the issue of each project having a different node module registry. For instance, project A sources its modules from http://registroy.foo.com, while project B pulls modules from http://re ...

A guide on converting a Javascript datetime to a C# DateTime parameter

In a previous question that I asked on StackOverflow, I mentioned that I was able to retrieve my values successfully as the object was no longer null. The object contains a DateTime property named CreatedOn, which I am sending from JavaScript using new Dat ...

Manipulating a global variable in VueJS

Currently, I am referring to Global data with VueJs 2 for my project, focusing on only one variable. In the code provided, I have included an @click event to update the variable. However, it throws an error stating "Uncaught ReferenceError: $myGlobalStuff ...

React: executing function before fetch completes

Whenever I trigger the ShowUserPanel() function, it also calls the getUsers function to retrieve the necessary data for populating the table in the var rows. However, when the ShowUserPanel function is initially called, the table appears empty without an ...

The problem arises from misinterpreting MIME types when serving SVG files, causing the resource to be seen as an image but transferred with

Having some issues targeting SVG images with CSS to use as backgrounds on certain elements. When I try to access the image directly here, it works perfectly fine. However, when using it in CSS, I encounter the following error: Resource interpreted as Imag ...

The issue of onClick failing to function when paired with the addEventListener for the

Looking into a react component for a profile button that opens a menu with three options: My Profile, Settings, and Logout. The issue seems to be with the onClick event on the a tags not working as expected (the console.log is not being printed). Interes ...

Leveraging Ajax and jQuery to create a POST request for adding a new record to a MySQL table within a Node.js server

My form is designed to collect user information like name, age, and more. The aim is to submit this data from the client side, inserting it into a MySQL table row. However, I'm facing difficulties in getting the data to successfully insert. Below are ...