<?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: rugk</title>
    <description>The latest articles on DEV Community by rugk (@rugk).</description>
    <link>https://dev.to/rugk</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%2F83846%2Fda5fc454-850a-4bf3-9b14-0993823a0ab8.png</url>
      <title>DEV Community: rugk</title>
      <link>https://dev.to/rugk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rugk"/>
    <language>en</language>
    <item>
      <title>DYK your website can get a dark mode toggled by your operating system?</title>
      <dc:creator>rugk</dc:creator>
      <pubDate>Tue, 21 May 2019 15:33:01 +0000</pubDate>
      <link>https://dev.to/rugk/dyk-your-website-can-get-a-dark-mode-toggled-by-your-operating-system-inl</link>
      <guid>https://dev.to/rugk/dyk-your-website-can-get-a-dark-mode-toggled-by-your-operating-system-inl</guid>
      <description>&lt;p&gt;Your website can have a light and dark mode that is automatically toggled by your operation system (OS) or browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making your website dark
&lt;/h2&gt;

&lt;p&gt;Here is the trick: You can use the new &lt;a href="https://developer.mozilla.org/docs/Web/CSS/@media/prefers-color-scheme"&gt;&lt;code&gt;prefers-color-scheme&lt;/code&gt;&lt;/a&gt; media query feature, specified &lt;a href="https://drafts.csswg.org/mediaqueries-5/#descdef-media-prefers-color-scheme"&gt;in the Media Queries Level 5&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You simply include a CSS like this:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefers-color-scheme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dark&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This would e.g. just do some simple CSS changes on the colors on your body.&lt;/p&gt;

&lt;h2&gt;
  
  
  Via JavaScript?
&lt;/h2&gt;

&lt;p&gt;You can also request the status of this property (just like any other media query) with &lt;a href="https://developer.mozilla.org/docs/Web/API/Window/matchMedia"&gt;&lt;code&gt;.matchMedia()&lt;/code&gt;&lt;/a&gt;. This e.g. returns the status:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;matchMedia&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"(prefers-color-scheme: dark)"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;matches&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(In this example, the dark mode would be disabled.)&lt;/p&gt;

&lt;h2&gt;
  
  
  There is a catch, is not there?
&lt;/h2&gt;

&lt;p&gt;Yes, as with many good things, there are some restrictions here…&lt;/p&gt;

&lt;p&gt;This feature &lt;a href="https://caniuse.com/#feat=prefers-color-scheme"&gt;is only supported&lt;/a&gt; by Safari 12.1 and Firefox 67, but I guess browser support will grow, as it is standardized.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;There is a nice blog article by &lt;em&gt;Andy Clarke&lt;/em&gt; about &lt;a href="https://stuffandnonsense.co.uk/blog/redesigning-your-product-and-website-for-dark-mode"&gt;how to design/adjust your website properly&lt;/a&gt; for that dark mode.&lt;/li&gt;
&lt;li&gt;For the just recently released Firefox 67, I've created an &lt;a href="https://addons.mozilla.org/de/firefox/addon/dark-mode-website-switcher/?src=external-devto"&gt;add-on called &lt;strong&gt;“Dark Website Forcer”&lt;/strong&gt;&lt;/a&gt; that you can use to toggle that setting directly in your browser.
Note it has some restrictions on what it can do (see the add-on description), and feel free to urge &lt;a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1547818"&gt;Mozilla for a better API&lt;/a&gt;.
Of course &lt;a href="https://github.com/rugk/website-dark-mode-switcher"&gt;it's open-source&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>mediaquery</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Fighting an autonomous button tag… – a story about debugging</title>
      <dc:creator>rugk</dc:creator>
      <pubDate>Thu, 18 Apr 2019 12:08:42 +0000</pubDate>
      <link>https://dev.to/rugk/fighting-an-autonomous-button-tag-a-story-about-debugging-4d7o</link>
      <guid>https://dev.to/rugk/fighting-an-autonomous-button-tag-a-story-about-debugging-4d7o</guid>
      <description>&lt;p&gt;I had this simple button tag and added some JS behavior to via a JavaScript lib that I've also written and that is already used for some other buttons on the page. Next to the button it displayed a message under some conditions and the button triggered a request that could change it. So, actually, I had expected nothing fancy here… (you can imagine, I was wrong.)&lt;/p&gt;

