rn-tweet-embed
Version:
WebView based embedded Tweet for React Native
143 lines (133 loc) • 4.57 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import WebView from 'react-native-webview';
import makeWebshell, { HandleHTMLDimensionsFeature } from '@formidable-webview/webshell';
import React, { useState, useCallback, useRef, useMemo } from 'react';
import { Animated, Image, Pressable, Text, View, StyleSheet } from 'react-native';
import { useIsMounted } from './useIsMounted';
import { tweetHtml } from './utils';
const Webshell = makeWebshell(WebView, new HandleHTMLDimensionsFeature());
const Tweet = props => {
var _props$webViewProps$s, _props$webViewProps;
const isMounted = useIsMounted();
const [height, setHeight] = useState(1000);
const [loading, setLoading] = useState(true);
const scale = useRef(new Animated.Value(1)).current;
const opacity = useRef(new Animated.Value(1)).current;
const _onDOMHTMLDimensions = useCallback(_ref => {
let {
content: {
height
}
} = _ref;
setHeight(height);
}, []);
const _onPressIn = useCallback(() => Animated.parallel([Animated.spring(scale, {
toValue: 1.03,
useNativeDriver: true
}), Animated.timing(opacity, {
toValue: 0.85,
useNativeDriver: true,
duration: 100
})]).start(), []);
const _onPressOut = useCallback(() => Animated.parallel([Animated.spring(scale, {
toValue: 1,
useNativeDriver: true
}), Animated.timing(opacity, {
toValue: 1,
useNativeDriver: true,
duration: 100
})]).start(), []);
const _onMessage = useCallback(_ref2 => {
let {
nativeEvent
} = _ref2;
try {
const {
loaded
} = JSON.parse(nativeEvent.data);
if (loaded && isMounted.current) setLoading(false);
} catch (e) {}
}, [isMounted === null || isMounted === void 0 ? void 0 : isMounted.current]);
const embedParams = useMemo(() => props.embedParams || {
align: 'center',
...(props.theme === 'dark' ? {
theme: 'dark'
} : {})
}, [props.theme, props.embedParams]);
const html = tweetHtml(embedParams, props.tweetId, props.tweetUrl);
if (!html || !html.length) return /*#__PURE__*/React.createElement(View, {
style: styles.msgContainer
}, /*#__PURE__*/React.createElement(Image, {
source: require('./assets/twitter_logo.png'),
resizeMode: "contain",
style: styles.logo
}), /*#__PURE__*/React.createElement(Text, {
style: {
color: props.theme === 'dark' ? 'rgba(255,255,255,0.8)' : 'darkgray',
marginLeft: 5
}
}, "Unable to render Tweet..."));
return /*#__PURE__*/React.createElement(Animated.View, {
style: [styles.container, {
height,
transform: [{
scale
}],
opacity
}]
}, /*#__PURE__*/React.createElement(Webshell, _extends({
javaScriptEnabled: true,
onMessage: _onMessage,
source: {
html
},
onDOMHTMLDimensions: _onDOMHTMLDimensions
}, props.webViewProps || {}, {
style: [styles.webView, (_props$webViewProps$s = (_props$webViewProps = props.webViewProps) === null || _props$webViewProps === void 0 ? void 0 : _props$webViewProps.style) !== null && _props$webViewProps$s !== void 0 ? _props$webViewProps$s : {}]
})), loading && /*#__PURE__*/React.createElement(View, {
style: styles.msgContainer
}, /*#__PURE__*/React.createElement(Image, {
source: require('./assets/twitter_logo.png'),
resizeMode: "contain",
style: styles.logo
}), /*#__PURE__*/React.createElement(Text, {
style: {
color: props.theme === 'dark' ? 'rgba(255,255,255,0.8)' : 'darkgray',
marginLeft: 5
}
}, "Loading...")), !!props.interceptPress && /*#__PURE__*/React.createElement(Pressable, {
onPressIn: _onPressIn,
onPressOut: _onPressOut,
style: styles.pressOverlay,
onPress: props.interceptPress
}));
};
const styles = StyleSheet.create({
container: {
backgroundColor: 'transparent',
position: 'relative'
},
webView: {
backgroundColor: 'transparent'
},
logo: {
height: 60,
width: 60
},
pressOverlay: {
position: 'absolute',
height: '100%',
width: '100%',
backgroundColor: 'transparent',
top: 0,
left: 0,
right: 0
},
msgContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
}
});
export default Tweet;
//# sourceMappingURL=Tweet.js.map