A
A
Alexander2021-06-06 21:38:41
React
Alexander, 2021-06-06 21:38:41

How to refer to an adjacent element in styled-components?

Can you please tell me how can I refer to another element in styled-components?
It is necessary to set styles for NavIcon when receiving props in SearchField

export const NavIcon = styled.svg`
  font-size: 20px;
  margin-right: 15px;
  color: inherit;
  &:hover {
    color: #000;
    cursor: pointer;
  }
`

export const SearchField = styled.input<{ isOpen: boolean }>`
  border-radius: 50px;
  border: 2px solid ${(props) => props.theme.colors.primary};
  padding: 8px 0;
  outline: none;
  transition: all 0.3s ease-in-out;
  opacity: 0;
  width: 0;
  margin-right: 15px;
  ${(props) => props.isOpen && `opacity: 1; width: 200px; padding: 8px 15px;`};
  ${(props) => props.isOpen && NavIcon...

`


<SearchForm ref={searchRef} onSubmit={(e) => e.preventDefault()}>
      <NavIcon
        as={BiSearch}
        onClick={() => {
          setIsOpen(!isOpen)
        }}
      />
      <SearchField isOpen={isOpen} type='text' placeholder='Search...' />
    </SearchForm>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan V, 2021-06-07
@verkhoturov

In css, you can refer to an adjacent element through "+". It also works in styled-components.

export const NavIcon = styled.svg`
  font-size: 20px;
  margin-right: 15px;
  color: inherit;
  &:hover {
    color: #000;
    cursor: pointer;
  }

  + ${SearchField} {
    ....
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question