@aiursoft/autodark.js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+ const initTheme = () => {
3
+ // Replace dark theme class
4
+ if (window.matchMedia &&
5
+ window.matchMedia('(prefers-color-scheme: dark)').matches) {
6
+ switchTheme('dark');
7
+ const bodyElement = document.querySelector('body');
8
+ if (bodyElement) {
9
+ bodyElement.style.color = 'white';
10
+ }
11
+ }
12
+ else {
13
+ switchTheme('light');
14
+ const bodyElement = document.querySelector('body');
15
+ if (bodyElement) {
16
+ bodyElement.style.color = 'black';
17
+ }
18
+ }
19
+ };
20
+ const switchTheme = (theme) => {
21
+ const isDark = theme === 'dark';
22
+ const themeSettings = {
23
+ body: isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
24
+ table: isDark
25
+ ? ['table-dark', 'table-light']
26
+ : ['table-light', 'table-dark'],
27
+ '.navbar': isDark
28
+ ? ['navbar-dark', 'navbar-light']
29
+ : ['navbar-light', 'navbar-dark'],
30
+ '.fixed-top': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
31
+ '.modal-content': isDark
32
+ ? ['bg-dark', 'bg-light']
33
+ : ['bg-light', 'bg-dark'],
34
+ '.container-fluid': isDark
35
+ ? ['bg-dark', 'bg-light']
36
+ : ['bg-light', 'bg-dark'],
37
+ '.list-group-item': isDark
38
+ ? ['bg-dark', 'bg-light']
39
+ : ['bg-light', 'bg-dark'],
40
+ '.content-wrapper': isDark
41
+ ? ['bg-dark', 'bg-light']
42
+ : ['bg-light', 'bg-dark'],
43
+ '.card': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
44
+ '.bg-light': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
45
+ '.bg-white': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
46
+ '.bd-footer': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
47
+ '.bg': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
48
+ };
49
+ Object.keys(themeSettings).forEach((key) => {
50
+ const settings = themeSettings[key];
51
+ const elements = key.startsWith('.')
52
+ ? document.querySelectorAll(key)
53
+ : document.getElementsByTagName(key);
54
+ Array.from(elements).forEach((el) => {
55
+ if (Array.isArray(settings)) {
56
+ el.classList.add(settings[0]);
57
+ el.classList.remove(settings[1]);
58
+ }
59
+ else {
60
+ throw new Error('Invalid settings');
61
+ }
62
+ });
63
+ });
64
+ };
65
+ const autoTheme = () => {
66
+ initTheme();
67
+ window
68
+ .matchMedia('(prefers-color-scheme: light)')
69
+ .addEventListener('change', initTheme);
70
+ window.addEventListener('load', initTheme);
71
+ };
72
+ export { autoTheme, switchTheme };
73
+ //# sourceMappingURL=autodark.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"autodark.js","sourceRoot":"","sources":["../../src/autodark.ts"],"names":[],"mappings":"AAAA,yDAAyD;AAEzD,MAAM,SAAS,GAAG,GAAS,EAAE;IAC3B,2BAA2B;IAC3B,IACE,MAAM,CAAC,UAAU;QACjB,MAAM,CAAC,UAAU,CAAC,8BAA8B,CAAC,CAAC,OAAO,EACzD,CAAC;QACD,WAAW,CAAC,MAAM,CAAC,CAAA;QACnB,MAAM,WAAW,GAAG,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAClD,IAAI,WAAW,EAAE,CAAC;YAChB,WAAW,CAAC,KAAK,CAAC,KAAK,GAAG,OAAO,CAAA;QACnC,CAAC;IACH,CAAC;SAAM,CAAC;QACN,WAAW,CAAC,OAAO,CAAC,CAAA;QACpB,MAAM,WAAW,GAAG,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAClD,IAAI,WAAW,EAAE,CAAC;YAChB,WAAW,CAAC,KAAK,CAAC,KAAK,GAAG,OAAO,CAA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YACrC,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,MAAM,SAAS,GAAG,GAAS,EAAE;IAC3B,SAAS,EAAE,CAAA;IACX,MAAM;SACH,UAAU,CAAC,+BAA+B,CAAC;SAC3C,gBAAgB,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAA;IACxC,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAC5C,CAAC,CAAA;AAED,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,CAAA"}
@@ -0,0 +1 @@
1
+ "use strict";(()=>{var r=()=>{if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){d("dark");let g=document.querySelector("body");g&&(g.style.color="white")}else{d("light");let g=document.querySelector("body");g&&(g.style.color="black")}},d=g=>{let t=g==="dark",b={body:t?["bg-dark","bg-light"]:["bg-light","bg-dark"],table:t?["table-dark","table-light"]:["table-light","table-dark"],".navbar":t?["navbar-dark","navbar-light"]:["navbar-light","navbar-dark"],".fixed-top":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".modal-content":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".container-fluid":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".list-group-item":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".content-wrapper":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".card":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".bg-light":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".bg-white":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".bd-footer":t?["bg-dark","bg-light"]:["bg-light","bg-dark"],".bg":t?["bg-dark","bg-light"]:["bg-light","bg-dark"]};Object.keys(b).forEach(e=>{let a=b[e],l=e.startsWith(".")?document.querySelectorAll(e):document.getElementsByTagName(e);Array.from(l).forEach(i=>{if(Array.isArray(a))i.classList.add(a[0]),i.classList.remove(a[1]);else throw new Error("Invalid settings")})})},o=()=>{r(),window.matchMedia("(prefers-color-scheme: light)").addEventListener("change",r),window.addEventListener("load",r)};})();
@@ -0,0 +1,3 @@
1
+ declare const switchTheme: (theme: string) => void;
2
+ declare const autoTheme: () => void;
3
+ export { autoTheme, switchTheme };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@aiursoft/autodark.js",
3
+ "version": "1.0.0",
4
+ "description": "Auto dark theme",
5
+ "main": "dist/cjs/autodark.js",
6
+ "module": "dist/esm/autodark.js",
7
+ "types": "dist/types/autodark.d.ts",
8
+ "scripts": {
9
+ "clean": "rm -rf ./dist",
10
+ "build-css": "node-sass src/autodark.scss dist/css/autodark.min.css --output-style compressed --source-map true",
11
+ "build-js": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
12
+ "build": "npm run build-css && npm run build-js && npm run minify-all",
13
+ "minify-cjs": "esbuild dist/cjs/autodark.js --bundle --minify --outfile=dist/cjs/autodark.min.js",
14
+ "minify-esm": "esbuild dist/esm/autodark.js --bundle --minify --outfile=dist/esm/autodark.min.js",
15
+ "minify-all": "npm run minify-cjs && npm run minify-esm",
16
+ "format": "prettier --write .",
17
+ "lint": "eslint src/ --ext .ts && prettier --check ."
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/jhlywa/chess.js"
22
+ },
23
+ "dependencies": {
24
+ "bootstrap": "^4.6.2"
25
+ },
26
+ "devDependencies": {
27
+ "esbuild": "^0.19.11",
28
+ "@typescript-eslint/eslint-plugin": "^6.16.0",
29
+ "@typescript-eslint/parser": "^6.16.0",
30
+ "eslint": "^8.56.0",
31
+ "node-sass": "^9.0.0",
32
+ "prettier": "^3.1.1",
33
+ "typescript": "^5.3.3"
34
+ },
35
+ "author": "Anduin Xue <anduin@aiursoft.com>",
36
+ "license": "MIT"
37
+ }
@@ -0,0 +1,59 @@
1
+ @import '../node_modules/bootstrap/scss/bootstrap';
2
+
3
+ .bg-dark {
4
+ background-color: #24292e !important;
5
+ }
6
+
7
+ @media (prefers-color-scheme: dark) {
8
+ .dropify-preview {
9
+ background-color: rgb(42, 43, 45) !important;
10
+ }
11
+ .dropify-wrapper {
12
+ background-color: rgb(32, 33, 35) !important;
13
+ border: 2px solid #666 !important;
14
+ &:hover {
15
+ background-image: linear-gradient(
16
+ -45deg,
17
+ #161616 25%,
18
+ transparent 25%,
19
+ transparent 50%,
20
+ #161616 50%,
21
+ #161616 75%,
22
+ transparent 75%,
23
+ transparent
24
+ ) !important;
25
+ }
26
+ }
27
+ .text-muted {
28
+ color: #8c959d !important;
29
+ }
30
+ .content-wrapper {
31
+ color: white !important;
32
+ }
33
+ .jumbotron {
34
+ background-color: rgb(30, 32, 36) !important;
35
+ }
36
+ .breadcrumb {
37
+ background-color: rgb(42, 43, 45) !important;
38
+ }
39
+ pre {
40
+ background-color: #444 !important;
41
+ color: #fff !important;
42
+ }
43
+ code {
44
+ background-color: #444 !important;
45
+ color: #fff !important;
46
+ }
47
+ .form-control {
48
+ background-color: rgb(35, 36, 37) !important;
49
+ color: rgb(240, 234, 224) !important;
50
+ border-color: rgb(71, 79, 87) !important;
51
+ }
52
+ .navbar-dark {
53
+ box-shadow: 0 10px 39px -6px rgba(0, 0, 0, 0.3);
54
+ }
55
+ .custom-select {
56
+ color: white !important;
57
+ background-color: #495057 !important;
58
+ }
59
+ }
@@ -0,0 +1,77 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+
3
+ const initTheme = (): void => {
4
+ // Replace dark theme class
5
+ if (
6
+ window.matchMedia &&
7
+ window.matchMedia('(prefers-color-scheme: dark)').matches
8
+ ) {
9
+ switchTheme('dark')
10
+ const bodyElement = document.querySelector('body')
11
+ if (bodyElement) {
12
+ bodyElement.style.color = 'white'
13
+ }
14
+ } else {
15
+ switchTheme('light')
16
+ const bodyElement = document.querySelector('body')
17
+ if (bodyElement) {
18
+ bodyElement.style.color = 'black'
19
+ }
20
+ }
21
+ }
22
+
23
+ const switchTheme = (theme: string): void => {
24
+ const isDark = theme === 'dark'
25
+ const themeSettings: { [key: string]: string[] } = {
26
+ body: isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
27
+ table: isDark
28
+ ? ['table-dark', 'table-light']
29
+ : ['table-light', 'table-dark'],
30
+ '.navbar': isDark
31
+ ? ['navbar-dark', 'navbar-light']
32
+ : ['navbar-light', 'navbar-dark'],
33
+ '.fixed-top': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
34
+ '.modal-content': isDark
35
+ ? ['bg-dark', 'bg-light']
36
+ : ['bg-light', 'bg-dark'],
37
+ '.container-fluid': isDark
38
+ ? ['bg-dark', 'bg-light']
39
+ : ['bg-light', 'bg-dark'],
40
+ '.list-group-item': isDark
41
+ ? ['bg-dark', 'bg-light']
42
+ : ['bg-light', 'bg-dark'],
43
+ '.content-wrapper': isDark
44
+ ? ['bg-dark', 'bg-light']
45
+ : ['bg-light', 'bg-dark'],
46
+ '.card': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
47
+ '.bg-light': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
48
+ '.bg-white': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
49
+ '.bd-footer': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
50
+ '.bg': isDark ? ['bg-dark', 'bg-light'] : ['bg-light', 'bg-dark'],
51
+ }
52
+
53
+ Object.keys(themeSettings).forEach((key) => {
54
+ const settings = themeSettings[key]
55
+ const elements = key.startsWith('.')
56
+ ? document.querySelectorAll(key)
57
+ : document.getElementsByTagName(key)
58
+ Array.from(elements).forEach((el: Element) => {
59
+ if (Array.isArray(settings)) {
60
+ el.classList.add(settings[0])
61
+ el.classList.remove(settings[1])
62
+ } else {
63
+ throw new Error('Invalid settings')
64
+ }
65
+ })
66
+ })
67
+ }
68
+
69
+ const autoTheme = (): void => {
70
+ initTheme()
71
+ window
72
+ .matchMedia('(prefers-color-scheme: light)')
73
+ .addEventListener('change', initTheme)
74
+ window.addEventListener('load', initTheme)
75
+ }
76
+
77
+ export { autoTheme, switchTheme }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist/cjs",
5
+ "module": "commonjs"
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist/esm",
5
+ "module": "esnext"
6
+ }
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "esModuleInterop": true,
4
+ "moduleResolution": "node",
5
+ "removeComments": false,
6
+ "sourceMap": true,
7
+ "strict": true,
8
+ "target": "ESNext"
9
+ },
10
+ "include": ["src/**/*", "build.js"]
11
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist/types",
5
+ "declaration": true,
6
+ "emitDeclarationOnly": true
7
+ }
8
+ }