Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
Button outside SVG not clickable

I have inside a button an animated SVG with some javascript, both have to be clickable BCS. of the animation and the state of the button itself which I need to handle.

can somebody help me

here you can see the full code in codesandbox https://codesandbox.io/s/vigilant-field-uyvvcr?file=/src/App.js

  const [activeFilter, setActiveFilter] = useState(false)

  const mouseEnter = () => {
    if (!activeFilter) {
      setActiveFilter(true)
    } else {
      setActiveFilter(false)
    }
    console.log(document.querySelector('object'))
  }

  return(
    <div className='search__container'>
      <div className='search'>
        <input placeholder='Search' type='text' />
        <button onClick={mouseEnter} className={`${activeFilter? 'active ' : ''}filterButton`}>
          <object type="image/svg+xml" data={FilterIcon} />
        </button>
      </div>
    </div>
  )
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Why does this happen?

The html object element creates a new browsing context (you can see if you inspect with the developer tools: notice an embedded #document under object). The click event captured within the svg fires and bubbles up this context, but never goes beyond it. As a consequence, it never reaches the button element.

How to solve?

You don't want to have two browsing contexts. In order to do so, you can simply get rid of the object and insert the svg as a direct child of the button.

For your particular case of using react

You could use svg as react component:

<button onClick={mouseEnter} className={`${activeFilter? 'active ' : ''}filterButton`}>
  <FilterIcon />
</button>

You may want to investigate SVGR.

Hopefully, as it seems you are using create-react-app, it should come for free.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda