avatarJen-Hsuan Hsieh (Sean)

Summary

The article provides a guide on implementing theming in Angular applications using CSS variables without the need for third-party libraries.

Abstract

The article titled "Theming for Angular Applications with CSS Variables" introduces a method for creating global themes in Angular applications. It emphasizes the elegance of using CSS variables over installing external libraries, as demonstrated in React applications. The implementation process includes installing the @fortawesome/angular-fontawesome package for theme switching, defining CSS variables in the :root selector, creating pre-defined themes, and developing a theme service and component to facilitate theme changes at runtime. The author, Sean, shares his experience as a software engineer and invites feedback on the approach, which is also demonstrated in a live demo. The article serves as a personal note and encourages readers to engage with the author's Facebook page and side project, Daily Learning, for further programming insights.

Opinions

  • The author, Sean, believes that theming in Angular can be achieved more elegantly without third-party libraries by leveraging CSS variables.
  • Sean suggests that defining colors as CSS variables in the :root selector provides a clean and efficient way to apply global styles.
  • The creation of a theme service and component indicates the author's preference for a structured approach to theme management, allowing for easy switching between themes.
  • By sharing his work and inviting feedback, Sean values community input and continuous learning within the software development field.
  • The inclusion of related topics and articles shows that the author sees the broader context of theming within the realm of web development and encourages exploration of other relevant subjects.

Theming for Angular Applications with CSS Variables

Introduction

In React applications, we could use createGlobalStyle from styled-component to introduce the global style as we mentioned before.

In Angular, it’s more elegant. We won’t have to install any third party libraries to create global themes. Instead, we could define CSS variables and switch their definitions during the runtime.

Agenda

  • Implementation
  • Demo

Implementation

1. Install required packages

Install the font-awesome/angular-fontawesome package for creating the theme switcher later.

npm install --save @font-awesome/angular-fontwaesome

2. Define CSS variables

First, we have to define colors as CSS variables in the styles.scss. The properties defined in the :root section could be referred to globally.

:root represents the ml> element and is identical to the selector html .

Then replace colors of all components to CSS variables.

3. Create pre-defined themes

Create a new TypeScript file.

  • Define required theme types (line 1 ~ line 4)
  • Define required themes in the JSON format (line11 ~ line 35)

4. Create a theme service to operate the theme

Create a new service from the ng-cli.

ng g s theme

Update the service.

  • Expose the method to retrieve the theme type from memory and localStorage (line 20 ~ line 23)
  • Expose the method to set the theme type to memory and localStorage (line 42 ~ line 47)

The key is document.documentElement(line 55). It stands for the root DOM. In setCurrentTheme() , we set the new value for the corresponding theme.

5. Create a theme component for the theme switcher

Create a new component from the ng-cli.

ng g c theme

Update the theme.component.ts.

  • In ngOnInit() phase, we set the corresponding icon for the theme type retrieved from the service (line 23 ~ line 26)
  • When the user toggles, we set the icon for the next theme type retrieved from the service (line 28 ~ line 32)

Update the theme.component.html to place the icon.

Update the theme.component.scss to switch the theme of the icon.

The theme switcher will look like the following images.

Demo

Feel free to check the following website.

Reference

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

  • The Facebook page for articles
  • The latest side project: Daily Learning

Related topics

How to use the two-way binding in Knout.js and ReactJS?

Learn how to use SignalR to build a chatroom application

My reflection of :

IT & Network:

Database:

Software testing:

Debugging:

DevOps:

Software Development
Themes
Angular
Css Variables
JavaScript
Recommended from ReadMedium