checkbox.scss 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. @use 'sass:map';
  2. @use 'common/var' as *;
  3. @use 'mixins/mixins' as *;
  4. @use 'mixins/var' as *;
  5. @use 'mixins/_button';
  6. $checkbox-height: () !default;
  7. $checkbox-height: map.merge($common-component-size, $checkbox-height);
  8. $checkbox-bordered-input-height: () !default;
  9. $checkbox-bordered-input-height: map.merge(
  10. (
  11. 'large': 14px,
  12. 'default': 12px,
  13. 'small': 12px,
  14. ),
  15. $checkbox-bordered-input-height
  16. );
  17. $checkbox-font-size: () !default;
  18. $checkbox-font-size: map.merge(
  19. (
  20. 'large': 14px,
  21. 'small': 12px,
  22. ),
  23. $checkbox-font-size
  24. );
  25. $checkbox-bordered-input-width: () !default;
  26. $checkbox-bordered-input-width: map.merge(
  27. $checkbox-bordered-input-height,
  28. $checkbox-bordered-input-width
  29. );
  30. @include b(checkbox) {
  31. @include set-component-css-var('checkbox', $checkbox);
  32. }
  33. @include b(checkbox) {
  34. color: getCssVar('checkbox-text-color');
  35. font-weight: getCssVar('checkbox-font-weight');
  36. font-size: getCssVar('font-size', 'base');
  37. position: relative;
  38. cursor: pointer;
  39. display: inline-flex;
  40. align-items: center;
  41. white-space: nowrap;
  42. user-select: none;
  43. margin-right: 30px;
  44. height: getCssVarWithDefault(
  45. 'checkbox-height',
  46. map.get($checkbox-height, 'default')
  47. );
  48. @include when(disabled) {
  49. cursor: not-allowed;
  50. }
  51. @include when(bordered) {
  52. padding: 0
  53. map.get($checkbox-bordered-padding-right, 'default')-$border-width 0
  54. map.get($checkbox-bordered-padding-left, 'default')-$border-width;
  55. border-radius: getCssVar('border-radius-base');
  56. border: getCssVar('border');
  57. box-sizing: border-box;
  58. &.is-checked {
  59. border-color: getCssVar('color-primary');
  60. }
  61. &.is-disabled {
  62. border-color: getCssVar('border-color-lighter');
  63. }
  64. @each $size in (large, small) {
  65. &.#{$namespace}-checkbox--#{$size} {
  66. padding: 0
  67. map.get($checkbox-bordered-padding-right, $size)-$border-width
  68. 0
  69. map.get($checkbox-bordered-padding-left, $size)-$border-width;
  70. border-radius: map.get($button-border-radius, $size);
  71. .#{$namespace}-checkbox__label {
  72. font-size: map.get($button-font-size, $size);
  73. }
  74. .#{$namespace}-checkbox__inner {
  75. height: map.get($checkbox-bordered-input-height, $size);
  76. width: map.get($checkbox-bordered-input-width, $size);
  77. }
  78. }
  79. }
  80. &.#{$namespace}-checkbox--small {
  81. .#{$namespace}-checkbox__inner {
  82. &::after {
  83. height: 6px;
  84. width: 2px;
  85. }
  86. }
  87. }
  88. }
  89. input:focus-visible {
  90. & + .#{$namespace}-checkbox__inner {
  91. outline: 2px solid getCssVar('checkbox-input-border-color-hover');
  92. outline-offset: 1px;
  93. border-radius: getCssVar('checkbox-border-radius');
  94. }
  95. }
  96. @include e(input) {
  97. white-space: nowrap;
  98. cursor: pointer;
  99. outline: none;
  100. display: inline-flex;
  101. position: relative;
  102. @include when(disabled) {
  103. .#{$namespace}-checkbox__inner {
  104. background-color: getCssVar('checkbox-disabled-input-fill');
  105. border-color: getCssVar('checkbox-disabled-border-color');
  106. cursor: not-allowed;
  107. &::after {
  108. cursor: not-allowed;
  109. border-color: getCssVar('checkbox-disabled-icon-color');
  110. will-change: transform;
  111. }
  112. }
  113. &.is-checked {
  114. .#{$namespace}-checkbox__inner {
  115. background-color: getCssVar('checkbox-disabled-checked-input-fill');
  116. border-color: getCssVar(
  117. 'checkbox-disabled-checked-input-border-color'
  118. );
  119. &::after {
  120. border-color: getCssVar('checkbox-disabled-checked-icon-color');
  121. }
  122. }
  123. }
  124. &.is-indeterminate {
  125. .#{$namespace}-checkbox__inner {
  126. background-color: getCssVar('checkbox-disabled-checked-input-fill');
  127. border-color: getCssVar(
  128. 'checkbox-disabled-checked-input-border-color'
  129. );
  130. &::before {
  131. background-color: getCssVar('checkbox-disabled-checked-icon-color');
  132. border-color: getCssVar('checkbox-disabled-checked-icon-color');
  133. }
  134. }
  135. }
  136. & + span.#{$namespace}-checkbox__label {
  137. color: getCssVar('disabled-text-color');
  138. cursor: not-allowed;
  139. }
  140. }
  141. @include when(checked) {
  142. .#{$namespace}-checkbox__inner {
  143. background-color: getCssVar('checkbox-checked-bg-color');
  144. border-color: getCssVar('checkbox-checked-input-border-color');
  145. &::after {
  146. transform: translate(-45%, -60%) rotate(45deg) scaleY(1);
  147. border-color: getCssVar('checkbox-checked-icon-color');
  148. }
  149. }
  150. & + .#{$namespace}-checkbox__label {
  151. color: getCssVar('checkbox-checked-text-color');
  152. }
  153. }
  154. @include when(focus) {
  155. // Visually distinguish when focus
  156. &:not(.is-checked) {
  157. .#{$namespace}-checkbox__original:not(:focus-visible) {
  158. border-color: getCssVar('checkbox-input-border-color-hover');
  159. }
  160. }
  161. }
  162. @include when(indeterminate) {
  163. .#{$namespace}-checkbox__inner {
  164. background-color: getCssVar('checkbox-checked-bg-color');
  165. border-color: getCssVar('checkbox-checked-input-border-color');
  166. &::before {
  167. content: '';
  168. position: absolute;
  169. display: block;
  170. background-color: getCssVar('checkbox-checked-icon-color');
  171. height: 2px;
  172. transform: scale(0.5);
  173. left: 0;
  174. right: 0;
  175. top: 5px;
  176. }
  177. &::after {
  178. display: none;
  179. }
  180. }
  181. }
  182. }
  183. @include e(inner) {
  184. display: inline-block;
  185. position: relative;
  186. border: getCssVar('checkbox-input-border');
  187. border-radius: getCssVar('checkbox-border-radius');
  188. box-sizing: border-box;
  189. width: getCssVar('checkbox-input-width');
  190. height: getCssVar('checkbox-input-height');
  191. background-color: getCssVar('checkbox-bg-color');
  192. z-index: getCssVar('index-normal');
  193. transition:
  194. border-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46),
  195. background-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46),
  196. outline 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46);
  197. &:hover {
  198. border-color: getCssVar('checkbox-input-border-color-hover');
  199. }
  200. &::after {
  201. box-sizing: content-box;
  202. content: '';
  203. border: 1px solid transparent;
  204. border-left: 0;
  205. border-top: 0;
  206. height: 7px;
  207. left: 50%;
  208. position: absolute;
  209. top: 50%;
  210. transform: translate(-45%, -60%) rotate(45deg) scaleY(0);
  211. width: 3px;
  212. transition: transform 0.15s ease-in 0.05s;
  213. transform-origin: center;
  214. }
  215. }
  216. @include e(original) {
  217. opacity: 0;
  218. outline: none;
  219. position: absolute;
  220. margin: 0;
  221. width: 0;
  222. height: 0;
  223. z-index: -1;
  224. }
  225. @include e(label) {
  226. display: inline-block;
  227. padding-left: 8px;
  228. line-height: 1;
  229. font-size: getCssVar('checkbox-font-size');
  230. }
  231. @each $size in (large, small) {
  232. &.#{$namespace}-checkbox--#{$size} {
  233. height: map.get($checkbox-height, $size);
  234. @include e(label) {
  235. font-size: map.get($checkbox-font-size, $size);
  236. }
  237. @include e(inner) {
  238. width: map.get($checkbox-font-size, $size);
  239. height: map.get($checkbox-font-size, $size);
  240. }
  241. }
  242. }
  243. &.#{$namespace}-checkbox--small {
  244. @include e(input) {
  245. @include when(indeterminate) {
  246. @include e(inner) {
  247. &::before {
  248. top: 4px;
  249. }
  250. }
  251. }
  252. }
  253. @include e(inner) {
  254. &::after {
  255. width: 2px;
  256. height: 6px;
  257. }
  258. }
  259. }
  260. &:last-of-type {
  261. margin-right: 0;
  262. }
  263. }