Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Change css of relative element based on change or click in reactjs

I have created a form with custom styled input component in react material ui,it goes something like this

onst StyledInputElement = styled('input')(`
  width: 200px;
  font-size: 1rem;
  font-family: IBM Plex Sans, sans-serif;
  font-weight: 400;
  line-height: 1.4375em;
  background: rgb(243, 246, 249);
  border: 1px solid #E5E8EC;
  border-radius: 10px;
  padding: 6px 10px;
  color: #20262D;
  transition: width 300ms ease;

  &:hover {
    background: #EAEEF3;
    border-color: #E5E8EC;
  }

  &:focus {
    outline: none;
    width: 320px;
    transition: width 200ms ease-out;
  }
`);

const CustomInput = React.forwardRef(function CustomInput(props, ref) {
  return (
    <InputUnstyled components={{ Input: StyledInputElement }} {...props} ref={ref} />
  );
});


const AuthDialog = (props) => {

  return (
    <>
      <CssBaseline />
      <Dialog
        fullWidthS
        onClose={() => {}}
        open={props.dilaogOpenProp}
        maxWidth="xs"
        sx={{
          backdropFilter: "blur(3px)",
        }}
      >
        <DialogTitle>Please login</DialogTitle>
        <DialogContent>
        <CustomInput aria-label="Demo input" placeholder="Username" />
        <br/>
        <CustomInput onClick aria-label="Demo input" placeholder="Password" />
        </DialogContent>
        <DialogActions>
          <Button onClick={props.handleDialogCloseProp}>Close</Button>
        </DialogActions>
      </Dialog>
      <Box
        sx={{
          minHeight: "100vh",
          background:
            "url(https://source.unsplash.com/random) no-repeat center center",
          backgroundSize: "cover",
        }}
      ></Box>
    </>
  );
}

export default AuthDialog;

Now it works something like this

Before click

enter image description here

As you can see on clicking one of the text fields is expanding but that leaves another text field to its same size, how do I apply text field expands CSS on both of the text fields even if I click any one of them?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use :focus-within on the parent of the two inputs.

const StyledContainer = styled('div')(`
   &:focus-within input {
     outline: none;
     width: 320px;
     transition: width 200ms ease-out;
   }
`)

Then surround your two inputs with this container:

<StyledContainer>
  <CustomInput aria-label="Demo input" placeholder="Username" />
  <br/>
  <CustomInput onClick aria-label="Demo input" placeholder="Password" />
</StyledContainer>

(Note: not supported by IE11)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!