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`
  
`

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

What could be the reason for the message "the project was initialized with an outdated and unsupported version of tools" while running npx create-react-app?

After transitioning to Ubuntu, I started encountering an issue every time I tried to create a React app. The note mentioned that the app being created was a class component. ankit@gram:~/Documents/Development/React/react-project$ npx create-react-app my-ap ...

The underscore convention for defining members in Typescript allows for clear and concise

Let's talk about a class called Email class Email { private _from: string; private _to: Array<string>; private _subject: string; } When an email object is created, it will look something like this: { _from:'', _to:'&apo ...

The (ReactJS + Redux) form fails to load when the /new segment is appended to the URL

I'm currently working on a project using React, Redux, and Rails. I've encountered an issue with loading the form. In my App.js file, I have set up a Router that defines the routes. import React, { Component } from 'react'; import { Br ...

Choose an XPath selector that targets a list item within a specific div element

Currently, I am using selenium to conduct testing on a website. I have employed an XPath selector in order to locate a specific item within the HTML structure: <div id="boundlist-1051" class="x-boundlist list_cfg_cls x-boundlist-floating x-layer x-boun ...

Show the last polygon that was created using OpenLayers on the screen

Using this example from OpenLayers website: I am attempting to create a polygon but I would like it to vanish once the polygon is finished. Could anyone offer assistance with this? Thank you :) ...

Building a bespoke search input for the DataTables JQuery extension

As the title indicates, I am in the process of developing a custom search box for the DataTables JQuery plugin. The default options do not suit my needs, and configuring the DOM is also not ideal as I aim to integrate it within a table row for display purp ...

unable to call the anonymous inline onclick method of JavaScript

In my HTML file, I have the following code: <a onclick="getRestaurantTime({{ $afternoonTiming[$j]->id}});">Hello</a> Furthermore, I have an anonymous function in restaurant.js file: var Restaurant = (function ($) { function getRestaur ...

Tips on utilizing an npm package for development without the need to rebuild it repeatedly

When working on my local npm package clone, I am utilizing `npm link` to connect it. Within the package.json file of this npm package, the entrypoint is configured as dist/index.js. To avoid constantly rebuilding the project during development, how can I ...

jQuery efficiently handles large amounts of text data transfer (gradual loading)

The issue at hand: My website relies on jQuery AJAX (post) for navigation, meaning the page doesn't refresh every time a user moves to a different section. This setup requires me to fetch data using AJAX and present it dynamically. While this works w ...

The spacing between the rows of the ListItem is excessively tall

Currently, I am working with Material UI to design a list. It seems that the height of my list elements needs to be reduced. I have attempted using <List dense={dense}>, but it appears to still be too spaced out. Is this issue related to padding or s ...

Strategies for resolving ButtonGroup overflow in Material UI Grid container

In an effort to create a responsive layout, I encountered an issue where the button group in my code seems to overflow out of the container and expand when the screen size is reduced. The code below illustrates this problem: <Container className=" ...

Error 400: The onCreate Trigger function for Cloud functions is experiencing issues with HTTP requests due to errors in the request

I am encountering an issue when attempting to add a trigger for Firestore OnCreate, as the deployment fails with HTTP Error: 400 stating that the request has errors. Essentially, my goal is to write to a new document if a record is created in a different ...

Regular expressions are used for validation purposes

I've been using the regular expression shown below to validate certain text: ^\d+(?:fs|sf)[-+]\d+[hmd]$/ Here are some sample texts I have validated with this regular expression: 20fs-4d 10sf+20m 3fs-2h So far, it's been working we ...

How to use Javascript to toggle a popup containing an autoplaying Vimeo video

I am looking to create a pop-up window containing a Vimeo video inside. I have a div on my webpage with an id of "showVideo". When this div is clicked, I want to display a pop-up (new div with the id of "opened-video"). The "opened-video" div contains an i ...

My Drop Down menu is not displaying the <a> in two lines, and I'm having trouble getting it to show correctly

Hello there! I've encountered an issue with my drop-down menu. The text within the <a> tags is too long to fit on a single line, and I'm struggling to make it display in two lines instead. I've been experimenting for the past couple o ...

Guide on creating a menu that remains open continuously through mouse hovering until the user chooses an option from the menu

I have a unique scenario where I am working with two images. When the mouse hovers over each image, two corresponding menu bars appear. However, the issue is that when the mouse moves away from the images, the menu disappears. Any suggestions on how to im ...

The $http Service encounters a failure with an unknown status code

Difficulty Integrating AngularJS, jQuery, and Adobe Panel Creation I recently updated the versions of AngularJS and jQuery for my project. Previously, I was using jquery-1.11.0.min.js and AngularJS 1.2.10. Now, I want to utilize the latest available versi ...

Troubles encountered when attempting to use Axios to call a third-party API in a React JS application

Challenge Description: I set out to create a dropdown menu of post-offices based on the user-entered pincode. To achieve this, I utilized an API and Axios for the backend request. While I successfully populate the dropdown with the necessary data, the is ...

Having issues with angular-file-upload failing to include the file in the request

I am currently using angular-file-upload in order to upload an image to a server that is utilizing Node and Express 4.0. However, I am encountering difficulties in accessing the file data on the server. Below is the relevant code snippet: <input type= ...

Youtube video embedding showing cut edges on smaller screens

Looking for assistance with a Next.js and tailwind site I am building. Having trouble getting the video component to display properly on mobile screens. Tried various fixes but the video still gets cut off on smaller screen sizes. If anyone has a soluti ...