IMPLEMENTATION OF DARK MODE IN REACT DEVICES

One of the apps features most requested is the dark mode (or night mode, as others call it). In the apps which we use every day, we see dark mode. From smartphones to web applications, the dark mode has become important to businesses wishing to look after their users’ eyes.

The dark mode is an add-on feature that often shows dark surfaces in the UI. Most big corporations (such as YouTube, Twitter, and Netflix) have brought their smartphone and web applications into dark mode.

Although we are not going to go into React and styled-components in detail, a simple knowledge of React, CSS, and styled-components would be useful.

A few days before this article was written, StackOverflow announced its release of dark mode, offering users the opportunity to switch between the two modes.

Dark mode eliminates eye pressure and aids when operating on a device or cell phone for a long period of time.

What Is Dark Mode?

The dark mode is the color scheme of any device showing light text and device elements on a dark backdrop, making it a little easier for the user to view cell phones, tablets, and computers. Dark mode decreases the light produced by the screen while preserving the minimum color-contrast ratios that are appropriate for reading.

Why Should You Care About Dark Mode?

Dark mode improves visual ergonomics by reducing eye strain, adapting the screen to current light conditions, and ensuring ease of use in dark environments or at night.

Let’s look at its benefits before we introduce dark mode in our app.

BATTERY SAVING

Dark mode in Web and mobile apps will extend a device’s battery life. Google stated that dark mode on OLED screens has been a tremendous help to battery life.

For example, the dark mode in the YouTube app saves about 15 percent more screen energy than a flat white background at 50 percent brightness. The dark GUI saves a whopping 60 percent of screen energy at 100 percent screen brightness.

DARK MODE IS BEAUTIFUL

The dark mode is stunning, and can greatly enhance the screen’s appeal.

Although most products go for the same bland white look, the dark mode offers something else that looks both mysterious and fresh.

It also offers great opportunities for a fresh presentation of graphic material, such as dashboards, videos, and images.

Now that you know why you should implement dark mode in your next web app, let’s dive deep into style-components, which in this tutorial’s defining resources.

The dark mode is the color scheme of any interface that displays light text and interface elements on a dark background, making viewing on mobile phones, tablets, and computers a little easier.

What Are Styled-Components?

We will be using the styled-components library very much in this post. There have always been a lot of ways to design a modern web application. A document-level there is the conventional styling approach which involves creating an index.css file and linking it to the HTML or styling within the HTML file.

A lot has changed in the way web apps are recently designed after CSS-in-JS was introduced.

CSS-in-JS refers to a pattern where JavaScript is used to compose the Html. To style components in a JavaScript file, it uses tagged template literals. The styled component is a CSS-in-JS library that allows you to use all the CSS features you need, including media queries, pseudo-selectors, and nesting.

Why Styled-Components?

For the following reasons styled-components were created:

  • No hell class name Instead of scratching your head to find an item class name, styled-components can create unique class names for your types. You will never have to think about misrepresentations, or using class names that have no meaning.
  • The use of designed component props allows one to expand the styling properties using the props parameter widely used in React — thereby dynamically influencing the component’s feeling through the state of the application. Supports Sass syntax
  • Writing Sass syntax out of the box without having to set up any preprocessors or extra build tools is possible with styled-components. In your style definitions, you can use the & character to target the current component, use pseudo-selectors, and experiment with nesting.

• Theming modeled components have full thematic support through the export of a ThemeProvider wrapper. This framework provides a theme for all components of React within itself via the Context API. All styled-components will have access to the given theme in the rendering tree, even though they are deeply multi-level. As we continue this tutorial, we will investigate the thematic features of modeled components more deeply

SETTING UP

1. CREATE THE FILE STRUCTURE

Creating a folder and inserting within it three empty text files: one with.html, one with.css, and one with.js. Also, create a folder of images for the photos you wish to view on the website.

JQuery also makes use of our “dark mode in CSS” demo. In the example, we are going to add the script right from the Cloudflare CDN to the HTML file so that it is always up to date. If you like, however, you can download and add the jQuery library as a local.js file as well.

Here’s how the file structure will look before coding begins:

– dark-mode-css/
– images/
– cake.jpg
– index.html
– script.js
– style.css

2. MARK UP THE HTML

Add the dark-mode switch to the top of the page in the HTML. Then create a < h1 > title tag, and a semantic < article > tag for the page’s content. Finally, just before the < /body > tag closes add the two < script > tags.

Please note that before the custom script you are adding the jQuery library so it can use its functionalities. The style.css file will go into the page’s < head > section.

<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<meta http-equiv=”X-UA-Compatible” content=”ie=edge”>
<title>Demo | Dark Mode in CSStitle>
<link rel=”stylesheet” href=”style.css”>
head>
<body>
<div class=”switch”>Dark mode:
<span class=”inner-switch”>OFFspan>
div>
<h1 class=”title”>The Best Recipes of All Timeh1>
<article>
<h1>Raspberry Cakeh1>
<p><small>Created with Cupcake Ipsumsmall>p>
<p>…p>
<img src=”images/cake.jpg”>
<p>…p>
article>
<script src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js”>script>
<script src=”script.js”>script>
body>
html>

