Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darkmode for single file #90

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions index-dark.css

This file was deleted.

62 changes: 52 additions & 10 deletions index.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:root {
[data-theme="light"] {
--main-bg-color: #C0C0C0;
--main-text-color: #000000;
--headline-text-color: #8be9fd;
Expand All @@ -18,8 +18,50 @@
--preview-border-color: #505050;
--dump-bg-color: #C0C0C0;
--dump-border-color: #E0E0E0;
--dump-tag: blue;
--dump-dlen: darkcyan;
--dump-ulen: darkgreen;
--dump-intro: blue;
--dump-outro: darkgreen;
--dump-skip: #666666;
--dump-skip-bg: #C0C0C0;
--dump-hex-current: #808080;
--dump-hex-current-hex: #A0A0A0;
--dump-hex-current-dlen: #004040;
--hover-bg-color: #E0E0E0;
}
[data-theme="dark"] {
--main-bg-color: #0d1116;
--main-text-color: #f8f8f2;
--headline-text-color: #8be9fd;
--button-border-color: #505050;
--button-bg-color: #303030;
--button-bghover-color: #404040;
--input-border-color: #505050;
--input-bg-color: #0c0e11;
--link-color: #58a6ff;
--link-hover-color: #9b9bea;
--header-bg-color: #161b22;
--page-bg-color: #000000;
--license-bg-color: #4b4a4a;
--license-border-color: black;
--sub-border-color: #383838;
--preview-bg-color: #989797;
--preview-border-color: #b5b3b3;
--dump-bg-color: #0c0e11;
--dump-border-color: #505050;
--dump-tag: #58a6ff;
--dump-dlen: darkcyan;
--dump-ulen: #00b6b6;
--dump-intro: #58a6ff;
--dump-outro: #00b6b6;
--dump-skip: #707070;
--dump-skip-bg: #222222;
--dump-hex-current: #727272;
--dump-hex-current-hex: #474747;
--dump-hex-current-dlen: #00b6b6;
--hover-bg-color: #505050;
}
html, body {
background-color: var(--page-bg-color);
color: var(--main-text-color);
Expand Down Expand Up @@ -193,15 +235,15 @@ header {
white-space: pre;
padding: 5px;
}
#dump .tag { color: blue; }
#dump .dlen { color: darkcyan; }
#dump .ulen { color: darkgreen; }
#dump .intro { color: blue; }
#dump .outro { color: darkgreen; }
#dump .skip { color: #666666; background-color: #C0C0C0; }
#dump .hexCurrent { background-color: #808080; }
#dump .hexCurrent .hex { background-color: #A0A0A0; }
#dump .hexCurrent .dlen { color: #004040; }
#dump .tag { color: var(--dump-tag); }
#dump .dlen { color: var(--dump-dlen); }
#dump .ulen { color: var(--dump-ulen); }
#dump .intro { color: var(--dump-intro); }
#dump .outro { color: var(--dump-outro); }
#dump .skip { color: var(--dump-skip); background-color: var(--dump-skip-bg); }
#dump .hexCurrent { background-color: var(--dump-hex-current); }
#dump .hexCurrent .hex { background-color: var(--dump-hex-current-hex); }
#dump .hexCurrent .dlen { color: var(--dump-hex-current-dlen); }
#file { display: none; }
#area { width: 100%; }
#contextmenu {
Expand Down
7 changes: 5 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<!DOCTYPE html>
<html>
<html data-theme="dark">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olibu is this necessary?
(and it is correct when OS theme is light? or would cause flickering on the other way?)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, I'd say it's fine as it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified it in dark and light mode to work without flickering. However, I set dark as the default. When testing I recognized that the dark flash is not as disturbing as the light flash.
Flickering stopped by separating the function into the theme.js

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="theme-color" content="#0d1116" media="(prefers-color-scheme: dark)">
<meta name="theme-color" content="#C0C0C0" media="(prefers-color-scheme: light)">
<title>ASN.1 JavaScript decoder</title>
<link rel="stylesheet" href="index.css" type="text/css" id="theme-base">
<link rel="stylesheet" href="index.css" type="text/css">
<link rel="icon" type="image/svg+xml" sizes="192x192" href="favicon.svg">
</head>
<body>
Expand Down Expand Up @@ -108,6 +110,7 @@ <h3>Links</h3>
</ul>
</div>

<script type="module" src="theme.js"></script>
<script type="module" src="index.js"></script>
</body>
</html>
34 changes: 0 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const
area = id('area'),
file = id('file'),
examples = id('examples'),
selectTheme = id('theme-select'),
selectDefs = id('definitions'),
selectTag = id('tags');

Expand Down Expand Up @@ -158,39 +157,6 @@ for (const [name, onClick] of Object.entries(butClickHandlers)) {
if (elem)
elem.onclick = onClick;
}
// set dark theme depending on OS settings
function setTheme() {
if (!selectTheme) {
console.log('Themes are currently not working with single file version.');
return;
}
let storedTheme = localStorage.getItem('theme');
let theme = 'os';
if (storedTheme)
theme = storedTheme;
selectTheme.value = theme;
if (theme == 'os') {
let prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
theme = prefersDarkScheme.matches ? 'dark': 'light';
}
if (theme == 'dark') {
const css1 = id('theme-base');
const css2 = css1.cloneNode();
css2.id = 'theme-override';
css2.href = 'index-' + theme + '.css';
css1.parentElement.appendChild(css2);
} else {
const css2 = id('theme-override');
if (css2) css2.remove();
}
}
setTheme();
if (selectTheme) {
selectTheme.addEventListener('change', function () {
localStorage.setItem('theme', selectTheme.value);
setTheme();
});
}
// this is only used if window.FileReader
function read(f) {
area.value = ''; // clear text area, will get b64 content
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"homepage": "https://lapo.it/asn1js/",
"files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "defs.js", "oids.js", "rfcdef.js" ],
"scripts": {
"lint": "npx eslint asn1.js base64.js hex.js int10.js defs.js oids.js rfcdef.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js vite.config.js",
"lint": "npx eslint asn1.js base64.js hex.js int10.js defs.js oids.js rfcdef.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js vite.config.js theme.js",
"lint-action": "npx @action-validator/cli .github/workflows/node.js.yml",
"build": "vite build",
"serve": "echo 'Connect to http://localhost:3000/' ; npx statik --port 3000 .",
Expand Down
41 changes: 41 additions & 0 deletions theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const selectTheme = document.getElementById('theme-select');
// set dark theme depending on OS settings
function setTheme(theme) {
if (!selectTheme) {
console.log('Themes are currently not working with single file version.');
return;
}
if (theme == 'os') {
let prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
if (prefersDarkScheme.matches) {
theme = 'dark';
} else {
theme = 'light';
}
}
document.documentElement.style['color-scheme'] = theme;
document.querySelector('html').setAttribute('data-theme', theme);
// set tthe theme-color for iOS devices
let bgColor = getComputedStyle(document.documentElement).getPropertyValue('--main-bg-color');
let metaThemeColor = document.querySelector('meta[name=theme-color]');
metaThemeColor.setAttribute('content', bgColor);
}
// activate selected theme
let theme = 'os';
const localStorageTheme = localStorage.getItem('theme');
if (localStorageTheme) {
theme = localStorageTheme;
}
if (theme == 'light') {
selectTheme.selectedIndex = 2;
} else if (theme == 'dark') {
selectTheme.selectedIndex = 1;
}
setTheme(theme);
// add handler to theme selction element
if (selectTheme) {
selectTheme.addEventListener ('change', function () {
localStorage.setItem('theme', selectTheme.value);
setTheme(selectTheme.value);
});
}
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { viteSingleFile } from 'vite-plugin-singlefile';
import pluginDom from 'vite-plugin-dom';
import { DomUtils } from 'htmlparser2';

const removeNodes = [ 'theme-select', 'rowExamples' ];
const removeNodes = [ 'rowExamples' ];

export default defineConfig({
plugins: [
Expand Down