&lt;h2&gt;
  
  
  The issue
&lt;/h2&gt;

&lt;p&gt;In the beginning, there was a bug: When clicking on that new button it would switch to a message that I did not expect and that should actually only be shown when I open the page.&lt;/p&gt;

&lt;p&gt;Checking it with the debugger and diving into the code, I saw my code was executed as expected, only &lt;em&gt;afterwards&lt;/em&gt; it did still show the strange behavior.&lt;/p&gt;

&lt;p&gt;Actually, I noticed it triggers the code twice and thus shows the different message, but I had no idea, why it was triggered twice.&lt;br&gt;
Note I had other buttons on the page and these worked fine when clicking on them. And I double-checked the HTML code, it is exactly the same… (I copied it anyway for a test, which did not help…)&lt;/p&gt;

&lt;p&gt;Thus I added more breakpoints and went through the JS code line by line… And one thing totally confused me: the initialization code runs again. Runs twice, and triggers my button function. (which is, by itself, expected when the init code runs, because I call it manually. But only once when the page is loaded.)&lt;/p&gt;

&lt;p&gt;However, nowhere did I call the init code again in the button click handler? Furthermore, how can it even run my init code? This code should only be run once when the page is loaded. 🤔&lt;/p&gt;

&lt;p&gt;Going it through I finally got it: As you can see in the code, I had the button wrapped in an a tag that was not used in this case (it would only be used when I added a link), so it has to be this: It redirects the page away, because I have no &lt;code&gt;href&lt;/code&gt; link set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;CLICK ME&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I've removed that and it finally… to my surprise… it did not work. It still had the mentioned bug…&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;em&gt;(real)&lt;/em&gt; issue
&lt;/h2&gt;

&lt;p&gt;So that thing needed to go on, and I already assumed a bug in the browser. After all, it should not run my init code.&lt;br&gt;
I already was about to create a minified test case, at least, so I could report it somewhere. The dramatical measure was to just remove much JS, reducing things that could interfere. Given all the other buttons work, it has to be some tiny bug in my new JS, after all.&lt;/p&gt;

&lt;p&gt;I ended up removing all my JS. This way, I could at least confirm that it is a bug in the HTML-only – if it did not appear now.&lt;/p&gt;

&lt;p&gt;To be sure, I also manually added a "click" event listener to the button, so I could see whether the click event itself caused it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, yes, the small click event was triggered. But, what's more important, in this really simple test case, the bug was gone! 🎉 So at least I got the confidence the bug is in my JS code, or so I thought… (later this would turn out as absolutely wrong…)&lt;/p&gt;

&lt;p&gt;Thinking about it a little more, I thought it must have been some kind of reload. However, I was not aware to have ever implemented this kind of thing.&lt;/p&gt;

&lt;p&gt;Then I more or less accidentally stumbled upon an example code for a button like this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;click&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You see the &lt;code&gt;type&lt;/code&gt; attribute? This blew my mind. From this point on, I know I've solved it… This time, for real. 😃&lt;/p&gt;

&lt;p&gt;A crazy simple HTML error, discovered only after hours of debugging.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happened?
&lt;/h2&gt;

&lt;p&gt;Actually, what the button did was reloading the site. Why? Well…, because it is a “form submission button“. Look at the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type"&gt;MDN doc for the button tag&lt;/a&gt; and  you'll see that, by default, a button is no button, but has the &lt;code&gt;type=submit&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;type&lt;/code&gt;&lt;br&gt;
 The type of the button. Possible values are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;submit&lt;/code&gt;: The button submits the form data to the server. &lt;strong&gt;This is the default if the attribute is not specified&lt;/strong&gt;, or if the attribute is dynamically changed to an empty or invalid value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;reset&lt;/code&gt;: The button resets all the controls to their initial values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;button&lt;/code&gt;: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;(highlighting by me)&lt;/p&gt;

