Created
September 9, 2021 04:07
-
-
Save DonHuskini/29bd3fc17b5b0017bbcd9d5bc24d9324 to your computer and use it in GitHub Desktop.
workaround aspect-ratio safari
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRef, useEffect, useState } from "react"; | |
export default function Test() { | |
const [height, setHeight] = useState(0); | |
const ref = useRef<HTMLDivElement>(null); | |
useEffect(() => { | |
if (ref.current) { | |
setHeight((ref.current.clientWidth * 16) / 9); | |
} | |
}, [ref]); | |
return ( | |
<div className="flex items-center justify-center w-screen h-screen bg-green-400"> | |
<div | |
style={{ height: `${height}px` }} | |
ref={ref} | |
className="w-96 rounded-md shadow-md bg-green-700" | |
/> | |
</div> | |
); | |
return ( | |
<div className="flex items-center justify-center w-screen h-screen bg-green-400"> | |
<div | |
style={{ aspectRatio: "9/16" }} | |
className="w-96 rounded-md shadow-md bg-green-700" | |
/> | |
</div> | |
); | |
} |
mesterum
commented
Feb 6, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment