<?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 Ertunga Bezirgan on Medium]]></title>
        <description><![CDATA[Stories by Ertunga Bezirgan on Medium]]></description>
        <link>https://medium.com/@ertungabezirgan?source=rss-8d592d6ff1e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*dBtAwhMdA7jMULiE</url>
            <title>Stories by Ertunga Bezirgan on Medium</title>
            <link>https://medium.com/@ertungabezirgan?source=rss-8d592d6ff1e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:54:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ertungabezirgan/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[Add themes to Angular using CSS variables]]></title>
            <link>https://medium.com/codeshakeio/add-themes-to-angular-using-css-variables-fc49798285c6?source=rss-8d592d6ff1e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc49798285c6</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Ertunga Bezirgan]]></dc:creator>
            <pubDate>Tue, 02 Aug 2022 06:27:33 GMT</pubDate>
            <atom:updated>2022-08-08T10:15:28.058Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eFsl8ktzisTq8cJo" /><figcaption>Photo by <a href="https://unsplash.com/@unarchive?utm_source=medium&amp;utm_medium=referral">Jeremy Bezanger</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Providing different themes for users to choose from has become pretty much a standard these days and it’s quite easy to implement using Angular. In this article, we are going to learn how to add themes to Angular apps using a simple service and the power of CSS variables (aka CSS custom properties).</p><p>Here is what we are aiming for</p><ul><li>Multiple themes to choose from (light, dark, and possibly more)</li><li>Store the selected theme in local storage</li><li>Apply stored theme upon revisit</li></ul><p>And here is what the final result looks like,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*Rv6jnxSxOoConzrlryRMBg.gif" /></figure><h4>Overview</h4><p>We are going to create a service to hold the “active theme” as a state and also it will be responsible for changing the theme. This is going to be achieved by swapping CSS classes associated with the theme. CSS classes in return will be responsible for the theme change via CSS variables.</p><h4>Define Themes</h4><p>Let’s jump right into it by defining the themes we want to have in the application. To do so, we are going to create a file called theme.ts and expose an object from it. Keys of the object will be the theme names and their values will be the CSS classes associated with the theme. Here is an example in which we defined light, dimmed, and dark themes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55d7afe86f899aff64a83a2ed59e5a84/href">https://medium.com/media/55d7afe86f899aff64a83a2ed59e5a84/href</a></iframe><h4>Theme Classes</h4><p>In the next step, we are going to create CSS classes. Navigate to your global stylesheet (styles.{scss,css,less}) and add the following code to it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d20be8e763987b11783b18c67e74786f/href">https://medium.com/media/d20be8e763987b11783b18c67e74786f/href</a></iframe><p>You can add more variables if you need to. Notice that I already used two of the CSS variables in the body element. Right now, it doesn’t do anything because we haven’t set the theme just yet.</p><h4>Theme Service</h4><p>Now that we defined our themes and created CSS classes, we are going to create a service to hold the active state and change it. Create a service called theme and add the following code to it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1266cced9fcb706ae31b8d788f66a30d/href">https://medium.com/media/1266cced9fcb706ae31b8d788f66a30d/href</a></iframe><p>There are a couple of things to mention here.</p><ul><li>BehaviorSubject is used to keep the active theme as a state.</li><li>activeTheme getter method is created to get the active theme. An observable alternative, activeTheme$, is also added in case we want to subscribe to it.</li><li>To set the theme, set(...) method is created which takes “theme name” as an argument. Using the themes we defined earlier in theme.ts, it will remove and add CSS classes to the documentElement.</li><li>Also, I’ll throw in a getter method called themeNames to list all the possible themes.</li></ul><h4>Usage</h4><p>To use the service, inject it into the app component and call the set(...) method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41059da90b40f7b0bed18406ac4641fc/href">https://medium.com/media/41059da90b40f7b0bed18406ac4641fc/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f2903a3082c09b3470443741ad027ff/href">https://medium.com/media/7f2903a3082c09b3470443741ad027ff/href</a></iframe><p>And that’s it, theme selection is working. This will set the ‘light’ theme by default. You can add more styles for the component and start using themes in the application.</p><p>This is nice and all but regardless of what the user selects, the selection will be lost on a page reload since it’s not saved anywhere. Let’s fix this problem by adding local storage to the implementation.</p><h4>With Local Storage</h4><p>We’ll change some code in the app component.</p><ol><li>Check local storage first to see if a value is set.</li><li>If there is a value, and the saved value is listed in the theme.ts we created earlier, set the theme. If not, set the ‘light’ theme as the default theme.</li><li>Start listening to the theme changes using activeTheme$ observable and set the new theme to the local storage.</li></ol><p>Here is the new implementation of app component,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/768665931b680fe4708c68347e5b215b/href">https://medium.com/media/768665931b680fe4708c68347e5b215b/href</a></iframe><p>And with these small changes, the selected theme will be set when you reload the page or revisit it later on.</p><h4>Bonus: Use it with Tailwind CSS</h4><p>The same service can be used to toggle light and dark modes in Tailwind CSS. You can refactor it in two ways:</p><ol><li>Either change theme.ts and remove “-theme” suffix from themes. And don’t forget to remove them from the classes in styles.{scss,css,less} as well. And finally, add the following line to your tailwind.config.js file.</li></ol><pre>module.exports = {<br>  // ...   <br>  darkMode: &#39;class&#39;<br>}</pre><p>2. Second way is to keep everything as is and just add the following to tailwind.config.js</p><pre>module.exports = {<br>  // ...   <br>  darkMode: [&#39;class&#39;, &#39;.dark-theme&#39;]<br>}</pre><h4>Done! 🎉</h4><p>And that’s really it!</p><p>Here is the complete source code.</p><p><a href="https://stackblitz.com/edit/angular-with-css-variables?file=src/app/app.component.html">Angular Theme with CSS Variables - StackBlitz</a></p><p>You can find more utilities like this in the following repo 🙌</p><p><a href="https://github.com/ertunga/ng-snippets">GitHub - ertunga/ng-snippets: A collection of essential Angular services, directives, pipes and other tools to prototype and develop quickly.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc49798285c6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codeshakeio/add-themes-to-angular-using-css-variables-fc49798285c6">Add themes to Angular using CSS variables</a> was originally published in <a href="https://medium.com/codeshakeio">codeshakeio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build a dropdown component using Angular CDK]]></title>
            <link>https://medium.com/codeshakeio/build-a-dropdown-component-using-angular-cdk-fa45455e6a73?source=rss-8d592d6ff1e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa45455e6a73</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[ui-library]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[components]]></category>
            <dc:creator><![CDATA[Ertunga Bezirgan]]></dc:creator>
            <pubDate>Wed, 12 May 2021 09:45:25 GMT</pubDate>
            <atom:updated>2021-05-12T09:45:25.484Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ga2FeWPUJBXd1Lw6" /><figcaption>Photo by <a href="https://unsplash.com/@evelynparis?utm_source=medium&amp;utm_medium=referral">Evelyn Paris</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article, we’ll go through the process of creating a custom, reusable dropdown component with Angular CDK’s overlay module. The way it is consumed will resemble the Angular Material’s menu component.</p><p>Let’s go over the requirements</p><ul><li>Toggle dropdown when the button is clicked</li><li>When clicked outside or on itself, the dropdown will be closed</li></ul><p>Here is what the final result looks like,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*G2KGZ4Ay2JO6g5fsiKciMQ.gif" /></figure><h3>Setup</h3><p>Before we start, make sure to install Angular CDK.</p><pre>npm install @angular/cdk</pre><p>Also, add the following prebuilt style to your global stylesheet. By default that would be the styles.scss file under src directory.</p><pre>@import &#39;~@angular/cdk/overlay-prebuilt.css&#39;;</pre><h3>Overview</h3><p>Our solution will have a similar approach to <a href="https://material.angular.io/components/menu/overview">Angular Material menu component</a> in the way it is consumed. That means we are going to use a directive that will get the reference for the dropdown it is going to control. Methods that open or close the dropdown will be written inside the directive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*Wkln_KsxUzCNUzma28M9ng.png" /></figure><p>dropdown-panel is the interface that our dropdown component will implement. templateRef holds the reference for the dropdown component template and closed is the EventEmitter that will emit when there is a click on the dropdown itself which then will be used for closing the dropdown.</p><p>In the end, this is how it will be used in the application</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afba20ada8108076ab6d0d6347dab8f7/href">https://medium.com/media/afba20ada8108076ab6d0d6347dab8f7/href</a></iframe><h3>Toggle Dropdown</h3><p>To kick things off, let’s create a directive called dropdownTriggerFor. Two important things to mention here:</p><ul><li>Toggle dropdown method is bound to the host element’s click event</li><li>Getting dropdown panel via @Input decorator so that we have access to the template reference of the dropdown and the EventEmitter which we will subscribe to close the dropdown</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6cdb92650b313adbb037f94cd098a7b/href">https://medium.com/media/a6cdb92650b313adbb037f94cd098a7b/href</a></iframe><h3>Create an Overlay</h3><p>The next step will be to create an overlay to which the dropdown will be attached. For that, we will use the <a href="https://material.angular.io/cdk/overlay/overview">Overlay</a> service from Angular CDK and call the create method from the service.</p><p>Here is what we need to do,</p><ol><li>Create the overlay with this.overlay.create({...}) method.</li><li>Using the dropdown panel we got from the @Input, create a TemplatePortal.</li><li>Attach TemplatePortal to the overlay using this.overlayRef.attach(...) method.</li><li>Start listening to the closing actions, so that we can close the dropdown when it emits a new value.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c75f4a29b9e03fc4289092a23754465d/href">https://medium.com/media/c75f4a29b9e03fc4289092a23754465d/href</a></iframe><p>Now, the create method takes a configuration object as a parameter to customize the overlay behaviour. Let’s go over some of its configurations.</p><ul><li><strong>scrollStrategy:</strong> Configures the behaviour of the overlay during scrolling. Possible options are noop, reposition, block and close.</li><li><strong>flexibleConnectedTo:</strong> Pass in a connection origin for the overlay. This could be an element or element reference or any given (x, y) coordinates within the page. We want to connect the overlay to the button, that’s why we are passing this.elementRef as the connection origin.</li><li><strong>withPositions:</strong> An array of positioning options for the overlay. Sorted from most to least desirable position. For brevity, I will pass in only one option that will render the dropdown underneath the button.</li></ul><p>We have created an overlay, now let’s attach our dropdown template to it. We do so by creating a TemplatePortal and pass in the templateRef and the viewContainerRef. Then we are going to attach created TemplatePortal to the overlay using the attach method.</p><h3>Close Dropdown</h3><p>Now that we took care of the opening part, let’s look into the scenarios in which we want to close the dropdown. As we stated in the requirements, it should be closed when there is a click on the outside or on itself. Clicks on the dropdown itself will be handled in the dropdown component, we will come to that in a second. Outside clicks can be captured with backdropClick observable. Also, detachments is an observable that emits when the overlay has been detached, that is something we should take into account. And last but not least, throw in the dropdown panel’s closed event emitter in there as well. We will combine them all using merge and subscribe to the resulting merged observable in the openDropdown method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ed135b96e7ffd1c4bd29e015cef3e25/href">https://medium.com/media/8ed135b96e7ffd1c4bd29e015cef3e25/href</a></iframe><p>To close the dropdown, we will call the destroyDropdown method which will detach the overlay.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b544baf78dabde45bced53b28bd4a12a/href">https://medium.com/media/b544baf78dabde45bced53b28bd4a12a/href</a></iframe><h3>Create a Dropdown Component</h3><p>The component is pretty simple. Just wrap ng-content with a div and add some style to it. As mentioned before, we will use closed here to emit a new value when there is a click on the dropdown itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a43817d7cc4a4fc68ffa7837026d84d/href">https://medium.com/media/7a43817d7cc4a4fc68ffa7837026d84d/href</a></iframe><p>We will also implement dropdown-panel interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36aa3cfc376f16b9197ba87672f8a3f0/href">https://medium.com/media/36aa3cfc376f16b9197ba87672f8a3f0/href</a></iframe><h3>Done! 🎉</h3><p>And that’s really it! We’ve seen how Angular CDK overlay can be used to create a reusable dropdown component and we are only scratching the surface here. CDK modules can help you develop many more kinds of components. Dialog, toast, drawer, tooltip, popover just to name a few.</p><p>Hope you find this article helpful. If you have any questions, don’t hesitate to drop a comment. 👋</p><p>Here is the complete source code,</p><p><a href="https://stackblitz.com/edit/angular-cdk-dropdown?embed=1&amp;file=src/app/dropdown/dropdown-trigger-for.directive.ts">angular-cdk-dropdown - StackBlitz</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa45455e6a73" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codeshakeio/build-a-dropdown-component-using-angular-cdk-fa45455e6a73">Build a dropdown component using Angular CDK</a> was originally published in <a href="https://medium.com/codeshakeio">codeshakeio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>