Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: redesign sidebar #8408

Merged
merged 46 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
deaac19
initial changes
shahednasser Jul 31, 2024
0d1f86d
redesign the sidebar + nav drawer
shahednasser Jul 31, 2024
5363f26
changes to sidebar items
shahednasser Jul 31, 2024
8d71e98
finish up sidebar redesign
shahednasser Aug 1, 2024
78ca522
support new sidebar in resources
shahednasser Aug 1, 2024
76338be
general fixes
shahednasser Aug 1, 2024
de5a859
integrate in ui
shahednasser Aug 2, 2024
b1c6057
support api reference
shahednasser Aug 2, 2024
450aec8
refactor
shahednasser Aug 2, 2024
ff33cd0
integrate in user guide
shahednasser Aug 2, 2024
2e538ad
docs: fix build errors
shahednasser Aug 2, 2024
3055b61
Merge branch 'develop' into docs/sidebar-redesign-v2
shahednasser Aug 2, 2024
d64da76
fix user guide build
shahednasser Aug 2, 2024
49d154a
more refactoring
shahednasser Aug 2, 2024
b586347
Merge branch 'develop' into docs/sidebar-redesign-v2
shahednasser Aug 2, 2024
c299ba2
Merge branch 'develop' into docs/sidebar-redesign-v2
shahednasser Aug 5, 2024
80efd14
Merge branch 'develop' into docs/sidebar-redesign-v2
shahednasser Aug 7, 2024
de19f52
added banner
shahednasser Aug 7, 2024
88e9e3c
added bottom logo + icon
shahednasser Aug 7, 2024
dfcb2b0
Merge branch 'develop' into docs/sidebar-redesign-v2
shahednasser Aug 8, 2024
a47f0a2
fix up sidebar
shahednasser Aug 8, 2024
ceb1684
fix up paddings
shahednasser Aug 8, 2024
79f6a45
fix shadow bottom
shahednasser Aug 8, 2024
34b1236
docs: add table of content (#8445)
shahednasser Aug 8, 2024
4bb2bfe
mobile fixes
shahednasser Aug 8, 2024
29edf88
truncate text in toc
shahednasser Aug 8, 2024
3e8e66f
mobile fixes
shahednasser Aug 8, 2024
6edd438
Merge branch 'develop' into docs/sidebar-redesign-v2
shahednasser Aug 13, 2024
ffe4785
merge fixes
shahednasser Aug 13, 2024
6793f41
implement redesign
shahednasser Aug 13, 2024
bc86e75
add hide sidebar
shahednasser Aug 13, 2024
4d9eefc
add menu action item
shahednasser Aug 13, 2024
6156fe5
finish up hide sidebar design
shahednasser Aug 13, 2024
fb3c40c
implement redesign in resources
shahednasser Aug 13, 2024
693e09a
Merge branch 'develop' into docs/sidebar-redesign-v2
shahednasser Aug 13, 2024
a033004
integrate in api reference
shahednasser Aug 14, 2024
174b113
integrate changes in ui
shahednasser Aug 14, 2024
abe1e9a
fixes to api reference scrolling
shahednasser Aug 14, 2024
f41c5d3
fix build error
shahednasser Aug 14, 2024
a5e24df
fix build errors
shahednasser Aug 14, 2024
c534dce
fixes
shahednasser Aug 14, 2024
f0f64a2
Merge branch 'develop' into docs/sidebar-redesign-v2
shahednasser Aug 14, 2024
bd3057d
fixes to sidebar
shahednasser Aug 14, 2024
b8c150f
general fixes
shahednasser Aug 14, 2024
c355f27
fix active category not closing
shahednasser Aug 14, 2024
46f6667
fix long titles
shahednasser Aug 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion www/apps/api-reference/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ReferencePage = async () => {
</h1>
<DividedLayout
mainContent={
<Section>
<Section checkActiveOnScroll={true}>
<h1 className="!text-h2 hidden lg:block">
Medusa V2 Admin API Reference
</h1>
Expand Down
7 changes: 5 additions & 2 deletions www/apps/api-reference/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "./globals.css"
import Navbar from "@/components/Navbar"
import Providers from "../providers"
import { WideLayout } from "docs-ui"
import { Inter, Roboto_Mono } from "next/font/google"
Expand Down Expand Up @@ -29,8 +28,12 @@ export default function RootLayout({
return (
<WideLayout
ProvidersComponent={Providers}
NavbarComponent={Navbar}
sidebarProps={{
expandItems: false,
}}
bodyClassName={clsx(inter.variable, robotoMono.variable)}
showToc={false}
showBanner={false}
>
{children}
</WideLayout>
Expand Down
2 changes: 1 addition & 1 deletion www/apps/api-reference/app/store/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ReferencePage = async () => {
</h1>
<DividedLayout
mainContent={
<Section>
<Section checkActiveOnScroll={true}>
<h1 className="!text-h2 hidden lg:block">
Medusa V2 Store API Reference
</h1>
Expand Down
70 changes: 22 additions & 48 deletions www/apps/api-reference/components/MDXComponents/H2/index.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,47 @@
"use client"

import { InView } from "react-intersection-observer"
import { isElmWindow, useScrollController, useSidebar } from "docs-ui"
import checkElementInViewport from "../../../utils/check-element-in-viewport"
import { useEffect, useMemo } from "react"
import { useScrollController, useSidebar } from "docs-ui"
import { useEffect, useMemo, useRef, useState } from "react"
import getSectionId from "../../../utils/get-section-id"

type H2Props = {
addToSidebar?: boolean
} & React.HTMLAttributes<HTMLHeadingElement>
type H2Props = React.HTMLAttributes<HTMLHeadingElement>

const H2 = ({ addToSidebar = true, children, ...props }: H2Props) => {
const { activePath, setActivePath, addItems } = useSidebar()
const { getScrolledTop, scrollableElement } = useScrollController()
const H2 = ({ children, ...props }: H2Props) => {
const headingRef = useRef<HTMLHeadingElement>(null)
const { activePath, addItems } = useSidebar()
const { scrollableElement, scrollToElement } = useScrollController()
const [scrolledFirstTime, setScrolledFirstTime] = useState(false)

const handleViewChange = (
inView: boolean,
entry: IntersectionObserverEntry
) => {
if (!addToSidebar) {
const id = useMemo(() => getSectionId([children as string]), [children])

useEffect(() => {
if (!scrollableElement || !headingRef.current || scrolledFirstTime) {
return
}
const heading = entry.target
if (
(inView ||
checkElementInViewport(heading.parentElement || heading, 40)) &&
getScrolledTop() !== 0 &&
activePath !== heading.id
) {
// can't use next router as it doesn't support
// changing url without scrolling
history.pushState({}, "", `#${heading.id}`)
setActivePath(heading.id)
}
}
const id = getSectionId([children as string])

useEffect(() => {
if (id === (activePath || location.hash.replace("#", ""))) {
const elm = document.getElementById(id)
elm?.scrollIntoView()
scrollToElement(
(headingRef.current.offsetParent as HTMLElement) || headingRef.current
)
}
setScrolledFirstTime(scrolledFirstTime)
}, [scrollableElement, headingRef, id])

useEffect(() => {
addItems([
{
type: "link",
path: `${id}`,
title: children as string,
loaded: true,
},
])
}, [])

const root = useMemo(() => {
return isElmWindow(scrollableElement) ? document.body : scrollableElement
}, [scrollableElement])
}, [id])

return (
<InView
as="h2"
threshold={0.4}
skip={!addToSidebar}
initialInView={false}
{...props}
onChange={handleViewChange}
id={id}
root={root}
>
<h2 {...props} id={id} ref={headingRef}>
{children}
</InView>
</h2>
)
}

Expand Down
4 changes: 1 addition & 3 deletions www/apps/api-reference/components/MDXComponents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const getCustomComponents = (scope?: ScopeType): MDXComponents => {
...UiMDXComponents,
Security: () => <Security specs={scope?.specs} />,
a: Link,
h2: (props: React.HTMLAttributes<HTMLHeadingElement>) => (
<H2 addToSidebar={scope?.addToSidebar} {...props} />
),
h2: (props: React.HTMLAttributes<HTMLHeadingElement>) => <H2 {...props} />,
}
}

Expand Down

This file was deleted.

50 changes: 0 additions & 50 deletions www/apps/api-reference/components/Navbar/index.tsx

This file was deleted.

22 changes: 20 additions & 2 deletions www/apps/api-reference/components/Section/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
"use client"

import clsx from "clsx"
import { useActiveOnScroll, useSidebar } from "docs-ui"
import { useEffect, useRef } from "react"

export type SectionProps = {
addToSidebar?: boolean
checkActiveOnScroll?: boolean
} & React.AllHTMLAttributes<HTMLDivElement>

const Section = ({ children, className }: SectionProps) => {
const Section = ({
children,
className,
checkActiveOnScroll = false,
}: SectionProps) => {
const sectionRef = useRef<HTMLDivElement>(null)
const { activeItemId } = useActiveOnScroll({
rootElm: sectionRef.current || undefined,
enable: checkActiveOnScroll,
useDefaultIfNoActive: false,
})
const { setActivePath } = useSidebar()

useEffect(() => {
if ("scrollRestoration" in history) {
Expand All @@ -17,6 +28,13 @@ const Section = ({ children, className }: SectionProps) => {
}
}, [])

useEffect(() => {
if (activeItemId.length) {
history.pushState({}, "", `#${activeItemId}`)
setActivePath(activeItemId)
}
}, [activeItemId])

return (
<div
ref={sectionRef}
Expand Down
25 changes: 15 additions & 10 deletions www/apps/api-reference/components/Tags/Operation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import TagsOperationDescriptionSection from "./DescriptionSection"
import DividedLayout from "@/layouts/Divided"
import { useLoading } from "@/providers/loading"
import SectionDivider from "../../Section/Divider"
import checkElementInViewport from "../../../utils/check-element-in-viewport"

const TagOperationCodeSection = dynamic<TagOperationCodeSectionProps>(
async () => import("./CodeSection")
Expand Down Expand Up @@ -40,7 +41,7 @@ const TagOperation = ({
)
const nodeRef = useRef<Element | null>(null)
const { loading, removeLoading } = useLoading()
const { scrollableElement } = useScrollController()
const { scrollableElement, scrollToTop } = useScrollController()
const root = useMemo(() => {
return isElmWindow(scrollableElement) ? document.body : scrollableElement
}, [scrollableElement])
Expand Down Expand Up @@ -75,24 +76,28 @@ const TagOperation = ({
[ref]
)

useEffect(() => {
const enableShow = () => {
setShow(true)
const scrollIntoView = useCallback(() => {
if (nodeRef.current && !checkElementInViewport(nodeRef.current, 10)) {
const elm = nodeRef.current as HTMLElement
scrollToTop(
elm.offsetTop + (elm.offsetParent as HTMLElement)?.offsetTop,
0
)
}
setShow(true)
}, [scrollToTop, nodeRef])

useEffect(() => {
if (nodeRef && nodeRef.current) {
removeLoading()
const currentHash = location.hash.replace("#", "")
if (currentHash === path) {
setTimeout(() => {
nodeRef.current?.scrollIntoView()
enableShow()
}, 100)
setTimeout(scrollIntoView, 100)
} else if (currentHash.split("_")[0] === path.split("_")[0]) {
enableShow()
setShow(true)
}
}
}, [nodeRef, path])
}, [nodeRef, path, scrollIntoView])

return (
<div
Expand Down
15 changes: 8 additions & 7 deletions www/apps/api-reference/components/Tags/Paths/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useBaseSpecs } from "@/providers/base-specs"
import getTagChildSidebarItems from "@/utils/get-tag-child-sidebar-items"
import { useLoading } from "@/providers/loading"
import DividedLoading from "@/components/DividedLoading"
import { SidebarItemSections, SidebarItemType } from "types"
import { SidebarItemSections, SidebarItem, SidebarItemCategory } from "types"
import basePathUrl from "../../../utils/base-path-url"

const TagOperation = dynamic<TagOperationProps>(
Expand Down Expand Up @@ -56,16 +56,17 @@ const TagPaths = ({ tag, className }: TagPathsProps) => {
useEffect(() => {
if (paths) {
const parentItem = findItemInSection(
items[SidebarItemSections.BOTTOM],
{ path: tagSlugName },
items[SidebarItemSections.DEFAULT],
{ title: tag.name },
false
)
const pathItems: SidebarItemType[] = getTagChildSidebarItems(paths)
) as SidebarItemCategory
const pathItems: SidebarItem[] = getTagChildSidebarItems(paths)
if ((parentItem?.children?.length || 0) < pathItems.length) {
addItems(pathItems, {
section: SidebarItemSections.BOTTOM,
section: SidebarItemSections.DEFAULT,
parent: {
path: tagSlugName,
title: tag.name,
path: "",
changeLoaded: true,
},
})
Expand Down
Loading
Loading