Skip to content

Commit

Permalink
refactor(v2): add missing theme-classic types (#4385)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored Mar 12, 2021
1 parent fa1d681 commit a39c62f
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import React from 'react';
import Layout from '@theme/Layout';
import {MDXProvider} from '@mdx-js/react';
import MDXComponents from '@theme/MDXComponents';
import type {Props} from '@theme/MDXPage';

function MDXPage(props) {
function MDXPage(props: Props): JSX.Element {
const {content: MDXPageContent} = props;
const {frontMatter, metadata} = MDXPageContent;
const {title, description} = frontMatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import React from 'react';
import Link from '@docusaurus/Link';
import Translate, {translate} from '@docusaurus/Translate';
import type {Metadata} from '@theme/BlogListPage';
import type {Props} from '@theme/BlogListPaginator';

function BlogListPaginator(props: {readonly metadata: Metadata}): JSX.Element {
function BlogListPaginator(props: Props): JSX.Element {
const {metadata} = props;
const {previousPage, nextPage} = metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function DocSidebarItemLink({
);
}

function DocSidebarItem(props) {
function DocSidebarItem(props): JSX.Element {
switch (props.item.type) {
case 'category':
return <DocSidebarItemCategory {...props} />;
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus-theme-classic/src/theme/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Link from '@docusaurus/Link';
import {useThemeConfig} from '@docusaurus/theme-common';
import useBaseUrl from '@docusaurus/useBaseUrl';
import styles from './styles.module.css';
import ThemedImage from '@theme/ThemedImage';
import ThemedImage, {Props as ThemedImageProps} from '@theme/ThemedImage';

function FooterLink({to, href, label, prependBaseUrlToHref, ...props}: any) {
const toUrl = useBaseUrl(to);
Expand All @@ -34,7 +34,10 @@ function FooterLink({to, href, label, prependBaseUrlToHref, ...props}: any) {
);
}

const FooterLogo = ({sources, alt}) => (
const FooterLogo = ({
sources,
alt,
}: Pick<ThemedImageProps, 'sources' | 'alt'>) => (
<ThemedImage className="footer__logo" alt={alt} sources={sources} />
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import {Props} from '@theme/IconArrow';
import type {Props} from '@theme/IconArrow';

const IconArrow = (props: Props): JSX.Element => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import clsx from 'clsx';

import {Props} from '@theme/IconEdit';
import type {Props} from '@theme/IconEdit';

import styles from './styles.module.css';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import {Props} from '@theme/IconLanguage';
import type {Props} from '@theme/IconLanguage';

const IconLanguage = ({
width = 20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import {Props} from '@theme/IconMenu';
import type {Props} from '@theme/IconMenu';

const IconMenu = ({
width = 30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import React from 'react';
import styles from './styles.module.css';
import Translate from '@docusaurus/Translate';
import type {Props} from '@theme/LastUpdated';

function LastUpdatedAtDate({
lastUpdatedAt,
formattedLastUpdatedAt,
}: {
lastUpdatedAt: number;
formattedLastUpdatedAt: string;
}) {
}): JSX.Element {
return (
<Translate
id="theme.lastUpdated.atDate"
Expand All @@ -34,7 +35,11 @@ function LastUpdatedAtDate({
);
}

function LastUpdatedByUser({lastUpdatedBy}: {lastUpdatedBy: string}) {
function LastUpdatedByUser({
lastUpdatedBy,
}: {
lastUpdatedBy: string;
}): JSX.Element {
return (
<Translate
id="theme.lastUpdated.byUser"
Expand All @@ -51,11 +56,7 @@ export default function LastUpdated({
lastUpdatedAt,
formattedLastUpdatedAt,
lastUpdatedBy,
}: {
lastUpdatedAt: number | undefined;
formattedLastUpdatedAt: string | undefined;
lastUpdatedBy: string | undefined;
}) {
}: Props): JSX.Element {
return (
<div className="col text--right">
<em>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React from 'react';
import ThemeProvider from '@theme/ThemeProvider';
import UserPreferencesProvider from '@theme/UserPreferencesProvider';
import {DocsPreferredVersionContextProvider} from '@docusaurus/theme-common';
import {Props} from '@theme/LayoutProviders';
import type {Props} from '@theme/LayoutProviders';

export default function LayoutProviders({children}: Props): JSX.Element {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
import React from 'react';

import Head from '@docusaurus/Head';

type SearchTagMetaProps = {
locale?: string;
version?: string;
tag?: string;
};
import type {Props} from '@theme/SearchMetadatas';

// Note: we don't couple this to Algolia/DocSearch on purpose
// We may want to support other search engine plugins too
Expand All @@ -22,7 +17,7 @@ export default function SearchMetadatas({
locale,
version,
tag,
}: SearchTagMetaProps): JSX.Element {
}: Props): JSX.Element {
return (
<Head>
{locale && <meta name="docusaurus_locale" content={`${locale}`} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import Translate from '@docusaurus/Translate';
import {useLocation} from '@docusaurus/router';
import styles from './styles.module.css';

function programmaticFocus(el) {
function programmaticFocus(el: HTMLElement) {
el.setAttribute('tabindex', '-1');
el.focus();
el.removeAttribute('tabindex');
}

function SkipToContent(): JSX.Element {
const containerRef = useRef(null);
const containerRef = useRef<HTMLDivElement>(null);
const location = useLocation();

const handleSkip = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();

const targetElement =
const targetElement: HTMLElement | null =
document.querySelector('main:first-of-type') ||
document.querySelector('.main-wrapper');

Expand All @@ -34,7 +34,7 @@ function SkipToContent(): JSX.Element {

useEffect(() => {
if (!location.hash) {
programmaticFocus(containerRef.current);
programmaticFocus(containerRef.current!);
}
}, [location.pathname]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import clsx from 'clsx';
import type {TOCProps} from '@theme/TOC';
import type {TOCInlineProps} from '@theme/TOCInline';
import styles from './styles.module.css';
import {TOCItem} from '@docusaurus/types';

Expand Down Expand Up @@ -39,7 +39,7 @@ function HeadingsInline({
);
}

function TOCInline({toc}: TOCProps): JSX.Element {
function TOCInline({toc}: TOCInlineProps): JSX.Element {
return (
<div className={clsx(styles.tableOfContentsInline)}>
<HeadingsInline toc={toc} />
Expand Down
10 changes: 6 additions & 4 deletions packages/docusaurus-theme-classic/src/theme/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import clsx from 'clsx';

import styles from './styles.module.css';

function isInViewport(element: HTMLElement) {
function isInViewport(element: HTMLElement): boolean {
const {top, left, bottom, right} = element.getBoundingClientRect();
const {innerHeight, innerWidth} = window;

Expand All @@ -24,7 +24,7 @@ function isInViewport(element: HTMLElement) {
const keys = {
left: 37,
right: 39,
};
} as const;

function Tabs(props: Props): JSX.Element {
const {lazy, block, defaultValue, values, groupId, className} = props;
Expand Down Expand Up @@ -79,14 +79,16 @@ function Tabs(props: Props): JSX.Element {
let focusElement;

switch (event.keyCode) {
case keys.right:
case keys.right: {
const nextTab = tabRefs.indexOf(event.target) + 1;
focusElement = tabRefs[nextTab] || tabRefs[0];
break;
case keys.left:
}
case keys.left: {
const prevTab = tabRefs.indexOf(event.target) - 1;
focusElement = tabRefs[prevTab] || tabRefs[tabRefs.length - 1];
break;
}
default:
break;
}
Expand Down
14 changes: 10 additions & 4 deletions packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@
* LICENSE file in the root directory of this source tree.
*/

import React, {ComponentProps} from 'react';
import React, {CSSProperties} from 'react';
import Toggle from 'react-toggle';
import type {Props} from '@theme/Toggle';
import {useThemeConfig} from '@docusaurus/theme-common';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

import clsx from 'clsx';
import styles from './styles.module.css';

const Dark = ({icon, style}) => (
interface IconProps {
icon: string;
style: CSSProperties;
}

const Dark = ({icon, style}: IconProps): JSX.Element => (
<span className={clsx(styles.toggle, styles.dark)} style={style}>
{icon}
</span>
);
const Light = ({icon, style}) => (
const Light = ({icon, style}: IconProps): JSX.Element => (
<span className={clsx(styles.toggle, styles.light)} style={style}>
{icon}
</span>
);

export default function (props: ComponentProps<typeof Toggle>): JSX.Element {
export default function (props: Props): JSX.Element {
const {
colorMode: {
switchConfig: {darkIcon, darkIconStyle, lightIcon, lightIconStyle},
Expand Down
46 changes: 41 additions & 5 deletions packages/docusaurus-theme-classic/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,43 @@ declare module '@theme/Layout' {
export default Layout;
}

declare module '@theme/LayoutHead' {
import type {Props} from '@theme/Layout';

const LayoutHead: (props: Props) => JSX.Element;
export default LayoutHead;
}

declare module '@theme/SearchMetadatas' {
export type Props = {
locale?: string;
version?: string;
tag?: string;
};

const SearchMetadatas: (props: Props) => JSX.Element;
export default SearchMetadatas;
}

declare module '@theme/LastUpdated' {
export type Props = {
lastUpdatedAt?: number;
formattedLastUpdatedAt?: string;
lastUpdatedBy?: string;
};

const LastUpdated: (props: Props) => JSX.Element;
export default LastUpdated;
}

declare module '@theme/SkipToContent' {
const SkipToContent: () => JSX.Element;
export default SkipToContent;
}

declare module '@theme/MDXComponents' {
import {ComponentProps} from 'react';
import CodeBlock from '@theme/CodeBlock';
import type {ComponentProps} from 'react';
import type CodeBlock from '@theme/CodeBlock';

export type MDXComponentsObject = {
readonly code: typeof CodeBlock;
Expand Down Expand Up @@ -465,10 +499,12 @@ declare module '@theme/TOCInline' {
}

declare module '@theme/Toggle' {
import {ComponentProps} from 'react';
import ReactToggle from 'react-toggle';
import type {ComponentProps} from 'react';
import type ReactToggle from 'react-toggle';

export type Props = ComponentProps<typeof ReactToggle>;

const Toggle: (props: ComponentProps<typeof ReactToggle>) => JSX.Element;
const Toggle: (props: Props) => JSX.Element;
export default Toggle;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function DocsPreferredVersionContextProvider({
children,
}: {
children: ReactNode;
}) {
}): JSX.Element {
if (isDocsPluginEnabled) {
return (
<DocsPreferredVersionContextProviderUnsafe>
Expand All @@ -149,7 +149,7 @@ function DocsPreferredVersionContextProviderUnsafe({
children,
}: {
children: ReactNode;
}) {
}): JSX.Element {
const contextValue = useContextValue();
return <Context.Provider value={contextValue}>{children}</Context.Provider>;
}
Expand Down

0 comments on commit a39c62f

Please sign in to comment.