F
Filament16mo ago
Aurko

How can I get the trigger of the filament's dark-light toggle button?

Hey, need help. how can I get the trigger of the filament's dark-light toggle button? I've created a custom page. So over there, based on the dark mode I want to update css of a specific div. That's why I need to catch the dark mode toggle.
27 Replies
Thijmen
Thijmen16mo ago
You can use the .dark tailwind class
Aurko
AurkoOP16mo ago
Yes, that's a way to get the current state. But dark mode toggle button can't trigger using this strategy, cat it?
Thijmen
Thijmen16mo ago
If you do something like .dark .custom-class you can add css in that way So that css will only be visible in darkmode Because that is what you want right? Custom css in darkmode?
Arko
Arko16mo ago
I like your name
Thijmen
Thijmen16mo ago
Daar heeft die veel aan 😂
Aurko
AurkoOP16mo ago
.a{ color: black; } .b{ color: red; } <p>Hello World</p> In here, I want to add class 'a' in light mode, and add class 'b' in dark mode. And this class-add and class-remove will do when the dark mode button triggers. This is my issue actually!
Thijmen
Thijmen16mo ago
if you do <p class="a dark:b"> it should work Make make the css in b important But i think that is the best way to handle this Have a look at the filament classes to see how they handle different style in darkmode
Arko
Arko16mo ago
Jij bent hier voor de support, ik voor de small talk Ieder zo z'n taak
Thijmen
Thijmen16mo ago
Prima verdeling
Aurko
AurkoOP16mo ago
<span class="text-red dark:text-blue">Welcome</span> just tried like this, not working.
Thijmen
Thijmen16mo ago
Did the css show up in inspect?
Arko
Arko16mo ago
Build the css Class probably isnt compiled by tailwind
Thijmen
Thijmen16mo ago
Do you have a custom theme? And custom css file should be registered in boot method with FilamentAsset:: i believe
Aurko
AurkoOP16mo ago
nope
Thijmen
Thijmen16mo ago
Then css isnt building
Aurko
AurkoOP16mo ago
custom css working on the light mode. actually developed keeping that in mind. but changing has become challenging.
Thijmen
Thijmen16mo ago
How did you register the css file?
Aurko
AurkoOP16mo ago
just below the <x-filament::page> tag of the page, then like this- <link rel="stylesheet" href="{{ asset('css/style.css') }}"> just like how we do in a regular blade page
Thijmen
Thijmen16mo ago
I don't think you can use the dark class then Because tailwind needs to load that So you should add that file to tailwind too
Aurko
AurkoOP16mo ago
how can I then?
Thijmen
Thijmen16mo ago
If you are going to use custom classes best way is to create a theme
Aurko
AurkoOP16mo ago
understood but I want thinking that if I can get the trigger of the dark mode toggle button, that would be easier to manage the classes.
Thijmen
Thijmen16mo ago
Adding that class to the dark class is way easier then looking at the toggle
<script>
const theme = localStorage.getItem('theme')

if (
theme === 'dark' ||
(!theme &&
window.matchMedia('(prefers-color-scheme: dark)')
.matches)
) {
document.documentElement.classList.add('dark')
}
</script>
<script>
const theme = localStorage.getItem('theme')

if (
theme === 'dark' ||
(!theme &&
window.matchMedia('(prefers-color-scheme: dark)')
.matches)
) {
document.documentElement.classList.add('dark')
}
</script>
I suggest looking if the document has the dark class But that is exactly what the .dark class does
Aurko
AurkoOP16mo ago
yeah, searched and searching. couldn't find that yet. The best if I able to get the id of the color mode toggle button.
Thijmen
Thijmen16mo ago
Just create a theme and start using the .dark class
Aurko
AurkoOP16mo ago
Found solution- window.addEventListener('dark-mode-toggled', (event) => { console.log(event.detail); });

Did you find this page helpful?