Skip to content

Commit

Permalink
fix: useIsDarkMode의 초기값을 undefined로 설정 (#212)
Browse files Browse the repository at this point in the history
* fix: isDarkMode의 초기값을 undefined로 설정

* chore : changeset 추가

* feat: ThemeMode 컴포넌트 수정 및 적용

* Delete .changeset/six-clocks-matter.md

---------

Co-authored-by: 박찬혁 <[email protected]>
  • Loading branch information
anonymousRecords and okinawaa authored Aug 16, 2024
1 parent 6e2c8e1 commit 1b62156
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 60 deletions.
50 changes: 26 additions & 24 deletions docs/src/components/Sandpack/Sandpack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@ import { SandpackProps, SandpackProvider } from '@codesandbox/sandpack-react';
import { atomDark } from '@codesandbox/sandpack-themes';
import { baseTemplate } from './baseTemplate';
import { CustomPreset } from './CustomPreset';
import { useIsDarkMode } from '@/hooks/use-is-dark-mode';
import { SandpackLogLevel } from '@codesandbox/sandpack-client';
import { ThemeMode } from '../theme-mode/ThemeMode';

export function Sandpack({ files }: SandpackProps) {
const isDarkMode = useIsDarkMode();

return (
<SandpackProvider
template="vanilla-ts"
theme={isDarkMode ? atomDark : undefined}
files={{
...baseTemplate.files,
...files,
}}
customSetup={{
dependencies: baseTemplate.dependencies,
devDependencies: baseTemplate.devDependencies,
}}
options={{
recompileDelay: 300,
initMode: 'user-visible',
initModeObserverOptions: { rootMargin: '1400px 0px' },
bundlerURL: 'https://sandpack-bundler.codesandbox.io/',
logLevel: SandpackLogLevel.None,
}}
>
<CustomPreset />
</SandpackProvider>
<ThemeMode>
{theme => (
<SandpackProvider
template="vanilla-ts"
theme={theme === 'dark' ? atomDark : undefined}
files={{
...baseTemplate.files,
...files,
}}
customSetup={{
dependencies: baseTemplate.dependencies,
devDependencies: baseTemplate.devDependencies,
}}
options={{
recompileDelay: 300,
initMode: 'user-visible',
initModeObserverOptions: { rootMargin: '1400px 0px' },
bundlerURL: 'https://sandpack-bundler.codesandbox.io/',
logLevel: SandpackLogLevel.None,
}}
>
<CustomPreset />
</SandpackProvider>
)}
</ThemeMode>
);
}
14 changes: 10 additions & 4 deletions docs/src/components/introduction/Adopters.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useIsDarkMode } from '@/hooks/use-is-dark-mode';
import Image from 'next/image';
import { ComponentProps } from 'react';
import { ThemeMode } from '../theme-mode/ThemeMode';

/**
* 이 곳에 적용한 조직을 추가해주세요.
Expand Down Expand Up @@ -28,12 +28,18 @@ export const adopterLogoImagePropsList = [
>;

export const Adopters = () => {
const isDarkMode = useIsDarkMode();

return (
<div className="flex flex-wrap gap-8 justify-center">
{adopterLogoImagePropsList.map(({ darkSrc, src, alt, ...props }) => (
<Image key={src} src={isDarkMode ? darkSrc : src} alt={alt} {...props} />
<ThemeMode key={src}>
{(theme) => (
<Image
src={theme === 'dark' ? darkSrc : src}
alt={alt}
{...props}
/>
)}
</ThemeMode>
))}
</div>
);
Expand Down
17 changes: 17 additions & 0 deletions docs/src/components/theme-mode/ThemeMode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { useIsDarkMode } from '@/hooks/use-is-dark-mode';

interface ThemeModeProps {
children: (theme: 'dark' | 'light') => React.ReactNode;
fallback?: React.ReactNode;
}

export function ThemeMode({ children, fallback = null }: ThemeModeProps) {
const isDarkMode = useIsDarkMode();

if (isDarkMode == null) {
return fallback;
}

return children(isDarkMode ? 'dark' : 'light');
}
2 changes: 1 addition & 1 deletion docs/src/hooks/use-is-dark-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from 'react';

export function useIsDarkMode() {
const { resolvedTheme } = useTheme();
const [isDarkMode, setIsDarkMode] = useState(false);
const [isDarkMode, setIsDarkMode] = useState<boolean>();

useEffect(() => {
setIsDarkMode(resolvedTheme === 'dark');
Expand Down
64 changes: 33 additions & 31 deletions docs/theme.config.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useIsDarkMode } from '@/hooks/use-is-dark-mode';
import { ThemeMode } from '@/components/theme-mode/ThemeMode';
import Image from 'next/image';
import { useRouter } from 'next/router';
import type { DocsThemeConfig } from 'nextra-theme-docs';
Expand All @@ -17,11 +17,13 @@ const config: DocsThemeConfig = {
};
}
},
logo: function useLogo() {
const isDarkMode = useIsDarkMode();

return <Image src={isDarkMode ? '/logo-white.png' : '/logo.png'} alt="logo" width={120} height={48} priority />;
},
logo: (
<ThemeMode>
{theme => (
<Image src={theme === 'dark' ? '/logo-white.png' : '/logo.png'} alt="logo" width="120" height="48" priority />
)}
</ThemeMode>
),
head: function useHead() {
const { title } = useConfig();

Expand Down Expand Up @@ -97,32 +99,32 @@ const config: DocsThemeConfig = {
toggleButton: true,
},
footer: {
text: function useText() {
const isDarkMode = useIsDarkMode();

return (
<div className="flex w-full flex-col items-center sm:items-start">
<div>
<a
className="flex items-center gap-1 text-current"
target="_blank"
rel="noopener noreferrer"
title="toss homepage"
href="https://toss.im"
>
<span>Powered by</span>
<Image
src={isDarkMode ? '/toss-logo-white.png' : '/toss-logo-gray.png'}
alt="Toss"
width="64"
height="32"
/>
</a>
</div>
<p className="mt-6 text-xs">© {new Date().getFullYear()} Viva Republica, Inc.</p>
text: (
<div className="flex w-full flex-col items-center sm:items-start">
<div>
<a
className="flex items-center gap-1 text-current"
target="_blank"
rel="noopener noreferrer"
title="toss homepage"
href="https://toss.im"
>
<span>Powered by</span>
<ThemeMode>
{theme => (
<Image
src={theme === 'dark' ? '/toss-logo-white.png' : '/toss-logo-gray.png'}
alt="Toss"
width="64"
height="32"
/>
)}
</ThemeMode>
</a>
</div>
);
},
<p className="mt-6 text-xs">© {new Date().getFullYear()} Viva Republica, Inc.</p>
</div>
),
},
toc: {
backToTop: true,
Expand Down

0 comments on commit 1b62156

Please sign in to comment.