@@ -73,13 +73,6 @@ export abstract class AbstractRegion<T> implements Region<T> {
7373 */
7474 protected static className : string ;
7575
76- /**
77- * True if the style has already been added to the document.
78- *
79- * @type {boolean }
80- */
81- protected static styleAdded : boolean = false ;
82-
8376 /**
8477 * The CSS style that needs to be added for this type of region.
8578 *
@@ -117,20 +110,24 @@ export abstract class AbstractRegion<T> implements Region<T> {
117110 this . AddStyles ( ) ;
118111 }
119112
113+ /**
114+ * @returns {string } The stylesheet ID
115+ */
116+ public static get sheetId ( ) : string {
117+ return 'MJX-' + this . name + '-styles' ;
118+ }
119+
120120 /**
121121 * @override
122122 */
123123 public AddStyles ( ) {
124- if ( this . CLASS . styleAdded ) {
124+ const id = this . CLASS . sheetId ;
125+ if ( ! this . CLASS . style || this . document . adaptor . head ( ) . querySelector ( '#' + id ) ) {
125126 return ;
126127 }
127- // TODO: should that be added to document.documentStyleSheet()?
128- const node = this . document . adaptor . node ( 'style' ) ;
128+ const node = this . document . adaptor . node ( 'style' , { id} ) ;
129129 node . innerHTML = this . CLASS . style . cssText ;
130- this . document . adaptor
131- . head ( this . document . adaptor . document )
132- . appendChild ( node ) ;
133- this . CLASS . styleAdded = true ;
130+ this . document . adaptor . head ( ) . appendChild ( node ) ;
134131 }
135132
136133 /**
@@ -177,7 +174,7 @@ export abstract class AbstractRegion<T> implements Region<T> {
177174 */
178175 public Hide ( ) {
179176 if ( ! this . div ) return ;
180- this . div . parentNode . removeChild ( this . div ) ;
177+ this . div . remove ( ) ;
181178 this . div = null ;
182179 this . inner = null ;
183180 }
@@ -335,6 +332,13 @@ export class ToolTip extends StringRegion {
335332 'border-radius' : 'inherit' ,
336333 padding : '0 2px' ,
337334 } ,
335+ '@media (prefers-color-scheme: dark)' : {
336+ [ '.' + ToolTip . className ] : {
337+ 'background-color' : '#222025' ,
338+ 'box-shadow' : '0px 5px 20px #000' ,
339+ border : '1px solid #7C7C7C' ,
340+ } ,
341+ } ,
338342 } ) ;
339343}
340344
@@ -348,6 +352,43 @@ export class LiveRegion extends StringRegion {
348352 * @override
349353 */
350354 protected static style : StyleJsonSheet = new StyleJsonSheet ( {
355+ ':root' : {
356+ '--mjx-fg-red' : '255, 0, 0' ,
357+ '--mjx-fg-green' : '0, 255, 0' ,
358+ '--mjx-fg-blue' : '0, 0, 255' ,
359+ '--mjx-fg-yellow' : '255, 255, 0' ,
360+ '--mjx-fg-cyan' : '0, 255, 255' ,
361+ '--mjx-fg-magenta' : '255, 0, 255' ,
362+ '--mjx-fg-white' : '255, 255, 255' ,
363+ '--mjx-fg-black' : '0, 0, 0' ,
364+ '--mjx-bg-red' : '255, 0, 0' ,
365+ '--mjx-bg-green' : '0, 255, 0' ,
366+ '--mjx-bg-blue' : '0, 0, 255' ,
367+ '--mjx-bg-yellow' : '255, 255, 0' ,
368+ '--mjx-bg-cyan' : '0, 255, 255' ,
369+ '--mjx-bg-magenta' : '255, 0, 255' ,
370+ '--mjx-bg-white' : '255, 255, 255' ,
371+ '--mjx-bg-black' : '0, 0, 0' ,
372+ '--mjx-live-bg-color' : 'white' ,
373+ '--mjx-live-shadow-color' : '#888' ,
374+ '--mjx-live-border-color' : '#CCCCCC' ,
375+ '--mjx-bg-alpha' : 0.2 ,
376+ '--mjx-fg-alpha' : 1 ,
377+ } ,
378+ '@media (prefers-color-scheme: dark)' : {
379+ ':root' : {
380+ '--mjx-bg-blue' : '132, 132, 255' ,
381+ '--mjx-bg-white' : '0, 0, 0' ,
382+ '--mjx-bg-black' : '255, 255, 255' ,
383+ '--mjx-fg-white' : '0, 0, 0' ,
384+ '--mjx-fg-black' : '255, 255, 255' ,
385+ '--mjx-live-bg-color' : '#222025' ,
386+ '--mjx-live-shadow-color' : 'black' ,
387+ '--mjx-live-border-color' : '#7C7C7C' ,
388+ '--mjx-bg-alpha' : 0.3 ,
389+ '--mjx-fg-alpha' : 1 ,
390+ } ,
391+ } ,
351392 [ '.' + LiveRegion . className ] : {
352393 position : 'absolute' ,
353394 top : 0 ,
@@ -360,20 +401,36 @@ export class LiveRegion extends StringRegion {
360401 left : 0 ,
361402 right : 0 ,
362403 margin : '0 auto' ,
363- 'background-color' : 'white ' ,
364- 'box-shadow' : '0px 5px 20px #888 ' ,
365- border : '2px solid #CCCCCC ' ,
404+ 'background-color' : 'var(--mjx-live-bg-color) ' ,
405+ 'box-shadow' : '0px 5px 20px var(--mjx-live-shadow-color) ' ,
406+ border : '2px solid var(--mjx-live-border-color) ' ,
366407 } ,
367408 [ '.' + LiveRegion . className + '_Show' ] : {
368409 display : 'block' ,
369410 } ,
370411 } ) ;
412+
413+ /**
414+ * @param {string } type The type of alpha to set (fg or bg)
415+ * @param {number } alpha The alpha value to use for the background
416+ * @param {Document } document The document whose CSS styles are to be adjusted
417+ */
418+ public static setAlpha ( type : string , alpha : number , document : Document ) {
419+ const style = document . head . querySelector ( '#' + this . sheetId ) as HTMLStyleElement ;
420+ if ( style ) {
421+ const name = `--mjx-${ type } -alpha` ;
422+ ( style . sheet . cssRules [ 0 ] as any ) . style . setProperty ( name , alpha ) ;
423+ ( style . sheet . cssRules [ 1 ] as any ) . cssRules [ 0 ] . style . setProperty ( name , alpha ** 0.7071 ) ;
424+ }
425+ }
371426}
372427
373428/**
374429 * Region class that enables auto voicing of content via SSML markup.
375430 */
376431export class SpeechRegion extends LiveRegion {
432+ protected static style : StyleJsonSheet = null ;
433+
377434 /**
378435 * Flag to activate auto voicing.
379436 */
@@ -583,6 +640,13 @@ export class HoverRegion extends AbstractRegion<HTMLElement> {
583640 [ '.' + HoverRegion . className + ' > div' ] : {
584641 overflow : 'hidden' ,
585642 } ,
643+ '@media (prefers-color-scheme: dark)' : {
644+ [ '.' + HoverRegion . className ] : {
645+ 'background-color' : '#222025' ,
646+ 'box-shadow' : '0px 5px 20px #000' ,
647+ border : '1px solid #7C7C7C' ,
648+ } ,
649+ } ,
586650 } ) ;
587651
588652 /**
0 commit comments