@@ -9,7 +9,10 @@ import { STORY_RENDERED } from '@storybook/core-events';
9
9
import React , { useEffect , useState } from 'react' ;
10
10
import ReactMarkdown from 'react-markdown' ;
11
11
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter' ;
12
- import { vs } from 'react-syntax-highlighter/dist/esm/styles/prism' ;
12
+ import {
13
+ vs ,
14
+ vscDarkPlus ,
15
+ } from 'react-syntax-highlighter/dist/esm/styles/prism' ;
13
16
import remarkGfm from 'remark-gfm' ;
14
17
15
18
const ADDON_ID = 'readme' ;
@@ -48,6 +51,10 @@ const Readme = props => {
48
51
} ) ;
49
52
} , [ ] ) ;
50
53
54
+ const shouldUseDarkTheme = ( ) => {
55
+ return window . matchMedia ( '(prefers-color-scheme: dark)' ) ;
56
+ } ;
57
+
51
58
const renderReadme = ( ) => (
52
59
< ReactMarkdown
53
60
children = { markdown }
@@ -60,7 +67,7 @@ const Readme = props => {
60
67
className = "storybook-readme-syntax-highlighter"
61
68
children = { String ( children ) . replace ( / \n $ / , '' ) }
62
69
style = { {
63
- ...vs ,
70
+ ...( shouldUseDarkTheme ( ) ? vscDarkPlus : vs ) ,
64
71
'pre[class*="language-"]' : { display : 'none' } ,
65
72
} }
66
73
language = { match [ 1 ] }
@@ -86,10 +93,6 @@ const Readme = props => {
86
93
return (
87
94
< div className = "markdown-body" style = { { padding : '32px' } } >
88
95
{ markdown ? renderReadme ( ) : renderNoReadme ( ) }
89
- < style > { overrideDarkThemeCSS } </ style >
90
- { /* TODO: This is a hack to prevent
91
- the media query on prefer-color-scheme: 'dark'. A more elegant solution
92
- should be found. */ }
93
96
</ div >
94
97
) ;
95
98
} ;
@@ -105,51 +108,3 @@ addons.register(ADDON_ID, api => {
105
108
) ,
106
109
} ) ;
107
110
} ) ;
108
-
109
- const overrideDarkThemeCSS = `
110
- div.markdown-body {
111
- color-scheme: light;
112
- --color-prettylights-syntax-comment: #6e7781;
113
- --color-prettylights-syntax-constant: #0550ae;
114
- --color-prettylights-syntax-entity: #8250df;
115
- --color-prettylights-syntax-storage-modifier-import: #24292f;
116
- --color-prettylights-syntax-entity-tag: #116329;
117
- --color-prettylights-syntax-keyword: #cf222e;
118
- --color-prettylights-syntax-string: #0a3069;
119
- --color-prettylights-syntax-variable: #953800;
120
- --color-prettylights-syntax-brackethighlighter-unmatched: #82071e;
121
- --color-prettylights-syntax-invalid-illegal-text: #f6f8fa;
122
- --color-prettylights-syntax-invalid-illegal-bg: #82071e;
123
- --color-prettylights-syntax-carriage-return-text: #f6f8fa;
124
- --color-prettylights-syntax-carriage-return-bg: #cf222e;
125
- --color-prettylights-syntax-string-regexp: #116329;
126
- --color-prettylights-syntax-markup-list: #3b2300;
127
- --color-prettylights-syntax-markup-heading: #0550ae;
128
- --color-prettylights-syntax-markup-italic: #24292f;
129
- --color-prettylights-syntax-markup-bold: #24292f;
130
- --color-prettylights-syntax-markup-deleted-text: #82071e;
131
- --color-prettylights-syntax-markup-deleted-bg: #FFEBE9;
132
- --color-prettylights-syntax-markup-inserted-text: #116329;
133
- --color-prettylights-syntax-markup-inserted-bg: #dafbe1;
134
- --color-prettylights-syntax-markup-changed-text: #953800;
135
- --color-prettylights-syntax-markup-changed-bg: #ffd8b5;
136
- --color-prettylights-syntax-markup-ignored-text: #eaeef2;
137
- --color-prettylights-syntax-markup-ignored-bg: #0550ae;
138
- --color-prettylights-syntax-meta-diff-range: #8250df;
139
- --color-prettylights-syntax-brackethighlighter-angle: #57606a;
140
- --color-prettylights-syntax-sublimelinter-gutter-mark: #8c959f;
141
- --color-prettylights-syntax-constant-other-reference-link: #0a3069;
142
- --color-fg-default: #24292f;
143
- --color-fg-muted: #57606a;
144
- --color-fg-subtle: #6e7781;
145
- --color-canvas-default: #ffffff;
146
- --color-canvas-subtle: #f6f8fa;
147
- --color-border-default: #d0d7de;
148
- --color-border-muted: hsla(210,18%,87%,1);
149
- --color-neutral-muted: rgba(175,184,193,0.2);
150
- --color-accent-fg: #0969da;
151
- --color-accent-emphasis: #0969da;
152
- --color-attention-subtle: #fff8c5;
153
- --color-danger-fg: #cf222e;
154
- }
155
- ` ;
0 commit comments