mozmorris / react-webcam

Webcam component
https://codepen.io/mozmorris/pen/JLZdoP
MIT License
1.65k stars 281 forks source link

Working with styled-components #315

Closed RubenZx closed 2 years ago

RubenZx commented 2 years ago

Is the component prepared to work with "styled-components"?

I'm not able to use the refprop. I'm having problems when I try to pass ref with the camera wrapped by styled.

The code that I have is the following:

import { useCallback, useRef } from "react";
import Webcam from "react-webcam";
import styled from "styled-components";

const StyledWebcam = styled(Webcam)`
  border-radius: 1.125rem;
`;

const MyCameraComponent = () => {
  const webcamRef = useRef<Webcam>(null);

  const capture = useCallback(() => {
    if (webcamRef.current) {
      const imageSrc = webcamRef.current.getScreenshot();
      // TODO
    }
  }, [webcamRef]);

  return (
    <>
      <StyledWebcam
        audio={false}
        ref={webcamRef}
        screenshotFormat="image/jpeg"
        videoConstraints={videoConstraints}
      />
      <button onClick={capture}>Capture photo</button>
    </>
  );
}

export default MyCameraComponent;

Versions that I'm using

mozmorris commented 2 years ago

Here's an example: https://codepen.io/mozmorris/pen/wveZEYd?editors=0010