Skip to content

Commit f94269b

Browse files
committed
feat: add theme props.
1 parent eb598a3 commit f94269b

File tree

7 files changed

+93
-56
lines changed

7 files changed

+93
-56
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@
5555
"@types/react": "~18.0.5",
5656
"@types/react-dom": "~18.0.1",
5757
"@types/react-test-renderer": "~18.0.0",
58-
"@uiw/react-github-corners": "~1.5.3",
59-
"@uiw/react-markdown-preview": "~3.5.1",
58+
"@uiw/react-github-corners": "~1.5.14",
59+
"@uiw/react-markdown-preview": "~4.0.6",
6060
"@uiw/reset.css": "~1.0.6",
61+
"@wcj/dark-mode": "^1.0.14",
6162
"compile-less-cli": "~1.8.11",
6263
"husky": "~7.0.4",
6364
"kkt": "~7.1.5",
@@ -75,9 +76,9 @@
7576
"@babel/plugin-transform-classes": "~7.16.7",
7677
"@babel/runtime": "~7.17.2",
7778
"@babel/standalone": "~7.17.6",
78-
"@codemirror/lang-javascript": "~0.19.7",
79+
"@codemirror/lang-javascript": "^0.20.0",
7980
"@uiw/copy-to-clipboard": "~1.0.12",
80-
"@uiw/react-codemirror": "~4.5.1",
81+
"@uiw/react-codemirror": "~4.7.0",
8182
"@uiw/react-codepen": "~1.0.2",
8283
"@uiw/react-codesandbox": "~1.1.4",
8384
"@uiw/react-split": "~5.8.7",
@@ -101,4 +102,4 @@
101102
"last 1 safari version"
102103
]
103104
}
104-
}
105+
}

src/ThirdPartyButton.less

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/ThirdPartyButton.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react';
22
import CodeSandbox, { CodeSandboxProps } from '@uiw/react-codesandbox';
33
import Codepen from '@uiw/react-codepen';
44
import { CodePreviewProps } from './';
5-
import './ThirdPartyButton.less';
65

76
type ThirdPartyButtonProps = {
87
prefixCls?: string;

src/index.less

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
@w-code-preview:~ "w-code-preview";
22

3+
[data-color-mode*='light'],
4+
[data-color-mode*='light'] body {
5+
--color-code-preview-bg: #fff3;
6+
--color-code-preview-border: #d5d5d5;
7+
--color-code-preview-hover: #838383;
8+
--code-preview-text: #d5d5d5;
9+
}
10+
11+
[data-color-mode*='dark'],
12+
[data-color-mode*='dark'] body {
13+
--color-code-preview-border: #ffffff1a;
14+
--color-code-preview-bg: #282c34;
15+
--color-code-preview-hover: #9c9c9c;
16+
--code-preview-text: #c9d1d9;
17+
}
18+
319
.@{w-code-preview} {
420
&.w-split-horizontal > .w-split-bar {
521
height: initial;
622
}
23+
> .w-split-bar {
24+
background-color: var(--color-code-preview-bg) !important;
25+
box-shadow: inset 1px 0 0 0 var(--color-code-preview-border), 1px 0 0 0 var(--color-code-preview-border) !important;
26+
}
727
}
28+
829
.@{w-code-preview} {
930
border-radius: 3px;
1031
margin-bottom: 16px;
11-
background-color: #fff;
32+
background-color: var(--color-code-preview-bg);
1233
max-height: 420px;
1334
min-height: 12px;
1435
z-index: 10;
@@ -18,7 +39,6 @@
1839
}
1940
&-fullScreen {
2041
position: fixed;
21-
background-color: #fff;
2242
border-radius: 0;
2343
border: 0;
2444
top: 0 !important;
@@ -87,7 +107,7 @@
87107
min-height: 80px;
88108
&-error {
89109
z-index: 2;
90-
background: #fff;
110+
background-color: var(--color-code-preview-bg);
91111
width: 100%;
92112
height: 100%;
93113
margin-bottom: 0 !important;
@@ -119,11 +139,9 @@
119139
}
120140
&-bar {
121141
cursor: pointer;
122-
background-color: rgba(0, 0, 0, 0.03);
123-
box-shadow: inset 1px 0 0 0 #d5d5d5;
142+
box-shadow: inset 1px 0 0 0 var(--color-code-preview-border);
124143
padding: 3px 0 8px 0;
125144
margin: 0 auto;
126-
color: rgba(0, 0, 0, 0.45);
127145
writing-mode: tb-rl;
128146
position: relative;
129147
display: flex;
@@ -141,9 +159,10 @@
141159
transition: all 0.3s;
142160
font-weight: bold;
143161
user-select: none;
162+
color: var(--code-preview-text);
144163
flex: 1;
145164
&:hover {
146-
color: #000;
165+
color: var(--color-code-preview-hover);
147166
}
148167
}
149168
&-iconbtns {
@@ -154,10 +173,10 @@
154173
}
155174
svg {
156175
transition: all 0.3s;
157-
fill: #9c9c9c;
176+
fill: var(--code-preview-text);
158177
display: block;
159178
&:hover {
160-
fill: #000;
179+
fill: var(--color-code-preview-hover);
161180
}
162181
}
163182
&-copied svg,
@@ -169,3 +188,42 @@
169188
cursor: inherit;
170189
}
171190
}
191+
192+
.@{w-code-preview} {
193+
&-thirdparty + &-thirdparty {
194+
margin-left: 10px;
195+
}
196+
&-thirdparty {
197+
display: inline-block;
198+
position: absolute;
199+
white-space: nowrap;
200+
line-height: 1;
201+
z-index: 2;
202+
right: 0;
203+
top: -23px;
204+
form {
205+
display: block;
206+
}
207+
form + form {
208+
margin-right: 5px;
209+
}
210+
button {
211+
border: 0;
212+
outline: 0;
213+
cursor: pointer;
214+
padding: 3px 4px 3px 4px;
215+
border-radius: 3px;
216+
transition: color 0.3s;
217+
color: rgba(0, 0, 0, 0.45);
218+
&:hover {
219+
color: rgba(0, 0, 0, 0.85);
220+
}
221+
svg {
222+
fill: var(--code-preview-text);
223+
}
224+
svg:not(:root) {
225+
overflow: hidden;
226+
}
227+
}
228+
}
229+
}

