AngularCLI集成material design自定義主題與總結(jié)

官方自定義主題指南:theming guide

一、官方指南說明:

典型樣例:在src目錄下新建material-design.scss

@import '~@angular/material/theming';
// Plus imports for other components in your app.
// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core();
// Define the palettes for your theme using the Material Design palettes available in palette.scss
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
// hue.
$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
// The warn palette is optional (defaults to red).
$candy-app-warn:    mat-palette($mat-red);
// Create the theme object (a Sass map containing all of the palettes).
$candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);
// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include angular-material-theme($candy-app-theme);

然后在angular-cli.json催式,styles配置增加剛剛的文件即可使用

說明:

自定義主題主要需要做兩件事:

  1. 引入mat-core() sass mixin早敬,它包含所有通用樣式,適用于多數(shù)組件戚丸,需要注意的是它只能在應(yīng)用中引入一次澄步,多次引入可能導(dǎo)致項目奔潰
  2. 定義主題的數(shù)據(jù)結(jié)構(gòu),作為主題的可用色板胸遇。它可以通過mat-light-theme或者 mat-dark-theme函數(shù)創(chuàng)建,輸出樣式傳遞到angular-material-theme mixin汉形,使其對所有組件有效
    主題文件不要引入到其他的scss文件中

自定義主題主要包含5中調(diào)色板:

  • A primary palette: colors most widely used across all screens and components.常規(guī)狀態(tài)下的所有屏幕樣式與組件樣式
  • An accent palette: colors used for the floating action button and interactive elements.鼠標(biāo)浮于上方或激活狀態(tài)
  • A warn palette: colors used to convey error state.表示錯誤狀態(tài)
  • A foreground palette: colors for text and icons.字體與按鈕
  • A background palette: colors used for element backgrounds.元素背景

如何引入

  • 如果使用Angular CLI纸镊,它支持自動便宜scss文件,所以只需要在angular-cli.json文件的styles列表中引入scss文件即可
  • 使用node-sass編譯scss成css文件概疆,然后index.html文件中引入css文件即刻生效
    語法實例:
node-sass src/unicorn-app-theme.scss dist/unicorn-app-theme.css
注意:

主題文件不可再引入到scss文件中薄腻,如果你想在其他scss文件中引入主題文件中定義的對象(如$candy-app-theme),你可以將自定義對象獨立出來見一個scss文件届案,然后將它們分別引入其他文件中庵楷,切不可將mat-core 與 angular-material-theme mixins重復(fù)引入scss文件中
如果想在組件中引入主題樣式,需要遵循view encapsulation中的組件樣式規(guī)范楣颠。

引入多個主題

#######樣例

@import '~@angular/material/theming';
// Plus imports for other components in your app.

// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// **Be sure that you only ever include this mixin once!**
@include mat-core();

// Define the default theme (same as the example above).
$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
$candy-app-theme:   mat-light-theme($candy-app-primary, $candy-app-accent);

// Include the default theme styles.
@include angular-material-theme($candy-app-theme);


// Define an alternate dark theme.
$dark-primary: mat-palette($mat-blue-grey);
$dark-accent:  mat-palette($mat-amber, A200, A100, A400);
$dark-warn:    mat-palette($mat-deep-orange);
$dark-theme:   mat-dark-theme($dark-primary, $dark-accent, $dark-warn);

// Include the alternative theme styles inside of a block with a CSS class. You can make this
// CSS class whatever you want. In this example, any component inside of an element with
// `.unicorn-dark-theme` will be affected by this alternate dark theme instead of the default theme.
.unicorn-dark-theme {
  @include angular-material-theme($dark-theme);
}

說明:
對于父組件中包含unicorn-dark-theme類名的尽纽,將使用$dark-theme中定義的主題

多個主題中衛(wèi)全局容器下的某個組件制定主題(如彈窗、首頁中包含的組件)

你可以通過OverlayContainer向組件中注入主題類:

import {OverlayContainer} from '@angular/material';

@NgModule({
  // ...
})
export class UnicornCandyAppModule {
  constructor(overlayContainer: OverlayContainer) {
    overlayContainer.themeClass = 'unicorn-dark-theme';
  }
}

OverlayContainer的themeClass可以在任何時候更改

為特定的組件定制主題

angular-material-theme mixin是為所有組件提供樣式支持童漩,當(dāng)你想為特定組件定制樣式的時候弄贿,你可以:

@import '~@angular/material/theming';
// Plus imports for other components in your app.

// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// **Be sure that you only ever include this mixin once!**
@include mat-core();

// Define the theme.
$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
$candy-app-theme:   mat-light-theme($candy-app-primary, $candy-app-accent);

// Include the theme styles for only specified components.
@include mat-core-theme($candy-app-theme);
@include mat-button-theme($candy-app-theme);
@include mat-checkbox-theme($candy-app-theme);

還是需要include mat-core-theme mixin

其他定制主題的內(nèi)容

theming-your-components.md

