From 6f0e81e4f1136d1821e569ff475731a418a5da1e Mon Sep 17 00:00:00 2001 From: mrholek Date: Sat, 30 Nov 2024 12:43:12 +0100 Subject: [PATCH] docs: refactor code --- packages/docs/src/components/Callout.tsx | 2 +- .../docs/src/components/ClassNamesDocs.tsx | 2 +- packages/docs/src/components/CodeBlock.tsx | 4 +- packages/docs/src/components/Example.tsx | 2 +- .../docs/src/components/ExampleSnippet.tsx | 2 +- packages/docs/src/templates/DocsLayout.tsx | 222 +++++++++++------- packages/docs/src/templates/MdxLayout.tsx | 53 +++-- 7 files changed, 180 insertions(+), 107 deletions(-) diff --git a/packages/docs/src/components/Callout.tsx b/packages/docs/src/components/Callout.tsx index b1299b12..a04e594d 100644 --- a/packages/docs/src/components/Callout.tsx +++ b/packages/docs/src/components/Callout.tsx @@ -1,5 +1,5 @@ import React, { FC, ReactNode } from 'react' -interface CalloutProps { +export interface CalloutProps { children: ReactNode color: string title?: string diff --git a/packages/docs/src/components/ClassNamesDocs.tsx b/packages/docs/src/components/ClassNamesDocs.tsx index 774da05f..e9017c78 100644 --- a/packages/docs/src/components/ClassNamesDocs.tsx +++ b/packages/docs/src/components/ClassNamesDocs.tsx @@ -72,7 +72,7 @@ const extractCLassNames = (code: string) => { return classNamesArray } -const ClassNamesDocs = ({ files }) => { +const ClassNamesDocs = ({ files }: { files: string | string[] }) => { const _files = Array.isArray(files) ? files : [files] const classNames = _files.flatMap((file) => { diff --git a/packages/docs/src/components/CodeBlock.tsx b/packages/docs/src/components/CodeBlock.tsx index d7fd2422..9b78aabc 100644 --- a/packages/docs/src/components/CodeBlock.tsx +++ b/packages/docs/src/components/CodeBlock.tsx @@ -1,12 +1,12 @@ import React, { FC } from 'react' import { Highlight, Prism } from 'prism-react-renderer' -interface CodeBlockProps { +export interface CodeBlockProps { children: any // eslint-disable-line @typescript-eslint/no-explicit-any } const CodeBlock: FC = ({ children }) => { - ;(typeof global === 'undefined' ? window : global).Prism = Prism + ;(typeof globalThis === 'undefined' ? globalThis : globalThis).Prism = Prism // eslint-disable-next-line unicorn/prefer-module require('prismjs/components/prism-bash') require('prismjs/components/prism-scss') diff --git a/packages/docs/src/components/Example.tsx b/packages/docs/src/components/Example.tsx index c8c2a47e..d5992eae 100644 --- a/packages/docs/src/components/Example.tsx +++ b/packages/docs/src/components/Example.tsx @@ -1,5 +1,5 @@ import React, { FC, ReactNode } from 'react' -interface ExampleProps { +export interface ExampleProps { children: ReactNode className: string } diff --git a/packages/docs/src/components/ExampleSnippet.tsx b/packages/docs/src/components/ExampleSnippet.tsx index 87a50bd7..1e5428f3 100644 --- a/packages/docs/src/components/ExampleSnippet.tsx +++ b/packages/docs/src/components/ExampleSnippet.tsx @@ -13,7 +13,7 @@ interface CodeSnippets { ts?: string } -interface ExampleSnippetProps { +export interface ExampleSnippetProps { children: ReactNode className?: string code: string | CodeSnippets diff --git a/packages/docs/src/templates/DocsLayout.tsx b/packages/docs/src/templates/DocsLayout.tsx index 289862ef..99b256f2 100644 --- a/packages/docs/src/templates/DocsLayout.tsx +++ b/packages/docs/src/templates/DocsLayout.tsx @@ -1,44 +1,131 @@ -import React, { FC } from 'react' +import React, { FC, ReactNode, useMemo } from 'react' import { Ads, Banner, Seo, Toc } from '../components' -import { CContainer, CNav, CNavItem, CNavLink } from '@coreui/react/src/' +import { CContainer, CNav, CNavItem, CNavLink } from '@coreui/react' +// @ts-expect-error json file +import jsonData from './../data/other_frameworks.json' + +import type { TocItem } from '../components/Toc' interface DocsLayoutProps { - children: any // eslint-disable-line @typescript-eslint/no-explicit-any - data: any // eslint-disable-line @typescript-eslint/no-explicit-any - location: any // eslint-disable-line @typescript-eslint/no-explicit-any - pageContext: any // eslint-disable-line @typescript-eslint/no-explicit-any + children: ReactNode + data: DocsData + location: Location + pageContext: PageContext } -// @ts-expect-error json file -import jsonData from './../data/other_frameworks.json' +interface DocsData { + allMdx: { + edges: Array<{ + node: { + fields: { + slug: string + } + } + }> + } + mdx?: { + tableOfContents: { + items: TocItem[] + } + } +} + +interface PageContext { + frontmatter?: Frontmatter +} -const humanize = (text: string) => { - const string = text +interface Frontmatter { + title?: string + description?: string + name?: string + other_frameworks?: string + pro_component?: boolean + route?: string +} + +interface OtherFrameworks { + [key: string]: { + [key: string]: string + } +} + +const humanize = (text: string): string => { + return text .split('-') - .reduce( - (accumulator, currentValue) => - accumulator + ' ' + currentValue[0].toUpperCase() + currentValue.slice(1), - ) - return string[0].toUpperCase() + string.slice(1) + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) + .join(' ') } +const DocsNav: FC<{ + route: string + locationPathname: string + hasNavAPI: boolean + hasNavStyling: boolean + hasNavAccessibility: boolean +}> = ({ route, locationPathname, hasNavAPI, hasNavStyling, hasNavAccessibility }) => ( + + + + Features + + + {hasNavAPI && ( + + + API + + + )} + {hasNavStyling && ( + + + Styling + + + )} + {hasNavAccessibility && ( + + + Accessibility + + + )} + +) + const DocsLayout: FC = ({ children, data, location, pageContext }) => { - const title = pageContext.frontmatter ? pageContext.frontmatter.title : '' - const description = pageContext.frontmatter ? pageContext.frontmatter.description : '' - const name = pageContext.frontmatter ? pageContext.frontmatter.name : '' - const other_frameworks = pageContext.frontmatter ? pageContext.frontmatter.other_frameworks : '' - const pro_component = pageContext.frontmatter ? pageContext.frontmatter.pro_component : '' - const route = pageContext.frontmatter ? pageContext.frontmatter.route : '' - const frameworks = other_frameworks ? other_frameworks.split(', ') : false - const otherFrameworks = JSON.parse(JSON.stringify(jsonData)) + const frontmatter = pageContext.frontmatter || {} - const hasNav = data?.allMdx?.edges.length > 1 - const hasNavAccesibility = - hasNav && data.allMdx.edges.some((edge: any) => edge.node.fields.slug.includes('accesibility')) - const hasNavAPI = - hasNav && data.allMdx.edges.some((edge: any) => edge.node.fields.slug.includes('api')) - const hasNavCustomizing = - hasNav && data.allMdx.edges.some((edge: any) => edge.node.fields.slug.includes('customizing')) + const { + title = '', + description = '', + name = '', + other_frameworks: otherFrameworksStr = '', + pro_component: proComponent = false, + route = '', + } = frontmatter + const frameworks = useMemo( + () => otherFrameworksStr.split(', ').filter(Boolean), + [otherFrameworksStr], + ) + const otherFrameworks: OtherFrameworks = useMemo(() => ({ ...jsonData }), []) + const hasNav = useMemo(() => data?.allMdx?.edges.length > 1, [data]) + const hasNavAccessibility = useMemo( + () => + hasNav && data.allMdx.edges.some((edge) => edge.node.fields.slug.includes('accessibility')), + [hasNav, data], + ) + const hasNavAPI = useMemo( + () => hasNav && data.allMdx.edges.some((edge) => edge.node.fields.slug.includes('api')), + [hasNav, data], + ) + const hasNavStyling = useMemo( + () => hasNav && data.allMdx.edges.some((edge) => edge.node.fields.slug.includes('styling')), + [hasNav, data], + ) return ( <> @@ -46,40 +133,13 @@ const DocsLayout: FC = ({ children, data, location, pageContext
{hasNav && ( - - - - Features - - - {hasNavAPI && ( - - - API - - - )} - {hasNavCustomizing && ( - - - Customizing - - - )} - {hasNavAccesibility && ( - - - Accesibility - - - )} - + )}
{name && name !== title ? ( @@ -94,38 +154,36 @@ const DocsLayout: FC = ({ children, data, location, pageContext {title} )} - +

{description}

- {frameworks && ( + {frameworks.length > 0 && ( <> -

Other frameworks

+

Other Frameworks

CoreUI components are available as native Angular, Bootstrap (Vanilla JS), and Vue components. To learn more please visit the following pages.

)}
- {data && data.mdx && } + {data.mdx && }
{children}
diff --git a/packages/docs/src/templates/MdxLayout.tsx b/packages/docs/src/templates/MdxLayout.tsx index 72f2e48c..a0b1c551 100644 --- a/packages/docs/src/templates/MdxLayout.tsx +++ b/packages/docs/src/templates/MdxLayout.tsx @@ -1,5 +1,5 @@ import React, { FC } from 'react' -import { graphql } from 'gatsby' +import { graphql, PageProps } from 'gatsby' import { MDXProvider } from '@mdx-js/react' import { Callout, @@ -11,16 +11,38 @@ import { ScssDocs, } from '../components' -interface MdxLayoutProps { - data: any // eslint-disable-line @typescript-eslint/no-explicit-any - children: any // eslint-disable-line @typescript-eslint/no-explicit-any +import { CalloutProps } from '../components/Callout' +import { CodeBlockProps } from '../components/CodeBlock' +import { ExampleProps } from '../components/Example' +import { ExampleSnippetProps } from '../components/ExampleSnippet' +import { ScssDocsProps } from '../components/ScssDocs' +import type { TocItem } from '../components/Toc' + +interface DataProps { + mdx: { + tableOfContents: Toc + } + allMdx: { + edges: Array<{ + node: { + id: string + fields: { + slug: string + } + } + }> + } +} + +interface Toc { + items: TocItem[] } -const MdxLayout: FC = ({ children }) => { +const MdxLayout: FC> = ({ children }) => { return ( { + Callout: (props: CalloutProps) => { const { children, title, ...rest } = props return ( @@ -29,10 +51,8 @@ const MdxLayout: FC = ({ children }) => { ) }, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ClassNamesDocs: (props: any) => , - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Example: (props: any) => { + ClassNamesDocs: (props: { files: string | string[] }) => , + Example: (props: ExampleProps) => { const { children, ...rest } = props return ( @@ -45,15 +65,10 @@ const MdxLayout: FC = ({ children }) => { ) }, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ExampleSnippet: (props: any) => , - // eslint-disable-next-line @typescript-eslint/no-explicit-any - JSXDocs: (props: any) => , - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ScssDocs: (props: any) => , - // eslint-disable-next-line @typescript-eslint/no-explicit-any - pre: (props: any) => , - // eslint-disable-next-line @typescript-eslint/no-explicit-any + ExampleSnippet: (props: ExampleSnippetProps) => , + JSXDocs: (props: { code: string }) => , + ScssDocs: (props: ScssDocsProps) => , + pre: (props: CodeBlockProps) => , }} > {children}