<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: tnmy-srkr</title>
    <description>The latest articles on DEV Community by tnmy-srkr (@tnmysrkr).</description>
    <link>https://dev.to/tnmysrkr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1035801%2F55590ca5-8e78-4ab3-ab9e-4b69741b11f5.png</url>
      <title>DEV Community: tnmy-srkr</title>
      <link>https://dev.to/tnmysrkr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tnmysrkr"/>
    <language>en</language>
    <item>
      <title>Tailwindcss manual dark mode</title>
      <dc:creator>tnmy-srkr</dc:creator>
      <pubDate>Sun, 22 Oct 2023 08:03:11 +0000</pubDate>
      <link>https://dev.to/tnmysrkr/tailwindcss-manual-dark-mode-5eoh</link>
      <guid>https://dev.to/tnmysrkr/tailwindcss-manual-dark-mode-5eoh</guid>
      <description>&lt;p&gt;sourse : &lt;a href="https://the-best.shahriyar.dev/p/5d5effc6-e23b-4e0c-96b8-3ca1ce6b772c"&gt;https://the-best.shahriyar.dev/p/5d5effc6-e23b-4e0c-96b8-3ca1ce6b772c&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Config:&lt;br&gt;
To enable the manual dark mode feature in your tailwindcss website first add darkMode: 'class' in your tailwind.config.js file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;module.exports = {&lt;br&gt;
    darkMode: 'class',&lt;br&gt;
    ...&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Hook:&lt;br&gt;
Then we have to create a custom hook for getting the current theme and toggling the theme.&lt;/p&gt;

&lt;p&gt;`import { useEffect, useState } from "react"&lt;/p&gt;

&lt;p&gt;const themes = {&lt;br&gt;
  dark: 'light',&lt;br&gt;
  light: 'dark'&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;const useTheme = () =&amp;gt; {&lt;br&gt;
  const [theme, setTheme] = useState('dark')&lt;/p&gt;

&lt;p&gt;const toggleTheme = () =&amp;gt; {&lt;br&gt;
    const _theme = themes[theme]&lt;br&gt;
    localStorage.setItem('theme', _theme)&lt;br&gt;
    updateTheme(_theme)&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;const updateTheme = (name) =&amp;gt; {&lt;br&gt;
    setTheme(name)&lt;br&gt;
    document.querySelector('html')&lt;br&gt;
      ?.classList.remove(themes[name])&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.querySelector('html')
  ?.classList.add(name)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;} &lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    const _theme = localStorage.getItem('theme') || 'dark'&lt;br&gt;
    updateTheme(_theme)&lt;br&gt;
  }, [])&lt;/p&gt;

&lt;p&gt;return { theme, toggleTheme }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;export default useTheme;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;Import and use:&lt;br&gt;
Finally, we can import the useTheme hook in any of our components and bind the toggleTheme function with a button or any element. Whenever we call the toggleTheme function it will change the website's theme to the opposite.&lt;/p&gt;

&lt;p&gt;`function ThemeButton() {&lt;br&gt;
    const { toggleTheme } = useTheme()&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
    &amp;lt;button onClick={toggleTheme}&amp;gt;Change Theme&amp;lt;/button&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