3. CREATE THE CSS FOR THE LIGHT MODE

First, let’s build the CSS for light mode, since this is the page’s default state. The following CSS makes use of a column-based flexbox style that allows you to easily place elements on the page, specifically the.switch class that handles the dark mode toggle wrapper and the < img > elements.

body {
font-family: sans-serif;
font-size: 1.125rem;
display: flex;
flex-direction: column;
max-width: 50rem;
margin: 0 auto;
padding: 0 0.9375rem;
}
small {
font-style: italic;
}
article {
display: flex;
flex-direction: column;
}
img {
max-width: 100%;
display: block;
align-self: center;
}
.switch {
align-self: flex-end;
margin: 0.9375rem;
}
.inner-switch {
display: inline-block;
cursor: pointer;
border: 1px solid #555;
border-radius: 1.25rem;
width: 3.125rem;
text-align: center;
font-size: 1rem;
padding: 0.1875rem;
margin-left: 0.3125rem;
}

The display: flex; rule applied to the tag allows the align-self to be used: flex-end; rule over the dark mode turn. The align-self CSS property aligns individual flex elements along the cross-axis (which is the left-to-right axis when a column is set to flex-direction).

In this way, the move is located automatically to the top right corner of the flex container — for all viewport sizes. .switch is a full-width layout row due to the flexbox, and its height does not fall at smaller screen sizes either.

Similarly, the display: flex; and flex-direction: column; rules added to the < article > tag allow align-self to be used: center; rule on all images inside the post. As a result, all the images can be conveniently focused without adding extra elements to the HTML, only for positioning purposes.

4. ADD THE SWITCH FUNCTIONALITY WITH JQUERY

The.js script file provides the toggle switch feature. Therefore, the dark mode is activated when the user clicks the toggle and the mark on the switch switches to “ON.” And if, when the page is in dark mode, the user clicks the toggle, the light mode is added and the mark switches to “OFF.”

$( “.inner-switch” ).on(“click”, function() {
if( $( “body” ).hasClass( “dark” )) {
$( “body” ).removeClass( “dark” );
$( “.inner-switch” ).text( “OFF” );
} else {
$( “body” ).addClass( “dark” );
$( “.inner-switch” ).text( “ON” );
}
});

The script above makes use of the following jQuery functions:

· on(“click”, function() {…}) is an event handler that triggers the action inside the function when the user clicks the .inner-switch element,

· hasClass() checks if the .dark class is assigned to the .inner-switch element or not (this is based on the state of the toggle),

· removeClass() removes the .dark class from the HTML when the user switches to light mode,

· addClass() adds the .dark class to the HTML when the user switches to dark mode,

· text() sets the text of the label on the switch — it’s either “OFF” or “ON”.

·

5. ADD THE CSS FOR THE DARK MODE

The last thing you need to do is to define those styles for the.dark class that the jQuery script above added to the HTML when dark mode is turned on. In addition to the.dark class, apply the dark mode theme to both its direct and indirect children using the.dark * universal CSS selector.


.dark,
.dark * {
background-color: #222;
color: #e6e6e6;
border-color: #e6e6e6;
}

The CSS belonging to the.dark class goes to the file ending with style.css. This is because CSS (= Cascading Style Sheets) is cascading in nature. The cascade may thus bypass the colors all over the page without bumping into specificity or other probelms.

You can also create a separate dark.css file for the dark mode styles if you are creating a more complex website. In this case, be careful that you always add the unique dark.css in the < head > section of the HTML after the generic style.css file, so that the cascade will work properly.

CREATING THEME-TOGGLING FUNCTIONALITY

1. Add HTML:

Using any item that should store the content for which you want the interface toggle. In our case, for simplicity we’ll use < body >:

Example

Add CSS:

Design the item < body > and build a toggle class in.dark-mode:

Example

body {
padding: 25px;
background-color: white;
color: black;
font-size: 25px;
}

.dark-mode {
background-color: black;
color: white;
}

3) Add JavaScript:

Get the < body > item and switch between the class of dark-mode:

Example

function myFunction() {
var element = document.body;
element.classList.toggle(“dark-mode”);
}

Conclusion

Dark mode is increasingly becoming a choice for the consumer, and it is much easier to integrate it into a web app by using the ThemeProvider theme wrapper in styled components. Go ahead and play with styled-components as you implement dark mode; you could add icons instead of a button.

As a reputed Software Solutions Developer we have expertise in providing dedicated

remote and outsourced technical resources for software services at very nominal cost. Besides experts in full stacks We also build web solutions, mobile apps and work on system integration, performance enhancement, cloud migrations and big data analytics. Don’t hesitate to get in touch with us!

This article is contributed by Ujjainee. She is currently pursuing Bachelor of Technology in Computer Science . She like most about Computer Engineering is that it allows her to learn and be creative. Also, She believe in sharing knowledge hence is very fond of writing technical contents for the same. Coding, analyzing and blogging are things which She can keep on doing!!

Leave a Reply

Your email address will not be published. Required fields are marked *