Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Oct 30, 2024
1 parent 2e41d8b commit 8735857
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
5 changes: 1 addition & 4 deletions src/MenuModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { css } from './utils/css'
import { containerQueryMobile } from './Layout'
import { Links } from './Links'
import { isBrowser } from './utils/isBrowser'
import { getViewportWidth } from './utils/getViewportWidth'

initCloseListeners()

Expand Down Expand Up @@ -129,10 +130,6 @@ function toggleMenuModal() {
autoScroll()
}
}
function getViewportWidth(): number {
// `window.innerWidth` inlcudes scrollbar width: https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
return document.documentElement.clientWidth
}
function autoScroll() {
const nav = document.querySelector('#menu-modal .navigation-content')!
const href = window.location.pathname
Expand Down
101 changes: 54 additions & 47 deletions src/navigation/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ export { NavigationContent }
export type { NavItem }
export type { NavItemAll }

import React from 'react'
import React, { useEffect, useState } from 'react'
import { assert, assertWarning, jsxToTextContent } from '../utils/server'
import './Navigation.css'
import { parseTitle } from '../parseTitle'
import { usePageContext } from '../renderer/usePageContext'
import '@docsearch/css'
import '../global.d.ts'
import { getViewportWidth } from '../utils/getViewportWidth'

type NavItem = {
level: number
Expand Down Expand Up @@ -38,52 +39,7 @@ function NavigationContent(props: {
.map((navItem, i) => <NavItemComponent navItem={navItem} key={i} />)
} else {
assert(!props.showOnlyRelevant)
const navItemsColumnLayout = groupByColumnLayout(navItemsWithComputed)
const paddingBottom = 40
navContent = (
<>
{navItemsColumnLayout.map(({ navItemsColumnEntries, isFullWidth }, i) => (
<div
key={i}
style={{
display: 'flex',
justifyContent: 'center',
}}
>
<div
className={`column-layout-${i}`}
style={{
flexGrow: 1,
columnGap: 20,
paddingBottom: isFullWidth ? paddingBottom : undefined,
}}
>
{navItemsColumnEntries.map((navItemColumnEntry, j) => (
<div
key={j}
className="column-layout-entry"
style={{
breakInside: 'avoid',
paddingBottom: !isFullWidth ? paddingBottom : undefined,
paddingTop: isFullWidth ? undefined : 0,
width: '100%',
}}
>
<div>
<NavItemComponent navItem={navItemColumnEntry} />
{navItemColumnEntry.navItemChilds.map((navItem, k) => (
<NavItemComponent navItem={navItem} key={k} />
))}
<CategoryBorder navItemLevel1={isFullWidth ? undefined : navItemColumnEntry!} />
</div>
</div>
))}
<CategoryBorder navItemLevel1={!isFullWidth ? undefined : navItemsColumnEntries[0]!} />
</div>
</div>
))}
</>
)
navContent = <NavigationColumnLayout navItemsWithComputed={navItemsWithComputed} />
}

return (
Expand All @@ -92,6 +48,57 @@ function NavigationContent(props: {
</div>
)
}

function NavigationColumnLayout(props: { navItemsWithComputed: NavItemComputed[] }) {
const navItemsColumnLayout = groupByColumnLayout(props.navItemsWithComputed)
const paddingBottom = 40

return (
<>
{navItemsColumnLayout.map(({ navItemsColumnEntries, isFullWidth }, i) => (
<div
key={i}
style={{
display: 'flex',
justifyContent: 'center',
}}
>
<div
className={`column-layout-${i}`}
style={{
flexGrow: 1,
columnGap: 20,
paddingBottom: isFullWidth ? paddingBottom : undefined,
}}
>
{navItemsColumnEntries.map((navItemColumnEntry, j) => (
<div
key={j}
className="column-layout-entry"
style={{
breakInside: 'avoid',
paddingBottom: !isFullWidth ? paddingBottom : undefined,
paddingTop: isFullWidth ? undefined : 0,
width: '100%',
}}
>
<div>
<NavItemComponent navItem={navItemColumnEntry} />
{navItemColumnEntry.navItemChilds.map((navItem, k) => (
<NavItemComponent navItem={navItem} key={k} />
))}
<CategoryBorder navItemLevel1={isFullWidth ? undefined : navItemColumnEntry!} />
</div>
</div>
))}
<CategoryBorder navItemLevel1={!isFullWidth ? undefined : navItemsColumnEntries[0]!} />
</div>
</div>
))}
</>
)
}

function CategoryBorder({ navItemLevel1 }: { navItemLevel1?: NavItemComputed }) {
return !navItemLevel1 ? null : (
<div
Expand Down
4 changes: 4 additions & 0 deletions src/utils/getViewportWidth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getViewportWidth(): number {
// `window.innerWidth` inlcudes scrollbar width: https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
return document.documentElement.clientWidth
}

0 comments on commit 8735857

Please sign in to comment.