Screen Size Component by OrcDev
This component will display the screen size of the user. It will update as the user resizes the window. You can see it in the bottom right corner of the screen.
You can watch the creation video of this component here:
"use client";
import { useEffect, useState } from "react";
export const ScreenSize = () => {
const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
useEffect(() => {
function updateDimensions() {
setDimensions({
width: window.innerWidth,
height: window.innerHeight,
});
}
updateDimensions();
window.addEventListener("resize", updateDimensions);
return () => {
window.removeEventListener("resize", updateDimensions);
};
}, []);
const { width, height } = dimensions;
return (
<div className="fixed bottom-5 right-5 flex items-center gap-2 rounded-full bg-black px-2.5 py-1 font-mono text-xs font-medium text-white">
<span>
{width.toLocaleString()} x {height.toLocaleString()}
</span>
<div className="h-4 w-px bg-gray-800" />
<span className="sm:hidden">XS</span>
<span className="hidden sm:inline md:hidden">SM</span>
<span className="hidden md:inline lg:hidden">MD</span>
<span className="hidden lg:inline xl:hidden">LG</span>
<span className="hidden xl:inline 2xl:hidden">XL</span>
<span className="hidden 2xl:inline">2XL</span>
</div>
);
};
Next.js Usage
You can use this component in your Next.js project by adding the following code to your layout file.
...
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en" suppressHydrationWarning>
<body
className={cn(
"min-h-screen bg-background font-sans antialiased",
fontSans.variable
)}
>
{children}
{process.env.ENVIRONMENT === "development" && <ScreenSize />}
</body>
</html>
);
}