Skip to content

Commit 41c00e6

Browse files
authored
Image - add OverlayIntensity (#1584)
* Image - add OverlayIntensity * Remove unused code * Modify opacity according to intensity and color * Fix typings
1 parent 0406a99 commit 41c00e6

20 files changed

+105
-22
lines changed

demo/src/screens/componentScreens/ImageScreen.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {Component} from 'react';
22
import {ScrollView} from 'react-native';
3-
import {View, Text, Image, Colors, Assets} from 'react-native-ui-lib';
3+
import {View, Text, Image, Colors, Assets, OverlayTypes} from 'react-native-ui-lib';
44
import {renderBooleanOption, renderRadioGroup, renderSliderOption} from '../ExampleScreenPresenter';
55

66
const IMAGE_URL =
@@ -38,11 +38,24 @@ enum SizeType {
3838
Percentage = '50%'
3939
}
4040

41-
class ImageScreen extends Component {
41+
interface State {
42+
cover: boolean;
43+
showOverlayContent: boolean;
44+
overlayType: 'none' | OverlayTypes['type'];
45+
overlayIntensity: OverlayTypes['intensity'];
46+
margin: number;
47+
showErrorImage: boolean;
48+
showSvg: boolean;
49+
isFile: boolean;
50+
sizeType: SizeType;
51+
}
52+
53+
class ImageScreen extends Component<{}, State> {
4254
state = {
4355
cover: true,
4456
showOverlayContent: false,
4557
overlayType: 'none',
58+
overlayIntensity: Image.overlayIntensityType.LOW,
4659
margin: 0,
4760
showErrorImage: false,
4861
showSvg: false,
@@ -75,13 +88,15 @@ class ImageScreen extends Component {
7588
}
7689

7790
renderImage() {
78-
const {cover, overlayType, margin, showErrorImage} = this.state;
91+
const {cover, overlayType, overlayIntensity, margin, showErrorImage} = this.state;
7992
return (
8093
<Image
94+
key={`${overlayType}-${overlayIntensity}`}
8195
source={{uri: showErrorImage ? BROKEN_URL : IMAGE_URL}}
8296
errorSource={Assets.images.demo.brokenImage}
8397
cover={cover}
8498
overlayType={overlayType !== 'none' ? overlayType : undefined}
99+
overlayIntensity={overlayIntensity}
85100
style={!cover && {width: DEFAULT_SIZE, height: DEFAULT_SIZE}}
86101
customOverlayContent={this.renderOverlayContent()}
87102
{...{[`margin-${margin}`]: true}}
@@ -109,7 +124,10 @@ class ImageScreen extends Component {
109124
{renderBooleanOption.call(this, 'Show as Cover Image', 'cover')}
110125
{renderBooleanOption.call(this, 'Show Overlay Content', 'showOverlayContent')}
111126
{renderBooleanOption.call(this, 'Show Error Image', 'showErrorImage')}
112-
{renderRadioGroup.call(this, 'Overlay Type', 'overlayType', {none: 'none', ...Image.overlayTypes})}
127+
<View row spread>
128+
{renderRadioGroup.call(this, 'Overlay Type', 'overlayType', {none: 'none', ...Image.overlayTypes})}
129+
{renderRadioGroup.call(this, 'Overlay Intensity', 'overlayIntensity', Image.overlayIntensityType)}
130+
</View>
113131
{renderSliderOption.call(this, 'Margin(margin-XX)', 'margin', {step: 4, min: 0, max: 40})}
114132
</>
115133
);

generatedTypes/src/components/image/index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { PureComponent } from 'react';
22
import { ImageProps as RNImageProps, ImageSourcePropType, NativeSyntheticEvent, ImageErrorEventData } from 'react-native';
33
import { ForwardRefInjectedProps, BaseComponentInjectedProps, MarginModifiers } from '../../commons/new';
4-
import { OverlayTypeType } from '../overlay';
4+
import { OverlayTypeType, OverlayIntensityType } from '../overlay';
55
export declare type ImageProps = RNImageProps & MarginModifiers & {
66
/**
77
* custom source transform handler for manipulating the image source (great for size control)
@@ -36,6 +36,10 @@ export declare type ImageProps = RNImageProps & MarginModifiers & {
3636
* https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/OverlaysScreen.tsx
3737
*/
3838
overlayType?: OverlayTypeType;
39+
/**
40+
* The intensity of the overlay ('LOW' | 'MEDIUM' | 'HIGH'), default is 'LOW'.
41+
*/
42+
overlayIntensity?: OverlayIntensityType;
3943
/**
4044
* Pass a custom color for the overlay
4145
*/
@@ -73,6 +77,7 @@ declare class Image extends PureComponent<Props, State> {
7377
BOTTOM: string;
7478
SOLID: string;
7579
};
80+
static overlayIntensityType: typeof OverlayIntensityType;
7681
sourceTransformer?: (props: any) => ImageSourcePropType;
7782
constructor(props: Props);
7883
static getDerivedStateFromProps(nextProps: Partial<Props>, prevState: State): {
@@ -123,6 +128,10 @@ declare const _default: React.ComponentClass<RNImageProps & Partial<Record<"marg
123128
* https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/OverlaysScreen.tsx
124129
*/
125130
overlayType?: string | undefined;
131+
/**
132+
* The intensity of the overlay ('LOW' | 'MEDIUM' | 'HIGH'), default is 'LOW'.
133+
*/
134+
overlayIntensity?: OverlayIntensityType | undefined;
126135
/**
127136
* Pass a custom color for the overlay
128137
*/

generatedTypes/src/components/overlay/index.d.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ declare const OVERLY_TYPES: {
66
BOTTOM: string;
77
SOLID: string;
88
};
9+
export declare enum OverlayIntensityType {
10+
LOW = "low",
11+
MEDIUM = "medium",
12+
HIGH = "high"
13+
}
914
export declare type OverlayTypeType = typeof OVERLY_TYPES[keyof typeof OVERLY_TYPES];
1015
export declare type OverlayTypes = {
1116
/**
1217
* The type of overlay to set on top of the image
1318
*/
1419
type?: OverlayTypeType;
20+
/**
21+
* The intensity of the gradient, default is 'LOW'.
22+
*/
23+
intensity?: OverlayIntensityType;
1524
/**
1625
* The overlay color
1726
*/
@@ -34,9 +43,8 @@ declare class Overlay extends PureComponent<OverlayTypes> {
3443
BOTTOM: string;
3544
SOLID: string;
3645
};
46+
static intensityTypes: typeof OverlayIntensityType;
3747
getStyleByType(type?: string | undefined): ("" | {
38-
backgroundColor: string;
39-
} | undefined)[] | ("" | {
4048
bottom: undefined;
4149
top: number;
4250
height: string;
@@ -51,9 +59,12 @@ declare class Overlay extends PureComponent<OverlayTypes> {
5159
}[];
5260
} | {
5361
tintColor: string;
54-
} | undefined)[] | undefined;
62+
} | undefined)[] | {
63+
backgroundColor: string;
64+
} | undefined;
5565
renderCustomContent: () => JSX.Element;
5666
renderImage: (style: any, source: ImageSourcePropType) => JSX.Element;
67+
getImageSource: (type?: string | undefined, intensity?: OverlayIntensityType | undefined) => any;
5768
render(): JSX.Element;
5869
}
5970
export default Overlay;

src/components/image/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Constants from '../../helpers/Constants';
1414
import {asBaseComponent, ForwardRefInjectedProps, BaseComponentInjectedProps, MarginModifiers} from '../../commons/new';
1515
// @ts-ignore
1616
import Assets from '../../assets';
17-
import Overlay, {OverlayTypeType} from '../overlay';
17+
import Overlay, {OverlayTypeType, OverlayIntensityType} from '../overlay';
1818
import SvgImage from './SvgImage';
1919

2020

@@ -52,6 +52,10 @@ export type ImageProps = RNImageProps & MarginModifiers & {
5252
* https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/OverlaysScreen.tsx
5353
*/
5454
overlayType?: OverlayTypeType;
55+
/**
56+
* The intensity of the overlay ('LOW' | 'MEDIUM' | 'HIGH'), default is 'LOW'.
57+
*/
58+
overlayIntensity?: OverlayIntensityType;
5559
/**
5660
* Pass a custom color for the overlay
5761
*/
@@ -89,6 +93,7 @@ class Image extends PureComponent<Props, State> {
8993
};
9094

9195
public static overlayTypes = Overlay.overlayTypes;
96+
public static overlayIntensityType = Overlay.intensityTypes;
9297

9398
sourceTransformer?: (props: any) => ImageSourcePropType;
9499

@@ -171,6 +176,7 @@ class Image extends PureComponent<Props, State> {
171176
cover,
172177
aspectRatio,
173178
overlayType,
179+
overlayIntensity,
174180
overlayColor,
175181
customOverlayContent,
176182
modifiers,
@@ -199,7 +205,12 @@ class Image extends PureComponent<Props, State> {
199205
source={source}
200206
>
201207
{(overlayType || customOverlayContent) && (
202-
<Overlay type={overlayType} color={overlayColor} customContent={customOverlayContent}/>
208+
<Overlay
209+
type={overlayType}
210+
intensity={overlayIntensity}
211+
color={overlayColor}
212+
customContent={customOverlayContent}
213+
/>
203214
)}
204215
</ImageView>
205216
);
Loading
1.22 KB
Loading
1.66 KB
Loading
3.19 KB
Loading
5.57 KB
Loading
Loading
Loading
1.72 KB
Loading
3.38 KB
Loading
5.76 KB
Loading

src/components/overlay/index.tsx

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import {isUndefined} from 'lodash';
12
import React, {PureComponent} from 'react';
23
import {StyleSheet, Image, ImageSourcePropType} from 'react-native';
34
import {Colors} from '../../style';
45
import View from '../view';
56

6-
const gradientImage = require('./assets/GradientOverlay.png');
7+
const gradientImageLow = require('./assets/GradientOverlayLow.png');
8+
const gradientImageMed = require('./assets/GradientOverlayMedium.png');
9+
const gradientImageHigh = require('./assets/GradientOverlayHigh.png');
710

811
const OVERLY_TYPES = {
912
VERTICAL: 'vertical',
@@ -12,13 +15,23 @@ const OVERLY_TYPES = {
1215
SOLID: 'solid'
1316
};
1417

18+
export enum OverlayIntensityType {
19+
LOW = 'low',
20+
MEDIUM = 'medium',
21+
HIGH = 'high'
22+
}
23+
1524
export type OverlayTypeType = typeof OVERLY_TYPES[keyof typeof OVERLY_TYPES];
1625

1726
export type OverlayTypes = {
1827
/**
1928
* The type of overlay to set on top of the image
2029
*/
2130
type?: OverlayTypeType;
31+
/**
32+
* The intensity of the gradient, default is 'LOW'.
33+
*/
34+
intensity?: OverlayIntensityType;
2235
/**
2336
* The overlay color
2437
*/
@@ -37,17 +50,29 @@ export type OverlayTypes = {
3750
class Overlay extends PureComponent<OverlayTypes> {
3851
static displayName = 'Overlay';
3952
static overlayTypes = OVERLY_TYPES;
53+
static intensityTypes = OverlayIntensityType;
4054

4155
getStyleByType(type = this.props.type) {
42-
const {color} = this.props;
56+
const {color, intensity} = this.props;
4357

4458
switch (type) {
4559
case OVERLY_TYPES.TOP:
4660
return [styles.top, color && {tintColor: color}];
4761
case OVERLY_TYPES.BOTTOM:
4862
return [styles.bottom, color && {tintColor: color}];
49-
case OVERLY_TYPES.SOLID:
50-
return [styles.solid, color && {backgroundColor: color}];
63+
case OVERLY_TYPES.SOLID: {
64+
if (isUndefined(color)) {
65+
const opacity =
66+
intensity === OverlayIntensityType.HIGH ? 0.75 : intensity === OverlayIntensityType.MEDIUM ? 0.55 : 0.4;
67+
return {backgroundColor: Colors.rgba(Colors.grey10, opacity)};
68+
} else if (color === Colors.white) {
69+
const opacity =
70+
intensity === OverlayIntensityType.HIGH ? 0.85 : intensity === OverlayIntensityType.MEDIUM ? 0.7 : 0.45;
71+
return {backgroundColor: Colors.rgba(Colors.white, opacity)};
72+
} else {
73+
return {backgroundColor: color};
74+
}
75+
}
5176
default:
5277
break;
5378
}
@@ -66,23 +91,35 @@ class Overlay extends PureComponent<OverlayTypes> {
6691
return <Image style={[styles.container, style]} resizeMode={'stretch'} source={source}/>;
6792
};
6893

94+
getImageSource = (type?: OverlayTypeType, intensity?: OverlayIntensityType) => {
95+
if (type !== OVERLY_TYPES.SOLID) {
96+
if (intensity === OverlayIntensityType.MEDIUM) {
97+
return gradientImageMed;
98+
} else if (intensity === OverlayIntensityType.HIGH) {
99+
return gradientImageHigh;
100+
} else {
101+
return gradientImageLow;
102+
}
103+
}
104+
};
105+
69106
render() {
70-
const {type, customContent} = this.props;
71-
const image = type !== OVERLY_TYPES.SOLID ? gradientImage : undefined;
107+
const {type, intensity, customContent} = this.props;
108+
const imageSource = this.getImageSource(type, intensity);
72109

73110
if (type === OVERLY_TYPES.VERTICAL) {
74111
return (
75112
<>
76-
{this.renderImage([this.getStyleByType(OVERLY_TYPES.TOP), styles.vertical], image)}
77-
{this.renderImage([this.getStyleByType(OVERLY_TYPES.BOTTOM), styles.vertical], image)}
113+
{this.renderImage([this.getStyleByType(OVERLY_TYPES.TOP), styles.vertical], imageSource)}
114+
{this.renderImage([this.getStyleByType(OVERLY_TYPES.BOTTOM), styles.vertical], imageSource)}
78115
{customContent && this.renderCustomContent()}
79116
</>
80117
);
81118
}
82119

83120
return (
84121
<>
85-
{type && this.renderImage(this.getStyleByType(), image)}
122+
{type && this.renderImage(this.getStyleByType(), imageSource)}
86123
{customContent && this.renderCustomContent()}
87124
</>
88125
);
@@ -108,9 +145,6 @@ const styles = StyleSheet.create({
108145
vertical: {
109146
height: '40%'
110147
},
111-
solid: {
112-
backgroundColor: Colors.rgba(Colors.grey10, 0.4)
113-
},
114148
customContent: {
115149
...StyleSheet.absoluteFillObject
116150
}

0 commit comments

Comments
 (0)