src/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ export interface CodePreviewProps extends SplitProps {
6666
btnText?: string;
6767
/** @default 'Hide Editor' */
6868
btnHideText?: string;
69+
/**
70+
* `light` / `dark` / `Extension` Defaults to `light`.
71+
* @default light
72+
*/
73+
theme?: ReactCodeMirrorProps['theme'];
6974
}
7075

7176
export interface CodePreviewState {
@@ -86,6 +91,7 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
8691
style,
8792
prefixCls = 'w-code-preview',
8893
className,
94+
theme = 'light',
8995
editProps = {},
9096
codePenOption,
9197
codeSandboxOption,
@@ -214,6 +220,7 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
214220
<CodeMirror
215221
value={(code || '').replace(/\n$/, '')}
216222
ref={editor}
223+
theme={theme}
217224
extensions={[javascript({ jsx: true })]}
218225
{...editProps}
219226
style={{ height: '100%' }}

website/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import GitHubCorners from '@uiw/react-github-corners';
33
import Markdown from '@uiw/react-markdown-preview';
4+
import '@wcj/dark-mode';
45
import DocumentStr from '../README.md';
56
import styles from './App.module.less';
67
import Example from './Example';
@@ -12,6 +13,7 @@ export default function App() {
1213
if (DocumentStrSource) DocumentStrSource = DocumentStr.replace(/([\s\S]*)<!--dividing-->/, '');
1314
return (
1415
<div className={styles.warpper}>
16+
<dark-mode light="Light" dark="Dark" style={{ position: 'fixed', left: 10, top: 5 }}></dark-mode>
1517
<GitHubCorners fixed href="https://github.com/uiwjs/react-code-preview" target="__blank" />
1618
<h1 className={styles.title}>
1719
React Code Preview <sup>{version}</sup>

website/Example.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, useState } from 'react';
1+
import { Fragment, useState, useEffect } from 'react';
22
import CodePreview from '../';
33
import { Switch } from 'uiw';
44
import * as UIW from 'uiw';
@@ -20,6 +20,7 @@ ReactDOM.render(
2020
);`;
2121

2222
const Example = () => {
23+
const [theme, setTheme] = useState<'light' | 'dark'>('light');
2324
const [code] = useState(defaultCode);
2425
const [state, setState] = useState({
2526
bgWhite: false,
@@ -87,10 +88,19 @@ const Example = () => {
8788
const newstate = { ...state, [`${keyName}`]: e.target.checked };
8889
setState({ ...newstate });
8990
}
91+
92+
useEffect(() => {
93+
setTheme(document.documentElement.getAttribute('data-color-mode') === 'dark' ? 'dark' : 'light');
94+
document.addEventListener('colorschemechange', (e: any) => {
95+
setTheme(e.detail.colorScheme);
96+
});
97+
}, []);
98+
9099
return (
91100
<Fragment>
92101
<CodePreview
93102
code={code}
103+
theme={theme}
94104
dependencies={{ ...UIW }}
95105
bordered={state.bordered}
96106
noScroll={state.noScroll}

0 commit comments

Comments
 (0)