<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MAJD JALAB</title>
    <description>The latest articles on DEV Community by MAJD JALAB (@majdjalab).</description>
    <link>https://dev.to/majdjalab</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F961391%2Ff3e02e26-c673-4068-bbaf-c4de2eb971ad.jpg</url>
      <title>DEV Community: MAJD JALAB</title>
      <link>https://dev.to/majdjalab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/majdjalab"/>
    <language>en</language>
    <item>
      <title>🌗 React Native Switch With Icon &amp; Dark Theme Without Packages!! Part 1</title>
      <dc:creator>MAJD JALAB</dc:creator>
      <pubDate>Sun, 04 Jun 2023 22:27:44 +0000</pubDate>
      <link>https://dev.to/majdjalab/react-native-switch-with-icon-dark-theme-without-packages-a1p</link>
      <guid>https://dev.to/majdjalab/react-native-switch-with-icon-dark-theme-without-packages-a1p</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hello, As this is my first post here, I'm thrilled to teach you how to create a Switch with Icon and Dark theme without relying on external packages, specifically using React Native. So, let's dive right in and get started on this exciting journey of UI customization.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbawa3vb0qsxn79izx6jn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbawa3vb0qsxn79izx6jn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;so let's get started!&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;First Let's Create a Switch With an Icon&lt;/h3&gt;

&lt;p&gt;to do this let's create &lt;code&gt;SwitchWithIcon.js&lt;/code&gt; file&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;import those components &amp;amp; useState to get the state of switch component
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import { TouchableOpacity, View, ImageBackground, StyleSheet } from 'react-native';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The component receives three props: onImage, offImage, and onToggle. onImage and offImage are the images to be displayed when the switch is in the "on" and "off" positions, respectively. onToggle is a callback function that is called when the switch is toggled.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const SwitchWithIcon = ({ onImage, offImage, onToggle }) =&amp;gt; {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Inside the component, two state variables are declared using the useState hook: isEnabled and isDarkMode. The isEnabled state variable represents the current state of the switch (whether it is on or off), and isDarkMode represents whether the component is in dark mode or not. Both state variables are initialized with the value false.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [isEnabled, setIsEnabled] = useState(false);
  const [isDarkMode, setIsDarkMode] = useState(false);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The toggleSwitch function is defined within the component. When called, it toggles the state of isEnabled using the logical NOT operator (!). It then toggles the state of isDarkMode by negating its current value. After that, it checks if the onToggle prop is provided and if it is a function. If it is, the onToggle function is called with the new state as an argument.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const toggleSwitch = () =&amp;gt; {
    const newState = !isEnabled;
    setIsEnabled(newState);
    setIsDarkMode(!isDarkMode);
    onToggle &amp;amp;&amp;amp; onToggle(newState);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The component returns a TouchableOpacity component, The onPress prop of the TouchableOpacity is set to the toggleSwitch function, so whenever the switch is pressed, the toggleSwitch function will be called.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
    &amp;lt;TouchableOpacity onPress={toggleSwitch} style={[styles.containerSwitch, { backgroundColor: isDarkMode ? '#fff' : '#222' }]}&amp;gt;
    &amp;lt;/TouchableOpacity&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Inside the TouchableOpacity, there is a View component representing the circular part of the switch. The style prop of this View component uses the translateX transform property to move the circle to the right by 20 units when isEnabled is true.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;View style={[styles.circle, { transform: [{ translateX: isEnabled ? 20 : 0 }] }]}&amp;gt;
        &amp;lt;ImageBackground source={isEnabled ? onImage : offImage} style={styles.imageBackground} /&amp;gt;
&amp;lt;/View&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;finally, let's customize the style
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const styles = StyleSheet.create({
  containerSwitch: {
    borderRadius: 15,
    width: 50,
    height: 28,
    justifyContent: 'center',
  },
  circle: {
    width: 30,
    height: 30,
    borderRadius: 50,
    alignItems: 'center',
    justifyContent: 'center',
  },
  imageBackground: {
    flex: 1,
    width: 30,
    height: 30,
  },
});
export default SwitchWithIcon;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;thank you for reading this and wait for more&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>reactnative</category>
      <category>css</category>
    </item>
  </channel>
</rss>
