File tree Expand file tree Collapse file tree 3 files changed +32
-5
lines changed
packages/react-native-web/src/exports/Image Expand file tree Collapse file tree 3 files changed +32
-5
lines changed Original file line number Diff line number Diff line change @@ -317,14 +317,14 @@ exports[`components/Image prop "style" removes other unsupported View styles 1`]
317
317
>
318
318
<div
319
319
class = " css-view-1dbjc4n r-backgroundColor-1niwhzg r-backgroundPosition-vvn4in r-backgroundRepeat-u6sd8q r-backgroundSize-4gszlv r-bottom-1p0dtai r-height-1pi2tsx r-left-1d2f490 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-width-13qz1uu r-zIndex-1wyyakw"
320
- style = " filter: url(#tint-31 );"
320
+ style = " filter: url(#tint-33 );"
321
321
/>
322
322
<svg
323
323
style = " position: absolute; height: 0px; visibility: hidden; width: 0px;"
324
324
>
325
325
<defs >
326
326
<filter
327
- id = " tint-31 "
327
+ id = " tint-33 "
328
328
>
329
329
<feflood
330
330
flood-color = " blue"
@@ -366,7 +366,7 @@ exports[`components/Image prop "style" supports "tintcolor" property (convert to
366
366
>
367
367
<div
368
368
class = " css-view-1dbjc4n r-backgroundColor-1niwhzg r-backgroundPosition-vvn4in r-backgroundRepeat-u6sd8q r-backgroundSize-4gszlv r-bottom-1p0dtai r-height-1pi2tsx r-left-1d2f490 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-width-13qz1uu r-zIndex-1wyyakw"
369
- style = " background-image: url(https://google.com/favicon.ico); filter: url(#tint-30 );"
369
+ style = " background-image: url(https://google.com/favicon.ico); filter: url(#tint-32 );"
370
370
/>
371
371
<img
372
372
alt = " "
@@ -379,7 +379,7 @@ exports[`components/Image prop "style" supports "tintcolor" property (convert to
379
379
>
380
380
<defs >
381
381
<filter
382
- id = " tint-30 "
382
+ id = " tint-32 "
383
383
>
384
384
<feflood
385
385
flood-color = " red"
Original file line number Diff line number Diff line change 1
1
/* eslint-env jasmine, jest */
2
2
/* eslint-disable react/jsx-no-bind */
3
3
4
+ import * as AssetRegistry from '../../../modules/AssetRegistry' ;
4
5
import Image from '../' ;
5
6
import ImageLoader from '../../../modules/ImageLoader' ;
6
7
import ImageUriCache from '../ImageUriCache' ;
8
+ import PixelRatio from '../../PixelRatio' ;
7
9
import React from 'react' ;
8
10
import { render } from '@testing-library/react' ;
9
11
@@ -200,6 +202,23 @@ describe('components/Image', () => {
200
202
loadCallback ( ) ;
201
203
expect ( container . firstChild ) . toMatchSnapshot ( ) ;
202
204
} ) ;
205
+
206
+ test ( 'it correctly selects the source scale' , ( ) => {
207
+ AssetRegistry . getAssetByID = jest . fn ( ( ) => ( {
208
+ httpServerLocation : 'static' ,
209
+ name : 'img' ,
210
+ scales : [ 1 , 2 , 3 ] ,
211
+ type : 'png'
212
+ } ) ) ;
213
+
214
+ PixelRatio . get = jest . fn ( ( ) => 1.0 ) ;
215
+ let { container } = render ( < Image source = { 1 } /> ) ;
216
+ expect ( container . querySelector ( 'img' ) . src ) . toBe ( 'http://localhost/static/img.png' ) ;
217
+
218
+ PixelRatio . get = jest . fn ( ( ) => 2.2 ) ;
219
+ ( { container } = render ( < Image source = { 1 } /> ) ) ;
220
+ expect ( container . querySelector ( 'img' ) . src ) . toBe ( 'http://localhost/static/[email protected] ' ) ;
221
+ } ) ;
203
222
} ) ;
204
223
205
224
describe ( 'prop "style"' , ( ) => {
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import { getAssetByID } from '../../modules/AssetRegistry';
18
18
import resolveShadowValue from '../StyleSheet/resolveShadowValue' ;
19
19
import ImageLoader from '../../modules/ImageLoader' ;
20
20
import ImageUriCache from './ImageUriCache' ;
21
+ import PixelRatio from '../PixelRatio' ;
21
22
import StyleSheet from '../StyleSheet' ;
22
23
import TextAncestorContext from '../Text/TextAncestorContext' ;
23
24
import View from '../View' ;
@@ -70,7 +71,14 @@ const resolveAssetUri = source => {
70
71
if ( typeof source === 'number' ) {
71
72
// get the URI from the packager
72
73
const asset = getAssetByID ( source ) ;
73
- const scale = asset . scales [ 0 ] ;
74
+ let scale = asset . scales [ 0 ] ;
75
+ if ( asset . scales . length > 1 ) {
76
+ const preferredScale = PixelRatio . get ( ) ;
77
+ // Get the scale which is closest to the preferred scale
78
+ scale = asset . scales . reduce ( ( prev , curr ) =>
79
+ Math . abs ( curr - preferredScale ) < Math . abs ( prev - preferredScale ) ? curr : prev
80
+ ) ;
81
+ }
74
82
const scaleSuffix = scale !== 1 ? `@${ scale } x` : '' ;
75
83
uri = asset ? `${ asset . httpServerLocation } /${ asset . name } ${ scaleSuffix } .${ asset . type } ` : '' ;
76
84
} else if ( typeof source === 'string' ) {
You can’t perform that action at this time.
0 commit comments