Skip to content

Commit 645b375

Browse files
committed
feat: clean up layout code
1 parent 793d4f0 commit 645b375

File tree

12 files changed

+93
-67
lines changed

12 files changed

+93
-67
lines changed

packages/markdown-editor/css/styles.css

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ body {
2727
@apply nx-max-h-screen nx-w-full nx-bg-white dark:nx-bg-dark dark:nx-text-gray-100;
2828
}
2929

30+
#__next {
31+
@apply nx-h-screen nx-overflow-hidden;
32+
}
33+
3034
a,
3135
summary,
3236
button,
@@ -65,15 +69,15 @@ summary {
6569
.nextra-sidebar-container {
6670
@apply nx-pt-[6.5rem];
6771
}
68-
&.nextra-nav-container {
72+
&.nextra-header-container {
6973
@apply nx-top-10 md:nx-top-0;
7074
}
7175
}
7276
.nextra-banner-hidden {
7377
.nextra-banner-container ~ div .nextra-sidebar-container {
7478
@apply nx-pt-16;
7579
}
76-
.nextra-nav-container {
80+
.nextra-header-container {
7781
@apply !nx-top-0;
7882
}
7983
}
@@ -120,7 +124,7 @@ article details > summary {
120124
.nextra-search ul {
121125
@apply nx-bg-white/70 nx-backdrop-blur-lg dark:nx-bg-dark/80;
122126
}
123-
.nextra-nav-container-blur {
127+
.nextra-header-container-blur {
124128
@apply nx-bg-white/[.85] nx-backdrop-blur-md dark:!nx-bg-dark/80;
125129
}
126130
}

packages/markdown-editor/src/components/deployButton.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ function DeployButton({}) {
3232
type="button"
3333
onClick={onClick}
3434
className={cn(
35-
'nx-select-none',
35+
'nx-z-20 nx-flex nx-select-none nx-items-center nx-transition-opacity',
3636
'nx-rounded nx-bg-white nx-px-2 nx-py-1.5 nx-font-mono nx-text-[16px] nx-font-medium nx-text-gray-500',
3737
'nx-border dark:nx-border-gray-100 dark:nx-bg-dark/50 dark:nx-text-gray-100',
3838
'contrast-more:nx-border-current contrast-more:nx-text-current contrast-more:dark:nx-border-current',
3939
'hover:nx-shadow-md:hover nx-shadow-gray-100 hover:nx-border-gray-300 hover:nx-bg-slate-50 hover:nx-shadow-gray-100',
4040
'dark:hover:nx-border-gray-50 dark:hover:nx-bg-neutral-900 dark:hover:nx-shadow-none',
41-
'nx-items-center nx-transition-opacity',
42-
'nx-z-20 nx-flex',
4341
isDeploying
4442
? 'nx-translate-y-[0px] nx-cursor-not-allowed nx-bg-gray-400/20 nx-opacity-50 dark:nx-bg-neutral-800'
4543
: 'nx-cursor-pointer active:nx-translate-y-[1px] active:nx-bg-gray-400/20 dark:active:nx-bg-neutral-800',

packages/markdown-editor/src/components/header.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export function Header({ flatDirectories, items }: NavBarProps): ReactElement {
7575
const { menu, setMenu } = useMenu()
7676

7777
return (
78-
<div className="nextra-nav-container nx-w-full">
78+
<div className="nextra-header-container nx-w-full">
7979
<div
8080
className={cn(
81-
'nextra-nav-container-blur',
81+
'nextra-header-container-blur',
8282
'nx-z-[-1] nx-h-full nx-w-full nx-bg-white dark:nx-bg-dark',
8383
'nx-shadow-[0_2px_4px_rgba(0,0,0,.02),0_1px_0_rgba(0,0,0,.06)] dark:nx-shadow-[0_-1px_0_rgba(255,255,255,.1)_inset]',
8484
'contrast-more:nx-shadow-[0_0_0_1px_#000] contrast-more:dark:nx-shadow-[0_0_0_1px_#fff]',

packages/markdown-editor/src/components/markdown-editor/hooks/useCodemirror.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getDefaultExtensions } from './../lib/getDefaultExtensions'
22
import { Annotation, EditorState, StateEffect } from '@codemirror/state'
33
import { EditorView, ViewUpdate } from '@codemirror/view'
44
import { useTheme } from 'next-themes'
5-
import { RefObject, useEffect, useState } from 'react'
5+
import { ForwardedRef, RefObject, useEffect, useState } from 'react'
66
import { useMarkdownEditor } from '../../../contexts/markdown-editor'
77
import { getEditorStat } from '../lib/getEditorStat'
88
import { hyperLink } from '@uiw/codemirror-extensions-hyper-link'
@@ -24,7 +24,7 @@ type Config = {
2424

2525
const External = Annotation.define<boolean>()
2626

27-
export const useCodemirror = (container: RefObject<HTMLElement>, config: Config = {}) => {
27+
export const useCodemirror = (container: ForwardedRef<HTMLDivElement>, config: Config = {}) => {
2828
const { theme: currentTheme } = useTheme()
2929
const { value, setValue, setStat } = useMarkdownEditor()
3030
const [state, setState] = useState<EditorState | null>(null)
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
import { useRef } from 'react'
1+
import { forwardRef, useEffect } from 'react'
22
import { useCodemirror } from './hooks/useCodemirror'
33
import { Toolbar } from './toolbar'
44

5-
export const MarkdownEditor = () => {
6-
const container = useRef<HTMLDivElement>(null)
7-
const containerHeight = 'calc(100vh - 64px)'
5+
export const MarkdownEditor = forwardRef<HTMLDivElement>(({}, container) => {
6+
const containerHeight = 'calc(100vh - (var(--nextra-navbar-height)))'
87
const { state, view } = useCodemirror(container, {
98
autoFocus: true,
109
minHeight: '100%',
1110
maxHeight: '100%',
1211
})
1312

1413
const onClick = () => {
14+
console.log('clicked!')
1515
view?.focus()
1616
}
1717

18+
useEffect(() => {
19+
onClick()
20+
}, [])
21+
1822
return (
1923
<>
20-
<Toolbar state={state} view={view} container={container} />
24+
<Toolbar state={state} view={view} />
2125
<div onClick={onClick}>
2226
<div
2327
ref={container}
@@ -28,4 +32,4 @@ export const MarkdownEditor = () => {
2832
</div>
2933
</>
3034
)
31-
}
35+
})

packages/markdown-editor/src/components/markdown-editor/toolbar/toolbar.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { useUpload } from '@/hooks/use-upload'
1111
type Props = {
1212
state: EditorState | null
1313
view: EditorView | null
14-
container: RefObject<HTMLElement>
1514
}
1615

1716
const seperator = {
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
import cn from 'clsx'
22
import { useConfig } from '@/contexts'
3-
import type { ReactElement } from 'react'
3+
import { forwardRef, type ReactElement } from 'react'
44
import { MDXRemote } from 'next-mdx-remote'
55
import { getComponents } from '@/mdx-components'
66
import { useMarkdownEditor } from '@/contexts/markdown-editor'
77

88
interface MarkdownPreviewProps {}
99

10-
export const MarkdownPreview = ({}: MarkdownPreviewProps): ReactElement => {
11-
const config = useConfig()
12-
const { mdxSource } = useMarkdownEditor()
10+
export const MarkdownPreview = forwardRef<HTMLDivElement, MarkdownPreviewProps>(
11+
({}, ref): ReactElement => {
12+
const config = useConfig()
13+
const { mdxSource } = useMarkdownEditor()
1314

14-
if (!mdxSource) {
15-
return <div>Mdx source Loading...</div>
16-
}
15+
if (!mdxSource) {
16+
return <div>Mdx source Loading...</div>
17+
}
1718

18-
const content = (
19-
<MDXRemote
20-
compiledSource={mdxSource.compiledSource}
21-
frontmatter={mdxSource.frontmatter}
22-
scope={mdxSource.scope}
23-
components={getComponents({
24-
isRawLayout: false,
25-
components: config.components,
26-
})}
27-
/>
28-
)
29-
30-
const body = config.main?.({ children: content }) || content
31-
return (
32-
<article
33-
className={cn('nextra-scrollbar nx-mt-[-24px] nx-h-screen nx-overflow-y-auto nx-break-words')}
34-
>
35-
<main className="nx-mt-6 nx-w-full nx-min-w-0 nx-max-w-6xl nx-px-6 nx-pt-6">{body}</main>
36-
</article>
37-
)
38-
}
19+
return (
20+
<div
21+
ref={ref}
22+
className={cn(
23+
'nextra-scrollbar nx-h-screen nx-overflow-y-auto nx-break-words nx-pb-16',
24+
)}
25+
>
26+
<main className="nx-mt-6 nx-w-full nx-min-w-0 nx-max-w-6xl nx-px-6 nx-pt-6">
27+
<MDXRemote
28+
compiledSource={mdxSource.compiledSource}
29+
frontmatter={mdxSource.frontmatter}
30+
scope={mdxSource.scope}
31+
components={getComponents({
32+
isRawLayout: false,
33+
components: config.components,
34+
})}
35+
/>
36+
</main>
37+
</div>
38+
)
39+
},
40+
)

packages/markdown-editor/src/components/modal/modal-wrapper.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@ type Props = {
77
onOverlayClick: VoidFunction
88
}
99

10-
const style = {
11-
backdrop: cn(
12-
'nx-fixed nx-inset-0 nx-w-[100vw] nx-h-[100svh] nx-bg-[var(--opaque-layer)] nx-z-10 nx-flex nx-items-center nx-justify-center',
13-
),
14-
}
15-
1610
export function ModalWrapper({ children, isVisible, onOverlayClick }: Props) {
1711
const [isClosed, setIsClosed] = useState(false)
1812
const backdropRef = useRef<HTMLDivElement>(null)
1913
const timeoutId = useRef<NodeJS.Timeout | null>(null)
2014

21-
const handleClickBackdrop: React.MouseEventHandler<HTMLDivElement> = useCallback(
22-
(ev) => {
23-
if (ev.target === backdropRef.current) {
15+
const onBackdropClick: React.MouseEventHandler<HTMLDivElement> = useCallback(
16+
(event) => {
17+
if (event.target === backdropRef.current) {
2418
onOverlayClick?.()
2519
}
2620
},
@@ -46,8 +40,11 @@ export function ModalWrapper({ children, isVisible, onOverlayClick }: Props) {
4640
return (
4741
<div
4842
ref={backdropRef}
49-
className={cn(style.backdrop, isVisible ? 'fadeIn' : 'fadeOut')}
50-
onClick={handleClickBackdrop}
43+
onClick={onBackdropClick}
44+
className={cn(
45+
'nx-fixed nx-inset-0 nx-z-10 nx-flex nx-h-[100svh] nx-w-[100vw] nx-items-center nx-justify-center nx-bg-[var(--opaque-layer)]',
46+
isVisible ? 'fadeIn' : 'fadeOut',
47+
)}
5148
>
5249
{children}
5350
</div>
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export { InnerLayout } from './lnner-layout'
21
export { Layout } from './layout'

packages/markdown-editor/src/layouts/layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MarkdownEditorProvider } from '@/contexts/markdown-editor'
33
import { SidebarProvider } from '@/contexts/sidebar'
44
import type { NextraThemeLayoutProps } from '@/nextra/types'
55
import type { ReactElement } from 'react'
6-
import { InnerLayout } from './lnner-layout'
6+
import { Main } from './main'
77
import { ModalProvider } from '@/contexts/modal'
88
import { Potals } from '@/components/potals'
99

@@ -18,7 +18,7 @@ export function Layout({ children, editorValue, ...context }: NextraDocLayoutPro
1818
<SidebarProvider>
1919
<MarkdownEditorProvider value={{ editorValue }}>
2020
<Potals />
21-
<InnerLayout {...context.pageOpts}>{children}</InnerLayout>
21+
<Main {...context.pageOpts}>{children}</Main>
2222
</MarkdownEditorProvider>
2323
</SidebarProvider>
2424
</ModalProvider>

packages/markdown-editor/src/layouts/lnner-layout.tsx renamed to packages/markdown-editor/src/layouts/main.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,31 @@ import { ActiveAnchorProvider } from '@/contexts'
44
import { useFSRoute } from '@/nextra/hooks'
55
import { normalizePages } from '@/nextra/normalize-pages'
66
import type { PageOpts } from '@/nextra/types'
7-
import { useMemo, type ReactElement, type ReactNode } from 'react'
7+
import { useEffect, useMemo, useRef, useState, type ReactElement, type ReactNode } from 'react'
88
import { Banner, Head, Header, Sidebar } from '@/components'
99
import { MarkdownPreview } from '@/components/markdown-preview'
1010
import { MarkdownEditor } from '@/components/markdown-editor'
1111

12-
type InnerLayoutProps = PageOpts & {
12+
type MainProps = PageOpts & {
1313
children: ReactNode
1414
}
1515

16-
export const InnerLayout = ({ frontMatter, headings, pageMap }: InnerLayoutProps): ReactElement => {
16+
export const Main = ({ frontMatter, headings, pageMap }: MainProps): ReactElement => {
1717
const fsPath = useFSRoute()
18+
const editorRef = useRef<HTMLDivElement>(null)
19+
const previewRef = useRef<HTMLDivElement>(null)
20+
21+
const rerender = useState({})[1]
22+
23+
useEffect(() => {
24+
const trigger = () => rerender({})
25+
trigger()
26+
}, [])
27+
28+
useEffect(() => {
29+
console.log('editorRef:', editorRef)
30+
console.log('previewRef:', previewRef)
31+
}, [editorRef, previewRef])
1832

1933
const { activeThemeContext, docsDirectories, flatDirectories, directories, topLevelNavbarItems } =
2034
useMemo(
@@ -30,9 +44,15 @@ export const InnerLayout = ({ frontMatter, headings, pageMap }: InnerLayoutProps
3044

3145
const themeContext = { ...activeThemeContext, ...frontMatter }
3246
const direction = 'ltr'
47+
const mainHeight = 'calc(100vh - (var(--nextra-navbar-height)))'
3348

3449
return (
35-
<div dir={direction} className={cn('nx-relative nx-flex nx-flex-wrap')}>
50+
<div
51+
dir={direction}
52+
className={cn(
53+
'nx-relative nx-flex nx-h-screen nx-flex-col nx-flex-nowrap nx-overflow-hidden',
54+
)}
55+
>
3656
<script
3757
dangerouslySetInnerHTML={{
3858
__html: `document.documentElement.setAttribute('dir','${direction}')`,
@@ -41,7 +61,10 @@ export const InnerLayout = ({ frontMatter, headings, pageMap }: InnerLayoutProps
4161
<Head />
4262
<Banner />
4363
<Header flatDirectories={flatDirectories} items={topLevelNavbarItems} />
44-
<div className={cn('nextra-main', 'nx-mx-auto nx-flex nx-w-full')}>
64+
<div
65+
className={cn('nextra-main', 'nx-mx-auto nx-flex nx-w-full')}
66+
style={{ height: mainHeight }}
67+
>
4568
<ActiveAnchorProvider>
4669
<Sidebar
4770
docsDirectories={docsDirectories}
@@ -53,10 +76,10 @@ export const InnerLayout = ({ frontMatter, headings, pageMap }: InnerLayoutProps
5376
/>
5477
<div className={cn('nx-flex nx-overflow-hidden')} style={{ width: 'calc(100% - 320px)' }}>
5578
<div className={cn('nextra-editor-container nx-w-1/2')}>
56-
<MarkdownEditor />
79+
<MarkdownEditor ref={editorRef} />
5780
</div>
5881
<div className={cn('nextra-preview-container nx-w-1/2')}>
59-
<MarkdownPreview />
82+
<MarkdownPreview ref={previewRef} />
6083
</div>
6184
</div>
6285
</ActiveAnchorProvider>

packages/markdown-editor/style.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)