Skip to content

Commit

Permalink
fix(options): 🐛 fix System display option on safari (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed Aug 29, 2022
1 parent a4d0940 commit 2a7a2ea
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/elements/SegmentedControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export class SegmentedControl extends Element {
}

public setValue(value: SupportedTypes, dispatchEvent = true) {
const segmentToCheck = this.segments.find(segment => segment.value === value);
let segmentToCheck = this.segments.find(segment => segment.value === value);

// ! see #68
if (!segmentToCheck && this.id === 'display-theme' && value === null) {
segmentToCheck = this.segments.find(segment => segment.value === 'system');
}

if (!segmentToCheck) throw new Error(`Failed to set unexpected value ${value} of type ${typeof value} on segmented control ${this.id}`);

Expand Down
4 changes: 2 additions & 2 deletions src/options/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ export const CONFIGURATION: {
},
extension: {
displayTheme: {
defaultValue: null,
defaultValue: 'system',
get input() {
delete (<Partial<typeof this>>this).input;
return this.input = SegmentedControl.getInstance<InputElementId, typeof this.defaultValue>({
id: 'display-theme',
segments: [
{
id: 'display-system-mode',
value: null,
value: 'system',
default: true,
},
{
Expand Down
6 changes: 4 additions & 2 deletions src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ const buttons: {

Storage.getOptions().then(({ extension: { displayTheme }, options: { displayAdvanced } }) => {
// set display theme
if (displayTheme) document.documentElement.classList.add(`${displayTheme}-mode`);
if (displayTheme && displayTheme !== 'system') {
document.documentElement.classList.add(`${displayTheme}-mode`);
}

// show advanced options if appropriate
AdvancedOptions.toggle(displayAdvanced);
Expand Down Expand Up @@ -402,7 +404,7 @@ CONFIGURATION.FIELDS['extension.displayTheme'].input.addEventListener('input', (
document.documentElement.classList.remove(token);
});

if (!displayTheme) return;
if (!displayTheme || displayTheme === 'system') return;

document.documentElement.classList.add(`${displayTheme}-mode`);
});
Expand Down
5 changes: 4 additions & 1 deletion src/popup/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ buttons.listAssignments.addEventListener('click', SavedCoursesList.listAssignmen
buttons.listCourses.addEventListener('click', SavedCoursesList.listCourses.bind(SavedCoursesList, undefined));

Storage.getOptions().then(({ extension: { displayTheme }, popup: { displayJSONButton } }) => {
if (displayTheme) document.documentElement.classList.add(`${displayTheme}-mode`);
if (displayTheme && displayTheme !== 'system') {
document.documentElement.classList.add(`${displayTheme}-mode`);
}

if (displayJSONButton) buttons.copyJSON.show();
});

Expand Down
3 changes: 2 additions & 1 deletion src/types/storage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type NeverEmpty<T extends string> = T extends '' ? never : T;
type NullIfEmpty<T extends string | null> = (T extends '' ? null : T) | null;

interface RequiredFields {
'extension.displayTheme': null | 'light' | 'dark';
// ! null is retained to avoid breaking changes, see #68
'extension.displayTheme': null | 'system' | 'light' | 'dark';
'popup.displayJSONButton': boolean;
'options.displayAdvanced': boolean;
'canvas.importMissingDueDates': boolean;
Expand Down

0 comments on commit 2a7a2ea

Please sign in to comment.