Skip to content

Commit 28f1ae2

Browse files
committed
feat: scorll to top: 0 after collapse all
1 parent 0f0996c commit 28f1ae2

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

packages/nextra-editor/src/components/sidebar/index.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function Sidebar({
5151

5252
const anchors = useMemo(() => headings.filter((v) => v.depth === 2), [headings])
5353
const sidebarRef = useRef<HTMLDivElement>(null)
54-
const containerRef = useRef<HTMLDivElement>(null)
54+
const sortableTreeRef = useRef<HTMLDivElement>(null)
5555
const mounted = useMounted()
5656

5757
useEffect(() => {
@@ -63,15 +63,14 @@ export function Sidebar({
6363
}, [menu])
6464

6565
useEffect(() => {
66-
const activeElement = sidebarRef.current?.querySelector('li.active')
67-
66+
const activeElement = sortableTreeRef.current?.querySelector('li.active')
6867
if (activeElement && (window.innerWidth > 767 || menu)) {
6968
const scroll = () => {
7069
scrollIntoView(activeElement, {
7170
block: 'center',
7271
inline: 'center',
7372
scrollMode: 'always',
74-
boundary: containerRef.current,
73+
boundary: sidebarRef.current,
7574
})
7675
}
7776
if (menu) {
@@ -93,6 +92,7 @@ export function Sidebar({
9392
setFocusedItem(null)
9493
setIsFolding(false)
9594
setMenu(false)
95+
sortableTreeRef.current?.scrollTo({ top: 0, behavior: 'smooth' })
9696
}, [isFolding])
9797

9898
const hasI18n = config.i18n.length > 0
@@ -136,7 +136,7 @@ export function Sidebar({
136136
? 'max-md:[transform:translate3d(0,0,0)]'
137137
: 'max-md:[transform:translate3d(0,-100%,0)]',
138138
)}
139-
ref={containerRef}
139+
ref={sidebarRef}
140140
>
141141
<div className="nx-px-4 nx-pt-4 md:nx-hidden">
142142
{renderComponent(config.search.component, {
@@ -147,7 +147,7 @@ export function Sidebar({
147147
items={sortableItems}
148148
onItemsChanged={setSortableItems}
149149
showSidebar={showSidebar}
150-
sidebarRef={sidebarRef}
150+
sidebarRef={sortableTreeRef}
151151
/>
152152
{mounted && window.innerWidth < 768 && (
153153
<Menu

packages/nextra-editor/src/components/sidebar/sidebar-controller/control-icon.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef, useState } from 'react'
1+
import { useRef, useState } from 'react'
22
import { useSidebar } from '@/contexts/sidebar'
33
import { NewFolderIcon } from '@/nextra/icons/new-folder'
44
import { useUrlSlug } from '@/hooks/use-url-slug'

packages/nextra-editor/src/components/sidebar/sidebar-controller/control-input.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ function ControlInput({ type }: Props): ReactElement {
3434
setError(`${typeName} 이름에 사용해서는 안 되는 기호가 포함되어 있습니다.`)
3535
} else if (title === '') {
3636
setError(`${typeName} 이름을 입력해야 합니다.`)
37+
} else if (title.startsWith(' ')) {
38+
setError(`${typeName} 이름은 공백으로 시작할 수 없습니다.`)
3739
} else {
3840
setError('')
3941
}
@@ -53,7 +55,12 @@ function ControlInput({ type }: Props): ReactElement {
5355
}
5456

5557
const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
56-
if (e.key === ' ') {
58+
if (e.key === ' ' && title === null) {
59+
e.preventDefault()
60+
return
61+
}
62+
63+
if (e.key === ' ' && title !== null) {
5764
e.preventDefault()
5865
setTitle((title) => `${title} `)
5966
return

0 commit comments

Comments
 (0)