<?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 Tarang Khandelwal on Medium]]></title>
        <description><![CDATA[Stories by Tarang Khandelwal on Medium]]></description>
        <link>https://medium.com/@tarangkhandelwal?source=rss-c481ac91a0a7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*KfA2izEDn5M26JJ9q4V1-g.jpeg</url>
            <title>Stories by Tarang Khandelwal on Medium</title>
            <link>https://medium.com/@tarangkhandelwal?source=rss-c481ac91a0a7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:35:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tarangkhandelwal/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[Doing more with ngx/translate.]]></title>
            <link>https://medium.com/@tarangkhandelwal/doing-more-with-ngx-translate-eb8b999a4143?source=rss-c481ac91a0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb8b999a4143</guid>
            <category><![CDATA[translation]]></category>
            <category><![CDATA[localization]]></category>
            <category><![CDATA[ngx-translate]]></category>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Tarang Khandelwal]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 05:31:26 GMT</pubDate>
            <atom:updated>2020-06-08T08:33:44.580Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Most applications on the web work in the English language. But not everyone in the world understands English. For products to increase their reach, they must operate in regional languages that people are comfortable with. Thus it is important today to have localization/internationalization (i18n) to be built-in in our web applications &amp; support many languages for products.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IUb2IJgLVaqAZlmb" /><figcaption>Photo by <a href="https://unsplash.com/@farzadme?utm_source=medium&amp;utm_medium=referral">Farzad Mohsenvand</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Different web frameworks have their way to allow having i18n apps. For Angular, most common way is to use the <a href="http://www.ngx-translate.com/">ngx/translate</a> package. It has a simple consumption model and works well for most projects.</p><p>I am part of a UI team for a product that acts as a platform for many other products, deployed on-premise and offered as SaaS service as well. The application’s UI could be launched in different application modes. The product could be integrated into other products (as plugin) &amp; can also work independently (same source code running in different modes). With ngx/translate, we wanted to do more than just translations into locale specific strings.</p><h3><strong>Translation use cases we wanted to address:</strong></h3><p>1. <strong>Conditional i18n strings:</strong> Based on application mode, we wanted to load some additional i18n strings as some UI parts were visible in specific modes only.</p><p>2. <strong>Overriding i18n values:</strong> As the product could be integrated as a plugin into other products, there were cases where certain terminology was different between them. e.g. Object Name column in one product was called ‘Name’ while in some other product it could be ‘Resource Name’.</p><p>To solve the above issues, we wanted something which is generic enough, easy to debug &amp; maintainable.<br>Like for handling terminology differences, we could have done something like if-else, or switch case sort of thing at all places but that would be spread across the components and is not very clean for large applications.</p><h3><strong><em>Customizing ngx-translate loader</em></strong></h3><p>While looking to solve these requirements elegantly, we ended up finding a solution that allowed having custom logic by extending default TranslateLoader in ngx-translate. We customized this as per our needs.</p><p><strong>Credits</strong>: <a href="https://github.com/denniske/ngx-translate-multi-http-loader">ngx-translate-multi-http-loader</a></p><h4>Extended TranslateLoader implementation</h4><ol><li><strong>Multiple i18n resource loader: </strong>We extended Translate Loader to support loading multiple i18n resource bundles, combine the strings into a single bundle.</li></ol><p><strong>Note:</strong> Order in which the resource bundles are loaded is important if you have the same key’s in multiple files.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28171%252C%2520184%252C%2520195%252C%25201%29%26t%3Dmaterial%26wt%3Dnone%26l%3Dapplication%252Ftypescript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dexport%252520interface%252520TranslationResource%252520%25257B%25250A%252520%252520prefix%25253A%252520string%25253B%25250A%252520%252520suffix%25253A%252520string%25253B%25250A%25257D%25250A%25250Aexport%252520class%252520MultiTranslateHttpLoader%252520implements%252520TranslateLoader%252520%25257B%25250A%252520%252520constructor%28%25250A%252520%252520%252520%252520private%252520http%25253A%252520HttpClient%25252C%25250A%252520%252520%252520%252520private%252520resources%25253A%252520TranslationResource%25255B%25255D%25250A%252520%252520%29%252520%25257B%25257D%25250A%25250A%252520%252520public%252520getTranslation%28locale%25253A%252520string%29%25253A%252520Observable%25253C%25257B%25255Bkey%25253A%252520string%25255D%25253A%252520string%25257D%25253E%252520%25257B%25250A%252520%252520%252520%252520const%252520requests%252520%25253D%252520this.resources.map%28resource%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520%252520%252520const%252520path%252520%25253D%252520resource.prefix%252520%25252B%252520locale%252520%25252B%252520resource.suffix%25253B%25250A%25250A%252520%252520%252520%252520%252520%252520return%252520this.http.get%28path%29.pipe%28catchError%28res%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25252F%25252F%252520return%252520empty%252520object%252520if%252520file%252520is%252520not%252520available.%25250A%252520%252520%252520%252520%252520%252520%252520%252520return%252520of%28%25257B%25257D%29%25253B%25250A%252520%252520%252520%252520%252520%252520%25257D%29%29%25253B%25250A%252520%252520%252520%252520%25257D%29%25253B%25250A%252520%252520%252520%252520return%252520forkJoin%28requests%29.pipe%28map%28response%252520%25253D%25253E%252520%252520%25257B%25250A%252520%252520%252520%252520%252520%252520const%252520mergedStrings%252520%25253D%252520Object.assign%28%252520%25257B%25257D%25252C%252520...response%252520%29%25253B%25250A%252520%252520%252520%252520%252520%252520return%252520mergedStrings%25253B%25250A%252520%252520%252520%252520%25257D%29%29%25253B%25250A%252520%252520%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28171%25252C%252520184%25252C%252520195%25252C%2525201%29%26t%3Dmaterial%26wt%3Dnone%26l%3Dapplication%25252Ftypescript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dexport%25252520interface%25252520TranslationResource%25252520%2525257B%2525250A%25252520%25252520prefix%2525253A%25252520string%2525253B%2525250A%25252520%25252520suffix%2525253A%25252520string%2525253B%2525250A%2525257D%2525250A%2525250Aexport%25252520class%25252520MultiTranslateHttpLoader%25252520implements%25252520TranslateLoader%25252520%2525257B%2525250A%25252520%25252520constructor%28%2525250A%25252520%25252520%25252520%25252520private%25252520http%2525253A%25252520HttpClient%2525252C%2525250A%25252520%25252520%25252520%25252520private%25252520resources%2525253A%25252520TranslationResource%2525255B%2525255D%2525250A%25252520%25252520%29%25252520%2525257B%2525257D%2525250A%2525250A%25252520%25252520public%25252520getTranslation%28locale%2525253A%25252520string%29%2525253A%25252520Observable%2525253C%2525257B%2525255Bkey%2525253A%25252520string%2525255D%2525253A%25252520string%2525257D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520const%25252520requests%25252520%2525253D%25252520this.resources.map%28resource%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520const%25252520path%25252520%2525253D%25252520resource.prefix%25252520%2525252B%25252520locale%25252520%2525252B%25252520resource.suffix%2525253B%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520return%25252520this.http.get%28path%29.pipe%28catchError%28res%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520return%25252520empty%25252520object%25252520if%25252520file%25252520is%25252520not%25252520available.%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%25252520of%28%2525257B%2525257D%29%2525253B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%29%29%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%29%2525253B%2525250A%25252520%25252520%25252520%25252520return%25252520forkJoin%28requests%29.pipe%28map%28response%25252520%2525253D%2525253E%25252520%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520const%25252520mergedStrings%25252520%2525253D%25252520Object.assign%28%25252520%2525257B%2525257D%2525252C%25252520...response%25252520%29%2525253B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520return%25252520mergedStrings%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%29%29%2525253B%2525250A%25252520%25252520%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/32de7ce55c9fdbc0e0681343def74779/href">https://medium.com/media/32de7ce55c9fdbc0e0681343def74779/href</a></iframe><p>2.<strong> Using custom loader to load translations: </strong>We provide our custom implementation to be used for loading translations with a dependency on HttpClient.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28171%252C%2520184%252C%2520195%252C%25201%29%26t%3Dmaterial%26wt%3Dnone%26l%3Dapplication%252Ftypescript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DTranslateModule.forRoot%28%25257B%25250A%252520%252520%252520%252520%252520%252520loader%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520provide%25253A%252520TranslateLoader%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520useFactory%25253A%252520HttpLoaderFactory%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520deps%25253A%252520%25255BHttpClient%25255D%25250A%252520%252520%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520%25257D%29&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28171%25252C%252520184%25252C%252520195%25252C%2525201%29%26t%3Dmaterial%26wt%3Dnone%26l%3Dapplication%25252Ftypescript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DTranslateModule.forRoot%28%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520loader%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520provide%2525253A%25252520TranslateLoader%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520useFactory%2525253A%25252520HttpLoaderFactory%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520deps%2525253A%25252520%2525255BHttpClient%2525255D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%2525257D%29&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/07e45a093b747397caac19dfa7e02d7d/href">https://medium.com/media/07e45a093b747397caac19dfa7e02d7d/href</a></iframe><p>3. <strong>Defining i18n resources to load: </strong>Here,<strong> </strong>we define logic about what i18n resource bundles to load and when. There can be n number of resources being added here and they would all be loaded, combined into a JSON object, and used for application i18n translations.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28171%252C%2520184%252C%2520195%252C%25201%29%26t%3Dmaterial%26wt%3Dnone%26l%3Dapplication%252Ftypescript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520appMode%252520%25253D%252520getAppMode%28%29%25253B%25250A%25250Aexport%252520function%252520HttpLoaderFactory%28http%25253A%252520HttpClient%29%252520%25257B%25250A%252520%252520%25252F%25252F%252520default%252520translations%25250A%252520%252520const%252520translationResouceList%25253A%252520TranslationResource%25255B%25255D%252520%25253D%252520%25255B%25250A%252520%252520%252520%252520%25257B%252520prefix%25253A%252520%252522.%25252Fassets%25252Fi18n%25252F%252522%25252C%252520suffix%25253A%252520%252522.json%252522%252520%25257D%25250A%252520%252520%25255D%25253B%25250A%25250A%252520%252520%25252F%25252F%252520load%252520overriding%252520strings%252520if%252520app%252520is%252520in%252520override%252520mode.%25250A%252520%252520if%252520%28appMode%252520%25253D%25253D%25253D%252520%2527override%2527%29%252520%25257B%25250A%252520%252520%252520%252520translationResouceList.push%28%25257B%25250A%252520%252520%252520%252520%252520%252520prefix%25253A%252520%252522.%25252Fassets%25252FoverrideI18n%25252F%252522%25252C%25250A%252520%252520%252520%252520%252520%252520suffix%25253A%252520%252522.json%252522%25250A%252520%252520%252520%252520%25257D%29%25253B%25250A%252520%252520%25257D%25250A%252520%252520return%252520new%252520MultiTranslateHttpLoader%28http%25252C%252520translationResouceList%29%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28171%25252C%252520184%25252C%252520195%25252C%2525201%29%26t%3Dmaterial%26wt%3Dnone%26l%3Dapplication%25252Ftypescript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520appMode%25252520%2525253D%25252520getAppMode%28%29%2525253B%2525250A%2525250Aexport%25252520function%25252520HttpLoaderFactory%28http%2525253A%25252520HttpClient%29%25252520%2525257B%2525250A%25252520%25252520%2525252F%2525252F%25252520default%25252520translations%2525250A%25252520%25252520const%25252520translationResouceList%2525253A%25252520TranslationResource%2525255B%2525255D%25252520%2525253D%25252520%2525255B%2525250A%25252520%25252520%25252520%25252520%2525257B%25252520prefix%2525253A%25252520%25252522.%2525252Fassets%2525252Fi18n%2525252F%25252522%2525252C%25252520suffix%2525253A%25252520%25252522.json%25252522%25252520%2525257D%2525250A%25252520%25252520%2525255D%2525253B%2525250A%2525250A%25252520%25252520%2525252F%2525252F%25252520load%25252520overriding%25252520strings%25252520if%25252520app%25252520is%25252520in%25252520override%25252520mode.%2525250A%25252520%25252520if%25252520%28appMode%25252520%2525253D%2525253D%2525253D%25252520%27override%27%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520translationResouceList.push%28%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520prefix%2525253A%25252520%25252522.%2525252Fassets%2525252FoverrideI18n%2525252F%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520suffix%2525253A%25252520%25252522.json%25252522%2525250A%25252520%25252520%25252520%25252520%2525257D%29%2525253B%2525250A%25252520%25252520%2525257D%2525250A%25252520%25252520return%25252520new%25252520MultiTranslateHttpLoader%28http%2525252C%25252520translationResouceList%29%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/63178380031fde6e49b96057e5226c26/href">https://medium.com/media/63178380031fde6e49b96057e5226c26/href</a></iframe><p>This allowed us to solve both our issues:</p><ol><li><strong>Conditional resource bundles:</strong> Based on the mode, we defined what resources we want to load.</li></ol><p><strong>2. Overriding certain i18n values:</strong> We kept the strings that are to be overridden in separate translation files &amp; loaded them on demand.</p><p>I have created a working example with the code sample I used above. It can be updated further as per the individual’s requirement.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstackblitz.com%2Fedit%2Fmulti-http-loader%3Fembed%3D1&amp;display_name=StackBlitz&amp;url=https%3A%2F%2Fstackblitz.com%2Fedit%2Fmulti-http-loader&amp;image=https%3A%2F%2Fc.staticblitz.com%2Fassets%2Ficon-1d2450aadcf984376236c23cd2ab0ba231ddd1d85c9b72990ed2ede5b4974d1b.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=stackblitz" width="745" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/82560091d86d5429cb4e60154be00bb6/href">https://medium.com/media/82560091d86d5429cb4e60154be00bb6/href</a></iframe><h3>Conclusion:</h3><p>There could have been several different ways to solve these requirements but we ended up customizing TranslateLoader.</p><p>This worked well for us where we have huge applications with several developers involved. It resulted in these:</p><ol><li>Single point of management of i18n resource loading &amp; related business logic.</li><li>Developers would not have to think about handling these cases in their components as it is managed at infrastructure level.</li><li>It becomes easy to Unit Test as it is clear what strings are to be visible in what application mode.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb8b999a4143" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Theming Angular App & its Libraries.]]></title>
            <link>https://medium.com/@tarangkhandelwal/theming-angular-app-its-libraries-8f9e46168751?source=rss-c481ac91a0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f9e46168751</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[css3-variables]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[theming]]></category>
            <dc:creator><![CDATA[Tarang Khandelwal]]></dc:creator>
            <pubDate>Fri, 04 Oct 2019 06:34:37 GMT</pubDate>
            <atom:updated>2019-10-16T14:36:23.376Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Journey from SASS based theming to CSS3 variables for angular app and its libraries.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jBKuJ8qjRHVLuIst" /><figcaption>Photo by <a href="https://unsplash.com/@kellitungay?utm_source=medium&amp;utm_medium=referral">Kelli Tungay</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I work on a big Angular app (~100+ modules) that offers dynamic themes: light and dark mode.</p><p>We had been using SASS variables and angular’s <em>:host-context</em> selector to theme our components but recently moved to CSS3 variable based themes which worked out great.</p><blockquote>SASS based approach</blockquote><p>In this approach a top level root element, &lt;body&gt; tag in our case had one of the classes applied to it to identify the current theme.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9a3401073efeba08898bb610eaaffe0/href">https://medium.com/media/b9a3401073efeba08898bb610eaaffe0/href</a></iframe><p>This approach was working fine in terms of changing visual aspects on theme switch. But with our app growing significantly there were few issues we realised:</p><ol><li>It was difficult to make sure all our components work on both themes. We had to depend on code reviews , testing efforts to hope things are fine across themes. As you can probably guess, its way more efforts and still chances of bugs hitting are more.</li><li>Performance hit: Increase in bundle size as similar styles for each theme were being bundled together. Only one of these styles were actually being applied at once.</li><li>Making sure that component colours are from specific palette only and not outside of it.</li></ol><blockquote><strong>CSS3 Variables to the rescue</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aRwe73a0a99S4mBL" /><figcaption>Photo by <a href="https://unsplash.com/@pankajpatel?utm_source=medium&amp;utm_medium=referral">Pankaj Patel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>With some research on alternative approaches to tackle these issues, we stumbled upon CSS3 variables.</p><p>In comparison to SASS variables which are compile time variables, these are supported natively in browsers.</p><p>This brings several benefits:</p><ol><li><strong>Easy maintainability</strong>: Unlike SASS, we can override the value of existing custom properties &amp; do not need separate variables per theme.</li><li><strong>Fast:</strong> Adding new theme to your App becomes super fast. You just need a file with new values for defined set of variables in your app.</li><li><strong>Extensibility:</strong> Variables could be loaded from Ts file or external source also like an API call. This could be really helpful for product applications that allows defining custom themes for partners or customers.</li><li><strong>Reliability: </strong>Making sure all components that use CSS3 variables are behaving as per variable values for theme. No chance of missing handling specific themes.</li><li><strong>Performance: </strong>Only one set of variables in browser at once. Variables can be swapped on theme change to load specific set of variables.</li></ol><blockquote><strong>Implementation details</strong></blockquote><p>Our app is structured in a way where we have a parent app and couple of shared libraries (Angular library). For our app to be 100% theme compatible we needed to make sure our app &amp; libraries also have support for light &amp; dark themes. <br><strong>Note:</strong> <em>We apply the variables only on root and not at local component levels</em></p><p>Theme variable files. Below are the example files we have for Parent App. Similar files exist in shared libraries also.</p><p>Light Theme: <em>Set of colours for light theme</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fdfd5de19e56007242cfebdfa6f3992/href">https://medium.com/media/4fdfd5de19e56007242cfebdfa6f3992/href</a></iframe><p>Dark theme: <em>Set of colours for dark theme</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fe6aed90685f2e3f182d264d7cc90f2/href">https://medium.com/media/3fe6aed90685f2e3f182d264d7cc90f2/href</a></iframe><p>Sample Component Style:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc425c987b83d2ddcd102f06b722eb87/href">https://medium.com/media/cc425c987b83d2ddcd102f06b722eb87/href</a></iframe><p>Importing styles from shared libraries into Parent App:</p><p>For theming the components that exists in shared libraries, the libraries need to output a file per theme with list of CSS3 variables used across the library.<br>These files should then be imported in parent app in light/dark theme files.</p><p>Our <em>light-theme.scss</em> should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ace24214a7a99aa7cbf64f1f66b1923/href">https://medium.com/media/2ace24214a7a99aa7cbf64f1f66b1923/href</a></iframe><p><strong>Note: </strong>For shared library to output multiple files post build, I am using <a href="https://github.com/mysticatea/cpx">cpx</a> module to simply copy files in the build directory.</p><pre>&quot;cpx &#39;./projects/shared-library/src/lib/style/*&#39; &#39;./dist/shared-library/style&#39; &quot;</pre><p>For each shared library, we have set files with CSS3 variables for each theme.</p><blockquote><strong>Present Day State</strong></blockquote><ol><li>We have removed all our SASS code that was doing the theming stuff &amp; fully based on CSS3 variables.</li><li>We have much better control on what colours are being used in the app.</li><li>Future plan is to have compile time checks using sass-lint/custom validators to make sure only CSS3 variables are used for defining colours.</li><li>For future: Compile time checks to make sure variable set is same across the themes or there exists fallback colours.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f9e46168751" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>