Skip to content

Commit

Permalink
Clickable headings; various design fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-md committed May 14, 2024
1 parent c613399 commit 50dedb3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
10 changes: 3 additions & 7 deletions components/ApiReference/Parameter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Typography from '@mui/material/Typography'
import Property from './Property'
import Hr from '../Hr'
import { MdxHeading } from '../../lib/mdx'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Parameters: React.FC<{ parameters: any[] }> = ({ parameters }) => {
Expand All @@ -11,9 +11,7 @@ const Parameters: React.FC<{ parameters: any[] }> = ({ parameters }) => {
<>
{query.length > 0 && (
<>
<Typography variant='h4' sx={{ mt: 2 }}>
Query Parameters
</Typography>
<MdxHeading headingLevel={4}>Query Parameters</MdxHeading>
<Hr />
</>
)}
Expand All @@ -29,9 +27,7 @@ const Parameters: React.FC<{ parameters: any[] }> = ({ parameters }) => {
))}
{body.length > 0 && (
<>
<Typography variant='h4' sx={{ mt: 2 }}>
Request Body
</Typography>
<MdxHeading headingLevel={4}>Request Body</MdxHeading>
<Hr />
</>
)}
Expand Down
9 changes: 5 additions & 4 deletions components/ApiReference/Response.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ExpandMore from '@mui/icons-material/ExpandMore'

import Property from './Property'
import Hr from '../Hr'
import { MdxHeading } from '../../lib/mdx'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Response: React.FC<{ response: any, index: number }> = ({
Expand Down Expand Up @@ -90,7 +91,9 @@ const Response: React.FC<{ response: any, index: number }> = ({
borderRadius: 1,
border: 'none',
mr: 1,
backgroundColor: isSuccess ? 'success.background' : 'error.background'
backgroundColor: isSuccess
? 'success.background'
: 'error.background'
}}
/>
</Grid>
Expand Down Expand Up @@ -136,9 +139,7 @@ const Response: React.FC<{ response: any, index: number }> = ({
const Responses: React.FC<{ responses: any[] }> = ({ responses }) => {
return (
<Grid sx={{ mt: 2 }}>
<Typography variant='h4' gutterBottom>
Responses
</Typography>
<MdxHeading headingLevel={4}>Responses</MdxHeading>
<Hr />
{responses?.map?.((response, index) => (
<Response key={index} index={index} response={response} />
Expand Down
6 changes: 3 additions & 3 deletions components/ApiReference/mainnet-swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@
],
"deprecated": false,
"path": "/v1/safes/{address}/balances/",
"title": "List a Safe's Balances",
"title": "List a Safe's Balances (Deprecated)",
"additionalInfo": ""
},
"parameters": {
Expand Down Expand Up @@ -3470,7 +3470,7 @@
"title": "Uuid",
"type": "string",
"format": "uuid",
"default": "0287b78d-96ed-4a9c-b80b-1965c1753dc9"
"default": "bb1a4b6f-98b1-4379-bdc6-dab2ce1fb87a"
},
"safes": {
"type": "array",
Expand Down Expand Up @@ -3539,7 +3539,7 @@
"title": "Uuid",
"type": "string",
"format": "uuid",
"default": "8da0a387-58c9-4028-9c98-73857f3c5a3b"
"default": "92c9255f-1c80-42bf-80d9-a86a4ea1b1e1"
},
"safes": {
"type": "array",
Expand Down
4 changes: 3 additions & 1 deletion components/Hr.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { palette } from '../styles/palette'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Hr: React.FC<{ style?: any }> = props => (
<hr
{...props}
style={{
border: '0px solid DimGray',
border: `0px solid ${palette.border.light}`,
borderBottomWidth: '1px',
...props.style
}}
Expand Down
60 changes: 46 additions & 14 deletions lib/mdx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Link from 'next/link'
import throttle from 'lodash/throttle'
import { type SxProps } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
import Button from '@mui/material/Button'
import {
CopyToClipboard,
Code,
Expand All @@ -25,7 +26,7 @@ import swagger from '../components/ApiReference/mainnet-swagger.json'
import pathsMetadata from '../components/ApiReference/paths-metadata.json'

export const slugify: (text: string) => string = text =>
text?.replace?.(/ /g, '-').replace(/\//g, '-')
text?.replace(' #', '')?.replace?.(/ /g, '-').replace(/\//g, '-')

export const getHeadingChildren: (heading: string) => Heading[] = heading => {
const headingPath = '/v1/' + heading + '/'
Expand Down Expand Up @@ -60,6 +61,7 @@ export const getHeadingsFromHtml: (
const headingText = heading
.replace(/<[^>]*>/g, '')
.replace(/&nbsp;/g, ' ')
.replace(' #', '')
.trim()
const link = `#${slugify(headingText)}`
return {
Expand All @@ -73,22 +75,52 @@ export const getHeadingsFromHtml: (
return []
}

export const MdxHeading: React.FC<{
headingLevel: number
children: ReactNode
}> = ({ headingLevel, children }) => (
<Typography
variant={`h${headingLevel}` as 'h1'}
textTransform='none'
id={slugify(children as string)}
sx={{
'&:hover .MuiButton-root': {
opacity: '1'
},
transition: '0.2s'
}}
>
{children as string}{' '}
{headingLevel > 1 && (
<Button
href={`#${slugify(children as string)}`}
disableRipple
sx={{
p: 0,
minWidth: 12,
minHeight: 24,
fontSize: 24,
backgroundColor: 'transparent',
color: 'grey.700',
opacity: '0',
transition: '0.2s',
'&:hover': {
backgroundColor: 'transparent'
}
}}
>
#
</Button>
)}
</Typography>
)

const getMarkdownHeaderComponent: (
headingLevel: number
) => React.FC<{ children: ReactNode }> =
headingLevel =>
({ children }) => {
const HeadingComponent: React.FC = () => (
<Typography
variant={`h${headingLevel}` as 'h1'}
textTransform='none'
id={slugify(children as string)}
>
{children as string}
</Typography>
)
return HeadingComponent({})
}
({ children }) =>
MdxHeading({ headingLevel, children })

export const useCurrentTocIndex: (
headings: Heading[],
Expand Down Expand Up @@ -129,7 +161,7 @@ export const useCurrentTocIndex: (
setCurrentIndex(
isNextHeadingInView ? nextHeading?.link ?? '' : active?.link ?? ''
)
} else setCurrentIndex('')
} else setCurrentIndex(_headings[0]?.children?.[0]?.link ?? '')
}, [headings, navHeight])

const scrollListener = useMemo(
Expand Down

0 comments on commit 50dedb3

Please sign in to comment.