import type { FunctionalComponent } from 'preact';
import { h, Fragment } from 'preact';
import { useState, useEffect } from 'preact/hooks';
import './ThemeToggleButton.css';

const themes = ['light', 'dark'];

const icons = [
  <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="currentColor">
    <path
      fillRule="evenodd"
      d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z"
      clipRule="evenodd"
    />
  </svg>,
  <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="currentColor">
    <path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z" />
  </svg>,
];

const ThemeToggle: FunctionalComponent = () => {
  const [theme, setTheme] = useState(() => {
    if (import.meta.env.SSR) {
      return undefined;
    }
    if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
      return localStorage.getItem('theme');
    }
    if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
      return 'dark';
    }
    return 'light';
  });

  useEffect(() => {
    const root = document.documentElement;
    if (theme === 'light') {
      root.classList.remove('theme-dark');
    } else {
      root.classList.add('theme-dark');
    }
  }, [theme]);

  return (
    <div class="theme-toggle">
      {themes.map((t, i) => {
        const icon = icons[i];
        const checked = t === theme;
        return (
          <label className={checked ? ' checked' : ''}>
            {icon}
            <input
              type="radio"
              name="theme-toggle"
              checked={checked}
              value={t}
              title={`Use ${t} theme`}
              aria-label={`Use ${t} theme`}
              onChange={() => {
                localStorage.setItem('theme', t);
                setTheme(t);
              }}
            />
          </label>
        );
      })}
    </div>
  );
};

