|
| 1 | +--- |
| 2 | +id: nb-icons |
| 3 | +title: NativeBase Icons |
| 4 | +--- |
| 5 | + |
| 6 | +`NativeBase Icons` was designed to make integration of icons in nativebase projects easier. It is a unified library which export icons and fonts for all the native and web platforms. |
| 7 | + |
| 8 | +## Built With |
| 9 | +- react-native-vector-icons |
| 10 | +- @expo/vector-icon |
| 11 | +- typescript |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +`yarn add @native-base/icons` |
| 16 | + |
| 17 | + |
| 18 | +### Next Js: |
| 19 | + |
| 20 | +- Add Fonts: |
| 21 | +1. To add all the fonts, write this piece of code in `_document.js` |
| 22 | + <br/> |
| 23 | + |
| 24 | + ```jsx |
| 25 | + import { default as NativebaseDocument } from "@native-base/next-adapter/document"; |
| 26 | + import fontsCSS from "@native-base/icons/FontsCSS" |
| 27 | + import { AppRegistry } from "react-native"; |
| 28 | + import { Main } from "next/document"; |
| 29 | + import * as React from "react"; |
| 30 | + |
| 31 | + class Document extends NativebaseDocument { |
| 32 | + // |
| 33 | + } |
| 34 | + |
| 35 | + async function getInitialProps({ renderPage }) { |
| 36 | + AppRegistry.registerComponent("Main", () => Main); |
| 37 | + const { getStyleElement } = AppRegistry.getApplication("Main"); |
| 38 | + const page = await renderPage(); |
| 39 | + const styles = [ |
| 40 | + // eslint-disable-next-line react/jsx-key |
| 41 | + <style dangerouslySetInnerHTML={{ __html: fontsCSS }} />, |
| 42 | + getStyleElement(), |
| 43 | + ]; |
| 44 | + return { ...page, styles: React.Children.toArray(styles) }; |
| 45 | + } |
| 46 | + |
| 47 | + Document.getInitialProps = getInitialProps; |
| 48 | + |
| 49 | + export default Document; |
| 50 | + ``` |
| 51 | + |
| 52 | +2. To add particular fonts: (For example, if you need to add AntDesignFonts and MaterialIconsFonts, write this piece of code in `_document.js`) |
| 53 | +<br/> |
| 54 | + |
| 55 | + ```jsx |
| 56 | + import { default as NativebaseDocument } from "@native-base/next-adapter/document"; |
| 57 | + import AntDesignFontFaceCSS from "@native-base/icons/FontsCSS/AntDesignFontFaceCSS"; |
| 58 | + import MaterialIconsFontFaceCSS from "@native-base/icons/FontsCSS/MaterialIconsFontFaceCSS"; |
| 59 | + |
| 60 | + const fontsCSS = AntDesignFontFaceCSS + MaterialIconsFontFaceCSS; |
| 61 | + |
| 62 | + class Document extends NativebaseDocument { |
| 63 | + // |
| 64 | + } |
| 65 | + |
| 66 | + async function getInitialProps({ renderPage }) { |
| 67 | + const res = await NativebaseDocument.getInitialProps({ renderPage }); |
| 68 | + const styles = [ |
| 69 | + // eslint-disable-next-line react/jsx-key |
| 70 | + <style dangerouslySetInnerHTML={{ __html: fontsCSS }} />, |
| 71 | + ...res.styles |
| 72 | + ]; |
| 73 | + return { ...res, styles: React.Children.toArray(styles) }; |
| 74 | + } |
| 75 | + |
| 76 | + Document.getInitialProps = getInitialProps; |
| 77 | + |
| 78 | + export default Document; |
| 79 | + ``` |
| 80 | + |
| 81 | +3. Update `next.config.js` with this code if you are using [`@native-base/next adapter`](https://github.com/GeekyAnts/native-base-next-adapter)): |
| 82 | + |
| 83 | + ```jsx |
| 84 | + const { withNativebase } = require("@native-base/next-adapter"); |
| 85 | + const path = require("path"); |
| 86 | +
|
| 87 | + module.exports = withNativebase({ |
| 88 | + dependencies: ["@native-base/icons"], |
| 89 | + nextConfig: { |
| 90 | + webpack: (config, options) => { |
| 91 | + config.module.rules.push({ |
| 92 | + test: /\.ttf$/, |
| 93 | + loader: "url-loader", // or directly file-loader |
| 94 | + include: path.resolve(__dirname, "node_modules/@native-base/icons"), |
| 95 | + }); |
| 96 | + config.resolve.alias = { |
| 97 | + ...(config.resolve.alias || {}), |
| 98 | + "react-native$": "react-native-web", |
| 99 | + }; |
| 100 | + config.resolve.extensions = [ |
| 101 | + ".web.js", |
| 102 | + ".web.ts", |
| 103 | + ".web.tsx", |
| 104 | + ...config.resolve.extensions, |
| 105 | + ]; |
| 106 | + return config; |
| 107 | + }, |
| 108 | + }, |
| 109 | + }); |
| 110 | + ``` |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +### Create React App: |
| 117 | + |
| 118 | +- Add Fonts: |
| 119 | +<br/> |
| 120 | + |
| 121 | +1. To add all the fonts, write this piece of code in `index.jsx/index.tsx`: |
| 122 | + <br/> |
| 123 | + |
| 124 | + ```jsx |
| 125 | + import fontsCSS from "@native-base/icons/FontsCSS"; |
| 126 | +
|
| 127 | + const style = document.createElement("style"); |
| 128 | + style.type = "text/css"; |
| 129 | + style.appendChild(document.createTextNode(fontsCSS)); |
| 130 | + document.head.appendChild(style); |
| 131 | + ``` |
| 132 | + <br/> |
| 133 | + |
| 134 | +2. To add a particular font |
| 135 | + <br/> |
| 136 | + |
| 137 | + ```jsx |
| 138 | + import AntDesignFontFaceCSS from "@native-base/icons/FontsCSS/AntDesignFontFaceCSS"; |
| 139 | + import MaterialIconsFontFaceCSS from "@native-base/icons/FontsCSS/MaterialIconsFontFaceCSS"; |
| 140 | + |
| 141 | + const fontsCSS = AntDesignFontFaceCSS + MaterialIconsFontFaceCSS; |
| 142 | + |
| 143 | + const style = document.createElement("style"); |
| 144 | + style.type = "text/css"; |
| 145 | + if (style.styleSheet) { |
| 146 | + style.styleSheet.cssText += fontsCSS; |
| 147 | + } else { |
| 148 | + style.appendChild(document.createTextNode(fontsCSS)); |
| 149 | + } |
| 150 | + document.head.appendChild(style); |
| 151 | + ``` |
| 152 | + |
| 153 | + |
| 154 | +### Expo |
| 155 | + |
| 156 | +- Add this configuration to .babel.config.js file in root directory: |
| 157 | + |
| 158 | + ```jsx |
| 159 | + module.exports = function (api) { |
| 160 | + api.cache(true); |
| 161 | + return { |
| 162 | + presets: ["babel-preset-expo"], |
| 163 | + plugins: [ |
| 164 | + [ |
| 165 | + "module-resolver", |
| 166 | + { |
| 167 | + alias: { |
| 168 | + "@native-base/icons": "@native-base/icons/lib", |
| 169 | + }, |
| 170 | + }, |
| 171 | + ], |
| 172 | + ], |
| 173 | + }; |
| 174 | + }; |
| 175 | + ``` |
| 176 | + |
| 177 | +## Usage |
| 178 | + |
| 179 | +```jsx |
| 180 | +import { Entypo } from "@native-base/icons"; |
| 181 | +
|
| 182 | +return <Icon as={Entypo} name="user"></Icon>; |
| 183 | +``` |
0 commit comments