mirror of
https://github.com/LinkStackOrg/LinkStack.git
synced 2025-04-16 03:07:21 +02:00
Update dark mode detection
This commit is contained in:
parent
1e07107d24
commit
9033c72af3
41
assets/js/detect-dark-mode.js
vendored
41
assets/js/detect-dark-mode.js
vendored
@ -1,8 +1,43 @@
|
|||||||
// Retrieve the value of the 'color-mode' key from local storage
|
// Function to get the value of a cookie by name
|
||||||
var colorMode = localStorage.getItem('color-mode');
|
function getCookie(name) {
|
||||||
|
const cookies = document.cookie.split('; ');
|
||||||
|
for (const cookie of cookies) {
|
||||||
|
const [key, value] = cookie.split('=');
|
||||||
|
if (key === name) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to set a cookie
|
||||||
|
function setCookie(name, value, days) {
|
||||||
|
let expires = '';
|
||||||
|
if (days) {
|
||||||
|
const date = new Date();
|
||||||
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
||||||
|
expires = `; expires=${date.toUTCString()}`;
|
||||||
|
}
|
||||||
|
document.cookie = `${name}=${value || ''}${expires}; path=/`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve the value of the 'color-mode' cookie
|
||||||
|
var colorMode = getCookie('color-mode');
|
||||||
|
|
||||||
// Check if the value is valid and set it to 'auto' if not
|
// Check if the value is valid and set it to 'auto' if not
|
||||||
if (!colorMode || (colorMode !== 'light' && colorMode !== 'dark')) {
|
if (!colorMode || (colorMode !== 'light' && colorMode !== 'dark')) {
|
||||||
colorMode = 'auto';
|
colorMode = 'auto';
|
||||||
localStorage.setItem('color-mode', colorMode);
|
setCookie('color-mode', colorMode, 365); // Set the cookie to expire in 1 year
|
||||||
|
}
|
||||||
|
|
||||||
|
// If color mode is 'auto', detect the user's color scheme
|
||||||
|
if (colorMode === 'auto') {
|
||||||
|
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||||
|
if (prefersDarkScheme) {
|
||||||
|
document.body.classList.add('dark');
|
||||||
|
}
|
||||||
|
} else if (colorMode === 'dark') {
|
||||||
|
if (!document.body.classList.contains('dark')) {
|
||||||
|
document.body.classList.add('dark');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1208,6 +1208,10 @@ MODAL; // <-- Indentation breaks my code editor :/
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@if($colorMode == 'auto')
|
||||||
|
<script src="{{asset('assets/js/detect-dark-mode.js')}}"></script>
|
||||||
|
@endif
|
||||||
|
|
||||||
{{-- Unused. Download for removed PNG QR Code generation feature. --}}
|
{{-- Unused. Download for removed PNG QR Code generation feature. --}}
|
||||||
{{-- <script>
|
{{-- <script>
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
@ -1235,8 +1239,6 @@ MODAL; // <-- Indentation breaks my code editor :/
|
|||||||
|
|
||||||
@stack('sidebar-scripts')
|
@stack('sidebar-scripts')
|
||||||
|
|
||||||
{{-- <script src="{{ asset('assets/js/detect-dark-mode.js') }}"></script> --}}
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener('livewire:navigated', () => {
|
document.addEventListener('livewire:navigated', () => {
|
||||||
var collapseElement = document.getElementById("admin-section");
|
var collapseElement = document.getElementById("admin-section");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user