按照指南操作過程中遇到的問題

  1. 將scss文件引入angular-cli.json,不生效(不是路徑問題),改用node-sass編譯

  2. 安裝node-sass的過程
    ···
    set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/
    cnpm i node-sass
    cnpm i -g node-sass
    ···
    安裝成功后在cmd 輸入 node-sass -v
    出現(xiàn)內(nèi)容

    image.png

    則說明安裝成功

  3. 輸入手動編譯命令報錯:node-sass src/assets/scss/material-lake.scss src/assets/css/material-lake.css
    Error: File to import not found or unreadable: ~@angular/material/_theming.


    image.png

    解決辦法是:改import的路徑矫膨,將@import ~@angular/material/theming';改成@import '../../../node_modules/@angular/material/theming';(最好去檢查下node_modules/@angular/material目錄是否有_theming.scss文件)差凹,import的theming帶不帶下劃線都不影響

  4. mat-palette的入?yún)ⅲ仨毷?a target="_blank" rel="nofollow">Material Design spec中制定的調(diào)色板名
    例如使用$mat-saphire 作為入?yún)崾揪幾g錯誤:Undefined variable

    image.png

  5. 處理了以上幾個問題侧馅,就可以成功編譯了


    image.png

    然后再index.html中引入css文件<link rel="stylesheet" href="src/assets/css/material-lake.css">

  6. 通過在styles.css文件中import的方式引入主題scss文件危尿,會提示scss加了注釋導(dǎo)致項目編譯報錯


    image.png
  7. 關(guān)于語法
    theme.scss文件中$candy-app-primary、$candy-app-accent馁痴、$candy-app-warn可以隨意替換谊娇,但數(shù)量不能超過三個,依次對應(yīng)primary罗晕、accent济欢、warn三種狀態(tài),不填或者在組件中入?yún)⒌臅r候?qū)戝e小渊,則取上一個樣式法褥。

  8. 關(guān)于多個主題的使用
    可以通過組件父類的限制,拓展出多個(自定義)樣式(默認(rèn)是只有三種狀態(tài)的樣式)

項目地址:

https://github.com/LeventZheng/ngx-lake (主要關(guān)注如何自定義與整合酬屉,頁面沒來得及排版)

后續(xù)計劃:

結(jié)合 https://material.angular.io/componentshttp://demos.creative-tim.com/material-dashboard-pro/examples/components/buttons.html 整理一套angular material組件庫出來

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末半等,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌酱鸭,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件垛吗,死亡現(xiàn)場離奇詭異凹髓,居然都是意外死亡,警方通過查閱死者的電腦和手機怯屉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進(jìn)店門蔚舀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人锨络,你說我怎么就攤上這事赌躺。” “怎么了羡儿?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵礼患,是天一觀的道長。 經(jīng)常有香客問我掠归,道長缅叠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任虏冻,我火速辦了婚禮肤粱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘厨相。我一直安慰自己领曼,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布蛮穿。 她就那樣靜靜地躺著庶骄,像睡著了一般。 火紅的嫁衣襯著肌膚如雪践磅。 梳的紋絲不亂的頭發(fā)上瓢姻,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機與錄音音诈,去河邊找鬼幻碱。 笑死,一個胖子當(dāng)著我的面吹牛细溅,可吹牛的內(nèi)容都是我干的褥傍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼喇聊,長吁一口氣:“原來是場噩夢啊……” “哼恍风!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤朋贬,失蹤者是張志新(化名)和其女友劉穎凯楔,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锦募,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡摆屯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了糠亩。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片虐骑。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖赎线,靈堂內(nèi)的尸體忽然破棺而出廷没,到底是詐尸還是另有隱情,我是刑警寧澤垂寥,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布颠黎,位于F島的核電站,受9級特大地震影響滞项,放射性物質(zhì)發(fā)生泄漏盏缤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一蓖扑、第九天 我趴在偏房一處隱蔽的房頂上張望唉铜。 院中可真熱鬧,春花似錦律杠、人聲如沸潭流。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽灰嫉。三九已至,卻和暖如春嗓奢,著一層夾襖步出監(jiān)牢的瞬間讼撒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工股耽, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留根盒,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓物蝙,卻偏偏與公主長得像炎滞,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子诬乞,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,139評論 25 707
  • 在現(xiàn)在的前端開發(fā)中册赛,前后端分離钠导、模塊化開發(fā)、版本控制森瘪、文件合并與壓縮牡属、mock數(shù)據(jù)等等一些原本后端的思想開始...
    Charlot閱讀 5,440評論 1 32
  • 零 都說生活真好玩,也對扼睬,因為生活總是玩我逮栅。 壹 陸曉離高三了。 穿著干凈的校服痰驱,背微微有些彎曲。鼻梁...
    賣報的寫手閱讀 1,048評論 1 5
  • 又到了一年一度的牛郎織女相會的日子,一年365天叫潦,只有今天他們才能相見蝇完,從小我就聽大人經(jīng)常說起這個凄美動人的故...
    七月小七閱讀 217評論 0 0
  • 在人間 文/一抹幽蘭 兩顆卑微的種子是可以自由戀愛的 風(fēng)是不需要掌握方向,聽誰指令的 盡管陽光推心置腹 也無法改變...
    一抹幽蘭_ff68閱讀 312評論 1 2