mixin.less 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // mixins for button
  2. // ------------------------
  3. .button-size(@height; @padding-horizontal; @font-size; @border-radius) {
  4. @padding-vertical: max(
  5. (round(((@height - @font-size * @line-height-base) / 2) * 10) / 10) - @border-width-base,
  6. 0
  7. );
  8. height: @height;
  9. padding: @padding-vertical @padding-horizontal;
  10. font-size: @font-size;
  11. border-radius: @border-radius;
  12. }
  13. .button-color(@color; @background; @border) {
  14. color: @color;
  15. border-color: @border; // a inside Button which only work in Chrome
  16. & when not(@background = null) {
  17. background: @background;
  18. }
  19. // http://stackoverflow.com/a/17253457
  20. > a:only-child {
  21. color: currentcolor;
  22. &::after {
  23. position: absolute;
  24. top: 0;
  25. right: 0;
  26. bottom: 0;
  27. left: 0;
  28. background: transparent;
  29. content: '';
  30. }
  31. }
  32. }
  33. .button-disabled(@color: @btn-disable-color; @background: @btn-disable-bg; @border: @btn-disable-border) {
  34. &[disabled] {
  35. &,
  36. &:hover,
  37. &:focus,
  38. &:active {
  39. .button-color(@color; @background; @border);
  40. text-shadow: none;
  41. box-shadow: none;
  42. }
  43. }
  44. }
  45. .button-variant-primary(@color; @background; @backgroundHover: yellow; @backgroundActive: yellow) {
  46. .button-color(@color; @background; @background);
  47. text-shadow: @btn-text-shadow;
  48. box-shadow: @btn-primary-shadow;
  49. &:hover,
  50. &:focus {
  51. & when (@theme = dark) {
  52. .button-color(
  53. @color; colorPalette('@{background}', 7); colorPalette('@{background}', 7)
  54. );
  55. }
  56. & when (not (@theme = dark) and not (@theme = variable)) {
  57. .button-color(
  58. @color; colorPalette('@{background}', 5); colorPalette('@{background}', 5)
  59. );
  60. }
  61. & when (@theme = variable) {
  62. .button-color(@color; @backgroundHover; @backgroundHover);
  63. }
  64. }
  65. &:active {
  66. & when (@theme = dark) {
  67. .button-color(
  68. @color; colorPalette('@{background}', 5); colorPalette('@{background}', 5)
  69. );
  70. }
  71. & when (not (@theme = dark) and not (@theme = variable)) {
  72. .button-color(
  73. @color; colorPalette('@{background}', 7); colorPalette('@{background}', 7)
  74. );
  75. }
  76. & when (@theme = variable) {
  77. .button-color(@color; @backgroundActive; @backgroundActive);
  78. }
  79. }
  80. .button-disabled();
  81. }
  82. .button-variant-other(@color; @background; @border) {
  83. .button-color(@color; @background; @border);
  84. &:hover,
  85. &:focus {
  86. & when (@theme = dark) {
  87. .button-color(@primary-5; @background; @primary-5);
  88. }
  89. & when (not (@theme = dark) and not (@theme = variable)) {
  90. .button-color(
  91. colorPalette('@{btn-primary-bg}', 5); @background;
  92. colorPalette('@{btn-primary-bg}', 5)
  93. );
  94. }
  95. & when (@theme = variable) {
  96. .button-color(@primary-color-hover; @background; @primary-color-hover);
  97. }
  98. }
  99. &:active {
  100. & when (@theme = dark) {
  101. .button-color(@primary-7; @background; @primary-7);
  102. }
  103. & when (not (@theme = dark) and not (@theme = variable)) {
  104. .button-color(
  105. colorPalette('@{btn-primary-bg}', 7); @background;
  106. colorPalette('@{btn-primary-bg}', 7)
  107. );
  108. }
  109. & when (@theme = variable) {
  110. .button-color(@primary-color-active; @background; @primary-color-active);
  111. }
  112. }
  113. .button-disabled();
  114. }
  115. .button-variant-ghost(@color; @border; @borderHover: yellow; @borderActive: yellow) {
  116. .button-color(@color; null; @border);
  117. text-shadow: none;
  118. &:hover,
  119. &:focus {
  120. & when (@border = transparent) {
  121. & when (@theme = dark) {
  122. .button-color(colorPalette('@{color}', 7); null; transparent);
  123. }
  124. & when (not (@theme = dark) and not (@theme = variable)) {
  125. .button-color(colorPalette('@{color}', 5); null; transparent);
  126. }
  127. & when (@theme = variable) {
  128. .button-color(@borderActive; transparent; transparent);
  129. }
  130. }
  131. & when not (@border = transparent) {
  132. & when (@theme = dark) {
  133. .button-color(
  134. colorPalette('@{color}', 7); null; colorPalette('@{color}', 7)
  135. );
  136. }
  137. & when (not (@theme = dark) and not (@theme = variable)) {
  138. .button-color(
  139. colorPalette('@{color}', 5); null; colorPalette('@{color}', 5)
  140. );
  141. }
  142. & when (@theme = variable) {
  143. .button-color(@borderHover; transparent; @borderHover);
  144. }
  145. }
  146. }
  147. &:active {
  148. & when (@border = transparent) {
  149. & when (@theme = dark) {
  150. .button-color(colorPalette('@{color}', 5); null; transparent);
  151. }
  152. & when (not (@theme = dark) and not (@theme = variable)) {
  153. .button-color(colorPalette('@{color}', 7); null; transparent);
  154. }
  155. & when (@theme = variable) {
  156. .button-color(@borderActive; transparent; transparent);
  157. }
  158. }
  159. & when not (@border = transparent) {
  160. & when (@theme = dark) {
  161. .button-color(
  162. colorPalette('@{color}', 5); null; colorPalette('@{color}', 5)
  163. );
  164. }
  165. & when (not (@theme = dark) and not (@theme = variable)) {
  166. .button-color(
  167. colorPalette('@{color}', 7); null; colorPalette('@{color}', 7)
  168. );
  169. }
  170. & when (@theme = variable) {
  171. .button-color(@borderActive; transparent; @borderActive);
  172. }
  173. }
  174. }
  175. .button-disabled();
  176. }
  177. .button-group-base(@btnClassName) {
  178. position: relative;
  179. display: inline-flex;
  180. > .@{btnClassName},
  181. > span > .@{btnClassName} {
  182. position: relative;
  183. &:hover,
  184. &:focus,
  185. &:active {
  186. z-index: 2;
  187. }
  188. &[disabled] {
  189. z-index: 0;
  190. }
  191. }
  192. .@{btnClassName}-icon-only {
  193. font-size: @font-size-base;
  194. }
  195. }
  196. // Base styles of buttons
  197. // --------------------------------------------------
  198. .btn() {
  199. position: relative;
  200. display: inline-block;
  201. font-weight: @btn-font-weight;
  202. white-space: nowrap;
  203. text-align: center;
  204. background-image: none;
  205. border: @btn-border-width @btn-border-style transparent;
  206. box-shadow: @btn-shadow;
  207. cursor: pointer;
  208. transition: all 0.3s @ease-in-out;
  209. user-select: none;
  210. touch-action: manipulation;
  211. .button-size(
  212. @btn-height-base; @btn-padding-horizontal-base; @font-size-base; @btn-border-radius-base
  213. );
  214. > .@{iconfont-css-prefix} {
  215. line-height: 1;
  216. }
  217. &,
  218. &:active,
  219. &:focus {
  220. outline: 0;
  221. }
  222. &:not([disabled]):hover {
  223. text-decoration: none;
  224. }
  225. &:not([disabled]):active {
  226. outline: 0;
  227. box-shadow: none;
  228. }
  229. &[disabled] {
  230. cursor: not-allowed;
  231. > * {
  232. pointer-events: none;
  233. }
  234. }
  235. &-lg {
  236. .button-size(
  237. @btn-height-lg; @btn-padding-horizontal-lg; @btn-font-size-lg; @btn-border-radius-base
  238. );
  239. }
  240. &-sm {
  241. .button-size(
  242. @btn-height-sm; @btn-padding-horizontal-sm; @btn-font-size-sm; @btn-border-radius-sm
  243. );
  244. }
  245. }
  246. // primary button style
  247. .btn-primary() {
  248. .button-variant-primary(@btn-primary-color; @btn-primary-bg; @primary-color-hover; @primary-color-active);
  249. }
  250. // default button style
  251. .btn-default() {
  252. .button-variant-other(@btn-default-color; @btn-default-bg; @btn-default-border; );
  253. &:hover,
  254. &:focus,
  255. &:active {
  256. text-decoration: none;
  257. background: @btn-default-bg;
  258. }
  259. }
  260. // ghost button style
  261. .btn-ghost() {
  262. .button-variant-other(@btn-ghost-color, @btn-ghost-bg, @btn-ghost-border);
  263. }
  264. // dashed button style
  265. .btn-dashed() {
  266. .button-variant-other(@btn-default-color, @btn-default-bg, @btn-default-border);
  267. border-style: dashed;
  268. }
  269. // danger button style
  270. .btn-danger() {
  271. .button-variant-primary(@btn-danger-color, @btn-danger-bg, @error-color-hover, @error-color-active);
  272. }
  273. // danger default button style
  274. .btn-danger-default() {
  275. .button-color(@error-color, @btn-default-bg, @error-color);
  276. &:hover,
  277. &:focus {
  278. & when (@theme = dark) {
  279. .button-color(
  280. colorPalette('@{error-color}', 7); @btn-default-bg; colorPalette('@{error-color}', 7)
  281. );
  282. }
  283. & when (not (@theme = dark) and not (@theme = variable)) {
  284. .button-color(
  285. colorPalette('@{error-color}', 5); @btn-default-bg; colorPalette('@{error-color}', 5)
  286. );
  287. }
  288. & when (@theme = variable) {
  289. .button-color(@error-color-hover, @btn-default-bg, @error-color-hover);
  290. }
  291. }
  292. &:active {
  293. & when (@theme = dark) {
  294. .button-color(
  295. colorPalette('@{error-color}', 5); @btn-default-bg; colorPalette('@{error-color}', 5)
  296. );
  297. }
  298. & when (not (@theme = dark) and not (@theme = variable)) {
  299. .button-color(
  300. colorPalette('@{error-color}', 7); @btn-default-bg; colorPalette('@{error-color}', 7)
  301. );
  302. }
  303. & when (@theme = variable) {
  304. .button-color(@error-color-active, @btn-default-bg, @error-color-active);
  305. }
  306. }
  307. .button-disabled();
  308. }
  309. // danger link button style
  310. .btn-danger-link() {
  311. .button-variant-other(@error-color, transparent, transparent);
  312. box-shadow: none;
  313. &:hover,
  314. &:focus {
  315. & when (@theme = dark) {
  316. .button-color(colorPalette('@{error-color}', 7); transparent; transparent);
  317. }
  318. & when (not (@theme = dark) and not (@theme = variable)) {
  319. .button-color(colorPalette('@{error-color}', 5); transparent; transparent);
  320. }
  321. & when (@theme = variable) {
  322. .button-color(@error-color-hover; transparent; transparent);
  323. }
  324. }
  325. &:active {
  326. & when (@theme = dark) {
  327. .button-color(colorPalette('@{error-color}', 5); transparent; transparent);
  328. }
  329. & when (not (@theme = dark) and not (@theme = variable)) {
  330. .button-color(colorPalette('@{error-color}', 7); transparent; transparent);
  331. }
  332. & when (@theme = variable) {
  333. .button-color(@error-color-active; transparent; transparent);
  334. }
  335. }
  336. .button-disabled(@disabled-color; transparent; transparent);
  337. }
  338. // link button style
  339. .btn-link() {
  340. .button-variant-other(@link-color, transparent, transparent);
  341. box-shadow: none;
  342. &:hover {
  343. background: @btn-link-hover-bg;
  344. }
  345. &:hover,
  346. &:focus,
  347. &:active {
  348. border-color: transparent;
  349. }
  350. .button-disabled(@disabled-color; transparent; transparent);
  351. }
  352. // link button disabled style
  353. .btn-href-disabled() {
  354. cursor: not-allowed;
  355. > * {
  356. pointer-events: none;
  357. }
  358. &,
  359. &:hover,
  360. &:focus,
  361. &:active {
  362. .button-color(@btn-disable-color,transparent, transparent);
  363. text-shadow: none;
  364. box-shadow: none;
  365. }
  366. }
  367. // text button style
  368. .btn-text() {
  369. .button-variant-other(@text-color, transparent, transparent);
  370. box-shadow: none;
  371. &:hover,
  372. &:focus {
  373. color: @text-color;
  374. background: @btn-text-hover-bg;
  375. border-color: transparent;
  376. }
  377. &:active {
  378. color: @text-color;
  379. background: fadein(@btn-text-hover-bg, 1%);
  380. border-color: transparent;
  381. }
  382. .button-disabled(@disabled-color; transparent; transparent);
  383. }
  384. .btn-danger-text() {
  385. .button-variant-other(@error-color, transparent, transparent);
  386. box-shadow: none;
  387. &:hover,
  388. &:focus {
  389. & when (@theme = dark) {
  390. .button-color(colorPalette('@{error-color}', 7); @btn-text-hover-bg; transparent);
  391. }
  392. & when (not (@theme = dark) and not (@theme = variable)) {
  393. .button-color(colorPalette('@{error-color}', 5); @btn-text-hover-bg; transparent);
  394. }
  395. & when (@theme = variable) {
  396. .button-color(@error-color-hover; @btn-text-hover-bg; transparent);
  397. }
  398. }
  399. &:active {
  400. & when (@theme = dark) {
  401. .button-color(colorPalette('@{error-color}', 5); fadein(@btn-text-hover-bg, 1%); transparent);
  402. }
  403. & when (not (@theme = dark) and not (@theme = variable)) {
  404. .button-color(colorPalette('@{error-color}', 7); fadein(@btn-text-hover-bg, 1%); transparent);
  405. }
  406. & when (@theme = variable) {
  407. .button-color(@error-color-active; fadein(@btn-text-hover-bg, 1%); transparent);
  408. }
  409. }
  410. .button-disabled(@disabled-color; transparent; transparent);
  411. }
  412. // round button
  413. .btn-round(@btnClassName: btn) {
  414. .button-size(@btn-circle-size; (@btn-circle-size / 2); @font-size-base; @btn-circle-size);
  415. &.@{btnClassName}-lg {
  416. .button-size(
  417. @btn-circle-size-lg; (@btn-circle-size-lg / 2); @btn-font-size-lg; @btn-circle-size-lg
  418. );
  419. }
  420. &.@{btnClassName}-sm {
  421. .button-size(
  422. @btn-circle-size-sm; (@btn-circle-size-sm / 2); @font-size-base; @btn-circle-size-sm
  423. );
  424. }
  425. }
  426. // square button: the content only contains icon
  427. .btn-square(@btnClassName: btn) {
  428. .square(@btn-square-size);
  429. .button-size(@btn-square-size; 0; @btn-square-only-icon-size; @btn-border-radius-base);
  430. & > * {
  431. font-size: @btn-square-only-icon-size;
  432. }
  433. &.@{btnClassName}-lg {
  434. .square(@btn-square-size-lg);
  435. .button-size(@btn-square-size-lg; 0; @btn-square-only-icon-size-lg; @btn-border-radius-base);
  436. & > * {
  437. font-size: @btn-square-only-icon-size-lg;
  438. }
  439. }
  440. &.@{btnClassName}-sm {
  441. .square(@btn-square-size-sm);
  442. .button-size(@btn-square-size-sm; 0; @btn-square-only-icon-size-sm; @btn-border-radius-base);
  443. & > * {
  444. font-size: @btn-square-only-icon-size-sm;
  445. }
  446. }
  447. }
  448. // circle button: the content only contains icon
  449. .btn-circle(@btnClassName: btn) {
  450. min-width: @btn-height-base;
  451. padding-right: 0;
  452. padding-left: 0;
  453. text-align: center;
  454. border-radius: 50%;
  455. &.@{btnClassName}-lg {
  456. min-width: @btn-height-lg;
  457. border-radius: 50%;
  458. }
  459. &.@{btnClassName}-sm {
  460. min-width: @btn-height-sm;
  461. border-radius: 50%;
  462. }
  463. }
  464. // Horizontal button groups style
  465. // --------------------------------------------------
  466. .btn-group(@btnClassName: btn) {
  467. .button-group-base(@btnClassName);
  468. .@{btnClassName} + .@{btnClassName},
  469. .@{btnClassName} + &,
  470. span + .@{btnClassName},
  471. .@{btnClassName} + span,
  472. > span + span,
  473. & + .@{btnClassName},
  474. & + & {
  475. margin-left: -1px;
  476. }
  477. .@{btnClassName}-primary + .@{btnClassName}:not(.@{btnClassName}-primary):not([disabled]) {
  478. border-left-color: transparent;
  479. }
  480. .@{btnClassName} {
  481. border-radius: 0;
  482. }
  483. > .@{btnClassName}:first-child,
  484. > span:first-child > .@{btnClassName} {
  485. margin-left: 0;
  486. }
  487. > .@{btnClassName}:only-child {
  488. border-radius: @btn-border-radius-base;
  489. }
  490. > span:only-child > .@{btnClassName} {
  491. border-radius: @btn-border-radius-base;
  492. }
  493. > .@{btnClassName}:first-child:not(:last-child),
  494. > span:first-child:not(:last-child) > .@{btnClassName} {
  495. border-top-left-radius: @btn-border-radius-base;
  496. border-bottom-left-radius: @btn-border-radius-base;
  497. }
  498. > .@{btnClassName}:last-child:not(:first-child),
  499. > span:last-child:not(:first-child) > .@{btnClassName} {
  500. border-top-right-radius: @btn-border-radius-base;
  501. border-bottom-right-radius: @btn-border-radius-base;
  502. }
  503. &-sm {
  504. > .@{btnClassName}:only-child {
  505. border-radius: @btn-border-radius-sm;
  506. }
  507. > span:only-child > .@{btnClassName} {
  508. border-radius: @btn-border-radius-sm;
  509. }
  510. > .@{btnClassName}:first-child:not(:last-child),
  511. > span:first-child:not(:last-child) > .@{btnClassName} {
  512. border-top-left-radius: @btn-border-radius-sm;
  513. border-bottom-left-radius: @btn-border-radius-sm;
  514. }
  515. > .@{btnClassName}:last-child:not(:first-child),
  516. > span:last-child:not(:first-child) > .@{btnClassName} {
  517. border-top-right-radius: @btn-border-radius-sm;
  518. border-bottom-right-radius: @btn-border-radius-sm;
  519. }
  520. }
  521. & > & {
  522. float: left;
  523. }
  524. & > &:not(:first-child):not(:last-child) > .@{btnClassName} {
  525. border-radius: 0;
  526. }
  527. & > &:first-child:not(:last-child) {
  528. > .@{btnClassName}:last-child {
  529. padding-right: 8px;
  530. border-top-right-radius: 0;
  531. border-bottom-right-radius: 0;
  532. }
  533. }
  534. & > &:last-child:not(:first-child) > .@{btnClassName}:first-child {
  535. padding-left: 8px;
  536. border-top-left-radius: 0;
  537. border-bottom-left-radius: 0;
  538. }
  539. }