I tried this solution but got an error.
Adding a Script to Detect Mobile View
<script>
// Function to check if the device is mobile
function isMobile() {
return window.innerWidth <= 768; // Adjust the breakpoint as needed
}
// Disable transitions for mobile devices
if (isMobile()) {
console.log("Mobile device detected. Disabling transitions.");
document.documentElement.setAttribute("data-transitions", "disabled");
} else {
console.log("Desktop device detected. Enabling transitions.");
document.documentElement.setAttribute("data-transitions", "enabled");
}
// Optional: Re-check on window resize
window.addEventListener("resize", () => {
if (isMobile()) {
document.documentElement.setAttribute("data-transitions", "disabled");
} else {
document.documentElement.setAttribute("data-transitions", "enabled");
}
});
</script>
Then conditionally apply transitions in layout file (e.g., Layout.astro), used the data-transitions attribute to conditionally apply transitions. For example:
import { ViewTransitions } from "astro:transitions";
<html>
<head>
<!-- Conditionally include ViewTransitions -->
{document.documentElement.getAttribute("data-transitions") === "enabled" && (
<ClientRouter /> )} // ❌ Error!
</head>
<body>
<slot /> <!-- Your page content goes here -->
</body>
</html>
So how could I conditionaly apply <ClientRouter /> depending on the breakingpoint?