在React專案中安裝並使用Less(用法總結)
-
安裝react-app-rewired,react-app-rewire-less,babel-plugin-import外掛
執行命令:
npm install react-app-rewired --save-dev npm install react-app-rewire-less --save-dev npm install babel-plugin-import --save-dev 複製程式碼
- 配置package.json檔案 找到scripts屬性,修改start的值為react-app-rewired start,如下圖:
- 根目錄下建立config-overrides.js檔案
const { injectBabelPlugin } = require('react-app-rewired'); const rewireLess = require('react-app-rewire-less'); module.exports = function override(config, env) { config = rewireLess.withLoaderOptions({ modifyVars: { "@primary-color": "#9F35FF" }, })(config, env); return config; } 複製程式碼
less的基本使用
LESS 做為 CSS 的一種形式的擴充套件,它並沒有閹割 CSS 的功能,而是在現有的 CSS 語法上,添加了很多額外的功能,瞭解完安裝過程,接下來看看less具體怎麼使用
變數
在less中利用@符號進行變數的定義,很容易理解: 複製程式碼
@nice-blue: #5B83AD; @light-blue: @nice-blue + #111; #header { color: @light-blue; } 複製程式碼
以上程式碼編譯後輸出為:
#header { color: #6c94be; } 複製程式碼
less中支援變數名定義為變數,例如:
@fnord: "I am fnord."; @var: 'fnord'; content: @@var; 複製程式碼
解析後:
content: "I am fnord."; 複製程式碼
注意: LESS 中的變數為完全的 ‘常量’ ,所以只能定義一次
混合
在 LESS 中我們可以定義一些通用的屬性集為一個class,然後在另一個class中去呼叫這些屬性. 下面有這樣一個class:
.bordered { border-top: dotted 1px black; border-bottom: solid 2px black; } 複製程式碼
那如果我們現在需要在其他class中引入那些通用的屬性集,那麼我們只需要在任何class中像下面這樣呼叫就可以:
#menu a { color: #111; .bordered; } .post a { color: red; .bordered; } 複製程式碼
.bordered class裡面的屬性樣式都會在 #menu a 和 .post a 中體現出來:
#menu a { color: #111; border-top: dotted 1px black; border-bottom: solid 2px black; } .post a { color: red; border-top: dotted 1px black; border-bottom: solid 2px black; } 複製程式碼
任何 CSS class, id 或者 元素 屬性集都可以以同樣的方式引入.
帶引數混合
在 LESS 中,你還可以像函式一樣定義一個帶引數的屬性集合:
.border-radius (@radius) { border-radius: @radius; -moz-border-radius: @radius; -webkit-border-radius: @radius; } 複製程式碼
然後在其他class中像這樣呼叫它:
#header { .border-radius(4px); } .button { .border-radius(6px); } 複製程式碼
我們還可以像這樣給引數設定預設值:
.border-radius (@radius: 5px) { border-radius: @radius; -moz-border-radius: @radius; -webkit-border-radius: @radius; } 複製程式碼
所以現在如果我們像這樣呼叫它的話:
#header { .border-radius; } 複製程式碼
radius的值就會是5px. 你也可以定義不帶引數屬性集合,如果你想隱藏這個屬性集合,不讓它暴露到CSS中去,但是你還想在其他的屬性集合中引用,你會發現這個方法非常的好用:
.wrap () { text-wrap: wrap; white-space: pre-wrap; white-space: -moz-pre-wrap; word-wrap: break-word; } pre { .wrap } 複製程式碼
輸出:
pre { text-wrap: wrap; white-space: pre-wrap; white-space: -moz-pre-wrap; word-wrap: break-word; } 複製程式碼
@arguments 變數
@arguments 包含了所有傳遞進來的引數. 如果你不想單獨處理每一個引數的話就可以像這樣寫:
.box-shadow (@x: 0, @y: 0, @blur: 1px, @color: #000) { box-shadow: @arguments; -moz-box-shadow: @arguments; -webkit-box-shadow: @arguments; } .box-shadow(2px, 5px); 複製程式碼
將會輸出:
box-shadow: 2px 5px 1px #000; -moz-box-shadow: 2px 5px 1px #000; -webkit-box-shadow: 2px 5px 1px #000; 複製程式碼
模式匹配和導引表示式
有些情況下,我們想根據傳入的引數來改變混合的預設呈現,比如下面這個例子:
.mixin (@s, @color) { ... } .class { .mixin(@switch, #888); } 複製程式碼
如果想讓 .mixin 根據不同的 @switch 值而表現各異,如下這般設定:
.mixin (dark, @color) { color: darken(@color, 10%); } .mixin (light, @color) { color: lighten(@color, 10%); } .mixin (@_, @color) { display: block; } 複製程式碼
現在,如果執行:
@switch: light; .class { .mixin(@switch, #888); } 複製程式碼
就會得到下列CSS:
.class { color: #a2a2a2; display: block; } 複製程式碼
如上, .mixin 就會得到傳入顏色的淺色。如果 @switch 設為dark,就會得到深色. 具體實現如下:
第一個混合定義並未被匹配,因為它只接受 dark 做為首參 第二個混合定義被成功匹配,因為它只接受 light 第三個混合定義被成功匹配,因為它接受任意值
只有被匹配的混合才會被使用。變數可以匹配任意的傳入值,而變數以外的固定值就僅僅匹配與其相等的傳入值。 我們也可以匹配多個引數:
.mixin (@a) { color: @a; } .mixin (@a, @b) { color: fade(@a, @b); } 複製程式碼
當我們想根據表示式進行匹配,而非根據值和引數匹配時,導引就顯得非常有用。如果你對函數語言程式設計非常熟悉,那麼你很可能已經使用過導引。
為了儘可能地保留CSS的可宣告性,LESS通過導引混合而非if/else語句來實現條件判斷,因為前者已在@media query特性中被定義。 以此例做為開始:
.mixin (@a) when (lightness(@a) >= 50%) { background-color: black; } .mixin (@a) when (lightness(@a) < 50%) { background-color: white; } .mixin (@a) { color: @a; } 複製程式碼
when關鍵字用以定義一個導引序列(此例只有一個導引)。接下來我們執行下列程式碼:
.class1 { .mixin(#ddd) } .class2 { .mixin(#555) } 複製程式碼
就會得到:
.class1 { background-color: black; color: #ddd; } .class2 { background-color: white; color: #555; } 複製程式碼
導引中可用的全部比較運算有: > >= = =< <。此外,關鍵字 true 只表示布林真值,下面兩個混合是相同的:
.truth (@a) when (@a) { ... } .truth (@a) when (@a = true) { ... } 複製程式碼
除去關鍵字true以外的值都被視示布林假:
.class { .truth(40); // Will not match any of the above definitions. } 複製程式碼
導引序列使用逗號‘ , ’—分割,當且僅當所有條件都符合時,才會被視為匹配成功。
.mixin (@a) when (@a > 10), (@a < -10) { ... } 複製程式碼
導引可以無引數,也可以對引數進行比較運算:
@media: mobile; .mixin (@a) when (@media = mobile) { ... } .mixin (@a) when (@media = desktop) { ... } .max (@a, @b) when (@a > @b) { width: @a } .max (@a, @b) when (@a < @b) { width: @b } 複製程式碼
最後,如果想基於值的型別進行匹配,我們就可以使用is*函式:
.mixin (@a, @b: 0) when (isnumber(@b)) { ... } .mixin (@a, @b: black) when (iscolor(@b)) { ... } 複製程式碼
下面就是常見的檢測函式:
- iscolor
- isnumber
- isstring
- iskeyword
- isurl
如果你想判斷一個值是純數字,還是某個單位量,可以使用下列函式:
- ispixel
- ispercentage
- isem
最後再補充一點,在導引序列中可以使用and關鍵字實現與條件:
.mixin (@a) when (isnumber(@a)) and (@a > 0) { ... } 複製程式碼
使用not關鍵字實現或條件
.mixin (@b) when not (@b > 0) { ... } 複製程式碼
巢狀規則
LESS 可以讓我們以巢狀的方式編寫層疊樣式. 讓我們先看下下面這段 CSS:
#header { color: black; } #header .navigation { font-size: 12px; } #header .logo { width: 300px; } #header .logo:hover { text-decoration: none; } 複製程式碼
在 LESS 中, 我們就可以這樣寫:
#header { color: black; .navigation { font-size: 12px; } .logo { width: 300px; &:hover { text-decoration: none } } } 複製程式碼
或者這樣寫:
#header{ color: black; .navigation { font-size: 12px } .logo{ width: 300px; &:hover{ text-decoration: none } } } 複製程式碼
程式碼更簡潔了,而且感覺跟 DOM 結構格式有點像. 注意 & 符號的使用—如果你想寫串聯選擇器,而不是寫後代選擇器,就可以用到 & 了. 這點對偽類尤其有用如 :hover 和 :focus 例如:
.bordered { &.float { float: left; } .top { margin: 5px; } } 複製程式碼
會輸出
.bordered.float { float: left; } .bordered .top { margin: 5px; } 複製程式碼
運算
任何數字、顏色或者變數都可以參與運算. 來看一組例子:
@base: 5%; @filler: @base * 2; @other: @base + @filler; color: #888 / 4; background-color: @base-color + #111; height: 100% / 2 + @filler; 複製程式碼
LESS 的運算已經超出了我們的期望,它能夠分辨出顏色和單位。如果像下面這樣單位運算的話:
@var: 1px + 5; 複製程式碼
LESS 會輸出 6px 括號也同樣允許使用:
width: (@var + 5) * 2; 複製程式碼
並且可以在複合屬性中進行運算:
border: (@width * 2) solid black 複製程式碼
名稱空間
有時候,你可能為了更好組織CSS或者單純是為了更好的封裝,將一些變數或者混合模組打包起來, 你可以像下面這 樣在 #bundle 中定義一些屬性集之後可以重複使用:
#bundle { .button () { display: block; border: 1px solid black; background-color: grey; &:hover { background-color: white } } .tab { ... } .citation { ... } } 複製程式碼
你只需要在 #header a 中像這樣引入 .button :
#header a { color: orange; #bundle > .button; } 複製程式碼