@rspress/core 1.0.0-beta.3 → 1.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/runtime/App.js +24 -4
- package/dist/runtime/index.d.ts +2 -2
- package/dist/runtime/index.js +2 -2
- package/dist/runtime/tsconfig.json +2 -1
- package/dist/runtime/utils.d.ts +1 -0
- package/dist/runtime/utils.js +7 -1
- package/dist/theme/bundle.css +132 -125
- package/dist/theme/bundle.d.ts +5 -2
- package/dist/theme/bundle.js +898 -910
- package/dist/theme/bundle.js.map +1 -1
- package/package.json +6 -6
- package/src/runtime/App.tsx +21 -1
- package/src/runtime/index.ts +2 -1
- package/src/runtime/tsconfig.json +2 -1
- package/src/runtime/utils.ts +9 -0
- package/src/theme-default/components/Aside/index.css +0 -1
- package/src/theme-default/components/Aside/index.tsx +7 -8
- package/src/theme-default/components/DocFooter/index.tsx +4 -1
- package/src/theme-default/components/HomeHero/index.module.scss +6 -0
- package/src/theme-default/components/HomeHero/index.tsx +8 -2
- package/src/theme-default/components/LocalSideBar/index.tsx +5 -15
- package/src/theme-default/components/Nav/NavBarTitle.tsx +42 -0
- package/src/theme-default/components/Nav/index.module.scss +18 -4
- package/src/theme-default/components/Nav/index.tsx +14 -54
- package/src/theme-default/components/NavHambmger/index.tsx +1 -3
- package/src/theme-default/components/NavScreen/index.tsx +31 -18
- package/src/theme-default/components/Sidebar/SidebarGroup.tsx +1 -1
- package/src/theme-default/components/Sidebar/index.module.scss +20 -11
- package/src/theme-default/components/Sidebar/index.tsx +25 -38
- package/src/theme-default/layout/DocLayout/index.module.scss +7 -3
- package/src/theme-default/layout/DocLayout/index.tsx +6 -19
- package/src/theme-default/layout/HomeLayout/index.tsx +1 -1
- package/src/theme-default/layout/Layout/index.tsx +3 -25
- package/src/theme-default/logic/index.ts +1 -0
- package/src/theme-default/logic/sideEffects.ts +8 -7
- package/src/theme-default/logic/useHiddenNav.ts +55 -0
- package/src/theme-default/styles/code.css +0 -1
|
@@ -2,13 +2,19 @@ import React, { useEffect, useState } from 'react';
|
|
|
2
2
|
import {
|
|
3
3
|
NormalizedSidebarGroup,
|
|
4
4
|
SidebarItem as ISidebarItem,
|
|
5
|
+
normalizeSlash,
|
|
5
6
|
} from '@rspress/shared';
|
|
6
|
-
import { matchRoutes } from 'react-router-dom';
|
|
7
7
|
import { routes } from 'virtual-routes';
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
isActive,
|
|
10
|
+
useDisableNav,
|
|
11
|
+
useLocaleSiteData,
|
|
12
|
+
useSidebarData,
|
|
13
|
+
} from '../../logic';
|
|
14
|
+
import { NavBarTitle } from '../Nav/NavBarTitle';
|
|
9
15
|
import styles from './index.module.scss';
|
|
10
16
|
import { SidebarItem } from './SidebarItem';
|
|
11
|
-
import {
|
|
17
|
+
import { matchRoutes, useLocation, removeBase } from '@/runtime';
|
|
12
18
|
|
|
13
19
|
export interface SidebarItemProps {
|
|
14
20
|
id: string;
|
|
@@ -24,9 +30,6 @@ export interface SidebarItemProps {
|
|
|
24
30
|
|
|
25
31
|
interface Props {
|
|
26
32
|
isSidebarOpen?: boolean;
|
|
27
|
-
pathname: string;
|
|
28
|
-
langRoutePrefix: string;
|
|
29
|
-
sidebarData: (NormalizedSidebarGroup | ISidebarItem)[];
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
export const highlightTitleStyle = {
|
|
@@ -42,24 +45,19 @@ export let matchCache: WeakMap<NormalizedSidebarGroup | ISidebarItem, boolean> =
|
|
|
42
45
|
new WeakMap();
|
|
43
46
|
|
|
44
47
|
export function SideBar(props: Props) {
|
|
45
|
-
const {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
siteData: { themeConfig },
|
|
53
|
-
page: { frontmatter },
|
|
54
|
-
} = usePageData();
|
|
55
|
-
const hideNavbar =
|
|
56
|
-
frontmatter?.hideNavbar ?? themeConfig?.hideNavbar ?? false;
|
|
48
|
+
const { isSidebarOpen } = props;
|
|
49
|
+
const { items: rawSidebarData } = useSidebarData();
|
|
50
|
+
const localesData = useLocaleSiteData();
|
|
51
|
+
const { pathname: rawPathname } = useLocation();
|
|
52
|
+
|
|
53
|
+
const langRoutePrefix = normalizeSlash(localesData.langRoutePrefix || '');
|
|
54
|
+
const hideNavbar = useDisableNav();
|
|
57
55
|
const [sidebarData, setSidebarData] = useState<
|
|
58
56
|
(ISidebarItem | NormalizedSidebarGroup)[]
|
|
59
57
|
>(rawSidebarData.filter(Boolean).flat());
|
|
60
58
|
const pathname = decodeURIComponent(rawPathname);
|
|
61
59
|
useEffect(() => {
|
|
62
|
-
if (
|
|
60
|
+
if (rawSidebarData === sidebarData) {
|
|
63
61
|
return;
|
|
64
62
|
}
|
|
65
63
|
// 1. Update sidebarData when pathname changes
|
|
@@ -92,10 +90,10 @@ export function SideBar(props: Props) {
|
|
|
92
90
|
}
|
|
93
91
|
}
|
|
94
92
|
};
|
|
95
|
-
const newSidebarData =
|
|
93
|
+
const newSidebarData = rawSidebarData.filter(Boolean).flat();
|
|
96
94
|
newSidebarData.forEach(traverse);
|
|
97
95
|
setSidebarData(newSidebarData);
|
|
98
|
-
}, [
|
|
96
|
+
}, [rawSidebarData, pathname]);
|
|
99
97
|
|
|
100
98
|
const removeLangPrefix = (path: string) => {
|
|
101
99
|
return path.replace(langRoutePrefix, '');
|
|
@@ -118,10 +116,13 @@ export function SideBar(props: Props) {
|
|
|
118
116
|
className={`${styles.sidebar} rspress-sidebar ${
|
|
119
117
|
isSidebarOpen ? styles.open : ''
|
|
120
118
|
}`}
|
|
121
|
-
style={{
|
|
122
|
-
...(hideNavbar ? { marginTop: 0 } : {}),
|
|
123
|
-
}}
|
|
124
119
|
>
|
|
120
|
+
{hideNavbar ? null : (
|
|
121
|
+
<div className={styles.navTitleMask}>
|
|
122
|
+
<NavBarTitle />
|
|
123
|
+
</div>
|
|
124
|
+
)}
|
|
125
|
+
|
|
125
126
|
<div className={`mt-1 ${styles.sidebarContent}`}>
|
|
126
127
|
<div
|
|
127
128
|
className="rspress-scrollbar"
|
|
@@ -150,20 +151,6 @@ export function SideBar(props: Props) {
|
|
|
150
151
|
</nav>
|
|
151
152
|
</div>
|
|
152
153
|
</div>
|
|
153
|
-
{/* SwitchAppearance at the bottom of sidebar */}
|
|
154
|
-
{/* <div
|
|
155
|
-
className="border-t border-solid border-gray-200 dark:border-gray-500 absolute"
|
|
156
|
-
style={{
|
|
157
|
-
left: '1.8rem',
|
|
158
|
-
width: 'calc(100% - 3.6rem)',
|
|
159
|
-
}}
|
|
160
|
-
>
|
|
161
|
-
<div className="mt-2 flex-center">
|
|
162
|
-
<NoSSR>
|
|
163
|
-
<SwitchAppearance />
|
|
164
|
-
</NoSSR>
|
|
165
|
-
</div>
|
|
166
|
-
</div> */}
|
|
167
154
|
</aside>
|
|
168
155
|
);
|
|
169
156
|
}
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
overflow-y: auto;
|
|
9
9
|
scrollbar-width: none;
|
|
10
10
|
width: 0;
|
|
11
|
+
max-height: calc(100vh - (var(--rp-nav-height) + 32px));
|
|
12
|
+
overflow: 'scroll';
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
.aside-container::-webkit-scrollbar {
|
|
@@ -23,7 +25,6 @@
|
|
|
23
25
|
view-transition-name: none;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
|
|
27
28
|
@keyframes fade-out {
|
|
28
29
|
from {
|
|
29
30
|
opacity: 1;
|
|
@@ -67,16 +68,19 @@
|
|
|
67
68
|
.content {
|
|
68
69
|
padding: 36px 24px 72px 24px;
|
|
69
70
|
}
|
|
71
|
+
|
|
72
|
+
:global(.rspress-doc) {
|
|
73
|
+
view-transition-name: none;
|
|
74
|
+
}
|
|
70
75
|
}
|
|
71
76
|
|
|
72
77
|
@media (min-width: 960px) {
|
|
73
78
|
.docLayout {
|
|
74
79
|
width: 100%;
|
|
75
|
-
|
|
80
|
+
display: flex;
|
|
76
81
|
}
|
|
77
82
|
|
|
78
83
|
.content {
|
|
79
|
-
margin-left: var(--rp-sidebar-width);
|
|
80
84
|
width: calc(100% - var(--rp-sidebar-width));
|
|
81
85
|
padding: 48px 48px 72px 0;
|
|
82
86
|
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { useLocation } from 'react-router-dom';
|
|
2
1
|
import { useEffect, useState } from 'react';
|
|
3
2
|
import { MDXProvider } from '@mdx-js/react';
|
|
4
3
|
import { getCustomMDXComponent } from '@theme';
|
|
5
4
|
import { Aside } from '../../components/Aside';
|
|
6
5
|
import { DocFooter } from '../../components/DocFooter';
|
|
7
|
-
import {
|
|
6
|
+
import { useDisableNav, useLocaleSiteData } from '../../logic';
|
|
8
7
|
import { SideMenu } from '../../components/LocalSideBar';
|
|
9
8
|
import { Overview } from '../../components/Overview';
|
|
10
9
|
import { TabDataContext } from '../../logic/TabDataContext';
|
|
11
10
|
import styles from './index.module.scss';
|
|
12
|
-
import { Content, usePageData
|
|
11
|
+
import { Content, usePageData } from '@/runtime';
|
|
13
12
|
|
|
14
13
|
export interface DocLayoutProps {
|
|
15
14
|
beforeDocFooter?: React.ReactNode;
|
|
@@ -29,12 +28,8 @@ export function DocLayout(props: DocLayoutProps) {
|
|
|
29
28
|
const { themeConfig } = siteData;
|
|
30
29
|
const localesData = useLocaleSiteData();
|
|
31
30
|
const sidebar = localesData.sidebar || {};
|
|
32
|
-
const { pathname } = useLocation();
|
|
33
31
|
|
|
34
|
-
const
|
|
35
|
-
const langRoutePrefix = normalizeSlash(localesData.langRoutePrefix || '');
|
|
36
|
-
const hideNavbar =
|
|
37
|
-
frontmatter?.hideNavbar ?? themeConfig?.hideNavbar ?? false;
|
|
32
|
+
const disableNavbar = useDisableNav();
|
|
38
33
|
// siderbar Priority
|
|
39
34
|
// 1. frontmatter.sidebar
|
|
40
35
|
// 2. themeConfig.locales.sidebar
|
|
@@ -65,17 +60,11 @@ export function DocLayout(props: DocLayoutProps) {
|
|
|
65
60
|
<div
|
|
66
61
|
className={`${styles.docLayout} pt-0`}
|
|
67
62
|
style={{
|
|
68
|
-
...(
|
|
63
|
+
...(disableNavbar ? { marginTop: 0 } : {}),
|
|
69
64
|
}}
|
|
70
65
|
>
|
|
71
66
|
{beforeDoc}
|
|
72
|
-
{hasSidebar ?
|
|
73
|
-
<SideMenu
|
|
74
|
-
pathname={pathname}
|
|
75
|
-
langRoutePrefix={langRoutePrefix}
|
|
76
|
-
sidebarData={sidebarData}
|
|
77
|
-
/>
|
|
78
|
-
) : null}
|
|
67
|
+
{hasSidebar ? <SideMenu /> : null}
|
|
79
68
|
<div
|
|
80
69
|
className={`${styles.content} rspress-doc-container flex flex-shrink-0`}
|
|
81
70
|
>
|
|
@@ -103,9 +92,7 @@ export function DocLayout(props: DocLayoutProps) {
|
|
|
103
92
|
<div
|
|
104
93
|
className={styles.asideContainer}
|
|
105
94
|
style={{
|
|
106
|
-
|
|
107
|
-
overflow: 'scroll',
|
|
108
|
-
...(hideNavbar
|
|
95
|
+
...(disableNavbar
|
|
109
96
|
? {
|
|
110
97
|
marginTop: 0,
|
|
111
98
|
paddingTop: '32px',
|
|
@@ -3,12 +3,11 @@ import '../../styles';
|
|
|
3
3
|
import React, { useEffect } from 'react';
|
|
4
4
|
import { Helmet } from 'react-helmet-async';
|
|
5
5
|
import Theme, { Nav } from '@theme';
|
|
6
|
-
import globalComponents from 'virtual-global-components';
|
|
7
6
|
import { DocLayout, DocLayoutProps } from '../DocLayout';
|
|
8
7
|
import { HomeLayoutProps } from '../HomeLayout';
|
|
9
8
|
import type { NavProps } from '../../components/Nav';
|
|
10
9
|
import { usePageData, Content, removeBase, withBase } from '@/runtime';
|
|
11
|
-
import { useLocaleSiteData } from '@/theme-default/logic';
|
|
10
|
+
import { useDisableNav, useLocaleSiteData } from '@/theme-default/logic';
|
|
12
11
|
|
|
13
12
|
export type LayoutProps = {
|
|
14
13
|
top?: React.ReactNode;
|
|
@@ -48,7 +47,6 @@ export const Layout: React.FC<LayoutProps> = props => {
|
|
|
48
47
|
afterFeatures,
|
|
49
48
|
};
|
|
50
49
|
const { siteData, page } = usePageData();
|
|
51
|
-
const { themeConfig } = siteData;
|
|
52
50
|
const {
|
|
53
51
|
pageType,
|
|
54
52
|
lang: currentLang,
|
|
@@ -58,10 +56,8 @@ export const Layout: React.FC<LayoutProps> = props => {
|
|
|
58
56
|
} = page;
|
|
59
57
|
const localesData = useLocaleSiteData();
|
|
60
58
|
const defaultLang = siteData.lang || '';
|
|
61
|
-
|
|
59
|
+
const hideNavbar = useDisableNav();
|
|
62
60
|
// Always show sidebar by default
|
|
63
|
-
const hideNavbar =
|
|
64
|
-
!(frontmatter?.navbar ?? true) || (themeConfig?.hideNavbar ?? false);
|
|
65
61
|
// Priority: front matter title > h1 title
|
|
66
62
|
let title = (frontmatter?.title as string) ?? articleTitle;
|
|
67
63
|
const mainTitle = siteData.title || localesData.title;
|
|
@@ -140,6 +136,7 @@ export const Layout: React.FC<LayoutProps> = props => {
|
|
|
140
136
|
{description ? <meta name="description" content={description} /> : null}
|
|
141
137
|
</Helmet>
|
|
142
138
|
{top}
|
|
139
|
+
|
|
143
140
|
{pageType !== 'blank' && !hideNavbar && (
|
|
144
141
|
<Nav
|
|
145
142
|
beforeNavTitle={beforeNavTitle}
|
|
@@ -150,25 +147,6 @@ export const Layout: React.FC<LayoutProps> = props => {
|
|
|
150
147
|
|
|
151
148
|
<section>{getContentLayout()}</section>
|
|
152
149
|
{bottom}
|
|
153
|
-
{
|
|
154
|
-
// Global UI
|
|
155
|
-
globalComponents.map((componentInfo, index) => {
|
|
156
|
-
if (Array.isArray(componentInfo)) {
|
|
157
|
-
const [component, props] = componentInfo;
|
|
158
|
-
return React.createElement(component, {
|
|
159
|
-
// The component order is stable
|
|
160
|
-
// eslint-disable-next-line react/no-array-index-key
|
|
161
|
-
key: index,
|
|
162
|
-
...props,
|
|
163
|
-
});
|
|
164
|
-
} else {
|
|
165
|
-
return React.createElement(componentInfo, {
|
|
166
|
-
// eslint-disable-next-line react/no-array-index-key
|
|
167
|
-
key: index,
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
})
|
|
171
|
-
}
|
|
172
150
|
</div>
|
|
173
151
|
);
|
|
174
152
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { usePrevNextPage } from './usePrevNextPage';
|
|
2
2
|
export { useEditLink } from './useEditLink';
|
|
3
3
|
export { useSidebarData } from './useSidebarData';
|
|
4
|
+
export { useHiddenNav, useDisableNav } from './useHiddenNav';
|
|
4
5
|
export { useLocaleSiteData } from './useLocaleSiteData';
|
|
5
6
|
export { setup, bindingAsideScroll, scrollToTarget } from './sideEffects';
|
|
6
7
|
export { usePathUtils } from './usePathUtils';
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { throttle } from 'lodash-es';
|
|
2
2
|
import { inBrowser } from '@rspress/shared';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
const DEFAULT_NAV_HEIGHT = 72;
|
|
4
|
+
export const DEFAULT_NAV_HEIGHT = 72;
|
|
6
5
|
|
|
7
|
-
export function scrollToTarget(
|
|
6
|
+
export function scrollToTarget(
|
|
7
|
+
target: HTMLElement,
|
|
8
|
+
isSmooth: boolean,
|
|
9
|
+
fallbackHeight = DEFAULT_NAV_HEIGHT,
|
|
10
|
+
) {
|
|
8
11
|
const targetPadding = parseInt(
|
|
9
12
|
window.getComputedStyle(target).paddingTop,
|
|
10
13
|
10,
|
|
@@ -13,7 +16,7 @@ export function scrollToTarget(target: HTMLElement, isSmooth: boolean) {
|
|
|
13
16
|
const targetTop =
|
|
14
17
|
window.scrollY +
|
|
15
18
|
target.getBoundingClientRect().top -
|
|
16
|
-
|
|
19
|
+
fallbackHeight -
|
|
17
20
|
targetPadding;
|
|
18
21
|
// Only scroll smoothly in page header anchor
|
|
19
22
|
window.scrollTo({
|
|
@@ -89,7 +92,6 @@ export function bindingAsideScroll() {
|
|
|
89
92
|
document.documentElement.scrollHeight
|
|
90
93
|
);
|
|
91
94
|
}
|
|
92
|
-
const marker = document.getElementById('aside-marker');
|
|
93
95
|
const aside = document.getElementById('aside-container');
|
|
94
96
|
const links = Array.from(
|
|
95
97
|
document.querySelectorAll<HTMLAnchorElement>('.rspress-doc .header-anchor'),
|
|
@@ -103,8 +105,7 @@ export function bindingAsideScroll() {
|
|
|
103
105
|
const headers = Array.from(aside?.getElementsByTagName('a') || []).map(item =>
|
|
104
106
|
decodeURIComponent(item.hash),
|
|
105
107
|
);
|
|
106
|
-
if (
|
|
107
|
-
marker.style.opacity = '0';
|
|
108
|
+
if (!headers.length) {
|
|
108
109
|
return;
|
|
109
110
|
}
|
|
110
111
|
// Util function to set dom ref after determining the active link
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { throttle } from 'lodash-es';
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { useLocation, usePageData } from '@/runtime';
|
|
4
|
+
|
|
5
|
+
// The two hooks has the similar name, but they are quite different.
|
|
6
|
+
// `useDisableNav` is used to determine whether the navigation bar is disabled. It depends on both the frontmatter and the themeConfig.
|
|
7
|
+
// `useHiddenNav` is used to determine whether the navigation bar is hidden. It depends on the themeConfig and the scroll event.
|
|
8
|
+
|
|
9
|
+
export function useDisableNav() {
|
|
10
|
+
const {
|
|
11
|
+
siteData: { themeConfig },
|
|
12
|
+
page: { frontmatter = {} },
|
|
13
|
+
} = usePageData();
|
|
14
|
+
// Priority: frontmatter.navbar > themeConfig.hideNavbar
|
|
15
|
+
return !(frontmatter?.navbar ?? true) || themeConfig?.hideNavbar === 'always';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function useHiddenNav() {
|
|
19
|
+
const {
|
|
20
|
+
siteData: { themeConfig },
|
|
21
|
+
} = usePageData();
|
|
22
|
+
const hiddenBehaivor = themeConfig.hideNavbar ?? 'never';
|
|
23
|
+
const [hiddenNav, setHiddenNav] = useState(false);
|
|
24
|
+
const { pathname } = useLocation();
|
|
25
|
+
const lastScrollTop = useRef(0);
|
|
26
|
+
|
|
27
|
+
if (hiddenBehaivor === 'never') {
|
|
28
|
+
return false;
|
|
29
|
+
} else if (hiddenBehaivor === 'always') {
|
|
30
|
+
return true;
|
|
31
|
+
} else {
|
|
32
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
setHiddenNav(false);
|
|
35
|
+
const onScrollListen = throttle(() => {
|
|
36
|
+
const { scrollTop } = document.documentElement;
|
|
37
|
+
if (scrollTop === lastScrollTop.current) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const shouldHidden =
|
|
41
|
+
lastScrollTop.current > 0 && scrollTop - lastScrollTop.current > 0;
|
|
42
|
+
setHiddenNav(shouldHidden);
|
|
43
|
+
lastScrollTop.current = scrollTop <= 0 ? 0 : scrollTop;
|
|
44
|
+
}, 200);
|
|
45
|
+
|
|
46
|
+
window.addEventListener('mousewheel', onScrollListen);
|
|
47
|
+
|
|
48
|
+
return () => {
|
|
49
|
+
window.removeEventListener('mousewheel', onScrollListen);
|
|
50
|
+
};
|
|
51
|
+
}, [pathname]);
|
|
52
|
+
|
|
53
|
+
return hiddenNav;
|
|
54
|
+
}
|
|
55
|
+
}
|