詳解在React項目中安裝并使用Less(用法總結(jié))
less的安裝配置
安裝react-app-rewired,react-app-rewire-less,babel-plugin-import插件
執(zhí)行命令:
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,如下圖:
根目錄下創(chuàng)建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 的功能,而是在現(xiàn)有的 CSS 語法上,添加了很多額外的功能,了解完安裝過程,接下來看看less具體怎么使用
變量
在less中利用@符號進行變量的定義,很容易理解:復(fù)制代碼
@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中去調(diào)用這些屬性. 下面有這樣一個class:
.bordered {
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
那如果我們現(xiàn)在需要在其他class中引入那些通用的屬性集,那么我們只需要在任何class中像下面這樣調(diào)用就可以:
#menu a {
color: #111;
.bordered;
}
.post a {
color: red;
.bordered;
}
.bordered class里面的屬性樣式都會在 #menu a 和 .post a 中體現(xiàn)出來:
#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 或者 元素 屬性集都可以以同樣的方式引入.
帶參數(shù)混合
在 LESS 中,你還可以像函數(shù)一樣定義一個帶參數(shù)的屬性集合:
.border-radius (@radius) {
border-radius: @radius;
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
}
然后在其他class中像這樣調(diào)用它:
#header {
.border-radius(4px);
}
.button {
.border-radius(6px);
}
我們還可以像這樣給參數(shù)設(shè)置默認值:
.border-radius (@radius: 5px) {
border-radius: @radius;
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
}
所以現(xiàn)在如果我們像這樣調(diào)用它的話:
#header {
.border-radius;
}
radius的值就會是5px. 你也可以定義不帶參數(shù)屬性集合,如果你想隱藏這個屬性集合,不讓它暴露到CSS中去,但是你還想在其他的屬性集合中引用,你會發(fā)現(xiàn)這個方法非常的好用:
.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 包含了所有傳遞進來的參數(shù). 如果你不想單獨處理每一個參數(shù)的話就可以像這樣寫:
.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;
模式匹配和導(dǎo)引表達式
有些情況下,我們想根據(jù)傳入的參數(shù)來改變混合的默認呈現(xiàn),比如下面這個例子:
.mixin (@s, @color) { ... }
.class {
.mixin(@switch, #888);
}
如果想讓 .mixin 根據(jù)不同的 @switch 值而表現(xiàn)各異,如下這般設(shè)置:
.mixin (dark, @color) {
color: darken(@color, 10%);
}
.mixin (light, @color) {
color: lighten(@color, 10%);
}
.mixin (@_, @color) {
display: block;
}
現(xiàn)在,如果運行:
@switch: light;
.class {
.mixin(@switch, #888);
}
就會得到下列CSS:
.class {
color: #a2a2a2;
display: block;
}
如上, .mixin 就會得到傳入顏色的淺色。如果 @switch 設(shè)為dark,就會得到深色. 具體實現(xiàn)如下:
第一個混合定義并未被匹配,因為它只接受 dark 做為首參 第二個混合定義被成功匹配,因為它只接受 light 第三個混合定義被成功匹配,因為它接受任意值
只有被匹配的混合才會被使用。變量可以匹配任意的傳入值,而變量以外的固定值就僅僅匹配與其相等的傳入值。 我們也可以匹配多個參數(shù):
.mixin (@a) {
color: @a;
}
.mixin (@a, @b) {
color: fade(@a, @b);
}
當我們想根據(jù)表達式進行匹配,而非根據(jù)值和參數(shù)匹配時,導(dǎo)引就顯得非常有用。如果你對函數(shù)式編程非常熟悉,那么你很可能已經(jīng)使用過導(dǎo)引。
為了盡可能地保留CSS的可聲明性,LESS通過導(dǎo)引混合而非if/else語句來實現(xiàn)條件判斷,因為前者已在@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關(guān)鍵字用以定義一個導(dǎo)引序列(此例只有一個導(dǎo)引)。接下來我們運行下列代碼:
.class1 { .mixin(#ddd) }
.class2 { .mixin(#555) }
就會得到:
.class1 {
background-color: black;
color: #ddd;
}
.class2 {
background-color: white;
color: #555;
}
導(dǎo)引中可用的全部比較運算有: > >= = =< <。此外,關(guān)鍵字 true 只表示布爾真值,下面兩個混合是相同的:
.truth (@a) when (@a) { ... }
.truth (@a) when (@a = true) { ... }
除去關(guān)鍵字true以外的值都被視示布爾假:
.class {
.truth(40); // Will not match any of the above definitions.
}
導(dǎo)引序列使用逗號‘ , '—分割,當且僅當所有條件都符合時,才會被視為匹配成功。
.mixin (@a) when (@a > 10), (@a < -10) { ... }
導(dǎo)引可以無參數(shù),也可以對參數(shù)進行比較運算:
@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
如果你想判斷一個值是純數(shù)字,還是某個單位量,可以使用下列函式:
- ispixel
- ispercentage
- isem
最后再補充一點,在導(dǎo)引序列中可以使用and關(guān)鍵字實現(xiàn)與條件:
.mixin (@a) when (isnumber(@a)) and (@a > 0) { ... }
使用not關(guān)鍵字實現(xiàn)或條件
.mixin (@b) when not (@b > 0) { ... }
嵌套規(guī)則
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 結(jié)構(gòu)格式有點像. 注意 & 符號的使用—如果你想寫串聯(lián)選擇器,而不是寫后代選擇器,就可以用到 & 了. 這點對偽類尤其有用如 :hover 和 :focus 例如:
.bordered {
&.float {
float: left;
}
.top {
margin: 5px;
}
}
會輸出
.bordered.float {
float: left;
}
.bordered .top {
margin: 5px;
}
運算
任何數(shù)字、顏色或者變量都可以參與運算. 來看一組例子:
@base: 5%; @filler: @base * 2; @other: @base + @filler; color: #888 / 4; background-color: @base-color + #111; height: 100% / 2 + @filler;
LESS 的運算已經(jīng)超出了我們的期望,它能夠分辨出顏色和單位。如果像下面這樣單位運算的話:
@var: 1px + 5;
LESS 會輸出 6px 括號也同樣允許使用:
width: (@var + 5) * 2;
并且可以在復(fù)合屬性中進行運算:
border: (@width * 2) solid black
命名空間
有時候,你可能為了更好組織CSS或者單純是為了更好的封裝,將一些變量或者混合模塊打包起來, 你可以像下面這 樣在 #bundle 中定義一些屬性集之后可以重復(fù)使用:
#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;
}
字符串插值
變量可以用類似ruby和php的方式嵌入到字符串中,像 @{name} 這樣的結(jié)構(gòu):
@base-url: "http://assets.fnord.com";
background-image: url("@{base-url}/images/bg.png");
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react學(xué)習(xí)筆記之state以及setState的使用
這篇文章主要介紹了react學(xué)習(xí)筆記之state以及setState的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
淺析history 和 react-router 的實現(xiàn)原理
react-router 版本更新非常快,但是它的底層實現(xiàn)原理確是萬變不離其中,在本文中會從前端路由出發(fā)到 react-router 原理總結(jié)與分享,本文對history 和 react-router實現(xiàn)原理講解的非常詳細,需要的朋友跟隨小編一起看看吧2023-08-08
react antd-design Select全選功能實例
這篇文章主要介紹了react antd-design Select全選功能實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
React如何實現(xiàn)像Vue一樣將css和js寫在同一文件
這篇文章主要介紹了React如何實現(xiàn)像Vue一樣將css和js寫在同一文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
npx create-react-app xxx創(chuàng)建項目報錯的解決辦法
這篇文章主要介紹了npx create-react-app xxx創(chuàng)建項目報錯的解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

