Skip to content

Commit 86cf0e6

Browse files
make other components dark mode compatible
1 parent 6b88724 commit 86cf0e6

File tree

10 files changed

+135
-84
lines changed

10 files changed

+135
-84
lines changed

beta/src/components/Layout/Page.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
* Copyright (c) Facebook, Inc. and its affiliates.
33
*/
44

5-
import {MenuProvider} from 'components/useMenu';
5+
import { MenuProvider } from 'components/useMenu';
66
import * as React from 'react';
7-
import {Nav} from './Nav';
8-
import {RouteItem, SidebarContext} from './useRouteMeta';
9-
import {Sidebar} from './Sidebar';
10-
import {Footer} from './Footer';
7+
import { Nav } from './Nav';
8+
import { RouteItem, SidebarContext } from './useRouteMeta';
9+
import { Sidebar } from './Sidebar';
10+
import { Footer } from './Footer';
1111
interface PageProps {
1212
children: React.ReactNode;
1313
routeTree: RouteItem;
1414
}
1515

16-
export function Page({routeTree, children}: PageProps) {
16+
export function Page({ routeTree, children }: PageProps) {
1717
return (
1818
<MenuProvider>
1919
<SidebarContext.Provider value={routeTree}>
20-
<div className="h-auto lg:h-screen flex flex-row">
20+
<div className=" bg-wash dark:bg-wash-dark transition-colors duration-500">
2121
<div className="h-auto lg:h-full overflow-y-scroll fixed flex flex-row lg:flex-col py-0 top-0 left-0 right-0 lg:max-w-xs w-full shadow lg:shadow-none z-50">
2222
<Nav />
2323
<Sidebar />
@@ -27,7 +27,7 @@ export function Page({routeTree, children}: PageProps) {
2727
<div className="w-full min-w-0">
2828
<main
2929
className="flex flex-1 self-stretch flex-col items-end"
30-
style={{justifyContent: 'space-around'}}>
30+
style={{ justifyContent: 'space-around' }}>
3131
{children}
3232
<Footer />
3333
</main>

beta/src/components/Layout/Sidebar/Sidebar.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
import * as React from 'react';
66
import cn from 'classnames';
7-
import {SidebarContext} from 'components/Layout/useRouteMeta';
8-
import {MenuContext} from 'components/useMenu';
9-
import {useMediaQuery} from '../useMediaQuery';
10-
import {SidebarRouteTree} from './SidebarRouteTree';
11-
import {Search} from 'components/Search';
12-
import {Button} from 'components/Button';
13-
import {MobileNav} from '../Nav/MobileNav';
7+
import { SidebarContext } from 'components/Layout/useRouteMeta';
8+
import { MenuContext } from 'components/useMenu';
9+
import { useMediaQuery } from '../useMediaQuery';
10+
import { SidebarRouteTree } from './SidebarRouteTree';
11+
import { Search } from 'components/Search';
12+
import { Button } from 'components/Button';
13+
import { MobileNav } from '../Nav/MobileNav';
1414

1515
const SIDEBAR_BREAKPOINT = 1023;
1616

17-
export function Sidebar({isMobileOnly}: {isMobileOnly?: boolean}) {
18-
const {menuRef, isOpen} = React.useContext(MenuContext);
17+
export function Sidebar({ isMobileOnly }: { isMobileOnly?: boolean }) {
18+
const { menuRef, isOpen } = React.useContext(MenuContext);
1919
const isMobileSidebar = useMediaQuery(SIDEBAR_BREAKPOINT);
2020
let routeTree = React.useContext(SidebarContext);
2121
const isHidden = isMobileOnly && !isMobileSidebar;
@@ -62,7 +62,7 @@ export function Sidebar({isMobileOnly}: {isMobileOnly?: boolean}) {
6262
return (
6363
<aside
6464
className={cn(
65-
`lg:flex-grow lg:flex flex-col w-full pt-4 pb-8 lg:pb-0 lg:max-w-xs fixed lg:sticky bg-wash dark:bg-wash-dark z-10`,
65+
`lg:flex-grow lg:flex flex-col w-full pt-4 pb-8 lg:pb-0 lg:max-w-xs fixed lg:sticky bg-transparent z-10`,
6666
isOpen ? 'block z-40' : 'hidden lg:block'
6767
)}
6868
aria-hidden={isHidden ? 'true' : 'false'}
@@ -76,7 +76,7 @@ export function Sidebar({isMobileOnly}: {isMobileOnly?: boolean}) {
7676
<nav
7777
role="navigation"
7878
ref={menuRef}
79-
style={{'--bg-opacity': '.2'} as React.CSSProperties} // Need to cast here because CSS vars aren't considered valid in TS types (cuz they could be anything)
79+
style={{ '--bg-opacity': '.2' } as React.CSSProperties} // Need to cast here because CSS vars aren't considered valid in TS types (cuz they could be anything)
8080
className="w-full h-screen lg:h-auto flex-grow pr-0 lg:pr-5 pt-6 pb-16 lg:pb-0 lg:py-6 md:pt-4 lg:pt-4 overflow-y-scroll lg:overflow-y-auto scrolling-touch scrolling-gpu">
8181
{isMobileSidebar ? (
8282
<MobileNav />

beta/src/components/MDX/CodeBlock/CodeBlock.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import {
1212
} from '@codesandbox/sandpack-react';
1313
import rangeParser from 'parse-numeric-range';
1414

15-
import {CodeBlockLightTheme} from '../Sandpack/Themes';
15+
import { CodeBlockDarkTheme, CodeBlockLightTheme } from '../Sandpack/Themes';
1616
import styles from './CodeBlock.module.css';
17+
import { ColorMode, ThemeContext } from 'modules/ThemeProvider';
1718

1819
interface InlineHiglight {
1920
step: number;
@@ -56,7 +57,7 @@ const CodeBlock = React.forwardRef(
5657
const inlineHighlightConfig = inlineHighlightLines.map(
5758
(line: InlineHiglight) => ({
5859
...line,
59-
elementAttributes: {'data-step': `${line.step}`},
60+
elementAttributes: { 'data-step': `${line.step}` },
6061
className: cn(
6162
'code-step bg-opacity-10 relative rounded-md p-1 ml-2',
6263
{
@@ -77,13 +78,22 @@ const CodeBlock = React.forwardRef(
7778
const language = className.substring(9);
7879
const filename = '/index.' + language;
7980
const decorators = getDecoratedLineInfo();
81+
82+
// get current color mode
83+
const { colorMode } = React.useContext(ThemeContext);
8084
return (
8185
<div
8286
translate="no"
8387
className={cn(
84-
'rounded-lg h-full w-full overflow-x-auto flex items-center bg-white shadow-lg',
88+
'rounded-lg h-full w-full overflow-x-auto flex items-center bg-white dark:bg-transparent shadow-lg',
8589
!noMargin && 'my-8'
86-
)}>
90+
)}
91+
style={colorMode === ColorMode.dark ?
92+
{
93+
borderColor: '#343A46',
94+
borderWidth: '1px'
95+
} : {}}
96+
>
8797
<SandpackProvider
8898
customSetup={{
8999
entry: filename,
@@ -93,7 +103,7 @@ const CodeBlock = React.forwardRef(
93103
},
94104
},
95105
}}>
96-
<SandpackThemeProvider theme={CodeBlockLightTheme}>
106+
<SandpackThemeProvider theme={colorMode === ColorMode.dark ? CodeBlockDarkTheme : CodeBlockLightTheme}>
97107
<ClasserProvider
98108
classes={{
99109
'sp-cm': styles.codeViewer,
@@ -161,8 +171,8 @@ function getInlineHighlights(metastring: string, code: string) {
161171
if (fromIndex === undefined) {
162172
throw Error(
163173
"Found '" +
164-
substr +
165-
"' twice. Specify fromIndex as the fourth value in the tuple."
174+
substr +
175+
"' twice. Specify fromIndex as the fourth value in the tuple."
166176
);
167177
}
168178
index = line.indexOf(substr, fromIndex);
@@ -177,4 +187,4 @@ function getInlineHighlights(metastring: string, code: string) {
177187
endColumn: index + substr.length,
178188
};
179189
});
180-
}
190+
}

beta/src/components/MDX/Sandpack/CustomPreset.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
* Copyright (c) Facebook, Inc. and its affiliates.
33
*/
44

5-
import React from 'react';
5+
import React, { useContext } from 'react';
66
import {
77
useSandpack,
88
useActiveCode,
99
SandpackCodeEditor,
1010
SandpackThemeProvider,
1111
} from '@codesandbox/sandpack-react';
1212

13-
import {IconChevron} from 'components/Icon/IconChevron';
14-
import {NavigationBar} from './NavigationBar';
15-
import {Preview} from './Preview';
16-
import {GithubLightTheme} from './Themes';
13+
import { IconChevron } from 'components/Icon/IconChevron';
14+
import { NavigationBar } from './NavigationBar';
15+
import { Preview } from './Preview';
16+
import { CodesandboxDarkTheme, GithubLightTheme } from './Themes';
17+
import { ColorMode, ThemeContext } from 'modules/ThemeProvider';
1718

1819
export function CustomPreset({
1920
isSingleFile,
@@ -22,12 +23,12 @@ export function CustomPreset({
2223
isSingleFile: boolean;
2324
onReset: () => void;
2425
}) {
25-
const lineCountRef = React.useRef<{[key: string]: number}>({});
26-
const {sandpack} = useSandpack();
27-
const {code} = useActiveCode();
26+
const lineCountRef = React.useRef<{ [key: string]: number }>({});
27+
const { sandpack } = useSandpack();
28+
const { code } = useActiveCode();
2829
const [isExpanded, setIsExpanded] = React.useState(false);
2930

30-
const {activePath} = sandpack;
31+
const { activePath } = sandpack;
3132
if (!lineCountRef.current[activePath]) {
3233
lineCountRef.current[activePath] = code.split('\n').length;
3334
}
@@ -41,11 +42,14 @@ export function CustomPreset({
4142
return isExpanded ? editorHeight : 406;
4243
};
4344

45+
// get the current colorMode
46+
const { colorMode } = useContext(ThemeContext);
47+
4448
return (
4549
<>
4650
<div className="shadow-lg dark:shadow-lg-dark rounded-lg">
4751
<NavigationBar showDownload={isSingleFile} onReset={onReset} />
48-
<SandpackThemeProvider theme={GithubLightTheme}>
52+
<SandpackThemeProvider theme={colorMode === ColorMode.dark ? CodesandboxDarkTheme : GithubLightTheme}>
4953
<div
5054
ref={sandpack.lazyAnchorRef}
5155
className="sp-layout rounded-t-none"
@@ -55,6 +59,7 @@ export function CustomPreset({
5559
minHeight: 216,
5660
}}>
5761
<SandpackCodeEditor
62+
5863
customStyle={{
5964
height: getHeight(),
6065
maxHeight: isExpanded ? '' : 406,
@@ -90,4 +95,4 @@ export function CustomPreset({
9095
</div>
9196
</>
9297
);
93-
}
98+
}

beta/src/components/MDX/Sandpack/Preview.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
/* eslint-disable react-hooks/exhaustive-deps */
66
import * as React from 'react';
7-
import {useSandpack, LoadingOverlay} from '@codesandbox/sandpack-react';
7+
import { useSandpack, LoadingOverlay } from '@codesandbox/sandpack-react';
88
import cn from 'classnames';
99

10-
import {Error} from './Error';
11-
import {computeViewportSize, generateRandomId} from './utils';
10+
import { Error } from './Error';
11+
import { computeViewportSize, generateRandomId } from './utils';
1212

1313
type CustomPreviewProps = {
1414
className?: string;
@@ -33,7 +33,7 @@ export function Preview({
3333
isExpanded,
3434
className,
3535
}: CustomPreviewProps) {
36-
const {sandpack, listen} = useSandpack();
36+
const { sandpack, listen } = useSandpack();
3737
const [isReady, setIsReady] = React.useState(false);
3838
const [iframeComputedHeight, setComputedAutoHeight] = React.useState<
3939
number | null
@@ -94,9 +94,9 @@ export function Preview({
9494
const viewportStyle = computeViewportSize('auto', 'portrait');
9595
const overrideStyle = error
9696
? {
97-
// Don't collapse errors
98-
maxHeight: undefined,
99-
}
97+
// Don't collapse errors
98+
maxHeight: undefined,
99+
}
100100
: null;
101101
const hideContent = !isReady || error;
102102

@@ -136,17 +136,17 @@ export function Preview({
136136
}}>
137137
<div
138138
className={cn(
139-
'p-0 sm:p-2 md:p-4 lg:p-8 bg-card dark:bg-wash-dark h-full relative rounded-b-lg lg:rounded-b-none'
139+
'p-0 sm:p-2 md:p-4 lg:p-8 bg-card dark:bg-transparent h-full relative rounded-b-lg lg:rounded-b-none'
140140
)}
141-
style={{overflow}}>
141+
style={{ overflow }}>
142142
<div
143143
style={{
144144
padding: 'initial',
145145
position: hideContent
146146
? 'relative'
147147
: isExpanded
148-
? 'sticky'
149-
: undefined,
148+
? 'sticky'
149+
: undefined,
150150
top: isExpanded ? '2rem' : undefined,
151151
}}>
152152
<iframe

beta/src/components/MDX/Sandpack/Themes.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Facebook, Inc. and its affiliates.
33
*/
44

5-
import {githubLightTheme, SandpackTheme} from '@codesandbox/sandpack-react';
5+
import { githubLightTheme, codesandboxDarkTheme, SandpackTheme } from '@codesandbox/sandpack-react';
66
import tailwindConfig from '../../../../tailwind.config';
77

88
export const GithubLightTheme: SandpackTheme = {
@@ -17,6 +17,18 @@ export const GithubLightTheme: SandpackTheme = {
1717
},
1818
};
1919

20+
export const CodesandboxDarkTheme: SandpackTheme = {
21+
...codesandboxDarkTheme,
22+
23+
typography: {
24+
...codesandboxDarkTheme.typography,
25+
bodyFont: tailwindConfig.theme.extend.fontFamily.sans.join(', '),
26+
monoFont: tailwindConfig.theme.extend.fontFamily.mono.join(', '),
27+
fontSize: tailwindConfig.theme.extend.fontSize.code,
28+
lineHeight: '24px',
29+
},
30+
};
31+
2032
export const CodeBlockLightTheme: SandpackTheme = {
2133
...githubLightTheme,
2234
palette: {
@@ -31,3 +43,18 @@ export const CodeBlockLightTheme: SandpackTheme = {
3143
lineHeight: '24px',
3244
},
3345
};
46+
47+
export const CodeBlockDarkTheme: SandpackTheme = {
48+
...codesandboxDarkTheme,
49+
palette: {
50+
...codesandboxDarkTheme.palette,
51+
defaultBackground: 'transparent',
52+
},
53+
typography: {
54+
...codesandboxDarkTheme.typography,
55+
bodyFont: tailwindConfig.theme.extend.fontFamily.sans.join(', '),
56+
monoFont: tailwindConfig.theme.extend.fontFamily.mono.join(', '),
57+
fontSize: tailwindConfig.theme.extend.fontSize.code,
58+
lineHeight: '24px',
59+
},
60+
};

0 commit comments

Comments
 (0)