Skip to content

Commit

Permalink
Merge branch 'develop' into docs/update-colors
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser committed Aug 15, 2024
2 parents b8eaf19 + b4f3b8a commit 83a26bc
Show file tree
Hide file tree
Showing 157 changed files with 5,077 additions and 2,007 deletions.
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
28 changes: 0 additions & 28 deletions www/apps/api-reference/components/Navbar/FeedbackModal/index.tsx

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

0 comments on commit 83a26bc

Please sign in to comment.