<?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 Tom Matthews on Medium]]></title>
        <description><![CDATA[Stories by Tom Matthews on Medium]]></description>
        <link>https://medium.com/@tommatthews_14845?source=rss-b56a51dc76da------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*sRWKUNbTGwC5dpUn.</url>
            <title>Stories by Tom Matthews on Medium</title>
            <link>https://medium.com/@tommatthews_14845?source=rss-b56a51dc76da------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 17:33:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tommatthews_14845/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[Angular Bootstrap with Dark Mode Toggle]]></title>
            <link>https://medium.com/@tommatthews_14845/angular-bootstrap-with-dark-mode-toggle-4539ede28e16?source=rss-b56a51dc76da------2</link>
            <guid isPermaLink="false">https://medium.com/p/4539ede28e16</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[bootstrap]]></category>
            <category><![CDATA[theme-switching]]></category>
            <dc:creator><![CDATA[Tom Matthews]]></dc:creator>
            <pubDate>Fri, 04 Sep 2020 16:30:39 GMT</pubDate>
            <atom:updated>2020-09-04T16:30:39.855Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2BKC_0hlri_U-9EQECR81g.png" /></figure><p>In this short article, I will cover how to set up a basic Angular application using Bootstrap styling and then extend the default style to add a dark mode.</p><h3>Introduction</h3><p>Standard Bootstrap is an easy way to add styling to your web application but unlike Material, it doesn’t have a simple way to apply a dark mode. We could set up our own overriding theme but this would literally be reinventing the wheel. Instead we will be using the bootstrap-dark library from <a href="https://github.com/ForEvolve/bootstrap-dark-docs">@ForEvolve</a>. While this is still in alpha, given the stability of Bootstrap 4 at this stage, it is more than likely sufficient for most websites.</p><p>For the purposes of this article, you will need to have Node and the Angular CLI installed and if you’ve got to looking at styling your application, I’ll assume you’ve got a decent working knowledge of Angular.</p><h4>TL:DR</h4><p>Basic components to toggle between two states, hold them in a simple state store service and render changes to the UI.</p><p>Demo application can be found : <a href="https://tomonline.github.io/">here</a><br>Sourcecode for the application can be found : <a href="https://github.com/tomonline/bootstrap-switcher">here</a></p><p>So we start with creating our application using the Angular CLI command</p><pre>ng new bootstrap-switcher</pre><p>Answer Yes or No to routing depending on how you want to use your application, and select your preferred method of styling. I usually use SCSS as it allows us to add scripting to our CSS sheets without losing the ability to write basic CSS</p><p>Then, we need to add the styling we need to install the @ForEvolve bootstrap-dark dependencies. I’m using this library because it’s designed for toggling and provides styling and nothing else.</p><pre>cd bootstrap-switcher<br>npm install @forevolve/bootstrap-dark</pre><p>Once this has installed, fire up your preferred IDE and edit the angular.json file. As we are going to be setting up a toggle between the two styles, we need to add in the two style sheets to the styles section under architect:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d8b1220c6c2789e9d4b59a4e238c3b0/href">https://medium.com/media/8d8b1220c6c2789e9d4b59a4e238c3b0/href</a></iframe><p>Finally, to see the dark style in action, navigate to the index.html page, and change the body tag from</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3509df4893a46827802b5ee046aa49ce/href">https://medium.com/media/3509df4893a46827802b5ee046aa49ce/href</a></iframe><p>to</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ede1b3b1b712829ae3dd3041752fb1b/href">https://medium.com/media/4ede1b3b1b712829ae3dd3041752fb1b/href</a></iframe><p>At this stage your application is set up and ready to use. Start the application with <strong>ng serve</strong> and you can see the impact on the default landing page. It kind of works, but at the same time it impacts the boilerplate text and styling applied on that page. However, this doesn’t matter as you’ll be deleting all of the content and starting with your own application.</p><h4>Remember!</h4><p>Remove the manually-defined class from the body tag and we’ll start with the application. If you leave it in, it’ll be there by default every time the page reloads you might get some odd behaviour.</p><p>Also note, the class of the HTML Body is outside of the scope of the Angular application. We can still access this though using the Renderer2 object which we’ll cover later.</p><p>With the application up and running, we now need to create three core elements:</p><ol><li>A service to hold the theme selection, receive and publish updates</li><li>A component to toggle between light and dark modes, this will push updates to the service</li><li>Some code to listen to theme selection updates and apply them to the application.</li></ol><p>Finally, as a little added extra, we will look at creating our own component that is styled without using bootstrap but will, nevertheless, respond to the toggle between light and dark modes.</p><h3>The Service</h3><p>Using the Angular CLI, create a theme service using the following command:</p><pre>ng generate service services/theme  </pre><p>This creates a services folder and then creates a service called ThemeService. I generally group all of my different application elements into separately named folders in order to make it easy to navigate around the application during development.</p><p>Within the service itself, we’ll use a BehaviourSubject to hold the theme selection and expose this as an Observable to which we can subscribe. We’ll also create a method that allows the toggle component to push updates to the service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47b8b8ddbddb9b148f42ab40c33c1a75/href">https://medium.com/media/47b8b8ddbddb9b148f42ab40c33c1a75/href</a></iframe><h3>The Toggle Component</h3><p>Next, we’ll create the component to provide the toggle functionality. Create a standard component using the following command:</p><pre>ng generate component components/theme-toggle  </pre><p>There are, of course, many different ways we can build the toggle component and many libraries are available in NPM which you can use. I’m going to keep it very simple and build a small switch using two DIV classes and some CSS styling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/63/1*t_jOXIxLZmkwrX4EwgpbYA.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b01c7ee53de62008dcaa9ed087990ca0/href">https://medium.com/media/b01c7ee53de62008dcaa9ed087990ca0/href</a></iframe><p>The layout code for this button is very simple, and simply defines two div elements, one inside the other. The .selected classes reposition and recolour the inner div.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b80c7b42ec6f3048fb3e179264e4f7bc/href">https://medium.com/media/b80c7b42ec6f3048fb3e179264e4f7bc/href</a></iframe><p>Once we have the layout and the styling, we need to add some functionality to switch between the two states (hence the toggle concept).</p><p>Firstly, we’ll add some basic switching in the component class. We <em>could</em> do this directly in the HTML code, but it becomes hard to do anything more than a basic two-state toggle. By starting off in the component class, we lay the groundwork for a more complex switcher later down the line.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb143465cc92cb55fdca3af3e7791ab4/href">https://medium.com/media/bb143465cc92cb55fdca3af3e7791ab4/href</a></iframe><p>The component class contains two basic things. A reference to the theme service which is initialised in the constructor, and a local variable to hold the current selection.</p><p>The toggleTheme() method simply switches between two states and then pushes the selected theme back into the theme service so it can be broad cast to the rest of the application.</p><p>Now that we have the backing code, we need to reference it in our HTML. In this case, we use the directive [ngClass] to add the additional selected class if the theme value is ‘bootstrap’ and listen to the (click) event which then fires the toggleTheme() method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d572f0fc73c9f4d3caa9ff9dcc165678/href">https://medium.com/media/d572f0fc73c9f4d3caa9ff9dcc165678/href</a></iframe><h3>Applying the Updated Theme</h3><p>Once we have the theme service set up and a toggle to push updates into the service, we need to listen to those changes and then apply them to the application as a whole. The way the @ForEvolve library works is that the CSS is laid out in two hierarchically distinct classes. The first class is either <strong>bootstrap </strong>or <strong>bootstrap-dark</strong> and the nested classes underneath that provide the actual styling.</p><p>So to make the toggle work the &lt;body&gt; tag should include a class of either <strong>bootstrap</strong> or <strong>bootstrap-dark</strong> which then dictates how all the components should display.</p><p>In Angular, the &lt;body&gt; tag of the HTML page is outside the scope of the application. A typical application would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/885222ded7cf1750a227e6279a459603/href">https://medium.com/media/885222ded7cf1750a227e6279a459603/href</a></iframe><p>The first element we can directly impact in the application is the &lt;app-root&gt;. There is a way to access the &lt;body&gt; tag, however, and that is to use the Renderer2 library provided by Angular. This library can make changes to the DOM scoped outside of the Angular application. Bear in mind that it is read-only though, so changes can be made but not read.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0f9e484f07f4b88620fd2c97a4cb6e8/href">https://medium.com/media/c0f9e484f07f4b88620fd2c97a4cb6e8/href</a></iframe><p>Here we reference the theme service in the constructor which provides us access to the selected theme. From here we can subscribe to the Observable method and update the &lt;body&gt; class as and when a change is made. Notice that in this code the theme service is providing an object with two strings, an oldValue and a newValue. As mentioned earlier we can’t read the DOM using Renderer2 so we can’t see what classes already have been applied to the &lt;body&gt; tag. However, by keeping track of the original and the changed values (old and new) in the theme service, we can simply remove the old one and add the new one here.</p><p>In order to provide this functionality, the updated theme service looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca3dd2480f85908bf245fb382652a570/href">https://medium.com/media/ca3dd2480f85908bf245fb382652a570/href</a></iframe><p>The difference here is simply that we’ve created a simple object to hold the old and new values (ThemeObject) and are updating this with the current value as well as the updated value before pusing it back through the BehaviourSubject using the .next() method.</p><h3>Creating Your Own Components</h3><p>At this stage we’ve got bootstrap set up to switch between light and dark modes. This is great for all of the bootstrap styled components, but what about your own components that you might have built which don’t directly use bootstrap styling? The root level style sheets use class hierarchies to determine whether something shows light or dark, but encapsulation means that we can’t directly access the root class in CSS. Handily though, there is a pseudo-class method available called :host-context(<em>classname</em>)</p><p>Using the :host-context() we can reference a class further up the DOM hierarchy, in this case, right at the root. In the application I have created a Credits component to show some details of how to get in touch with me. I want this to follow the light and dark theme selection, so I create a .credits class with styling that is common to both theme styles, and then two separate classes to be used separately when the &lt;body&gt; tag has either the <strong>.bootstrap </strong>or the <strong>.bootstrap-dark</strong> class applied to it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ca56e292bfea5f04c1940716b364144/href">https://medium.com/media/3ca56e292bfea5f04c1940716b364144/href</a></iframe><h3>Summary</h3><p>In these simple steps we can switch between styles and listen to those changes in our own components.</p><p>Personal note: I have been developing with Angular for several years now, starting with AngularJS and moving up through the Angular <em>n</em> versions. During this time, like many of you, I have been guided by what has been available online which has been invaluable. Having reached a degree of competence with the tools I wanted to try and give back to the community. I’d love to hear your thoughts on the article, does it work, is it sufficient detail, too much or too little?</p><p>Sample application can be found on : <a href="https://tomonline.github.io/">GitHub Pages</a></p><p>Sample code for the application can be fond on : <a href="https://github.com/tomonline/bootstrap-switcher">Github Repository</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4539ede28e16" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>