forked from langfuse/langfuse-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobileSwitch.tsx
More file actions
33 lines (27 loc) · 806 Bytes
/
mobileSwitch.tsx
File metadata and controls
33 lines (27 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import React, { useState, useEffect, useRef } from "react";
// Define the breakpoint
const MOBILE_BREAKPOINT = 650;
export default function MobileSwitch(props: {
mobile: React.ElementType;
desktop: React.ElementType;
}) {
const [isMobile, setIsMobile] = useState<boolean | null>(null);
const objectRef = useRef(null);
useEffect(() => {
const handleResize = () => {
setIsMobile(objectRef.current.offsetWidth <= MOBILE_BREAKPOINT);
};
handleResize();
// Attach event listener
window.addEventListener("resize", handleResize);
// Cleanup
return () => {
window.removeEventListener("resize", handleResize);
};
}, []);
return (
<div ref={objectRef}>
{isMobile !== null ? (isMobile ? props.mobile : props.desktop) : null}
</div>
);
}