Referencing another component in StyledComponents

I'm looking to modify the properties of my parent component's :after selector whenever the child element is focused. It seemed straightforward at first, but for some reason, I just can't seem to make it work. Here's a glimpse of my component structure:

Component Structure:

<Wrapper>
 <Input type='text'/>
</Wrapper>

Specifications:

const Wrapper = styled.div`
  &:after {
    display: block;
    content: "";
    border-bottom: solid 3px black;
    transform: scaleX(0);
    transition: transform 250ms ease-in-out;
  }
`
const Input = styled.input`
border: none;
  width: 90%;
  transform-origin: center;
  display: inline-block;
  :focus {
    outline: none;
    border-bottom: 1px solid black;
  }
  &:focus ${Wrapper}:after {
    transform: scaleX(1);
  }
`

I want the parent component to show a border-bottom when the child is focused, and remove it when the child loses focus. Any suggestions on how I can accomplish this? What am I overlooking or doing incorrectly?

Any assistance would be greatly appreciated.

Answer №1

If you're looking to achieve this effect using only styles, it may be a challenge. However, I have an alternative solution that could work for you.

To handle the input focus state, consider saving it like this:

  const [isFocused,setIsFocused] = useState(false);


  <Input onFocus={()=>{setIsFocused(true)}} onBlur={()=>{setIsFocused(false)}} />

Then, make sure to pass isFocused to your wrapper component:

<Wrapper focused={isFocused} >

With this setup, you can now apply styles based on the isFocused value:

const Wrapper = styled.div`
   border-bottom:${props=>props.focused&&"1px solid black"};
`;

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

Utilize a drop-down selector in JavaScript to search and refine the results

Looking for a way to enhance your product search form? Consider adding an option to select a specific shop from the dropdown menu. This will allow users to search for products within a particular store if desired. <form action="#" method="get"> &l ...

Turn off browser feature that automatically adds overflow:hidden on touch screen devices

Encountering a problem with browsers (potentially Chrome) adding CSS rules on touch-enabled devices. In a website developed for a client (specifically for Chrome), the scroll bars disappear on elements when accessed from their ultrabooks. The client still ...

'Mastering the implementation of promises in React context using TypeScript'

I've been diving into the world of incorporating TypeScript in React and I'm facing a challenge with implementing async functions on context. The error that's popping up reads as follows: Argument of type '{ userData: null; favoriteCoc ...

Using Material UI's onClose as an alternative to disableBackdropClick

I currently have a dialogue box displayed on my webpage. <Dialog open={open} data-testid="myTestDialog" disableEscapeKeyDown={true} disableBackdropClick={true} > After visiting the documentation at https://material-ui.c ...

Ensure to preselect the radio button based on the Day's value

I have set up my Radio buttons with corresponding content to be displayed when clicked. I want to automatically pre-select the tab button based on the current day. For example, if today is Sunday, the page should load showing the contents for Sunday. If i ...

Issues Arising from NextJS Builds when Using SSR and Styled-Components

I'm deploying a NextJS app to Vercel and utilizing styled-components. This is the code snippet from my _document.tsx file: import Document from 'next/document' import { ServerStyleSheet } from 'styled-components' import flush from ...

file_put_contents - store user-defined variables

When I execute this script, it successfully generates the html page as expected. However, I am encountering challenges in incorporating variables, such as the $_GET request. The content is enclosed in speech marks and sent to a new webpage on my site usin ...

Executing a JavaScript utility before running a collection in Postman: Step-by-step guide

Regarding my needs Before executing the Postman Collection, I need to complete a few preliminary steps: I need to use a JavaScript utility to generate JSON file containing input BODY data The generated JSON file will then be used by another utility to cre ...

When a user clicks on a button, AJAX and jQuery work together to initiate a setInterval function that continually

Currently, I have two scripts in place. The first script is responsible for fetching a specific set of child nodes from an XML file through AJAX and using them to create a menu displayed as a list of buttons within #loadMe. What's remarkable about thi ...

Creating Visuals from Text with ReactJS/NextJS

Looking to transform text into an image with a shareable link in ReactJS, similar to Spotify's lyrics sharing feature. I haven't attempted any solutions so far. I've explored various libraries without success. ...

Unleashing the power of dynamic column width in Material UI Grid

Is it possible to dynamically change the column width for grid items within the same container in material UI grid? Sometimes I have 2 grid items that need to display in the same row, and other times I have 4 grid items that I want to appear in the same r ...

What are the semantics behind implementing Basic Authentication in AngularJS and Spring Boot?

Currently going through a tutorial that involves AngularJS and Spring Security. The AngularJS navigation controller triggers an authenticate function upon page load: var authenticate = function(credentials, callback) { var headers = credentials ? { ...

What is the process for configuring socket.io to solely listen on a single designated route?

Is there a way to make socket.io listen only on my /home route, instead of every route? I tried changing the configuration but it only displayed a JSON file on the home path. const server = require('http').Server(app); const io = require('s ...

Is there a way to turn off _moz_resizing on my browser?

I am currently using the nicEdit editor and have successfully integrated my own custom image resizing script. However, I am facing an issue where the default _moz_resizing feature in Firefox is interfering with my custom script. My goal is to have specifi ...

Having trouble getting your Bootstrap v4 carousel to function properly?

Currently, I have implemented the carousel feature from Bootstrap v4 in a Vue web application. I am following the same structure as shown in the sample example provided by Bootstrap, but unfortunately, it is not functioning correctly on my local setup and ...

Retrieving information from an object using a randomly generated identifier

Imagine having an object structured like this. var foo = { "dfsghasdgsad":{ "name":"bob", "age":"27" } }; The variable foo will consistently only have one object, but the key is dynamically created. How can I access the values "bob" and "27" ...

Utilizing Material-UI checkboxes in combination with ReactJS and Redux for enhanced functionality

I am currently working on a project where I need to display selected checkbox items using material-ui's checkbox component. Although I have successfully displayed the items with checkboxes, I am facing difficulties in displaying the selected items. ...

My website isn't reloading due to the service worker cache after transitioning from React to Next.js. What's the best way to trigger an update?

We recently migrated a website from React to Next.js and encountered an issue with the service worker for caching. After running the unregister() code, we noticed that on certain machines the code did not execute properly, resulting in those machines still ...

How can I locate ThreeJS coordinates within the Panoramic GUI?

While I've dabbled with ThreeJS in the past, I'm currently working on a new project that involves setting up hotspots in a panoramic view. I vaguely recall using the camera dolly tool from this link to visualize camera locations. However, I' ...

The <nav> and <section> elements are not appearing on the page

Website: CSS: Hello there! I am experiencing an issue where the nav and section elements are not displaying on my website. They seem to only show the height and background-color attributes. Interestingly, they appear correctly on my old Firefox version ( ...