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

261
Views
Nullify transforms (primarily rotation) in getBoundingClientRect?

I'm currently using this as a workout (just flat out removing the transform, getting the rect, and putting it back).

  useEffect(() => {
    if (typeof document !== undefined) {
      const ctn = document.getElementById(id);
      const parent = ctn.parentElement;
      const parentStyles = getComputedStyle(parent);
      const transform = parentStyles.transform;
      parent.style.transform = "rotate(0deg)";
      const box = parent.getBoundingClientRect(); 
      if (box.width > box.height * 0.5008) { // These numbers are specific to the aspect ratio of the phone frame, unrelated to question
        setHeight("100%");
        setWidth(box.height * 0.5008);
      } else {
        setHeight(box.width * 1.9888);
        setWidth("100%");
      }
      setBorderRadius(box.width * 0.125);
      parent.style.transform = transform;
    }
  }, [screen]);

The reason why I'm asking is because this is for a 'mock-up' component, i.e. you give it an image and it returns a nice mock-up. The general idea is that you just wrap it in whatever sized container you want, and it will auto-size everything with JS so the mock-up looks perfect. It's essentially 2 layers, the phone-frame img up top and the screen img below. I grab the width/height of the parent container and do a calculation to make the "screen" image line up perfectly with the phone.

No rotate, working good

If I don't completely remove the rotation transform before using getBoundingClientRect(), it calculates the width with the transform included, which screws up the element sizing. My current solution just feels sort of janky, and it makes you unable to change transforms on the parent container's CSS style sheet without a JS solution (because the transform just gets readded to inline-styles).

about 4 years ago · Juan Pablo Isaza
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!