&lt;p&gt;This crazy part of the HTML5 spec requires me to &lt;strong&gt;explicitly define that a button is a button&lt;/strong&gt;, because otherwise it is regarded as a submit button by default and if there is no data to submit (no &lt;code&gt;input&lt;/code&gt; elements in the form), it just reloads the site. In my case, I even had data to submit, but it was not shown to me due to the circumstances… (see question below)&lt;/p&gt;

&lt;h3&gt;
  
  
  So why did not I saw it reloading?
&lt;/h3&gt;

&lt;p&gt;If I had a normal web page, I would have immediately seen the reloading. However, in my case, I was developing a browser extension/add-on's &lt;a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Options_pages"&gt;options page&lt;/a&gt;. There is neither an address bar nor a reload indicator shown. So I could not see anything… It would have been possible to open the page in an extra tab for debugging, at least, and it retro-perspective I should have done so, but at that time I did not get that. Knowing the reload behavior would have made some things much easier as I would not have to guess which strange browser bug triggers the JS code that is supposed to only run at the page load.&lt;br&gt;&lt;br&gt;
Also note my page was fast and simple enough, so I could not see any visual indicators of reloading or so.&lt;/p&gt;

&lt;h3&gt;
  
  
  So why did it work when removing all JS?
&lt;/h3&gt;

&lt;p&gt;The removal of all JS should have tested whether the bug is caused by HTML only – which, as we now know, is actually the case. However, it has pointed me in the opposite direction: the bug was not apparent anymore, letting me think the bug is in my JS code.&lt;/p&gt;

&lt;p&gt;The reason for this was that I obviously removed all JavaScript code. Thus, the code that was triggered on reload (the init code) that showed me the visual clue of the bug (the different message) was also removed and could not trigger. 😆&lt;br&gt;
 So the site was still reloading, but I did not see the reloading anymore as I also removed the code that would trigger on reload.&lt;/p&gt;

&lt;h3&gt;
  
  
  So why did the other buttons work?
&lt;/h3&gt;

&lt;p&gt;Part of why this drove me crazy is the fact that I had exactly the same buttons attached with click event handlers in exactly the same way in another part of the page.&lt;/p&gt;

&lt;p&gt;However, as I now know, all these buttons semantically were submit buttons, as I have always missed the &lt;code&gt;type="button"&lt;/code&gt;. Despite that, they did not trigger a reload as they were not included in the &lt;code&gt;form&lt;/code&gt; tag. Because what I did not mention yet: Obviously, the “submit“ button can only “submit“ the form if I is inside of a &lt;code&gt;form&lt;/code&gt; tag. Without it, the browser silently ignores the semantically invalid button… arrgh…&lt;br&gt;
That's why the other buttons did not trigger a reload.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do you blame the spec?
&lt;/h3&gt;

&lt;p&gt;The article description has indicated it, has not it? Actually, despite the bad circumstances and me heading directly into the wrong way, I partially do, yes. Why should I have to &lt;em&gt;explicitly define&lt;/em&gt; that a button has no functionality? Why should I have to define that a button is a button? Why does it have a functionality by default? Especially in such a hidden way…&lt;/p&gt;

&lt;h3&gt;
  
  
  What project is this in/about?
&lt;/h3&gt;

&lt;p&gt;It was an issue &lt;a href="https://github.com/rugk/awesome-emoji-picker/pull/10"&gt;in the options page for a new feature in one of my add-ons&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>debugging</category>
      <category>webdev</category>
      <category>html</category>
      <category>webextension</category>
    </item>
  </channel>
</rss>
