Skip to content

Commit dc1193c

Browse files
committed
Now switches highligter theme instead
1 parent 4fef47c commit dc1193c

File tree

1 file changed

+9
-54
lines changed

1 file changed

+9
-54
lines changed

storyhelpers/storybook-readme/register.js

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { STORY_RENDERED } from '@storybook/core-events';
99
import React, { useEffect, useState } from 'react';
1010
import ReactMarkdown from 'react-markdown';
1111
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';
1316
import remarkGfm from 'remark-gfm';
1417

1518
const ADDON_ID = 'readme';
@@ -48,6 +51,10 @@ const Readme = props => {
4851
});
4952
}, []);
5053

54+
const shouldUseDarkTheme = () => {
55+
return window.matchMedia('(prefers-color-scheme: dark)');
56+
};
57+
5158
const renderReadme = () => (
5259
<ReactMarkdown
5360
children={markdown}
@@ -60,7 +67,7 @@ const Readme = props => {
6067
className="storybook-readme-syntax-highlighter"
6168
children={String(children).replace(/\n$/, '')}
6269
style={{
63-
...vs,
70+
...(shouldUseDarkTheme() ? vscDarkPlus : vs),
6471
'pre[class*="language-"]': { display: 'none' },
6572
}}
6673
language={match[1]}
@@ -86,10 +93,6 @@ const Readme = props => {
8693
return (
8794
<div className="markdown-body" style={{ padding: '32px' }}>
8895
{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. */}
9396
</div>
9497
);
9598
};
@@ -105,51 +108,3 @@ addons.register(ADDON_ID, api => {
105108
),
106109
});
107110
});
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

Comments
 (0)