Skip to content

Commit e2a0cc1

Browse files
authored
Merge pull request #56 from yuhaofe/fix-width
Fix horizontal scroll width and theme settings
2 parents 8fb013d + 2327bf6 commit e2a0cc1

File tree

6 files changed

+70
-69
lines changed

6 files changed

+70
-69
lines changed

src/Popup.scss

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ body {
7373
background-color: var(--bg-color);
7474
margin: 0px 0px;
7575
padding: 0px;
76-
display: flex;
77-
flex-direction: column;
7876
min-width: 300px;
7977
max-width: 800px;
8078
max-height: 600px;
@@ -86,16 +84,7 @@ body.startup {
8684
height: var(--startup-height)!important;
8785
}
8886

89-
body > :first-child {
90-
flex: 0 0 auto;
91-
}
92-
93-
body > :nth-child(2) {
94-
flex: 1 1 auto;
95-
}
96-
9787
body > :nth-child(3), body > :nth-child(3):hover, body > :nth-child(3):focus, body > :nth-child(3):active {
98-
flex: 0 0 auto;
9988
box-sizing: content-box;
10089
border-top-width: 1px!important;
10190
border-top-color: var(--line-color);

src/Popup.tsx

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import PopupHeader from './components/PopupHeader';
55
import PopupContainer from './components/PopupContainer';
66
import PopupFooter from './components/PopupFooter';
77
import ContextMenu from './components/ContextMenu';
8+
import { applyTheme } from './utils/theme';
89
import './Popup.scss';
910

11+
1012
interface PopupProps {
1113
config: Configuration
1214
}
@@ -62,52 +64,4 @@ function adjustHeight(length: number) {
6264
const rootStyle = document.documentElement.style;
6365
const height = (length + 2) * 30;
6466
rootStyle.setProperty('--startup-height', (height > 600) ? '600px' : height + 'px');
65-
}
66-
67-
function applyTheme(theme: 'auto' | 'light' | 'dark') {
68-
const rootElm = document.documentElement;
69-
70-
const applyDarkTheme = () => {
71-
rootElm.classList.add('theme-dark');
72-
rootElm.classList.remove('theme-light');
73-
chrome.action.setIcon({
74-
path: {
75-
"16": "/icons/qbm16-dark.png",
76-
"32": "/icons/qbm32-dark.png"
77-
}
78-
});
79-
}
80-
81-
const applyLightTheme = () => {
82-
rootElm.classList.add('theme-light');
83-
rootElm.classList.remove('theme-dark');
84-
chrome.action.setIcon({
85-
path: {
86-
"16": "/icons/qbm16.png",
87-
"32": "/icons/qbm32.png"
88-
}
89-
});
90-
}
91-
92-
switch (theme) {
93-
case 'light':
94-
applyLightTheme();
95-
break;
96-
case 'dark':
97-
applyDarkTheme();
98-
break;
99-
case 'auto':
100-
default:
101-
const mql = window.matchMedia('(prefers-color-scheme: dark)');
102-
const colorSchemeTest = (e: MediaQueryListEvent | MediaQueryList) => {
103-
if (e.matches) {
104-
applyDarkTheme();
105-
} else {
106-
applyLightTheme();
107-
}
108-
};
109-
mql.onchange = colorSchemeTest;
110-
colorSchemeTest(mql);
111-
break;
112-
}
11367
}

src/components/PopupContainer/OptionsItem.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,21 @@ export interface OptionsItem {
2020
type: 'radio' | 'checkbox';
2121
/** This item's options */
2222
options: Option[];
23+
/** callback after value change */
24+
callback?: (val: any) => void;
2325
}
2426

2527
interface OptionsItemProps extends OptionsItem { }
2628

27-
export default function OptionsItem({ name: itemName, storage, type, options }: OptionsItemProps) {
29+
export default function OptionsItem({ name: itemName, storage, type, options, callback }: OptionsItemProps) {
2830
const [value, setValue] = useState(options[0].value);
2931
const name = itemName.replace('_', '-');
3032

3133
const saveValue = async (val: any) => {
32-
chrome.storage.local.set({ [storage]: val })
33-
.then(() => {
34-
setValue(val);
35-
});
36-
37-
if (itemName === 'root_folder') {
38-
chrome.storage.local.set({ startup: [val, 18] });
34+
await chrome.storage.local.set({ [storage]: val });
35+
setValue(val);
36+
if (callback && typeof callback === 'function') {
37+
callback(val);
3938
}
4039
};
4140

src/components/PopupContainer/OptionsPage.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { h } from 'preact';
22
import { useEffect, useState } from 'preact/hooks';
33
import OptionsItemComponent, { OptionsItem } from './OptionsItem';
4+
import { applyTheme } from '../../utils/theme';
45
import "./OptionsPage.scss";
56

67
const optionsItems: OptionsItem[] = [
@@ -92,7 +93,10 @@ const optionsItems: OptionsItem[] = [
9293
name: 'root_folder',
9394
storage: 'root',
9495
type: 'radio',
95-
options: [{ name: '', value: '0' }]
96+
options: [{ name: '', value: '0' }],
97+
callback: (val) => {
98+
chrome.storage.local.set({ startup: [val, 18] });
99+
}
96100
},
97101
{
98102
name: 'color_theme',
@@ -111,7 +115,10 @@ const optionsItems: OptionsItem[] = [
111115
name: 'dark',
112116
value: 'dark',
113117
}
114-
]
118+
],
119+
callback: (val) => {
120+
applyTheme(val);
121+
}
115122
},
116123
{
117124
name: 'scroll_layout',

src/components/PopupContainer/PopupContainer.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
position: relative;
55
max-width: 800px;
66
min-width: 300px;
7+
max-height: 538px;
78

89
&-horiz {
910
overflow-x: auto;

src/utils/theme.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
let mql: MediaQueryList | null = null;
2+
3+
export function applyTheme(theme: 'auto' | 'light' | 'dark') {
4+
const rootElm = document.documentElement;
5+
6+
const applyDarkTheme = () => {
7+
rootElm.classList.add('theme-dark');
8+
rootElm.classList.remove('theme-light');
9+
chrome.action.setIcon({
10+
path: {
11+
"16": "/icons/qbm16-dark.png",
12+
"32": "/icons/qbm32-dark.png"
13+
}
14+
});
15+
}
16+
17+
const applyLightTheme = () => {
18+
rootElm.classList.add('theme-light');
19+
rootElm.classList.remove('theme-dark');
20+
chrome.action.setIcon({
21+
path: {
22+
"16": "/icons/qbm16.png",
23+
"32": "/icons/qbm32.png"
24+
}
25+
});
26+
}
27+
28+
switch (theme) {
29+
case 'light':
30+
applyLightTheme();
31+
break;
32+
case 'dark':
33+
applyDarkTheme();
34+
break;
35+
case 'auto':
36+
default:
37+
const colorSchemeTest = (e: MediaQueryListEvent | MediaQueryList) => {
38+
if (e.matches) {
39+
applyDarkTheme();
40+
} else {
41+
applyLightTheme();
42+
}
43+
};
44+
if(!mql) {
45+
mql = window.matchMedia('(prefers-color-scheme: dark)');
46+
mql.addEventListener("change", colorSchemeTest);
47+
}
48+
colorSchemeTest(mql);
49+
break;
50+
}
51+
}

0 commit comments

Comments
 (0)