Skip to content

Commit 8140204

Browse files
committed
type fixes
1 parent b132393 commit 8140204

File tree

2 files changed

+121
-132
lines changed

2 files changed

+121
-132
lines changed

packages/docusaurus-plugin-ideal-image/src/plugin-ideal-image.d.ts

Lines changed: 121 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
/// <reference types="@docusaurus/module-type-aliases" />
9+
810
declare module '@docusaurus/plugin-ideal-image' {
911
export type PluginOptions = {
1012
/**
@@ -51,17 +53,6 @@ declare module '@docusaurus/plugin-ideal-image' {
5153
export type Options = Partial<PluginOptions>;
5254
}
5355

54-
declare module '@theme/IdealImageLegacy' {
55-
import type {ReactNode} from 'react';
56-
import type {ImageProps} from './theme/IdealImageLegacy/types';
57-
58-
export type {IconKey, State} from './theme/IdealImageLegacy/types';
59-
60-
export interface Props extends ImageProps {}
61-
62-
export default function IdealImageLegacy(props: ImageProps): ReactNode;
63-
}
64-
6556
declare module '@theme/IdealImage' {
6657
import type {ComponentProps, ReactNode} from 'react';
6758

@@ -85,3 +76,122 @@ declare module '@theme/IdealImage' {
8576
}
8677
export default function IdealImage(props: Props): ReactNode;
8778
}
79+
80+
declare module '@theme/IdealImageLegacy' {
81+
/**
82+
* @see https://github.com/endiliey/react-ideal-image/blob/master/index.d.ts
83+
* Note: the original type definition is WRONG. getIcon & getMessage receive
84+
* full state object.
85+
*/
86+
87+
import type {
88+
ComponentProps,
89+
ComponentType,
90+
CSSProperties,
91+
ReactNode,
92+
} from 'react';
93+
94+
export type LoadingState = 'initial' | 'loading' | 'loaded' | 'error';
95+
96+
export type State = {
97+
pickedSrc: {
98+
size: number;
99+
};
100+
loadInfo: 404 | null;
101+
loadState: LoadingState;
102+
};
103+
104+
export type IconKey =
105+
| 'load'
106+
| 'loading'
107+
| 'loaded'
108+
| 'error'
109+
| 'noicon'
110+
| 'offline';
111+
112+
export type SrcType = {
113+
width: number;
114+
src?: string;
115+
size?: number;
116+
format?: 'webp' | 'jpeg' | 'png' | 'gif';
117+
};
118+
119+
type ThemeKey = 'placeholder' | 'img' | 'icon' | 'noscript';
120+
121+
export interface ImageProps
122+
extends Omit<ComponentProps<'img'>, 'srcSet' | 'placeholder'> {
123+
/**
124+
* This function decides what icon to show based on the current state of the
125+
* component.
126+
*/
127+
getIcon?: (state: State) => IconKey;
128+
/**
129+
* This function decides what message to show based on the icon (returned
130+
* from `getIcon` prop) and the current state of the component.
131+
*/
132+
getMessage?: (icon: IconKey, state: State) => string | null;
133+
/**
134+
* This function is called as soon as the component enters the viewport and
135+
* is used to generate urls based on width and format if `props.srcSet`
136+
* doesn't provide `src` field.
137+
*/
138+
getUrl?: (srcType: SrcType) => string;
139+
/**
140+
* The Height of the image in px.
141+
*/
142+
height: number;
143+
/**
144+
* This provides a map of the icons. By default, the component uses icons
145+
* from material design, Implemented as React components with the SVG
146+
* element. You can customize icons
147+
*/
148+
icons?: Partial<{[icon in IconKey]: ComponentType}>;
149+
/**
150+
* This prop takes one of the 2 options, xhr and image.
151+
* Read more about it:
152+
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#cancel-download
153+
*/
154+
loader?: 'xhr' | 'image';
155+
/**
156+
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#lqip
157+
*/
158+
placeholder: {color: string} | {lqip: string};
159+
/**
160+
* This function decides if image should be downloaded automatically. The
161+
* default function returns false for a 2g network, for a 3g network it
162+
* decides based on `props.threshold` and for a 4g network it returns true
163+
* by default.
164+
*/
165+
shouldAutoDownload?: (options: {
166+
connection?: 'slow-2g' | '2g' | '3g' | '4g';
167+
size?: number;
168+
threshold?: number;
169+
possiblySlowNetwork?: boolean;
170+
}) => boolean;
171+
/**
172+
* This provides an array of sources of different format and size of the
173+
* image. Read more about it:
174+
* https://github.com/stereobooster/react-ideal-image/blob/master/introduction.md#srcset
175+
*/
176+
srcSet: SrcType[];
177+
/**
178+
* This provides a theme to the component. By default, the component uses
179+
* inline styles, but it is also possible to use CSS modules and override
180+
* all styles.
181+
*/
182+
theme?: Partial<{[key in ThemeKey]: string | CSSProperties}>;
183+
/**
184+
* Tells how much to wait in milliseconds until consider the download to be
185+
* slow.
186+
*/
187+
threshold?: number;
188+
/**
189+
* Width of the image in px.
190+
*/
191+
width: number;
192+
}
193+
194+
export interface Props extends ImageProps {}
195+
196+
export default function IdealImageLegacy(props: ImageProps): ReactNode;
197+
}

packages/docusaurus-plugin-ideal-image/src/theme/IdealImageLegacy/types.d.ts

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)