export default ThemeToggle;
/></form></td></tr>
<tr><td class='sub'>Unnamed repository; edit this file 'description' to name the repository.</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/ansg191/coredns/about/'>about</a><a href='/ansg191/coredns/'>summary</a><a href='/ansg191/coredns/refs/?id=ad8021230c775f129556fe7f681f0b09a59d480e'>refs</a><a class='active' href='/ansg191/coredns/log/vendor/golang.org/x/net/CONTRIBUTING.md?follow=1'>log</a><a href='/ansg191/coredns/tree/vendor/golang.org/x/net/CONTRIBUTING.md?id=ad8021230c775f129556fe7f681f0b09a59d480e'>tree</a><a href='/ansg191/coredns/commit/vendor/golang.org/x/net/CONTRIBUTING.md?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;follow=1'>commit</a><a href='/ansg191/coredns/diff/vendor/golang.org/x/net/CONTRIBUTING.md?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;follow=1'>diff</a></td><td class='form'><form class='right' method='get' action='/ansg191/coredns/log/vendor/golang.org/x/net/CONTRIBUTING.md'>
<input type='hidden' name='id' value='ad8021230c775f129556fe7f681f0b09a59d480e'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/ansg191/coredns/log/?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;follow=1'>root</a>/<a href='/ansg191/coredns/log/vendor?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;follow=1'>vendor</a>/<a href='/ansg191/coredns/log/vendor/golang.org?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;follow=1'>golang.org</a>/<a href='/ansg191/coredns/log/vendor/golang.org/x?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;follow=1'>x</a>/<a href='/ansg191/coredns/log/vendor/golang.org/x/net?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;follow=1'>net</a>/<a href='/ansg191/coredns/log/vendor/golang.org/x/net/CONTRIBUTING.md?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;follow=1'>CONTRIBUTING.md</a> (<a href='/ansg191/coredns/log/vendor/golang.org/x/net/CONTRIBUTING.md?id=ad8021230c775f129556fe7f681f0b09a59d480e'>unfollow</a>)</div><div class='content'><table class='list nowrap'><tr class='nohover'><th class='left'>Age</th><th class='left'>Commit message (<a href='/ansg191/coredns/log/vendor/golang.org/x/net/CONTRIBUTING.md?id=ad8021230c775f129556fe7f681f0b09a59d480e&amp;showmsg=1&amp;follow=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Files</th><th class='left'>Lines</th></tr>
<tr><td><span title='2018-06-21 11:40:19 +0100'>2018-06-21</span></td><td><a href='/ansg191/coredns/commit/plugin/forward/proxy_test.go?id=422aec5f5fc9d1f4d4017357f86098ec34eb1676&amp;follow=1'>plugin/forward: Increase minimum read timeout to 200ms (#1889)</a></td><td><img src='//www.gravatar.com/avatar/90f0e512184d202445616446467a3da9?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Tobias Schmidt
</td><td>5</td><td><span class='deletions'>-10</span>/<span class='insertions'>+10</span></td></tr>
<tr><td><span title='2018-06-20 11:35:57 +0100'>2018-06-20</span></td><td><a href='/ansg191/coredns/commit/plugin/forward/metrics.go?id=e3534205c7c3a89b09c9a9e2aca1a2159eb9ecde&amp;follow=1'>Rename forward metrics socket_count_total to sockets_open (#1885)</a></td><td><img src='//www.gravatar.com/avatar/90f0e512184d202445616446467a3da9?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Tobias Schmidt
</td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td><span title='2018-06-20 11:35:07 +0100'>2018-06-20</span></td><td><a href='/ansg191/coredns/commit/plugin/proxy/upstream.go?id=41c724780f6a4e9e8461b56472660d9ad3e49540&amp;follow=1'>remove deprecated code (#1888)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>3</td><td><span class='deletions'>-4</span>/<span class='insertions'>+0</span></td></tr>
<tr><td><span title='2018-06-19 19:50:08 +0100'>2018-06-19</span></td><td><a href='/ansg191/coredns/commit/plugin/cache/handler.go?id=9c2dc7a1564a4cfc5c197d99c828364fc02bfbae&amp;follow=1'>Ensure cache.ResponseWriter can be used asynchronously during prefetch (#1884)</a></td><td><img src='//www.gravatar.com/avatar/90f0e512184d202445616446467a3da9?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Tobias Schmidt
</td><td>2</td><td><span class='deletions'>-8</span>/<span class='insertions'>+40</span></td></tr>
<tr><td><span title='2018-06-19 17:57:35 +0100'>2018-06-19</span></td><td><a href='/ansg191/coredns/commit/man/coredns-rewrite.7?id=f78f30231df90da6184d5f811ecf9c06b0160c2b&amp;follow=1'>Release 1.1.4</a><span class='decoration'><a class='tag-deco' href='/ansg191/coredns/tag/?h=v1.1.4'>v1.1.4</a></span></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>3</td><td><span class='deletions'>-4</span>/<span class='insertions'>+7</span></td></tr>
<tr><td><span title='2018-06-18 11:16:56 +0100'>2018-06-18</span></td><td><a href='/ansg191/coredns/commit/plugin/backend_lookup.go?id=ab9efdcac4914b8bed486d32128ae00adba0dc90&amp;follow=1'>Follow up on #1867 : use one map in backend_lookup (#1869)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>1</td><td><span class='deletions'>-32</span>/<span class='insertions'>+33</span></td></tr>
<tr><td><span title='2018-06-18 10:02:05 +0100'>2018-06-18</span></td><td><a href='/ansg191/coredns/commit/plugin/rewrite/README.md?id=6fbc1f8990601c322255f78476b281ba967dbd24&amp;follow=1'>Plugin/Rewrite - review documentation of "append" (#1877)</a></td><td><img src='//www.gravatar.com/avatar/85ef5ede2ac268872e757897d59a4021?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Francois Tur
</td><td>1</td><td><span class='deletions'>-2</span>/<span class='insertions'>+2</span></td></tr>
<tr><td><span title='2018-06-15 08:12:56 -0700'>2018-06-15</span></td><td><a href='/ansg191/coredns/commit/plugin/template/setup.go?id=177e32b62e1bd937317ec3fd7c7f3a8114c52d77&amp;follow=1'>plugin/forward: add REFUSED test (#1878)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>10</td><td><span class='deletions'>-10</span>/<span class='insertions'>+62</span></td></tr>
<tr><td><span title='2018-06-15 07:37:22 +0100'>2018-06-15</span></td><td><a href='/ansg191/coredns/commit/plugin/forward/proxy.go?id=70c957d885a6c6b33a0828c680e451a9e3a3994a&amp;follow=1'>Plugin/Forward - autotune the dialTimeout for connection (#1852)</a></td><td><img src='//www.gravatar.com/avatar/85ef5ede2ac268872e757897d59a4021?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Francois Tur
</td><td>5</td><td><span class='deletions'>-52</span>/<span class='insertions'>+84</span></td></tr>
<tr><td><span title='2018-06-13 11:02:14 +0100'>2018-06-13</span></td><td><a href='/ansg191/coredns/commit/man/coredns.1?id=6f865a9de7d5ddc3daf52512016a5bbf72e3fb9a&amp;follow=1'>Run make -f Makefile.doc (#1875)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>9</td><td><span class='deletions'>-71</span>/<span class='insertions'>+50</span></td></tr>
<tr><td><span title='2018-06-13 10:38:24 +0100'>2018-06-13</span></td><td><a href='/ansg191/coredns/commit/Makefile?id=d6288c25ba4d2ce06e9c7c2ac63fc6b01379a533&amp;follow=1'>Bump dns version to 1.0.8 (#1873)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td><span title='2018-06-12 14:54:37 +0100'>2018-06-12</span></td><td><a href='/ansg191/coredns/commit/test/file_upstream_test.go?id=26c41a0c177fbe89f1f81214634aed49790581f4&amp;follow=1'>plugin/file: fix local CNAME lookup (#1866)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>5</td><td><span class='deletions'>-28</span>/<span class='insertions'>+83</span></td></tr>
<tr><td><span title='2018-06-11 19:23:25 -0700'>2018-06-11</span></td><td><a href='/ansg191/coredns/commit/plugin/pkg/dnsutil/dedup.go?id=6e466d509281953fdf0209a5b50611e89b4689ae&amp;follow=1'>Remove dnsutil.Dedup (#1867)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>6</td><td><span class='deletions'>-61</span>/<span class='insertions'>+66</span></td></tr>
<tr><td><span title='2018-06-07 13:48:23 -0700'>2018-06-07</span></td><td><a href='/ansg191/coredns/commit/.github/PULL_REQUEST_TEMPLATE.md?id=58d69913639b7b210ed32afacb447ba53c7487a3&amp;follow=1'>Update the pr template (#1862)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>1</td><td><span class='deletions'>-5</span>/<span class='insertions'>+1</span></td></tr>
<tr><td><span title='2018-06-07 16:21:17 +0100'>2018-06-07</span></td><td><a href='/ansg191/coredns/commit/plugin/pkg/replacer/replacer_test.go?id=18b11fc85156be8b027c61665c80e239324e2ebf&amp;follow=1'>Normalizing Response Duration in Log Plugin (#1860)</a></td><td><img src='//www.gravatar.com/avatar/d88558178af5dfa7a346c9a0f737a4f6?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Malcolm Akinje
</td><td>2</td><td><span class='deletions'>-1</span>/<span class='insertions'>+5</span></td></tr>
<tr><td><span title='2018-06-05 17:21:09 +0100'>2018-06-05</span></td><td><a href='/ansg191/coredns/commit/plugin/forward/proxy.go?id=751a08d6a2e3ace62e40ed8170015e7051d0020c&amp;follow=1'>plugin/forward: fix alignment for sync.Atomic (#1855)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>1</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td><span title='2018-06-05 17:20:38 +0100'>2018-06-05</span></td><td><a href='/ansg191/coredns/commit/Makefile.release?id=ec334014d73222981ef84667f0e82d04d1033537&amp;follow=1'>Update Makefile.release for docker image build (#1856)</a></td><td><img src='//www.gravatar.com/avatar/10c4cad3ba13f0378dbd67185314d3d4?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Yong Tang
</td><td>1</td><td><span class='deletions'>-15</span>/<span class='insertions'>+0</span></td></tr>
<tr><td><span title='2018-06-04 19:20:21 +0100'>2018-06-04</span></td><td><a href='/ansg191/coredns/commit/Dockerfile?id=a77d9834d867b44a7f10d1481916498332c586ff&amp;follow=1'>Docker: drop alpine (#1843)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>1</td><td><span class='deletions'>-3</span>/<span class='insertions'>+6</span></td></tr>
<tr><td><span title='2018-06-02 20:28:56 +0100'>2018-06-02</span></td><td><a href='/ansg191/coredns/commit/test/plugin_dnssec_test.go?id=70a53e25c942d95de98e30d9f5f5636d0d315d25&amp;follow=1'>Fix presubmit (#1850)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>2</td><td><span class='deletions'>-15</span>/<span class='insertions'>+15</span></td></tr>
<tr><td><span title='2018-06-02 11:48:39 -0700'>2018-06-02</span></td><td><a href='/ansg191/coredns/commit/plugin/test/helpers.go?id=22c0b30d5f7468c2477291419bee3e88dfba490c&amp;follow=1'>presubmit: Check errorf as well (#1845)</a></td><td><img src='//www.gravatar.com/avatar/25ba01eee5a1c0e007e810afe805766c?s=13&amp;d=retro' width='13' height='13' alt='Gravatar' /> Miek Gieben
</td><td>19</td><td><span class='deletions'>-52</span>/<span class='insertions'>+57</span></td></tr>
