Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ideal-image-plugin): internalize legacy component code #11010

Merged
merged 6 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"CHANGELOG.md",
"patches",
"packages/docusaurus-theme-translations/locales",
"packages/docusaurus-plugin-ideal-image/src/theme/IdealImageLegacy",
"package.json",
"yarn.lock",
"project-words.txt",
Expand Down
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ packages/create-docusaurus/templates/facebook

website/_dogfooding/_swizzle_theme_tests
website/_dogfooding/_asset-tests/badSyntax.js


packages/docusaurus-plugin-ideal-image/src/theme/IdealImageLegacy
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-ideal-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@docusaurus/theme-translations": "3.7.0",
"@docusaurus/types": "3.7.0",
"@docusaurus/utils-validation": "3.7.0",
"@slorber/react-ideal-image": "^0.0.14",
"react-waypoint": "^10.3.0",
"sharp": "^0.32.3",
"tslib": "^2.6.0",
Expand Down
124 changes: 0 additions & 124 deletions packages/docusaurus-plugin-ideal-image/src/deps.d.ts

This file was deleted.

121 changes: 121 additions & 0 deletions packages/docusaurus-plugin-ideal-image/src/plugin-ideal-image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

/// <reference types="@docusaurus/module-type-aliases" />

declare module '@docusaurus/plugin-ideal-image' {
export type PluginOptions = {
/**
Expand Down Expand Up @@ -74,3 +76,122 @@ declare module '@theme/IdealImage' {
}
export default function IdealImage(props: Props): ReactNode;
}

declare module '@theme/IdealImageLegacy' {
/**
* @see https://github.com/endiliey/react-ideal-image/blob/master/index.d.ts
* Note: the original type definition is WRONG. getIcon & getMessage receive
* full state object.
*/

import type {
ComponentProps,
ComponentType,
CSSProperties,
ReactNode,
} from 'react';

export type LoadingState = 'initial' | 'loading' | 'loaded' | 'error';

export type State = {
pickedSrc: {
size: number;
};
loadInfo: 404 | null;
loadState: LoadingState;
};

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

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

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

export interface ImageProps
extends Omit<ComponentProps<'img'>, 'srcSet' | 'placeholder'> {
/**
* This function decides what icon to show based on the current state of the
* component.
*/
getIcon?: (state: State) => 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: State) => string | null;
/**
* 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<{[icon in 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<{[key in 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;
}

export interface Props extends ImageProps {}

export default function IdealImageLegacy(props: ImageProps): ReactNode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/

import React, {type ReactNode} from 'react';
import {translate} from '@docusaurus/Translate';
import ReactIdealImage, {
type IconKey,
type State,
} from '@slorber/react-ideal-image';
import {translate} from '@docusaurus/Translate';
} from '@theme/IdealImageLegacy';

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

Expand Down Expand Up @@ -93,7 +93,6 @@ export default function IdealImage(props: Props): ReactNode {

return (
<ReactIdealImage
{...propsRest}
height={img.src.height ?? 100}
width={img.src.width ?? 100}
placeholder={{lqip: img.preSrc}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2017 stereobooster

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Legacy React IdealImage lib

This is legacy code from an npm package we forked, then internalized

See also:

- https://github.com/slorber/docusaurus-react-ideal-image
- https://github.com/endiliey/react-ideal-image
- https://github.com/stereobooster/react-ideal-image

---

TODO: we need to clean it up, remove what we don't need, and maintain it up to date
Loading
Loading