UNPKG

@expo/vector-icons

Version:

Built-in support for 10 popular icon fonts and the tooling to create your own Icon components from your font and glyph map. This is a wrapper around react-native-vector-icons to make it compatible with Expo.

59 lines (48 loc) 1.41 kB
import React from 'react'; import { Text } from 'react-native'; import { Font } from 'expo'; import createIconSet from './vendor/react-native-vector-icons/lib/create-icon-set'; import createIconButtonComponent from './vendor/react-native-vector-icons/lib/icon-button'; export default function(glyphMap, fontName, expoAssetId) { const font = { [fontName]: expoAssetId }; const RNVIconComponent = createIconSet(glyphMap, fontName); class Icon extends React.Component { static propTypes = RNVIconComponent.propTypes; static defaultProps = RNVIconComponent.defaultProps; state = { fontIsLoaded: Font.isLoaded(fontName), }; async componentWillMount() { this._mounted = true; if (!this.state.fontIsLoaded) { await Font.loadAsync(font); this._mounted && this.setState({ fontIsLoaded: true }); } } componentWillUnmount() { this._mounted = false; } setNativeProps(props) { if (this._icon) { this._icon.setNativeProps(props); } } render() { if (!this.state.fontIsLoaded) { return <Text />; } return ( <RNVIconComponent ref={view => { this._icon = view; }} {...this.props} /> ); } } Icon.Button = createIconButtonComponent(Icon); Icon.glyphMap = glyphMap; Icon.font = font; return Icon; }