如何在Angular Material中制作自定義主題

by Charlee Li

通過李李

如何在Angular Material中制作自定義主題 (How to make a custom theme in Angular Material)

Angular Material is a great library that implements Material Design for Angular 2+. The official document is sufficient regarding the component usages, while there are few articles about how to customize the theme itself, specifically, the colors used in the theme.

Angular Material是一個很棒的庫,可實現Angular 2+的Material Design 。 關于組件用法的官方文檔就足夠了,而關于如何自定義主題本身(特別是主題中使用的顏色)的文章很少。

In this post I would like to summarize what I’ve learned these months from customizing Angular Material themes.

在這篇文章中,我想總結一下這幾個月來我從定制Angular Material主題中學到了什么。

Note this article is NOT about AngularJS Material, which is used for AngularJS 1.x.

請注意,本文與用于AngularJS 1.x的 AngularJS材質 無關 。

Some common posts about customizing themes are:

有關定制主題的一些常見文章是:

  • “Theming your Angular Material app”, the official guide for custom themes,

    自定義主題的官方指南“ 主題化您的Angular Material應用 ”,

  • “The complete guide to Angular Material Themes” by Tomas Trajan, which provides many undocumented instructions. Strongly recommended.

    Tomas Trajan 撰寫的 “ Angular Material Themes完整指南 ”,其中提供了許多未記錄的說明。 強烈推薦

I didn’t find other useful posts and would appreciate if anyone could provide some resources in the comments.

我沒有找到其他有用的帖子,如果有人可以在評論中提供一些資源,我將不勝感激。

如何創建自定義主題 (How to Create a Custom Theme)

Creating a material theme is extremely simple: you only need to pick three colors — primary, accent, and warn — and Angular Material will do the rest for you. The material palette page explains how it works clearly, and you can also create a theme visually with Color Tool.

創建材質主題非常簡單:您只需選擇三種顏色( 色, 強調色和警告色),而Angular Material會為您完成其余工作。 材質調色板頁面說明了其清晰的工作原理,您還可以使用Color Tool直觀地創建主題。

In regards to code, all you need to do is to create the following theme file:

關于代碼,您需要做的就是創建以下主題文件:

// theme.scss@import '~@angular/material/theming';
$my-theme-primary: mat-palette($mat-green);$my-theme-accent : mat-palette($mat-amber);$my-theme-warn   : mat-palette($mat-red);
$my-theme: mat-light-theme(    $my-theme-primary,    $my-theme-accent,    $my-theme-warn);

Then you need to apply this theme in your main style.scss file:

然后,您需要在主style.scss文件中應用此主題:

@import "theme.scss";
@include mat-core();@include angular-material-theme($my-theme);

如何在組件中使用自定義主題 (How to Use Custom Theme in Components)

After creating our own theme, requirements like this will rise:

創建我們自己的主題后,將產生如下要求:

I want to create a text box. The text color, background color, and border color should all come from our own theme, not by hard coding.
我想創建一個文本框。 文字顏色,背景顏色和邊框顏色都應來自我們自己的主題,而不是硬編碼。

This requirement is pretty common — anyway, being able to be used in components is exactly why we want to create a custom theme. The problem is how.

這項要求很常見-無論如何,能夠在組件中使用正是我們想要創建自定義主題的原因。 問題是如何。

混合方法 (The mixin approach)

The first official document I shared proposed a way of using SCSS’s mixin. I call it a “bottom-up” approach, which includes the following steps:

我共享的第一份正式文檔提出了一種使用SCSS的mixin的方法。 我稱之為“自下而上”的方法,其中包括以下步驟:

  1. Each component defines a theme mixin, and retrieves colors from $theme parameter.

    每個組件都定義一個主題混合,并從$theme參數檢索顏色。

  2. A global theme.scss defines the custom theme, then includes all the component theme mixins and calls them with the custom theme.

    全局theme.scss定義了自定義主題,然后包括所有組件主題mixins并使用自定義主題對其進行調用。

In addition to the theme.scss definition mentioned above, each component needs to create a theme file like this:

除了上面提到的theme.scss定義外,每個組件還需要創建一個主題文件,如下所示:

