UNPKG

3.58 kBTypeScriptView Raw
1import { requireNativeViewManager } from '@unimodules/core';
2import PropTypes from 'prop-types';
3import * as React from 'react';
4import { View, ViewPropTypes } from 'react-native';
5
6type PropsType = React.ComponentProps<typeof View> & {
7 /**
8 * AdMob iOS library banner size constants
9 * (https://developers.google.com/admob/ios/banner)
10 * banner (320x50, Standard Banner for Phones and Tablets)
11 * largeBanner (320x100, Large Banner for Phones and Tablets)
12 * mediumRectangle (300x250, IAB Medium Rectangle for Phones and Tablets)
13 * fullBanner (468x60, IAB Full-Size Banner for Tablets)
14 * leaderboard (728x90, IAB Leaderboard for Tablets)
15 * smartBannerPortrait (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
16 * smartBannerLandscape (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
17 *
18 * banner is default
19 */
20 bannerSize:
21 | 'banner'
22 | 'largeBanner'
23 | 'mediumRectangle'
24 | 'fullBanner'
25 | 'leaderboard'
26 | 'smartBannerPortrait'
27 | 'smartBannerLandscape',
28 /**
29 * AdMob ad unit ID
30 */
31 adUnitID?: string,
32
33 /**
34 * Test device ID
35 */
36 testDeviceID?: string,
37
38 /**
39 * AdMob iOS library events
40 */
41 onAdViewDidReceiveAd?: () => void,
42 onDidFailToReceiveAdWithError?: (string) => void,
43 onAdViewWillPresentScreen?: () => void,
44 onAdViewWillDismissScreen?: () => void,
45 onAdViewDidDismissScreen?: () => void,
46 onAdViewWillLeaveApplication?: () => void,
47};
48
49type StateType = {
50 style: { width?: number, height?: number },
51};
52
53export default class AdMobBanner extends React.Component<PropsType, StateType> {
54 static propTypes = {
55 bannerSize: PropTypes.oneOf([
56 'banner',
57 'largeBanner',
58 'mediumRectangle',
59 'fullBanner',
60 'leaderboard',
61 'smartBannerPortrait',
62 'smartBannerLandscape',
63 ]),
64 adUnitID: PropTypes.string,
65 testDeviceID: PropTypes.string,
66 onAdViewDidReceiveAd: PropTypes.func,
67 onDidFailToReceiveAdWithError: PropTypes.func,
68 onAdViewWillPresentScreen: PropTypes.func,
69 onAdViewWillDismissScreen: PropTypes.func,
70 onAdViewDidDismissScreen: PropTypes.func,
71 onAdViewWillLeaveApplication: PropTypes.func,
72 ...ViewPropTypes,
73 };
74
75 static defaultProps = { bannerSize: 'smartBannerPortrait' };
76
77 state = { style: {} };
78
79 _handleSizeChange = ({ nativeEvent }: { nativeEvent: { width: number, height: number } }) => {
80 const { height, width } = nativeEvent;
81 this.setState({ style: { width, height } });
82 };
83
84 _handleDidFailToReceiveAdWithError = ({ nativeEvent }: { nativeEvent: { error: string } }) =>
85 this.props.onDidFailToReceiveAdWithError &&
86 this.props.onDidFailToReceiveAdWithError(nativeEvent.error);
87
88 render() {
89 return (
90 <View style={this.props.style}>
91 <ExpoBannerView
92 style={this.state.style}
93 adUnitID={this.props.adUnitID}
94 bannerSize={this.props.bannerSize}
95 testDeviceID={this.props.testDeviceID}
96 onSizeChange={this._handleSizeChange}
97 onAdViewDidReceiveAd={this.props.onAdViewDidReceiveAd}
98 onDidFailToReceiveAdWithError={this._handleDidFailToReceiveAdWithError}
99 onAdViewWillPresentScreen={this.props.onAdViewWillPresentScreen}
100 onAdViewWillDismissScreen={this.props.onAdViewWillDismissScreen}
101 onAdViewDidDismissScreen={this.props.onAdViewDidDismissScreen}
102 onAdViewWillLeaveApplication={this.props.onAdViewWillLeaveApplication}
103 />
104 </View>
105 );
106 }
107}
108
109const ExpoBannerView = requireNativeViewManager('ExpoAdsAdMobBannerView');