<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Endy Hardy on Medium]]></title>
        <description><![CDATA[Stories by Endy Hardy on Medium]]></description>
        <link>https://medium.com/@ndyhrdy?source=rss-8465610505a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ryeVwugnB1HWnUS2j-TeTA.jpeg</url>
            <title>Stories by Endy Hardy on Medium</title>
            <link>https://medium.com/@ndyhrdy?source=rss-8465610505a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:18:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ndyhrdy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React Native In-App Remote Notification UI]]></title>
            <link>https://levelup.gitconnected.com/react-native-in-app-remote-notification-ui-8e367fcbe96b?source=rss-8465610505a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e367fcbe96b</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[messaging]]></category>
            <category><![CDATA[notifications]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[ui]]></category>
            <dc:creator><![CDATA[Endy Hardy]]></dc:creator>
            <pubDate>Tue, 08 Sep 2020 19:29:33 GMT</pubDate>
            <atom:updated>2020-09-09T07:38:04.801Z</atom:updated>
            <content:encoded><![CDATA[<p>A quick recipe to creating an interactive remote notification banner UI in a React Native app, with animations and interactivity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g-LSFw0HmlWax3XWDz3ESw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@jonasleupe?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Jonas Leupe</a> on <a href="https://unsplash.com/s/photos/notifications?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Remote notifications are an essential part of many types of apps and their use-cases. They are the preferred way for systems to communicate time-sensitive information, from marketing to transactional purposes, to their users individually (using device tokens), a specified target group (using topics), or as a whole. To do this, React Native developers often turn to the ever-popular <a href="https://rnfirebase.io/https://rnfirebase.io/">RNFirebase</a> package by Invertase, which provides a wrapper around the native Firebase SDK for Android and iOS, and taking advantage of the <a href="https://rnfirebase.io/messaging/usage">Messaging</a> module to listen to incoming remote messages sent from Firebase Cloud Messaging (FCM).</p><h3>Why?</h3><p>One might wonder, why the need for a custom notification UI? Won’t remote notifications just automatically be displayed in the notification shade? Looking at the docs, apparently, notifications cannot be displayed while the app is in foreground (while the user is actively using the app) — see table below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*ZwgscNPaFQlWmPAIl_u8sQ.png" /><figcaption>Notification display support in RNFirebase — Link <a href="https://rnfirebase.io/messaging/usage#notifications">here</a></figcaption></figure><p>Notifications will only be shown in the notification shade if the application state is in background or quit/terminated. Hence, some form of in-app UI is needed to display the notifications to the user as they are received.</p><h3>How do I look?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*3uyUOHqsc3y0lrdFhRv88g.jpeg" /><figcaption>Looks easy, right?</figcaption></figure><p>As it is fully custom-made, the notification UI design and interactivity can be tailored to match the app’s overall look and feel. The following style is how I prefer to make it myself.</p><h4>Notification component</h4><p>Creating a new standalone component to handle all in-app notification logic is key, since the UI would need to be able to be displayed in and interact with all parts of the app. It can then be attached to the root component (or somewhere close).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fd26d69e19e7f4980d3d93b03fdd216/href">https://medium.com/media/9fd26d69e19e7f4980d3d93b03fdd216/href</a></iframe><p><em>Mind the TypeScript :)</em></p><h4>Basic UI</h4><p>Now, let’s make a basic design to display the notification title and body. Here I use <a href="https://github.com/vadimdemedes/tailwind-rn">TailwindRN</a> to handle the styling (because why not). I honestly love using <a href="https://tailwindcss.com/">TailwindCSS</a> on the web, and I’m grateful I can get a similar experience in React Native. If you haven’t tried it, I strongly suggest that you give it a shot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/374b6efb52dd851a7f12a1f2f1cbd524/href">https://medium.com/media/374b6efb52dd851a7f12a1f2f1cbd524/href</a></iframe><p>Now we’re getting somewhere.</p><p>In the example, the component basically displays a dark gray touchable banner, with white title and body text, inside an absolute-ly positioned container (in this case, anchored to the bottom of the screen). The banner uses a touchable component for it to be able to handle actions on presses (more on that later).</p><h3>Be a good listener</h3><p>Next, the component would need to listen (subscribe) to incoming remote messages and store it in state. To do that, it needs to create a listener on mount by invoking the onMessage method of the Messaging module. It also needs to make sure that the remote message is indeed a ‘notification’ (has a title and a body). Display the title and body inside the Text components made previously.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a5a05dac0232565287d77d961406d60/href">https://medium.com/media/5a5a05dac0232565287d77d961406d60/href</a></iframe><p>Go on, try it out! A message sent to the device should automatically display its content in the banner. It should also disappear by itself after 5 seconds.</p><h3>Show ‘em your moves!</h3><p>Now comes the fun part — Animations! But don’t get too carried away, it’s just a notification banner and, at least for me, movement in the UI should be kept minimal and non-distracting. Adding subtle enter and exit transition animations to the component goes a little something like this.</p><ol><li>Define a transition animation value, starting from 0. The value would then range from 300 (hidden) to 0 (shown), to be used as the container’s translateY value. <strong>Note:</strong> 300 is just a convenience value to skip measuring real banner height.</li><li>Define the ‘exit’ transition animation, which should change the transition value from 0 to 300.</li><li>Define the ‘enter’ transition animation, which is like the ‘exit’ animation, but the other way around.</li><li>Fire the ‘enter’ animation when a new remote message comes in.</li><li>Fire the ‘exit’ animation 5 seconds the message came in (modify the flow in the previous step slightly).</li><li>Change the container View component to an Animated.View component, and set its translateY value from the transition animation value.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64086c814f11b9f3207d34f26d855a62/href">https://medium.com/media/64086c814f11b9f3207d34f26d855a62/href</a></iframe><p>Sick moves! Looks pretty, huh? More importantly, it limits itself to only adding a sense of interactivity and natural movement, by not being too distracting or flashy.</p><h3>Out of the way!</h3><p>Another basic feature of notifications we often see is its ability to be dismissed. On the Android notification shade, this is usually done by swiping left or right. Let’s see what we can do with React Native on this front.</p><p>React Native provides an easy way to handle gestures on a component using its PanResponder API. To handle swipes, the component’s pan handlers would need to respond to horizontal movement and translate it into the container’s translateX value. That movement value can also be interpolated to an opacity value to give it a ‘fading’ effect as it is being dismissed. In sequence, it goes something like this.</p><ol><li>Define a swipe animation value. This value would map to the pan responder’s horizontal movement.</li><li>Define a resetSwipe animation, to reset the container to its original position if the horizontal movement is not fast enough to qualify as a dismiss action.</li><li>Define left and right swipeOut animations, to completely move the container out of the screen if the horizontal movement qualifies as a dismiss action.</li><li>Define a panResponder to attach to the touchable component and handle gestures acted upon it. Map the onPanResponderMove event’s dx (horizontal movement) to the swipe animation value.</li><li>Attach the panResponder&#39;s panHandlers to the touchable component.</li><li>Add a translateX value to the container component to react to the swipe animation value.</li><li>Define an opacity value as an interpolation from the swipe animation value.</li><li>Add the opacity value to the container component.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb7ac6388c5c175676bc0f440448e800/href">https://medium.com/media/bb7ac6388c5c175676bc0f440448e800/href</a></iframe><p>Now, by swiping left or right fast enough, we can set the notification aside and out of the way. Neat!</p><p>And just like that, the app now has the ability to display (with animation) and dismiss in-app remote notifications. What a time to be alive.</p><p>Another great thing about remote messages is that they can contain data (other than title and body), that we can get and use in the app. A couple of common use-cases are:</p><ol><li>Setting a route/deeplink URL for the app to navigate to when the user presses the notification. This can be achieved in conjunction with <a href="https://reactnavigation.org/">React Navigation</a> — stay tuned for an article on this!</li><li>Setting a specified key to refresh/revalidate data from server. This can be done in conjunction with <a href="https://swr.vercel.app/">SWR </a>— also, stay tuned for this!</li></ol><p>Also check out how to make another commonly used mobile app UI, the <a href="https://medium.com/@ndyhrdy/making-the-bottom-sheet-modal-using-react-native-e226a30bed13">bottom swipeable modal</a>, in React Native.</p><p>It’s amazing how React Native with Firebase can enable more and more developers to easily create and ship quality apps in such a short time. I hope this recipe of an article can do a bit of good to everyone taking the time to read. As always, thank you for reading, it means the world to me. Please do leave responses if you like. I would always love to connect to new people, whoever you are. Find me at <a href="https://endyhardy.co/">endyhardy.co</a> for more info.</p><p>Have a great day, stay safe, wear your mask, wash your hands, and keep on creating!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e367fcbe96b" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/react-native-in-app-remote-notification-ui-8e367fcbe96b">React Native In-App Remote Notification UI</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Configuring Env Vars for React Native Apps on Bitrise]]></title>
            <link>https://levelup.gitconnected.com/configuring-env-vars-for-react-native-apps-on-bitrise-601362d602f9?source=rss-8465610505a------2</link>
            <guid isPermaLink="false">https://medium.com/p/601362d602f9</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[env]]></category>
            <category><![CDATA[bitrise]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <dc:creator><![CDATA[Endy Hardy]]></dc:creator>
            <pubDate>Mon, 27 Jul 2020 02:59:12 GMT</pubDate>
            <atom:updated>2020-07-27T02:59:12.912Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*BSOUn4f42S4eNmli" /><figcaption>Talkin’ ‘bout the environment</figcaption></figure><p>You might have heard or even been using this cool package for managing environment variables in React Native called <a href="https://github.com/luggit/react-native-config">react-native-config</a>. It lets you define your app’s environment variables on a .env file, and inject the values into the Javascript codebase.</p><p>react-native-config by default only reads values from .env at the root of the project. You can also specify which file it should read from. But .env files are excluded from git, since different machines would usually need different values depending on their environment, which presents a problem when building on a CI tool (in my case, <a href="https://www.bitrise.io/">Bitrise</a>) where values are provided only as variables. Here’s how I configured my Bitrise build workflow to get react-native-config to read environment values.</p><p>A typical .env file usually looks something like this:</p><pre>API_BASE_URL=https://myapi.test<br>API_CLIENT_ID=1234<br>API_CLIENT_SECRET=my-secret</pre><p>Then in Javascript files, the values would be accessed like so:</p><pre>import Config from &#39;react-native-config&#39;</pre><pre>console.log(Config.API_BASE_URL);     // <a href="https://myapi.test/">https://myapi.test</a><br>console.log(Config.API_CLIENT_ID);    // 1234<br>console.log(Config.API_CLIENT_SECRET) // my-secret</pre><p>However, .env files are not committed to git, hence Bitrise would have no access to these values when building. So I headed over to the Workflow Editor: Env Vars section…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cWHfUfK75UewoiqyiaO98w.png" /><figcaption>Bitrise Env Vars tab in Workflow Editor</figcaption></figure><p>And so I thought “Hey! This is neat! I could just put the values here then Bitrise and react-native-config will work together to magically read the values, right?”. <strong>Wrong</strong>. Like the naive “devops” engineer I am, I pasted in the values and went ahead starting a build. Surely enough, the build succeeded (yay!), yet once I ran the app, I’m greeted with a nice familiar crash.</p><p>Upon investigating, I found that the crash was caused by undefined env values. “How could this be? I was so sure…” — no I wasn’t, I never was. It turns out, those env vars I have pasted in was only to provide values to the build “workflow”. So to fix this, I added a ‘Script’ step in the workflow right after the “Git Clone Repository” step, to read the env values and write them into a new .env file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*idRywTOPfMj_RvFc49wUNQ.png" /><figcaption>Add ‘Script’ step after cloning</figcaption></figure><p>In the script content, I put a set of commands like this:</p><pre>echo &quot;API_BASE_URL=$API_BASE_URL&quot; &gt;&gt; $BITRISE_SOURCE_DIR/.env<br>echo &quot;API_CLIENT_ID=$API_CLIENT_ID&quot; &gt;&gt; $BITRISE_SOURCE_DIR/.env<br>echo &quot;API_CLIENT_SECRET=$API_CLIENT_SECRET&quot; &gt;&gt; $BITRISE_SOURCE_DIR/.env</pre><pre>cat $BITRISE_SOURCE_DIR/.env</pre><p>So to put simply, all it does is read values provided in the Workflow Env Vars, echo it out line by line in a standard dotenv format into a new .env file in the root source directory. It also echoes out the contents of the new .env file to the logs for good measure. Now react-native-config will actually have a file to look for values!</p><p>Quite convenient, though I couldn’t achieve what I initially wanted. I would only need to remember adding new variables into the script step.</p><p>On a side note, this was a reasonably hasty attempt in achieving my specific case, for which I have not thoroughly reviewed the performance and security-wise. If you happen to know a better method of doing this, please do let me know :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=601362d602f9" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/configuring-env-vars-for-react-native-apps-on-bitrise-601362d602f9">Configuring Env Vars for React Native Apps on Bitrise</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dark Mode on React Native with Redux]]></title>
            <link>https://medium.com/swlh/dark-mode-on-react-native-with-redux-9c2535f9c2e0?source=rss-8465610505a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c2535f9c2e0</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[dark-mode]]></category>
            <category><![CDATA[themes]]></category>
            <dc:creator><![CDATA[Endy Hardy]]></dc:creator>
            <pubDate>Tue, 14 Apr 2020 09:08:21 GMT</pubDate>
            <atom:updated>2020-04-14T16:37:08.856Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4eKOyHYluRZRgvovki6DGQ.png" /></figure><p>With all of the rage of having Dark Mode for literally every app out there, I think I’m way past due to post an article about it. So this will be about how I managed to get Dark Mode (and basically, Color Themes) working on a React Native app with the help of Redux.</p><p><em>Disclamer: This is by no means a tutorial or a “best practices” post about how to do Dark Mode. It’s more of a diary I’d like to look back to one day, hopefully to realize how naive I was and have a good ol’ nostalgic laugh after.</em></p><h3>What I’m Aiming For</h3><p>…is basically a simple yet fullproof way of getting a React Native app to change its theme colors instantly using Redux. I like the idea of an app that changes modes/themes without restarting.</p><h4>A Word of Warning</h4><p>Note that implementing theme colors on an existing codebase may well be a long process, depending on how you have structured your components and styles, since it involves rewriting existing styles and making the theme available everywhere it’s needed. But it will be fun nonetheless!</p><h3>Colors!</h3><p>First things first, I need to set up my theme color configurations somewhere in the codebase. Keeping things simple, I’m gonna put them in a themes.js file in the root project directory. Inside, there are configurations for a default theme and a default dark theme, which can be defined like so:</p><pre>const defaultColors = {<br>  primary: &#39;#ED8936&#39;,<br>  black: &#39;#000000&#39;,<br>  white: &#39;#FFFFFF&#39;,<br>  &#39;100&#39;: &#39;#F7FAFC&#39;,<br>  &#39;200&#39;: &#39;#EDF2F7&#39;,<br>  &#39;300&#39;: &#39;#E2E8F0&#39;,<br>  &#39;400&#39;: &#39;#CBD5E0&#39;,<br>  &#39;500&#39;: &#39;#A0AEC0&#39;,<br>  &#39;600&#39;: &#39;#718096&#39;,<br>  &#39;700&#39;: &#39;#4A5568&#39;,<br>  &#39;800&#39;: &#39;#2D3748&#39;,<br>  &#39;900&#39;: &#39;#1A202C&#39;,<br>};</pre><pre>const darkColors = {<br>  primary: &#39;#F6AD55&#39;,<br>  black: defaultColors.white,<br>  white: defaultColors.black,<br>  &#39;100&#39;: defaultColors[900],<br>  &#39;200&#39;: defaultColors[800],<br>  &#39;300&#39;: defaultColors[700],<br>  &#39;400&#39;: defaultColors[600],<br>  &#39;500&#39;: defaultColors[500],<br>  &#39;600&#39;: defaultColors[400],<br>  &#39;700&#39;: defaultColors[300],<br>  &#39;800&#39;: defaultColors[200],<br>  &#39;900&#39;: defaultColors[100],<br>};</pre><p>Fun fact, the gray color scheme above are the default gray colors from my current go-to CSS framework — <a href="https://tailwindcss.com/">Tailwind CSS</a>. As you can see, the gray colors (black, white, 100–900) in the darkColors scheme are actually mirrored from the gray colors in the defaultColors scheme. Take note that the intermediary colors between black and white are sequenced in order of lightness: 100 (lightest) to 900 (darkest). It means that when getting a 100 shade, it will return a light shade for light (default) theme, and a darker shade for dark theme.</p><p>Of course, you can always go a step further and add in lighter/darker shades for the primary color, or shades for success, danger, warning colors, etc. You don’t even have to have gray colors for 100–900 — have fun using subtle colors! — as long as they are sequenced from lightest to darkest.</p><p>Both these themes have an orange-y primary color. Next step is to define the names of these themes into an object to export. And also maybe add two other themes in with blue-ish primary color, one light and one dark.</p><pre>const themes = {<br>  &#39;default&#39;: {...defaultColors},<br>  &#39;dark&#39;: {...darkColors},<br>  &#39;blue&#39;: {...defaultColors, primary: &#39;#4299E1&#39;},<br>  &#39;blue-dark&#39;: {...darkColors, primary: &#39;#63B3ED&#39;},<br>};</pre><pre>export default themes;</pre><p>Finally, create and export a function to get a color based on the color name and theme. Something like this:</p><pre>export const getThemeColor = (color, theme = &#39;default&#39;) =&gt; {<br>  const themeColor = themes[theme][color];<br>  const fallbackColor = themes.default[color];</pre><pre>  return themeColor || fallbackColor;<br>};</pre><h3>The Redux Part</h3><p>The great thing about Redux is that we can easily inject values directly to our components as long as they are wrapped inside Redux’s &lt;Provider&gt; . This — at least for me — makes Redux ideal for the job, since the theme configuration would be something that should be accessible by every presentational component.</p><p><em>I know, I know, right about now, Redux is slowly giving way to React Hooks, and hooks would probably be the preferred way of managing application-wide state in the future. But in the meantime, I’ll be sticking with Redux.</em></p><p>Define a state and reducer to store and change the app configuration values, which includes our selected theme name:</p><pre>// /redux/reducers/config.js</pre><pre>import {THEME_SET} from &#39;../actions/config&#39;;</pre><pre>const defaultState = {<br>  theme: &#39;default&#39;,<br>};</pre><pre>export default (state = defaultState, action) =&gt; {<br>  switch (action.type) {<br>    case THEME_SET:<br>      return {...state, theme: action.payload};<br>    default:<br>      return state;<br>  }<br>};</pre><p>The state above will be mapped to every presentational component to define their style values. For apps utilizing the React Navigation library, or other navigational libraries, map the theme state to the “screen” components.</p><p>Of course, sticking to Redux conventions, also define the function and type for dispatching the action to change our theme selection:</p><pre>// /redux/actions/config.js</pre><pre>export const THEME_SET = &#39;THEME_SET&#39;;</pre><pre>export const setTheme = theme =&gt; ({<br>  type: THEME_SET,<br>  payload: theme,<br>});</pre><p>The action above will then be mapped to our theme selector component, which may be some kind of an Appearance Settings screen.</p><h3>Selecting a Theme</h3><p>Most apps allow their users to change the theme color via the Settings screen, or for more simpler Light/Dark toggle — on the navigation drawer. Basically, it can be anything. For an example, below is a screen with a component that gets, lists and previews theme colors and changes the theme config by pressing on a theme preview, which in turn passes a theme name (default, dark, blue, or blue-dark) to the redux action.</p><pre>import themes from &#39;./themes&#39;<br>import {setTheme} from &#39;./redux/actions/config&#39;</pre><pre>const AppearanceSettings = ({onSelectTheme}) =&gt; ( <br>  &lt;View style={styles.container}&gt;<br>    &lt;Header title=&quot;Appearance&quot; /&gt;<br>    &lt;ScrollView&gt;<br>      &lt;Text&gt;Themes&lt;/Text&gt;<br>      &lt;Themes themes={themes} onSelect={theme =&gt; onSelectTheme(theme)} /&gt;<br>    &lt;/ScrollView&gt;<br>  &lt;/View&gt;<br>)</pre><pre>export default connect({}, {<br>  onSelectTheme: setTheme<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*os-olz2IiKg9T8RoykFJHg.png" /><figcaption>Something like this beauty right here.</figcaption></figure><p>Now the user can change the theme all they like. But there’s one thing missing: how do we make all our screens and components react to the selected theme? Read on…</p><h3>Styling based on Selected Theme</h3><p>Here’s the hard part (if the previous steps weren’t hard enough): getting the screens (and components) to be styled based on the selected theme name. Actually, it’s not as hard for brand new apps. Whatever the case, we can still enjoy the process of getting the colors right on every component. Here goes.</p><p>For every presentational React component that are given some kind of styling, they would need to have access to the theme configuration. For React Navigation-based apps, I like to do it by attaching it to the screen components, then passing down either the theme config or the actual computed styles down to child components.</p><p>Connect and map the selected theme name to the component props:</p><pre>export default connect(state =&gt; ({<br>  theme: state.config.theme<br>}), {})(Home);</pre><p>Now the component can access the theme name inside the theme prop. Next, define the styles:</p><pre>import {getThemeColor} from &#39;./themes&#39;</pre><pre>const getStyles = theme =&gt;<br>  StyleSheet.create({<br>    container: {<br>      flex: 1,<br>      backgroundColor: getThemeColor(&#39;white&#39;, theme),<br>    },<br>  });</pre><p>It looks like a standard RN StyleSheet definition, but with a twist:</p><ol><li>It’s now wrapped inside a function that takes a theme name as an argument,</li><li>It now uses getThemeColor from the themes file to get colors based on the provided theme name. As shown in the first step, getThemeColor takes in a color name and a theme name as arguments to return a theme color.</li></ol><p>The last piece of the puzzle is to get the StyleSheet object based on the selected theme. Where and when we get the styles depends on how our screen is placed within the application flow relative to the theme selector screen we talked about on the previous step.</p><p>To make it more efficient, one might think that we should define our styles inside of the component class constructor, or as an initial side effect defined in a useEffect hook, and I think that might be true. But that means if the said component has been mounted prior, it won’t be able to instantly update its styling when the user changes the theme.</p><pre>class Home extends Component {<br>  constructor(props) {<br>    this.styles = getStyles(props.theme);<br>  }<br>  render() {<br>    return (<br>      &lt;View style={this.styles.container}&gt;<br>        ...<br>      &lt;/View&gt;<br>    );<br>  }<br>}</pre><p>Another method is to simply define the styles for every render. Easy peasy.</p><pre>const Home = ({theme}) =&gt; {<br>  const styles = getStyles(theme);<br>  return (<br>    &lt;View style={styles.container}&gt;<br>      ...<br>    &lt;/View&gt;<br>  );<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dWMMJUzR-1PajD2TXux3wA.png" /><figcaption>Light vs Dark</figcaption></figure><p>Oh, I almost forgot! The status bar may also need to reflect the selected theme by correctly setting the barStyle attribute. This can be done by adding a &lt;StatusBar&gt; component on the root component wrapped by Redux’s &lt;Provider&gt;:</p><pre>&lt;Provider store={store}&gt;<br>  ...<br>  &lt;Status barStyle={store.getState().config.theme.includes(&#39;dark&#39;) ? &#39;light-content&#39; : &#39;dark-content&#39;} /&gt;<br>&lt;/Provider&gt;</pre><h3>Moving Forwards: How to Style</h3><p>From my (short) experience writing styles with themes, is to base every other theme from a light-colored default theme, and think using the default theme mindset. Let me explain.</p><p>Say we want to style a screen with a plain background and a contrasting text on it. Start by defining the container styling with a “white” background, and the text with a “black” (or 800/900 shade) color. Try not to think into what it will look like in dark mode first, since that will add confusion into what color name we want. Then, check that if it visually makes sense in default theme. Later on, we can try switching to a dark theme and see how it looks in comparison.</p><p><strong>And that’s it!</strong></p><p>As a takeaway, I think color themeing feature in an app should be something thought of and prepared for in the beginnings of an app’s creation, since it involves so much of the app. Some may argue the cost of developing a dark mode or themes outweighs the benefits, and that could also be true to some extent. But I do admit I’m one of those people who looks at dark mode and thinks it’s way cooler than the default light mode.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c2535f9c2e0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/dark-mode-on-react-native-with-redux-9c2535f9c2e0">Dark Mode on React Native with Redux</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making the Bottom Sheet Modal using React Native]]></title>
            <link>https://medium.com/@ndyhrdy/making-the-bottom-sheet-modal-using-react-native-e226a30bed13?source=rss-8465610505a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e226a30bed13</guid>
            <category><![CDATA[interaction-design]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[modal]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Endy Hardy]]></dc:creator>
            <pubDate>Sun, 22 Dec 2019 10:40:27 GMT</pubDate>
            <atom:updated>2019-12-22T10:40:27.114Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s a piece of UI that serves many purpose and implemented in so many ways. It has become one of the best ways to contain secondary information and control to a page, while keeping the cohesion with the page.</p><p>The bottom sheet (or as I like to call it — the swipeable bottom modal), is a container that overlays a parent page (like a modal), initializes by popping up from the bottom of the screen (near where your thumb is likely to be), and can be dismissed with a vertical swipe down.</p><p>It’s one of those things where I try to avoid reaching for a third-party package given its simple purpose, but it’s also quite tricky to pull off. Here’s how I got it to work with React Native.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k5mvWe7YDQeZ_Jje1_rE-Q.jpeg" /><figcaption>What I was aiming for.</figcaption></figure><p>Enter /components/BottomSheet.js</p><p>First, start by extending React Native’s built-in Modal component, and creating the overlay:</p><pre>&lt;Modal<em><br>  </em>animated<br>  animationType=&quot;fade&quot;<br>  visible={this.props.visible}<br>  transparent<br>  onRequestClose={() =&gt; this._handleDismiss()}<em>&gt;<br></em>  &lt;View style={styles.overlay}&gt;<br>    ...<br>  &lt;/View&gt;<br>&lt;/Modal&gt;</pre><pre>const styles = StyleSheet.create({<br>  overlay: {<br>    backgroundColor: &#39;rgba(0,0,0,0.2)&#39;,<br>    flex: 1,<br>    justifyContent: &#39;flex-end&#39;,<br>  },<br>});</pre><p>To make the container slide in from the bottom of the screen, create an Animated.View component for the container, and set the top styling to respond to the animation value.</p><pre>...<br>&lt;View style={styles.overlay}&gt;<br>  &lt;Animated.View style={[styles.container, {top}]}&gt;<br>    {this.props.children}<br>  &lt;/Animated.View&gt;<br>&lt;/View&gt;<br>...</pre><pre>const styles = StyleSheet.create({<br>  ...<br>  container: {<br>    backgroundColor: &#39;white&#39;,<br>    paddingTop: 12,<br>    borderTopRightRadius: 12,<br>    borderTopLeftRadius: 12,<br>  },<br>});</pre><p>Define an animation value in the component state. Then, make two animations for: resetting and sliding the container to the normal position; and dismissing and sliding the container out of view.</p><pre>constructor(props) {<br>  super(props);<br>  this.state = {<br>    panY: new Animated.Value(Dimensions.get(&#39;screen&#39;).height)<br>  };<br>  this._resetPositionAnim = Animated.timing(this.state.panY, {<br>    toValue: 0,<br>    duration: 300,<br>  }<br>  this._closeAnim = Animated.timing(this.state.panY, {<br>    toValue: Dimensions.get(&#39;screen&#39;).height,<br>    duration: 500,<br>  }<br>}</pre><p>Of course, you can use whichever animation type you want. Just make sure the initial value and the value after dismiss will shove the container completely out of view. You may have noticed I have named the animation value as panY. That’s because we’re gonna use it to respond to the swiping gesture.</p><p>Interpolate the animation value from the container’s top position, to cancel limit the output value to zero. This is to prevent the modal from ‘detaching’ from the bottom of the screen when the user swipes too far upwards.</p><pre>render() {<br>  const top = this.state.panY.interpolate({<br>    inputRange: [-1, 0, 1],<br>    outputRange: [0, 0, 1],<br>  });<br>  ...<br>  &lt;Animated.View style={[styles.container, {top}]}&gt;<br>    {this.props.children}<br>  &lt;/Animated.View&gt;<br>  ...<br>}</pre><p>Next, define and attach pan responders to the container. For this, we can create a View component that wraps around the animated container. The view will be made to respond to two events: on move (to swipe the modal up and down) and on release (to detect if we should dismiss the modal or return it to its normal position).</p><pre>constructor(props) {<br>  ...<br>  this._panResponders = PanResponder.create({<br>    onStartShouldSetPanResponder: () =&gt; true,<br>    onMoveShouldSetPanResponder: () =&gt; false,<br>    onPanResponderMove: Animated.event([<br>      null, {dy: this.state.panY}<br>    ]),<br>    onPanResponderRelease: (e, gs) =&gt; {<br>      if (gs.dy &gt; 0 &amp;&amp; gs.vy &gt; 2) {<br>        return this._closeAnim.start(() =&gt; this.props.onDismiss());<br>      }<br>      return this._resetPositionAnim.start();<br>    },<br>  });<br>}</pre><p>Set onMoveShouldSetPanResponder to false so it wont take over events for components within the container itself.</p><p>On the onPanResponderMove event, give an animated event object, which will update the animated value panY as the user moves their touch. On the onPanResponderRelease event, give a callback that will decide if it should dismiss or reset the modal, based on the direction (dy) and speed (vy) of the gesture.</p><p>Finally, a couple more functions to handle dismissing and initial slide-in:</p><pre>componentDidUpdate(prevProps, prevState) {<br>  if (<br>    prevProps.visible !== this.props.visible <br>    &amp;&amp; this.props.visible<br>  ) {<br>    this._resetPositionAnim.start();<br>  }<br>}</pre><pre>_handleDismiss() {<br>  this._closeAnim.start(() =&gt; this.props.onDismiss());<br>}</pre><p>That’s it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*NAJwJQOXMb_aLzGKDS9q8g.gif" /></figure><p>On a side note, the example doesn’t yet account for modal content exceeding the screen height. A scroll view inside the container, coupled with maximum container height might be a way to solve it. Also, to dismiss the modal from its parent component, it should call the _handleDismiss function. This can be done by exposing the function using a ref.</p><p>Thanks for reading! Check out my other stories about mobile development using React Native!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e226a30bed13" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>