From 0d3cfcf0256df6986c4438f2fc021520b69e6a30 Mon Sep 17 00:00:00 2001 From: "Jules Sam. Randolph" Date: Thu, 23 Jul 2020 20:39:24 -0300 Subject: [PATCH] feat: a revamped scalable HTMLImage component MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING: imagesMaxWidth prop is discontinued in favor of contentWidth and computeImagesMaxWidth. See RFC001 for an exhaustive description. This patch offers a rewrite of the HTMLImage component to get a close match with RFC001, “a deterministic approach to images scaling”. The only part of this RFC which is unsupported is the unitConverter behavior, which is planned for a later major release. In addition to the capabilities defined in the RFC, this patch provides the following features and behaviors: - new property enableExperimentalPercentWidth. It allows percent width for tags, computed relatively to contentWidth. - take margins into account when scaling down images; - support for overriding image styles, including resizeMode; - support for minWidth, minHeight, maxWidth, maxHeight styles. This patch also fixes #141, #172 and provides the features offered in PR #242 and #315. OPTIMIZATIONS: The imageBoxDimensions are computed when required. In addition, requirements are also recomputed when necessary, preventing expensive operations from happening too often. Also, because we need to flatten the style prop to infer requirements, this flatten value is now cached and re-evaluated when appropriate. Finally, styles have been moved to a stylesheet when that is possible, to avoid commiting updates to the native side. Note that these performance optimizations are made possible by the high coverage rate of the HTMLImage component. fixes #141 fixes #172 closes #315 closes #242 --- README.md | 93 ++-- src/HTML.js | 32 +- src/HTMLImage.js | 490 ++++++++++++++---- src/HTMLRenderers.js | 301 ++++++----- src/__mocks__/react-native.js | 24 +- src/__tests__/README.MD | 4 +- src/__tests__/component.html.test.js | 17 + src/__tests__/component.image.test.js | 322 ++++++++++++ .../regression.141.custom-blur-image.test.js | 2 +- types/react-native-render-html/index.d.ts | 19 +- 10 files changed, 999 insertions(+), 305 deletions(-) create mode 100644 src/__tests__/component.html.test.js create mode 100644 src/__tests__/component.image.test.js diff --git a/README.md b/README.md index 84823cad4..3fcdb5576 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ - [![platforms](https://img.shields.io/badge/platforms-Android%20%7C%20iOS-brightgreen.svg?colorB=191A17)](#) [![npm](https://img.shields.io/npm/v/react-native-render-html/latest)](https://www.npmjs.com/package/react-native-render-html) [![](https://img.shields.io/npm/v/react-native-render-html/next)](#) @@ -68,8 +67,8 @@ An iOS/Android pure javascript react-native component that renders your HTML int ### Stable releases -| Minor | Branch | Documentation | Test Status | Latest | -| ----- | --------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | +| Minor | Branch | Documentation | Test Status | Latest | +| ----- | --------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ | | 4.2   |  [release/4.2](https://github.com/archriss/react-native-render-html/tree/release/4.2)   | [release/4.2/README.md](https://github.com/archriss/react-native-render-html/blob/release/4.2/README.md) | ![CI](https://github.com/archriss/react-native-render-html/workflows/CI/badge.svg?branch=release%2F4.2) | [![npm](https://img.shields.io/npm/v/react-native-render-html/release/4.2)](#) | | 4.1   |  [release/4.1](https://github.com/archriss/react-native-render-html/tree/release/4.1)   | [release/4.1/README.md](https://github.com/archriss/react-native-render-html/blob/release/4.1/README.md) | ![CI](https://github.com/archriss/react-native-render-html/workflows/CI/badge.svg?branch=release%2F4.1) | [![npm](https://img.shields.io/npm/v/react-native-render-html/release/4.1)](#) | | 4.0   |  [release/4.0](https://github.com/archriss/react-native-render-html/tree/release/4.0)   | [release/4.0/README.md](https://github.com/archriss/react-native-render-html/blob/release/4.0/README.md) | ![CI](https://github.com/archriss/react-native-render-html/workflows/CI/badge.svg?branch=release%2F4.0) | [![npm](https://img.shields.io/npm/v/react-native-render-html/release/4.0)](#) | @@ -106,7 +105,7 @@ npm install react-native-render-html ```javascript import React, { Component } from "react"; -import { ScrollView, Dimensions } from "react-native"; +import { ScrollView, useWindowDimensions } from "react-native"; import HTML from "react-native-render-html"; const htmlContent = ` @@ -116,54 +115,52 @@ const htmlContent = ` Look at how happy this native cat is `; -export default class Demo extends Component { - render() { - return ( - - - - ); - } +export default function Demo() { + const contentWidth = useWindowDimensions().width; + return ( + + + + ); } ``` ## Props -| Prop | Description | Type | Required/Default | -| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------- | ------------------------------------------------------------ | -| `renderers` | Your [custom renderers](#creating-custom-renderers) | `object` | Optional, some default ones are supplied (``, ``...) | -| `renderersProps` | Set of props accessible into your [custom renderers](#creating-custom-renderers) in `passProps` (4th argument) | `object` | Optional | -| `html` | HTML string to parse and render | `string` | Required | -| `uri` | _(experimental)_ remote website to parse and render | `string` | Optional | -| `decodeEntities` | Decode HTML entities of your content | `bool` | Optional, defaults to `true` | -| `imagesMaxWidth` | Resize your images to this maximum width, see [images](#images) | `number` | Optional | -| `staticContentMaxWidth` | Set a maximum width to non-responsive content (`