Skip to content

Commit

Permalink
feat: add nav
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dedance committed Oct 23, 2023
1 parent 8939c89 commit 64a0bae
Show file tree
Hide file tree
Showing 12 changed files with 734 additions and 33 deletions.
6 changes: 6 additions & 0 deletions e2e/playground/basic/docs/rpress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ import { defineConfig } from 'r-press/dist'
export default defineConfig({
title: 'RPress',
description: 'RPress Playground',
themeConfig: {
nav: [
{ text: "主页", link: "/" },
{ text: "指南", link: "/guide/" },
],
},
})
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"devDependencies": {
"@commitlint/cli": "^17.7.2",
"@commitlint/config-conventional": "^17.7.0",
"@iconify-json/carbon": "^1.1.21",
"@playwright/test": "^1.26.1",
"@types/fs-extra": "^11.0.2",
"@types/hast": "^2.3.4",
Expand All @@ -43,6 +44,7 @@
"execa": "5.1.1",
"lint-staged": "^14.0.1",
"prettier": "^3.0.3",
"sass": "^1.69.4",
"simple-git-hooks": "^2.9.0",
"tsup": "^7.2.0",
"tsx": "^3.13.0",
Expand Down
50 changes: 37 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions src/node/plugin-r-press/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { relative } from 'path'
import { SiteConfig } from 'shared/types'
import { Plugin } from 'vite'
import { relative, join } from 'path'
import { ROOT } from 'node/constant'
import type { SiteConfig } from 'shared/types'
import type { Plugin } from 'vite'

const SITE_DATA_ID = 'rpress:site-data'

Expand All @@ -10,6 +11,21 @@ export function pluginConfig(
): Plugin {
return {
name: 'r-press:site-data',
config() {
return {
root: ROOT,
resolve: {
alias: {
'@runtime': join(ROOT, 'src', 'runtime', 'index.ts'),
},
},
css: {
modules: {
localsConvention: 'camelCaseOnly',
},
},
}
},
resolveId(id) {
if (id === SITE_DATA_ID) {
return '\0' + SITE_DATA_ID
Expand Down
8 changes: 8 additions & 0 deletions src/node/unocssOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { presetAttributify, presetWind, presetIcons } from 'unocss'

const options: VitePluginConfig = {
presets: [presetAttributify(), presetWind({}), presetIcons()],
rules: [
[
/^divider-(\w+)$/,
([, w]) => ({
[`border-${w}`]: '1px solid var(--island-c-divider-light)',
}),
],
],
}

export default options
23 changes: 8 additions & 15 deletions src/runtime/theme-default/Layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { usePageData } from '../../'
import { Nav } from '../components/Nav'
import '../styles/base.css'
import '../styles/vars.css'
import 'uno.css'

export function Layout() {
const pageData = usePageData()
// 获取 pageType
const { pageType } = pageData
// 根据 pageType 分发不同的页面内容
const getContent = () => {
if (pageType === 'home') {
return <div>Home 页面</div>
} else if (pageType === 'doc') {
return <div>正文页面</div>
} else {
return <div>404 页面</div>
}
}
return <div>{getContent()}</div>
return (
<div>
<Nav />
</div>
)
}
20 changes: 20 additions & 0 deletions src/runtime/theme-default/components/Nav/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.link:hover {
color: var(--island-c-brand);
transition: color 0.2s;
}

.link {
display: block;
font-size: 14px;
font-weight: 500;
}

.social-link-icon {
display: flex;
fill: currentColor;
transition: color 0.2s;
color: var(--island-c-text-2);
&:hover {
color: var(--island-c-text-1);
}
}
55 changes: 55 additions & 0 deletions src/runtime/theme-default/components/Nav/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { usePageData } from '../../../'
import styles from './index.module.scss'
import type { NavItemWithLink } from 'shared/types'

export function MenuItem(item: NavItemWithLink) {
return (
<div className="text-sm font-medium mx-3">
<a href={item.link} className={styles.link}>
{item.text}
</a>
</div>
)
}

export function Nav() {
const { siteData } = usePageData()
const nav = siteData.themeConfig.nav || []
return (
// {/* 视频中包含 relative="~",属于无效代码,需要去掉 */}
<header fixed="~" pos="t-0 l-0" w="full">
<div
flex="~"
items="center"
justify="between"
// {/* divider-bottom 为自定义规则*/}
className="px-8 h-14 divider-bottom"
>
<div>
<a
href="/"
hover="opacity-60"
className="w-full h-full text-1rem font-semibold flex items-center"
>
Island.js
</a>
</div>
<div flex="~">
{/* 普通菜单 */}
<div flex="~">
{nav.map((item) => (
<MenuItem {...item} key={item.text} />
))}
</div>
{/* 白天/夜间模式切换 */}
{/* 相关链接 */}
<div className={styles.socialLinkIcon} ml="2">
<a href="/">
<div className="i-carbon-logo-github w-5 h-5 fill-current"></div>
</a>
</div>
</div>
</div>
</header>
)
}
Loading

0 comments on commit 64a0bae

Please sign in to comment.