Skip to content

Commit

Permalink
refactor(plugin-ideal-image): migrate package to TS (#5940)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Nov 17, 2021
1 parent 3bf59a6 commit ac1df88
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 14 deletions.
7 changes: 5 additions & 2 deletions packages/docusaurus-plugin-ideal-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"main": "lib/index.js",
"types": "src/plugin-ideal-image.d.ts",
"scripts": {
"build": "tsc && node copyUntypedFiles.js",
"watch": "node copyUntypedFiles.js && tsc --watch"
"build": "yarn build:server && yarn build:browser && yarn build:copy && yarn build:prettier",
"build:server": "tsc --project tsconfig.server.json",
"build:browser": "tsc --project tsconfig.browser.json",
"build:copy": "node copyUntypedFiles.js",
"build:prettier": "prettier --config ../../.prettierrc --write \"lib/**/*.js\""
},
"publishConfig": {
"access": "public"
Expand Down
95 changes: 94 additions & 1 deletion packages/docusaurus-plugin-ideal-image/src/deps.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,97 @@
* LICENSE file in the root directory of this source tree.
*/

declare module '@endiliey/react-ideal-image';
/**
* @see https://github.com/endiliey/react-ideal-image/blob/master/index.d.ts
*/
declare module '@endiliey/react-ideal-image' {
export type LoadingState = 'initial' | 'loading' | 'loaded' | 'error';

export type IconKey =
| 'load'
| 'loading'
| 'loaded'
| 'error'
| 'noicon'
| 'offline';

export interface SrcType {
width: number;
src?: string;
size?: number;
format?: 'webp' | 'jpeg' | 'png' | 'gif';
}

type ThemeKey = 'placeholder' | 'img' | 'icon' | 'noscript';

export interface ImageProps {
/**
* This function decides what icon to show based on the current state of the component.
*/
getIcon?: (state: LoadingState) => IconKey;
/**
* This function decides what message to show based on the icon (returned from getIcon prop) and
* the current state of the component.
*/
getMessage?: (icon: IconKey, state: LoadingState) => string;
/**
* This function is called as soon as the component enters the viewport and is used to generate urls
* based on width and format if props.srcSet doesn't provide src field.
*/
getUrl?: (srcType: SrcType) => string;
/**
* The Height of the image in px.
*/
height: number;
/**
* This provides a map of the icons. By default, the component uses icons from material design,
* implemented as React components with the SVG element. You can customize icons
*/
icons: Partial<Record<IconKey, ComponentType>>;
/**
* This prop takes one of the 2 options, xhr and image.
* Read more about it:
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#cancel-download
*/
loader?: 'xhr' | 'image';
/**
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#lqip
*/
placeholder: {color: string} | {lqip: string};
/**
* This function decides if image should be downloaded automatically. The default function
* returns false for a 2g network, for a 3g network it decides based on props.threshold
* and for a 4g network it returns true by default.
*/
shouldAutoDownload?: (options: {
connection?: 'slow-2g' | '2g' | '3g' | '4g';
size?: number;
threshold?: number;
possiblySlowNetwork?: boolean;
}) => boolean;
/**
* This provides an array of sources of different format and size of the image.
* Read more about it:
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#srcset
*/
srcSet: SrcType[];
/**
* This provides a theme to the component. By default, the component uses inline styles,
* but it is also possible to use CSS modules and override all styles.
*/
theme?: Partial<Record<ThemeKey, string | CSSProperties>>;
/**
* Tells how much to wait in milliseconds until consider the download to be slow.
*/
threshold?: number;
/**
* Width of the image in px.
*/
width: number;
}

type IdealImageComponent = ComponentClass<ImageProps>;

declare const IdealImage: IdealImageComponent;
export default IdealImage;
}
21 changes: 18 additions & 3 deletions packages/docusaurus-plugin-ideal-image/src/plugin-ideal-image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,23 @@ declare module '@docusaurus/plugin-ideal-image' {
declare module '@theme/IdealImage' {
import type {ComponentProps} from 'react';

export interface Props extends ComponentProps<'img'> {
img: any;
}
export type SrcType = {
width: number;
path?: string;
size?: number;
format?: 'webp' | 'jpeg' | 'png' | 'gif';
};

export type SrcImage = {
height?: number;
width?: number;
preSrc: string;
src: string;
images: SrcType[];
};

export type Props = ComponentProps<'img'> & {
img: {default: string} | {src: SrcImage; preSrc: string} | string;
};
export default function IdealImage(props: Props): JSX.Element;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
*/

import React from 'react';
import IdealImage from '@endiliey/react-ideal-image';
import ReactIdealImage from '@endiliey/react-ideal-image';

function Image(props) {
import type {Props} from '@theme/IdealImage';

function IdealImage(props: Props): JSX.Element {
const {alt, className, img} = props;

// In dev env just use regular img with original file
if (typeof img === 'string' || typeof img.default === 'string') {
if (typeof img === 'string' || 'default' in img) {
return (
<img
src={img?.default ?? img}
src={typeof img === 'string' ? img : img.default}
className={className}
alt={alt}
{...props}
Expand All @@ -24,7 +26,7 @@ function Image(props) {
}

return (
<IdealImage
<ReactIdealImage
{...props}
alt={alt}
className={className}
Expand All @@ -40,4 +42,4 @@ function Image(props) {
);
}

export default Image;
export default IdealImage;
8 changes: 8 additions & 0 deletions packages/docusaurus-plugin-ideal-image/tsconfig.browser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"jsx": "react-native"
},
"include": ["src/theme/", "src/*.d.ts"]
}
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-ideal-image/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"lib": ["DOM", "ES2019"],
"rootDir": "src",
"baseUrl": "src",
"outDir": "lib"
}
}
4 changes: 4 additions & 0 deletions packages/docusaurus-plugin-ideal-image/tsconfig.server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["src/*.ts"]
}

0 comments on commit ac1df88

Please sign in to comment.