Skip to content

Commit b189f7e

Browse files
authored
Merge pull request #1422 from mathjax/feature/no-dark-mode
Implement a no-dark-mode component. (mathjax/MathJax#3502)
2 parents 8a35cea + 60d2484 commit b189f7e

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

components/mjs/source.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const source = {
8282
'[mathmaps]': `${src}/../../bundle/sre/mathmaps`,
8383
'ui/lazy': `${src}/ui/lazy/lazy.js`,
8484
'ui/menu': `${src}/ui/menu/menu.js`,
85+
'ui/no-dark-mode': `${src}/ui/nodarkmode/nodarkmode.js`,
8586
'ui/safe': `${src}/ui/safe/safe.js`,
8687
'mml-chtml': `${src}/mml-chtml/mml-chtml.js`,
8788
'mml-chtml-nofont': `${src}/mml-chtml-nofont/mml-chtml-nofont.js`,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"webpack": {
3+
"name": "ui/no-dark-mode"
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {VERSION} from '#js/components/version.js';
2+
import '#js/ui/no-dark-mode/no-dark-mode.js';
3+
4+
if (MathJax.loader) {
5+
MathJax.loader.checkVersion('ui/no-dark-mode', VERSION, 'extension');
6+
}
7+

ts/ui/no-dark-mode/no-dark-mode.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*************************************************************
2+
*
3+
* Copyright (c) 2026 The MathJax Consortium
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @file An extension to disable dark mode for sites that don't support that
20+
*
21+
* @author dpvc@mathjax.org (Davide Cervone)
22+
*/
23+
24+
import { MathJax } from '../../components/global.js';
25+
import { MathJaxObject } from '../../components/startup.js';
26+
import { AbstractHandler } from '../../core/Handler.js';
27+
28+
[
29+
[
30+
MathJax._.ui?.dialog,
31+
'core',
32+
() => {
33+
const { DraggableDialog } = MathJax._.ui.dialog.DraggableDialog;
34+
delete DraggableDialog.styles['@media (prefers-color-scheme: dark)'];
35+
},
36+
],
37+
38+
[
39+
MathJax._.a11y?.explorer,
40+
'a11y/explorer',
41+
() => {
42+
const Region = MathJax._.a11y.explorer.Region;
43+
for (const region of ['LiveRegion', 'HoverRegion', 'ToolTip']) {
44+
Region[region].style.styles['@media (prefers-color-scheme: dark)'] = {};
45+
}
46+
Region.LiveRegion.style.styles['@media (prefers-color-scheme: dark)'][
47+
'mjx-ignore'
48+
] = { ignore: 1 };
49+
(MathJax as MathJaxObject).startup.extendHandler(
50+
(handler: AbstractHandler<any, any, any>) => {
51+
delete (handler.documentClass as any).speechStyles[
52+
'@media (prefers-color-scheme: dark) /* explorer */'
53+
];
54+
return handler;
55+
}
56+
);
57+
},
58+
],
59+
60+
[
61+
MathJax._.output?.chtml,
62+
'output/chtml',
63+
() => {
64+
const { CHTML } = MathJax._.output.chtml_ts;
65+
delete CHTML.commonStyles['@media (prefers-color-scheme: dark)'];
66+
const { ChtmlMaction } = MathJax._.output.chtml.Wrappers.maction;
67+
delete ChtmlMaction.styles[
68+
'@media (prefers-color-scheme: dark) /* chtml maction */'
69+
];
70+
},
71+
],
72+
73+
[
74+
MathJax._.output?.svg,
75+
'output/svg',
76+
() => {
77+
const { SVG } = MathJax._.output.svg_ts;
78+
delete SVG.commonStyles['@media (prefers-color-scheme: dark)'];
79+
const { SvgMaction } = MathJax._.output.svg.Wrappers.maction;
80+
delete SvgMaction.styles[
81+
'@media (prefers-color-scheme: dark) /* svg maction */'
82+
];
83+
},
84+
],
85+
].forEach(([immediate, extension, ready]) => {
86+
if (immediate) {
87+
ready();
88+
} else {
89+
const config = MathJax.config.loader;
90+
config[extension] ??= {};
91+
const check = config[extension].checkReady;
92+
config[extension].checkReady = async () => {
93+
if (check) await check();
94+
return ready();
95+
};
96+
}
97+
});

0 commit comments

Comments
 (0)