// src/app/comp-a/comp-a.theme.scss@import '~@angular/material/theming';
@mixin comp-a-theme($theme) {          // define mixin  $primary: map-get($theme, primary);  // retrieve color def  button {                             // apply theme to component    background-color: mat-color($primary);  }}

And probably you want a custom-theme.scss to import all the component level themes:

可能您希望custom-theme.scss導入所有組件級主題:

// src/app/custom-theme.scss@import '~@angular/material/theming';@import 'src/app/comp-a/comp-a.theme';@import 'src/app/comp-b/comp-b.theme';
@mixin custom-themes($theme) {  @include comp-a-theme($theme);  @include comp-b-theme($theme);}

Then import the above custom-theme.scss in your theme.scss:

然后將上面的custom-theme.scss導入您的theme.scss

// theme.scss...@import './custom-theme';@include custom-themes($my-theme);

This hierarchy works, and probably is the only way when you need to support multiple themes.

這種層次結構有效,并且可能是您需要支持多個主題的唯一方法。

However, most of the time we only support one theme, and using a mixin could be cumbersome. Mainly there are three disadvantages with this approach:

但是,大多數時候我們只支持一個主題,使用mixin可能很麻煩。 這種方法主要存在三個缺點:

  1. Every single color reference needs a separate .theme.scss file.

    每個單色參考都需要一個單獨的.theme.scss文件。

  2. custom-theme.scss must know exactly which components provide custom themes. This creates unnecessary dependencies.

    custom-theme.scss必須準確知道哪些組件提供了自定義主題。 這將創建不必要的依賴關系。

  3. Most importantly, component level theme files are not encapsulated.

    最重要的是,未封裝組件級主題文件。

The first and second points are pretty self-explanatory. Let me explain a little bit about point 3. This involves some background knowledge called “View Encapsulation”.

第一點和第二點很不言而喻。 讓我對點3進行一些解釋。這涉及一些稱為“視圖封裝”的背景知識。

Angular uses a technique called “View Encapsulation” to keep component CSS local. In other words, rules defined for one component will stay in that component and will not affect other components.

Angular使用一種稱為“ 視圖封裝 ”的技術將組件CSS保持在本地 。 換句話說,為一個組件定義的規則將保留在該組件中,并且不會影響其他組件。

In this way you can define CSS class name freely in your component without worrying about naming conflicts. However, view encapsulation is done only if the CSS is defined through @Component, i.e. @Component({ styleUrls: ['./comp-a.scss'] }).

這樣,您可以在組件中自由定義CSS類名,而不必擔心命名沖突。 但是,僅在通過@Component定義CSS的情況下才進行視圖封裝,即@Component({ styleUrls: ['./comp-a.scss'] })

As to our custom theme file comp-a.theme.scss, since it is imported directly by custom-theme.scss, its rules are not encapsulated so it will apply to all elements on the page. In the example above, I used the following code (which was WRONG!):

至于我們的自定義主題文件comp-a.theme.scss ,由于它是由custom-theme.scss直接導入的,因此其規則未封裝,因此將適用于頁面上的所有元素。 在上面的示例中,我使用了以下代碼(錯誤!):

@mixin comp-a-theme($theme) {  button { ... }    // This will apply to ALL buttons!}

But this will apply the style to all the buttons instead of those buttons belonging to comp-a only. You have to do something like comp-a button in order to make this work correctly.

但這會將樣式應用于所有按鈕,而不是僅屬于comp-a的那些按鈕。 您必須執行諸如comp-a button的操作才能使其正常工作。

直接方法 (The direct approach)

Therefore I propose a better approach. Instead of using a mixin, we let each component include the theme file and use the color definition directly.

因此,我提出了一種更好的方法。 讓每個組件都包括主題文件并直接使用顏色定義,而不是使用mixin。

In this approach, the component theme file will look like this:

用這種方法,組件主題文件將如下所示:

// NOTE: just do this in your regular scss file.// No need to create separate theme file!// src/app/comp-a/comp-a.scss@import 'src/theme.scss';
$primary: map-get($my-theme, primary);button {  background-color: mat-color($primary);}

And that’s all.

就這樣。

Let’s see how this works. First, theme related rules are put into the component SCSS file, so no extra component level theme file required. Second, the main theme.scss does not need to know component level themes (since it does not need to import them), so a simple theme definition is adequate. Third, the component SCSS file is used with @Component so it is encapsulated correctly, which means we can simply define rules for button.

讓我們看看它是如何工作的。 首先,將與主題相關的規則放入組件SCSS文件中,因此不需要額外的組件級主題文件。 其次,主theme.scss不需要知道組件級主題(因為它不需要導入它們),因此簡單的主題定義就足夠了。 第三,組件SCSS文件與@Component使用,因此它被正確封裝,這意味著我們可以簡單地為button定義規則。

預定義的主題鍵 (Predefined Theme Keys)

Probably you have noticed the next problem. What are the foreground, primary in above theme files ( map-get($my-theme, primary))? Are there any other keys I can use?

可能您已經注意到下一個問題。 上面的主題文件( map-get($my-theme, primary) )中的foregroundprimary是什么? 我還能使用其他任何鍵嗎?

Well these “keys” refer to different colors defined in the theme. However I could not find any documents explaining these “keys”, so the only way I could find out is to read the source code. (Although it is said that good programmers should read the code, having to read the code is definitely not a good sign for a library.)

這些“鍵”指的是主題中定義的不同顏色。 但是,我找不到解釋這些“鍵”的任何文檔,所以我唯一能找到的方法就是閱讀源代碼 。 (盡管據說好的程序員應該閱讀代碼, 但是必須閱讀代碼絕對不是一個好的庫。)

Open node_modules/@angular/material/_theming.scss and you will see the definitions for these keys. For future reference, I would like to summarize the keys here.

打開node_modules/@angular/material/_theming.scss ,您將看到這些鍵的定義。 為了將來參考,我想在這里總結一下按鍵。

$theme  |- primary  |- accent  |- warn  |- foreground  |   |- base  |   |- divider  |   |- dividers  |   |- disabled  |   |- disabled-button  |   |- disabled-text  |   |- hint-text  |   |- secondary-text  |   |- icon  |   |- icons  |   |- text  |   |- slider-min  |   |- slider-off  |   `- slider-off-active  |- background  |   |- status-bar  |   |- app-bar  |   |- background  |   |- hover  |   |- card  |   |- dialog  |   |- disabled-button  |   |- raised-button  |   |- focused-button  |   |- selected-button  |   |- selected-disabled-button  |   |- disabled-button-toggle  |   |- unselected-chip  |   `- disabled-list-option  `- is-dark         // bool, whether dark theme or not

For example, if you want to render a disabled text in your component, you may want to use the following code:

例如,如果要在組件中呈現禁用的文本,則可能需要使用以下代碼:

$foreground: map-get($my-theme, foreground);.disabled-text {  color: mat-color($foreground, disabled-text);}

Okay these are some lessons I’ve learned from struggling with Angular Material. Hope this post is helpful if you are facing similar problems.

好的,這些是我從Angular Material的努力中學到的教訓。 如果您遇到類似的問題,希望這篇文章對您有所幫助。

翻譯自: https://www.freecodecamp.org/news/how-to-make-a-custom-theme-in-angular-material-d47122a1e361/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392469.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392469.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392469.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

最感嘆的莫過于一見如故,最悲傷的莫過于再見陌路。最深的孤獨,是你明知道自己的渴望,卻得對它裝聾作啞。最美的你不是生如夏花,而是在時間的長河里,波瀾不驚。...

最感嘆的莫過于一見如故,最悲傷的莫過于再見陌路。最深的孤獨,是你明知道自己的渴望,卻得對它裝聾作啞。最美的你不是生如夏花,而是在時間的長河里,波瀾不驚。轉載于:https://www.cnblogs.com/dj258/p/7003890.html

java vimrc_.vimrc技巧

-------------------------------------------------------------------" 設置字符編碼。參考:http://www.rainux.org/blog/index.php/2005/10/20/106" encoding: Vim 內部使用的字符編碼方式,包括 Vim 的buffer (緩沖區)、菜單文" 本、消…

將PDF和Gutenberg文檔格式轉換為文本:生產中的自然語言處理

Estimates state that 70%–85% of the world’s data is text (unstructured data). Most of the English and EU business data formats as byte text, MS Word, or Adobe PDF. [1]據估計,全球數據的70%–85%是文本(非結構化數據)。 大多數…

Go_筆試題記錄-指針與值類型實現接口的區別

1、如果Add函數的調用代碼為: func main() {var a Integer 1var b Integer 2var i interface{} &asum : i.(*Integer).Add(b)fmt.Println(sum) } 則Add函數定義正確的是() A.type Integer int func (a Integer) Add(b Integer) Intege…

leetcode 48. 旋轉圖像

解題思路 將數組從里到外分為若干層, 數組 [1,2,3], [4,5,6][7,8,9]的最外層即為 [1,2,3] [4 6][7,8,9] ,將一層分為4條邊,如741 123,將741放到123的位置,123放到369的位置,如此類推(但是放置的…

如何恢復誤刪的OneNote頁面

今天不小心把半個月的日記刪掉了!(為了減少頁面數量,每個月的日記會放在同一個頁面上)。 幸運的是OneNote有自動備份功能,喜極而泣。 操作方法來自微軟支持 打開丟失了最近筆記的筆記本。 單擊“文件”>“信息”&g…

javascript函數式_JavaScript中的函數式編程原理

javascript函數式After a long time learning and working with object-oriented programming, I took a step back to think about system complexity.經過長時間的學習和使用面向對象的編程,我退后了一步來思考系統的復雜性。 “Complexity is anything that mak…

java writeint_Java DataOutputStream.writeInt(int v)類型

DataOutputStream.writeInt(int v)方法示例DataOutputStream的DataOutputStream.writeInt(int v)方法具有以下語法。public final void writeInt(int v) throws IOException示例在下面的代碼中展示了如何使用DataOutputStream.writeInt(int v)方法。import java.io.DataInputSt…

協方差意味著什么_“零”到底意味著什么?

協方差意味著什么When I was an undergraduate student studying Data Science, one of my professors always asked the same question for every data set we worked with — “What does zero mean?”當我是一名研究數據科學的本科生時,我的一位教授總是對我們處…

Go_筆試題記錄-不熟悉的

1、golang中沒有隱藏的this指針,這句話的含義是() A. 方法施加的對象顯式傳遞,沒有被隱藏起來 B. golang沿襲了傳統面向對象編程中的諸多概念,比如繼承、虛函數和構造函數 C. golang的面向對象表達更直觀,對…

leetcode 316. 去除重復字母(單調棧)

給你一個字符串 s ,請你去除字符串中重復的字母,使得每個字母只出現一次。需保證 返回結果的字典序最小(要求不能打亂其他字符的相對位置)。 注意:該題與 1081 https://leetcode-cn.com/problems/smallest-subsequenc…

Go-json解碼到結構體

廢話不多說,直接干就得了,上代碼 package mainimport ("encoding/json""fmt" )type IT struct {Company string json:"company" Subjects []string json:"subjects"IsOk bool json:"isok"…

leetcode 746. 使用最小花費爬樓梯(dp)

數組的每個索引作為一個階梯,第 i個階梯對應著一個非負數的體力花費值 costi。 每當你爬上一個階梯你都要花費對應的體力花費值,然后你可以選擇繼續爬一個階梯或者爬兩個階梯。 您需要找到達到樓層頂部的最低花費。在開始時,你可以選擇從索…

安卓中經常使用控件遇到問題解決方法(持續更新和發現篇幅)(在textview上加一條線、待續)...

TextView設置最多顯示30個字符。超過部分顯示...(省略號)&#xff0c;有人說分別設置TextView的android:signature"true",而且設置android:ellipsize"end";可是我試了。居然成功了&#xff0c;供大家參考 [java] view plaincopy<TextView android:id…

網絡工程師晉升_晉升為工程師的最快方法

網絡工程師晉升by Sihui Huang黃思慧 晉升為工程師的最快方法 (The Fastest Way to Get Promoted as an Engineer) We all want to live up to our potential, grow in our career, and do the best work of our lives. Getting promoted at work not only proves that we hav…

java 銀行存取款_用Java編寫銀行存錢取錢

const readline require(‘readline-sync‘)//引用readline-synclet s 2;//錯誤的次數for (let i 0; i < 3; i) {console.log(‘請輸入名&#xff1a;(由英文組成)‘);let user readline.question();console.log(‘請輸入密碼&#xff1a;(由數字組成)‘);let password …

垃圾郵件分類 python_在python中創建SMS垃圾郵件分類器

垃圾郵件分類 python介紹 (Introduction) I have always been fascinated with Google’s gmail spam detection system, where it is able to seemingly effortlessly judge whether incoming emails are spam and therefore not worthy of our limited attention.我一直對Goo…

leetcode 103. 二叉樹的鋸齒形層序遍歷(層序遍歷)

給定一個二叉樹&#xff0c;返回其節點值的鋸齒形層序遍歷。&#xff08;即先從左往右&#xff0c;再從右往左進行下一層遍歷&#xff0c;以此類推&#xff0c;層與層之間交替進行&#xff09;。例如&#xff1a; 給定二叉樹 [3,9,20,null,null,15,7],3/ \9 20/ \15 7 返回…

簡單易用的MongoDB

從我第一次聽到Nosql這個概念到如今已經走過4個年頭了&#xff0c;但仍然沒有具體的去做過相應的實踐。最近獲得一段學習休息時間&#xff0c;購買了Nosql技術實踐一書&#xff0c;正在慢慢的學習。在主流觀點中&#xff0c;Nosql大體分為4類&#xff0c;鍵值存儲數據庫&#x…

html畫布圖片不顯示_如何在HTML5畫布上顯示圖像

html畫布圖片不顯示by Nash Vail由Nash Vail Ok, so here’s a question: “Why do we need an article for this, Nash?”好的&#xff0c;這是一個問題&#xff1a;“為什么我們需要為此寫一篇文章&#xff0c;納什&#xff1f;” Well, grab a seat.好吧&#xff0c;坐下…