Added dark mode detection

Added automatic dark mode detection to home.blade.php via a cookie with js.cookie.min.js (see previous commit).

Detecting the dark mode setting is done with JavaScript. The script detects the preferred dark mode setting in the client's browser and saves it with a cookie. If the script detects a change in this setting, it corrects the cookie. 

Loading the appropriate skeleton.css is done via PHP with an if-else statement. If the cookie (saved by the previous script) color scheme equals 'dark' it will load skeleton-dark.css (found in /littlelink/css). 

If dark mode is not detected OR nothing is detected/cookie is getting blocked, the statement defaults to skeleton-light.css (also found in /littlelink/css).

You could change what the if-else statement defaults to (i.e. which color scheme is loaded if the cookie fails to detect) by switching 'skeleton-dark.css' 'skeleton-light.css around' (see comment in if statement).

Everything added is commented and marked with begin/end.


Credit to 'user1575941' on Stack Overflow who's commented code REALLY helped getting behind all this.
This commit is contained in:
JulianPrieber 2022-02-18 10:02:43 +01:00 committed by GitHub
parent b81190bddc
commit cc71e0d6ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 0 deletions

View File

@ -11,6 +11,34 @@
<link rel="stylesheet" href="{{ asset('littlelink/css/hover-min.css') }}"> <link rel="stylesheet" href="{{ asset('littlelink/css/hover-min.css') }}">
<link rel="stylesheet" href="{{ asset('littlelink/css/animate.css') }}"> <link rel="stylesheet" href="{{ asset('littlelink/css/animate.css') }}">
<link rel="icon" type="image/png" href="{{ asset('littlelink/images/avatar.png') }}"> <link rel="icon" type="image/png" href="{{ asset('littlelink/images/avatar.png') }}">
<!-- begin dark mode detection -->
<script src="{{ asset('littlelink/js/js.cookie.min.js') }}"></script>
<script>
// code to set the `color_scheme` cookie
var $color_scheme = Cookies.get("color_scheme");
function get_color_scheme() {
return (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) ? "dark" : "light";
}
function update_color_scheme() {
Cookies.set("color_scheme", get_color_scheme());
}
// read & compare cookie `color-scheme`
if ((typeof $color_scheme === "undefined") || (get_color_scheme() != $color_scheme))
update_color_scheme();
// detect changes and change the cookie
if (window.matchMedia)
window.matchMedia("(prefers-color-scheme: dark)").addListener( update_color_scheme );
</script>
<?php // loads dark mode CSS if dark mode detected
$color_scheme = isset($_COOKIE["color_scheme"]) ? $_COOKIE["color_scheme"] : false; ?>
@if ($color_scheme == 'dark')
<!-- switch the two <link> Tags below to default to dark mode if cookie detection fails -->
<link rel="stylesheet" href="{{ asset('littlelink/css/skeleton-dark.css') }}">
@else
<link rel="stylesheet" href="{{ asset('littlelink/css/skeleton-light.css') }}">
@endif
<!-- end dark mode detection -->
</head> </head>
<body> <body>
<div class="container"> <div class="container">