diff --git a/components/ArticleNavigation/index.jsx b/components/ArticleNavigation/index.jsx new file mode 100644 index 0000000..180c890 --- /dev/null +++ b/components/ArticleNavigation/index.jsx @@ -0,0 +1,56 @@ +import CrossLink from '@node-core/ui-components/Common/BaseCrossLink'; + +import { sidebar } from '../../site.json' with { type: 'json' }; + +import styles from './index.module.css'; + +const flattenItems = items => + items.flatMap(item => + item.items?.length ? flattenItems(item.items) : [item] + ); + +const articles = sidebar.flatMap(({ items }) => flattenItems(items)); + +/** + * Previous/next article navigation following the same order as the sidebar. + * + * @param {{ pathname: string }} props + */ +const ArticleNavigation = ({ pathname }) => { + const currentIndex = articles.findIndex(({ link }) => link === pathname); + + if (currentIndex === -1) { + return null; + } + + const previous = articles[currentIndex - 1]; + const next = articles[currentIndex + 1]; + + if (!previous && !next) { + return null; + } + + return ( + + ); +}; + +export default ArticleNavigation; diff --git a/components/ArticleNavigation/index.module.css b/components/ArticleNavigation/index.module.css new file mode 100644 index 0000000..304e7ac --- /dev/null +++ b/components/ArticleNavigation/index.module.css @@ -0,0 +1,17 @@ +.crossLinks { + margin-top: 1rem; + display: grid; + width: 100%; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 1rem; +} + +@media (max-width: 767px) { + .crossLinks { + grid-template-columns: minmax(0, 1fr); + } + + .crossLinks > :empty { + display: none; + } +} diff --git a/components/Layout/index.jsx b/components/Layout/index.jsx index 08f553e..b16c42c 100644 --- a/components/Layout/index.jsx +++ b/components/Layout/index.jsx @@ -8,6 +8,7 @@ import NavBar from '../Navigation'; import MetaBar from '../Metabar'; import SideBar from '../Sidebar'; import Footer from '../Footer'; +import ArticleNavigation from '../ArticleNavigation'; /** * @typedef {Object} Props @@ -20,27 +21,32 @@ import Footer from '../Footer'; /** * @param {Props} props */ -export default ({ metadata, headings, readingTime, children }) => ( - <> - - - - -
- -
+export default ({ metadata, headings, readingTime, children }) => { + const pathname = `/learn${metadata.path.replace('/index', '')}`; + + return ( + <> + + + + +
+
- -
-
{children}
+
+ +
+
{children}
+ +
+
- -
-
-