switch.scss 6.9 KB

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