Skip to content

Commit b590349

Browse files
authored
Merge pull request #60 from missing-elements/fix/theme-followups
fix: select forced theme palettes by a data attribute
2 parents f86b525 + a85dd4d commit b590349

4 files changed

Lines changed: 56 additions & 29 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Example:
189189

190190
The component includes and applies a default Paper & Ink theme from `src/themes/paper-and-ink.css`.
191191

192-
Use `viewer-css-theme` attribute to set light or dark theme manually:
192+
Use `viewer-css-theme` attribute to set light or dark theme manually. `LIGHT` and `DARK` override the system color scheme, `AUTOMATIC` (the default) follows it:
193193

194194
```html
195195
<pdfjs-viewer-element

‎src/pdfjs-viewer-element.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,17 @@ export class PdfjsViewerElement extends HTMLElement {
209209
const viewerOptions = this.iframe.contentWindow?.PDFViewerApplicationOptions
210210
viewerOptions?.set('viewerCssTheme', theme)
211211

212+
// The theme stylesheet selects its forced palettes by `data-color-scheme`; the inline
213+
// `color-scheme` is what PDF.js itself sets for the browser UI.
212214
const doc = this.iframe.contentDocument
213215
if (!doc?.documentElement) return
214216
const mode = theme === ViewerCssTheme.LIGHT ? 'light' : theme === ViewerCssTheme.DARK ? 'dark' : ''
215217
if (mode) {
216218
doc.documentElement.style.setProperty('color-scheme', mode)
219+
doc.documentElement.setAttribute('data-color-scheme', mode)
217220
} else {
218221
doc.documentElement.style.removeProperty('color-scheme')
222+
doc.documentElement.removeAttribute('data-color-scheme')
219223
}
220224
}
221225

@@ -487,6 +491,8 @@ export class PdfjsViewerElement extends HTMLElement {
487491
private setupViewerApp = async () => {
488492
const viewerApp = await this.onViewerAppCreated()
489493
this.applyViewerOptions()
494+
// Applied before initialization so a forced palette is in place when the viewer first paints.
495+
this.applyViewerTheme()
490496
await viewerApp?.initializedPromise
491497

492498
this.applyViewerTheme()

‎src/themes/paper-and-ink.css‎

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ input[type="number"] {
364364
--editor-toolbar-highlight-image: var(--pi-icon-highlight);
365365
}
366366
@media (prefers-color-scheme: dark) {
367-
:where(:root:not([style*="color-scheme: light"])) {
367+
/* The component sets html[data-color-scheme] for viewer-css-theme="LIGHT" and "DARK"; the system
368+
dark rules are skipped for LIGHT. :where() adds no specificity, so the nested rules keep the
369+
weight of the light rules above and consumer overrides at :root level still win. */
370+
:where(:root:not([data-color-scheme="light"])) {
368371
&:root {
369372
--main-color: #f2eee8;
370373
--toolbar-icon-bg-color: #f2eee8;
@@ -528,8 +531,9 @@ input[type="number"] {
528531
}
529532

530533
/* Support forced dark mode from viewer-css-theme="DARK".
531-
The component sets html color-scheme inline, which does not trigger prefers-color-scheme media queries. */
532-
:root[style*="color-scheme"][style*="dark"] {
534+
The inline color-scheme does not trigger prefers-color-scheme media queries, so the component
535+
also sets html[data-color-scheme]. */
536+
:root[data-color-scheme="dark"] {
533537
--main-color: #f2eee8;
534538
--toolbar-icon-bg-color: #f2eee8;
535539
--toolbar-icon-hover-bg-color: #ffffff;
@@ -581,7 +585,7 @@ input[type="number"] {
581585
--editorFreeHighlight-editing-cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24'%3E%3Cpath d='M2 18c2.5-4 6-6.8 11-8.8l2.2 3.2C10.4 14.2 7 16.6 4.8 20z' fill='%237db0e4' fill-opacity='.6'/%3E%3Cpath d='M13 9.2l2.2 3.2M2 18c2.5-4 6-6.8 11-8.8' fill='none' stroke='%23f2eee8' stroke-width='1.4' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") 1 18, pointer;
582586
}
583587

584-
:root[style*="color-scheme"][style*="dark"] #viewsManager {
588+
:root[data-color-scheme="dark"] #viewsManager {
585589
--sidebar-bg-color: rgb(27 26 24 / 0.96);
586590
--header-bg: rgb(36 34 32 / 0.96);
587591
--text-color: #f2eee8;
@@ -611,14 +615,14 @@ input[type="number"] {
611615
--image-shadow: 0 1px 2px rgb(0 0 0 / 28%), 0 0 0 1px var(--image-border-color);
612616
}
613617

614-
:root[style*="color-scheme"][style*="dark"] .sidebar {
618+
:root[data-color-scheme="dark"] .sidebar {
615619
--sidebar-bg-color: #1f1d1a;
616620
--sidebar-border-color: rgb(242 238 232 / 20%);
617621
--sidebar-box-shadow: 0 1px 3px rgb(0 0 0 / 35%), 0 6px 16px rgb(0 0 0 / 32%);
618622
--resizer-hover-bg-color: #7db0e4;
619623
}
620624

621-
:root[style*="color-scheme"][style*="dark"] .dialog {
625+
:root[data-color-scheme="dark"] .dialog {
622626
--dialog-bg-color: #23201c;
623627
--dialog-border-color: #4c463e;
624628
--dialog-shadow: 0 8px 24px rgb(0 0 0 / 38%);
@@ -648,33 +652,33 @@ input[type="number"] {
648652
--radio-checked-border-color: #7db0e4;
649653
}
650654

651-
:root[style*="color-scheme"][style*="dark"] .colorPicker {
655+
:root[data-color-scheme="dark"] .colorPicker {
652656
--hover-outline-color: #9ac2ea;
653657
--selected-outline-color: #7db0e4;
654658
--swatch-border-color: #4c463e;
655659
}
656660

657-
:root[style*="color-scheme"][style*="dark"] .toggle-button {
661+
:root[data-color-scheme="dark"] .toggle-button {
658662
--color-accent-primary: light-dark(#4a90d9, #7db0e4);
659663
--color-accent-primary-hover: light-dark(#3f83cb, #9ac2ea);
660664
--color-accent-primary-active: light-dark(#2f73b8, #b6d4f1);
661665
--color-canvas: light-dark(#ffffff, #1f1d1a);
662666
--border-color-interactive: light-dark(#cfcac3, #f2eee8);
663667
}
664668

665-
:root[style*="color-scheme"][style*="dark"] .toggle-button[aria-pressed=true] {
669+
:root[data-color-scheme="dark"] .toggle-button[aria-pressed=true] {
666670
--toggle-dot-background-color-on-pressed: #1f1d1a;
667671
}
668672

669-
:root[style*="color-scheme"][style*="dark"] .messageBar {
673+
:root[data-color-scheme="dark"] .messageBar {
670674
--message-bar-close-button-color: #f2eee8;
671675
--message-bar-close-button-color-hover: #ffffff;
672676
--message-bar-close-button-hover-bg-color: rgb(125 176 228 / 22%);
673677
--message-bar-close-button-active-bg-color: rgb(125 176 228 / 30%);
674678
--message-bar-close-button-focus-bg-color: rgb(125 176 228 / 26%);
675679
}
676680

677-
:root[style*="color-scheme"][style*="dark"] #editorUndoBar {
681+
:root[data-color-scheme="dark"] #editorUndoBar {
678682
--text-primary-color: #f2eee8;
679683
--message-bar-icon-color: #7db0e4;
680684
--message-bar-bg-color: #273341;
@@ -691,6 +695,10 @@ input[type="number"] {
691695

692696
/* Neutral refresh: remove warm tones while preserving Paper & Ink structure. */
693697
:root {
698+
/* viewer.css switches these on the system scheme alone; the theme keeps them in step with its
699+
palettes so a forced LIGHT stays light. */
700+
--button-disabled-opacity: 0.4;
701+
--hover-filter: brightness(0.9);
694702
--body-bg-color: #f1f3f5;
695703
--toolbar-bg-color: #ffffff;
696704
--toolbar-border-color: #d0d7de;
@@ -777,8 +785,11 @@ input[type="number"] {
777785
}
778786

779787
@media (prefers-color-scheme: dark) {
780-
:where(:root:not([style*="color-scheme: light"])) {
788+
/* Guarded like the first palette above. */
789+
:where(:root:not([data-color-scheme="light"])) {
781790
&:root {
791+
--button-disabled-opacity: 0.6;
792+
--hover-filter: brightness(1.4);
782793
--main-color: #e6edf3;
783794
--toolbar-icon-bg-color: #e6edf3;
784795
--toolbar-icon-hover-bg-color: #ffffff;
@@ -877,7 +888,9 @@ input[type="number"] {
877888
}
878889

879890
/* Support forced dark mode from viewer-css-theme="DARK" with neutral palette. */
880-
:root[style*="color-scheme"][style*="dark"] {
891+
:root[data-color-scheme="dark"] {
892+
--button-disabled-opacity: 0.6;
893+
--hover-filter: brightness(1.4);
881894
--main-color: #e6edf3;
882895
--toolbar-icon-bg-color: #e6edf3;
883896
--toolbar-icon-hover-bg-color: #ffffff;
@@ -908,7 +921,7 @@ input[type="number"] {
908921
--thickness-bg: #161b22;
909922
}
910923

911-
:root[style*="color-scheme"][style*="dark"] #viewsManager {
924+
:root[data-color-scheme="dark"] #viewsManager {
912925
--sidebar-bg-color: rgb(13 17 23 / 0.96);
913926
--header-bg: rgb(22 27 34 / 0.96);
914927
--text-color: #e6edf3;
@@ -922,12 +935,13 @@ input[type="number"] {
922935
--image-page-number-fg: #e6edf3;
923936
}
924937

925-
:root[style*="color-scheme"][style*="dark"] .sidebar {
938+
:root[data-color-scheme="dark"] .sidebar {
926939
--sidebar-bg-color: #161b22;
927940
--sidebar-border-color: rgb(230 237 243 / 22%);
941+
--sidebar-box-shadow: 0 1px 3px rgb(0 0 0 / 35%), 0 6px 16px rgb(0 0 0 / 32%);
928942
}
929943

930-
:root[style*="color-scheme"][style*="dark"] .dialog {
944+
:root[data-color-scheme="dark"] .dialog {
931945
--dialog-bg-color: #161b22;
932946
--dialog-border-color: #3d444d;
933947
--text-primary-color: #e6edf3;
@@ -950,20 +964,20 @@ input[type="number"] {
950964
--radio-border-color: #53606d;
951965
}
952966

953-
:root[style*="color-scheme"][style*="dark"] .colorPicker {
967+
:root[data-color-scheme="dark"] .colorPicker {
954968
--swatch-border-color: #3d444d;
955969
}
956970

957-
:root[style*="color-scheme"][style*="dark"] .toggle-button {
971+
:root[data-color-scheme="dark"] .toggle-button {
958972
--color-canvas: light-dark(#ffffff, #161b22);
959973
--border-color-interactive: light-dark(#c6ccd2, #e6edf3);
960974
}
961975

962-
:root[style*="color-scheme"][style*="dark"] .toggle-button[aria-pressed=true] {
976+
:root[data-color-scheme="dark"] .toggle-button[aria-pressed=true] {
963977
--toggle-dot-background-color-on-pressed: #161b22;
964978
}
965979

966-
:root[style*="color-scheme"][style*="dark"] #editorUndoBar {
980+
:root[data-color-scheme="dark"] #editorUndoBar {
967981
--text-primary-color: #e6edf3;
968982
--undo-button-bg-color: #21262d;
969983
--undo-button-bg-color-hover: #2a313c;

‎tests/basic.test.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,32 @@ describe('Basic tests', async () => {
5959
const doc = getIframe().contentDocument!
6060
const themeSheet = Array.from(doc.styleSheets).find(sheet => sheet.href?.endsWith('/paper-and-ink.css'))!
6161
const darkRules = Array.from(themeSheet.cssRules).filter(
62-
(rule): rule is CSSMediaRule => rule instanceof getIframe().contentWindow.CSSMediaRule
63-
&& rule.conditionText === '(prefers-color-scheme: dark)'
62+
(rule): rule is CSSMediaRule => (rule as CSSMediaRule).conditionText === '(prefers-color-scheme: dark)'
6463
)
64+
// One system dark block per palette; the neutral palette hides the warm one in the colors below.
6565
expect(darkRules).toHaveLength(2)
66+
// `--hover-filter` comes from viewer.css, which switches it on the system scheme alone.
6667
const colors = () => [
6768
getComputedStyle(doc.documentElement).getPropertyValue('--body-bg-color').trim(),
68-
getComputedStyle(doc.querySelector('#viewsManager')!).getPropertyValue('--text-color').trim()
69+
getComputedStyle(doc.querySelector('#viewsManager')!).getPropertyValue('--text-color').trim(),
70+
getComputedStyle(doc.documentElement).getPropertyValue('--hover-filter').trim()
6971
]
70-
for (const systemDark of [false, true, false, true]) {
72+
const light = ['#f1f3f5', '#1f2328', 'brightness(0.9)']
73+
const dark = ['#0d1117', '#e6edf3', 'brightness(1.4)']
74+
for (const systemDark of [false, true, false]) {
7175
// Toggle matching of the system dark rules without depending on the host's theme.
7276
darkRules.forEach(rule => { rule.media.mediaText = systemDark ? 'all' : 'not all' })
7377
viewer.setAttribute('viewer-css-theme', 'LIGHT')
74-
expect(colors()).toEqual(['#f1f3f5', '#1f2328'])
78+
expect(doc.documentElement.dataset.colorScheme).eq('light')
79+
expect(colors()).toEqual(light)
7580
viewer.setAttribute('viewer-css-theme', 'DARK')
76-
expect(colors()).toEqual(['#0d1117', '#e6edf3'])
81+
expect(doc.documentElement.dataset.colorScheme).eq('dark')
82+
expect(colors()).toEqual(dark)
7783
viewer.setAttribute('viewer-css-theme', 'AUTOMATIC')
78-
expect(colors()).toEqual(systemDark ? ['#0d1117', '#e6edf3'] : ['#f1f3f5', '#1f2328'])
84+
expect(doc.documentElement.dataset.colorScheme).toBeUndefined()
85+
expect(colors()).toEqual(systemDark ? dark : light)
7986
viewer.removeAttribute('viewer-css-theme')
80-
expect(colors()).toEqual(systemDark ? ['#0d1117', '#e6edf3'] : ['#f1f3f5', '#1f2328'])
87+
expect(colors()).toEqual(systemDark ? dark : light)
8188
}
8289
})
8390

0 commit comments

Comments
 (0)