_text-field-theme.scss 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. //
  2. // Copyright 2022 Google Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. // Selector '.mdc-*' should only be used in this project.
  23. // stylelint-disable selector-class-pattern --
  24. // NOTE: this is the implementation of the aforementioned classes.
  25. @use 'sass:map';
  26. @use '@material/tokens/resolvers';
  27. @use '@material/floating-label/mixins' as floating-label-mixins;
  28. @use '@material/theme/theme';
  29. @use '@material/typography/typography';
  30. $_light-theme: (
  31. active-indicator-color: null,
  32. active-indicator-height: null,
  33. caret-color: null,
  34. container-color: null,
  35. container-height: null,
  36. container-shape: null,
  37. disabled-active-indicator-color: null,
  38. disabled-active-indicator-height: null,
  39. disabled-active-indicator-opacity: null,
  40. disabled-container-color: null,
  41. disabled-container-opacity: null,
  42. disabled-input-text-color: null,
  43. disabled-input-text-opacity: null,
  44. disabled-label-text-color: null,
  45. disabled-label-text-opacity: null,
  46. disabled-leading-icon-color: null,
  47. disabled-leading-icon-opacity: null,
  48. disabled-outline-color: null,
  49. disabled-outline-opacity: null,
  50. disabled-outline-width: null,
  51. disabled-supporting-text-color: null,
  52. disabled-supporting-text-opacity: null,
  53. disabled-trailing-icon-color: null,
  54. disabled-trailing-icon-opacity: null,
  55. error-active-indicator-color: null,
  56. // Token key `error-caret-color` not available on DSDB.
  57. error-caret-color: null,
  58. error-focus-active-indicator-color: null,
  59. error-focus-caret-color: null,
  60. error-focus-input-text-color: null,
  61. error-focus-label-text-color: null,
  62. error-focus-leading-icon-color: null,
  63. error-focus-outline-color: null,
  64. error-focus-supporting-text-color: null,
  65. error-focus-trailing-icon-color: null,
  66. error-hover-active-indicator-color: null,
  67. // Token key `error-hover-caret-color` not available on DSDB.
  68. error-hover-caret-color: null,
  69. error-hover-input-text-color: null,
  70. error-hover-label-text-color: null,
  71. error-hover-leading-icon-color: null,
  72. error-hover-outline-color: null,
  73. error-hover-state-layer-color: null,
  74. error-hover-state-layer-opacity: null,
  75. error-hover-supporting-text-color: null,
  76. error-hover-trailing-icon-color: null,
  77. error-input-text-color: null,
  78. error-label-text-color: null,
  79. error-leading-icon-color: null,
  80. error-outline-color: null,
  81. error-supporting-text-color: null,
  82. error-trailing-icon-color: null,
  83. focus-active-indicator-color: null,
  84. focus-active-indicator-height: null,
  85. // Token key `focus-caret-color` not available on DSDB.
  86. focus-caret-color: null,
  87. focus-input-text-color: null,
  88. focus-label-text-color: null,
  89. focus-leading-icon-color: null,
  90. focus-outline-color: null,
  91. focus-outline-width: null,
  92. focus-supporting-text-color: null,
  93. focus-trailing-icon-color: null,
  94. hover-active-indicator-color: null,
  95. hover-active-indicator-height: null,
  96. // Token key `hover-caret-color` not available on DSDB.
  97. hover-caret-color: null,
  98. hover-input-text-color: null,
  99. hover-label-text-color: null,
  100. hover-leading-icon-color: null,
  101. hover-outline-color: null,
  102. hover-outline-width: null,
  103. hover-state-layer-color: null,
  104. hover-state-layer-opacity: null,
  105. hover-supporting-text-color: null,
  106. hover-trailing-icon-color: null,
  107. input-text-color: null,
  108. input-text-font: null,
  109. input-text-line-height: null,
  110. input-text-placeholder-color: null,
  111. input-text-prefix-color: null,
  112. input-text-size: null,
  113. input-text-suffix-color: null,
  114. input-text-tracking: null,
  115. input-text-type: null,
  116. input-text-weight: null,
  117. label-text-color: null,
  118. label-text-font: null,
  119. label-text-line-height: null,
  120. label-text-populated-line-height: null,
  121. label-text-populated-size: null,
  122. label-text-size: null,
  123. label-text-tracking: null,
  124. label-text-type: null,
  125. label-text-weight: null,
  126. leading-icon-color: null,
  127. leading-icon-size: null,
  128. outline-color: null,
  129. outline-width: null,
  130. supporting-text-color: null,
  131. supporting-text-font: null,
  132. supporting-text-line-height: null,
  133. supporting-text-size: null,
  134. supporting-text-tracking: null,
  135. supporting-text-type: null,
  136. supporting-text-weight: null,
  137. trailing-icon-color: null,
  138. trailing-icon-size: null,
  139. );
  140. @mixin theme-styles($theme, $resolvers: resolvers.$material) {
  141. @include theme.validate-theme-styles($_light-theme, $theme);
  142. // TODO(b/261633818): Remove support for extraneous `*-caret-color` tokens.
  143. @include _caret-color(
  144. (
  145. default: map.get($theme, caret-color),
  146. hover: map.get($theme, hover-caret-color),
  147. focus: map.get($theme, focus-caret-color),
  148. )
  149. );
  150. @include _error-caret-color(
  151. (
  152. default: map.get($theme, error-caret-color),
  153. hover: map.get($theme, error-hover-caret-color),
  154. focus: map.get($theme, error-focus-caret-color),
  155. )
  156. );
  157. @include _input-text-color(
  158. (
  159. default: map.get($theme, input-text-color),
  160. hover: map.get($theme, hover-input-text-color),
  161. focus: map.get($theme, focus-input-text-color),
  162. disabled: map.get($theme, disabled-input-text-color),
  163. )
  164. );
  165. @include _error-input-text-color(
  166. (
  167. default: map.get($theme, error-input-text-color),
  168. hover: map.get($theme, error-hover-input-text-color),
  169. focus: map.get($theme, error-focus-input-text-color),
  170. )
  171. );
  172. @include _label-text-color(
  173. (
  174. default: map.get($theme, label-text-color),
  175. hover: map.get($theme, hover-label-text-color),
  176. focus: map.get($theme, focus-label-text-color),
  177. disabled: map.get($theme, disabled-label-text-color),
  178. )
  179. );
  180. @include _error-label-text-color(
  181. (
  182. default: map.get($theme, error-label-text-color),
  183. hover: map.get($theme, error-hover-label-text-color),
  184. focus: map.get($theme, error-focus-label-text-color),
  185. )
  186. );
  187. @include _leading-icon-color(
  188. (
  189. default: map.get($theme, leading-icon-color),
  190. hover: map.get($theme, hover-leading-icon-color),
  191. focus: map.get($theme, focus-leading-icon-color),
  192. disabled: map.get($theme, disabled-leading-icon-color),
  193. )
  194. );
  195. @include _error-leading-icon-color(
  196. (
  197. default: map.get($theme, error-leading-icon-color),
  198. hover: map.get($theme, error-hover-leading-icon-color),
  199. focus: map.get($theme, error-focus-leading-icon-color),
  200. )
  201. );
  202. @include _trailing-icon-color(
  203. (
  204. default: map.get($theme, trailing-icon-color),
  205. hover: map.get($theme, hover-trailing-icon-color),
  206. focus: map.get($theme, focus-trailing-icon-color),
  207. disabled: map.get($theme, disabled-trailing-icon-color),
  208. )
  209. );
  210. @include _error-trailing-icon-color(
  211. (
  212. default: map.get($theme, error-trailing-icon-color),
  213. hover: map.get($theme, error-hover-trailing-icon-color),
  214. focus: map.get($theme, error-focus-trailing-icon-color),
  215. )
  216. );
  217. @include _supporting-text-color(
  218. (
  219. default: map.get($theme, supporting-text-color),
  220. hover: map.get($theme, hover-supporting-text-color),
  221. focus: map.get($theme, focus-supporting-text-color),
  222. disabled: map.get($theme, disabled-supporting-text-color),
  223. )
  224. );
  225. @include _error-supporting-text-color(
  226. (
  227. default: map.get($theme, error-supporting-text-color),
  228. hover: map.get($theme, error-hover-supporting-text-color),
  229. focus: map.get($theme, error-focus-supporting-text-color),
  230. )
  231. );
  232. @include _input-text-typography(
  233. (
  234. font: map.get($theme, input-text-font),
  235. line-height: map.get($theme, input-text-line-height),
  236. size: map.get($theme, input-text-size),
  237. tracking: map.get($theme, input-text-tracking),
  238. weight: map.get($theme, input-text-weight),
  239. )
  240. );
  241. @include _label-text-typography(
  242. (
  243. font: map.get($theme, label-text-font),
  244. line-height: map.get($theme, label-text-line-height),
  245. size: map.get($theme, label-text-size),
  246. tracking: map.get($theme, label-text-tracking),
  247. weight: map.get($theme, label-text-weight),
  248. )
  249. );
  250. @include _label-text-populated-typography(
  251. (
  252. line-height: map.get($theme, label-text-populated-line-height),
  253. size: map.get($theme, label-text-populated-size),
  254. )
  255. );
  256. @include _supporting-text-typography(
  257. (
  258. font: map.get($theme, supporting-text-font),
  259. line-height: map.get($theme, supporting-text-line-height),
  260. size: map.get($theme, supporting-text-size),
  261. tracking: map.get($theme, supporting-text-tracking),
  262. weight: map.get($theme, supporting-text-weight),
  263. )
  264. );
  265. }
  266. @mixin _caret-color($colors) {
  267. @include if-enabled {
  268. @include _set-caret-color(map.get($colors, default));
  269. @include if-hovered {
  270. @include _set-caret-color(map.get($colors, hover));
  271. }
  272. @include if-focused {
  273. @include _set-caret-color(map.get($colors, focus));
  274. }
  275. }
  276. }
  277. @mixin _set-caret-color($color) {
  278. .mdc-text-field__input {
  279. @include theme.property(caret-color, $color);
  280. }
  281. }
  282. @mixin _error-caret-color($colors) {
  283. &.mdc-text-field--invalid {
  284. @include _caret-color($colors);
  285. }
  286. }
  287. @mixin _input-text-color($colors) {
  288. @include if-enabled {
  289. @include _set-input-text-color(map.get($colors, default));
  290. @include if-hovered {
  291. @include _set-input-text-color(map.get($colors, hover));
  292. }
  293. @include if-focused {
  294. @include _set-input-text-color(map.get($colors, focus));
  295. }
  296. }
  297. @include if-disabled {
  298. @include _set-input-text-color(map.get($colors, disabled));
  299. }
  300. }
  301. @mixin _set-input-text-color($color) {
  302. @if $color {
  303. .mdc-text-field__input {
  304. @include theme.property(color, $color);
  305. }
  306. }
  307. }
  308. @mixin _error-input-text-color($color) {
  309. &.mdc-text-field--invalid {
  310. @include _input-text-color($color);
  311. }
  312. }
  313. @mixin _label-text-color($colors) {
  314. @include if-enabled {
  315. @include _set-label-text-color(map.get($colors, default));
  316. @include if-focused {
  317. @include _set-label-text-color(map.get($colors, focus));
  318. }
  319. @include if-hovered {
  320. @include _set-label-text-color(map.get($colors, hover));
  321. }
  322. }
  323. @include if-disabled {
  324. @include _set-label-text-color(map.get($colors, disabled));
  325. }
  326. }
  327. @mixin _set-label-text-color($color) {
  328. .mdc-floating-label,
  329. .mdc-floating-label--float-above {
  330. @include floating-label-mixins.ink-color($color);
  331. }
  332. }
  333. @mixin _error-label-text-color($color) {
  334. &.mdc-text-field--invalid {
  335. @include _label-text-color($color);
  336. }
  337. }
  338. @mixin _leading-icon-color($colors) {
  339. @include if-enabled {
  340. @include _set-leading-icon-color(map.get($colors, default));
  341. @include if-hovered {
  342. @include _set-leading-icon-color(map.get($colors, hover));
  343. }
  344. @include if-focused {
  345. @include _set-leading-icon-color(map.get($colors, focus));
  346. }
  347. }
  348. @include if-disabled {
  349. @include _set-leading-icon-color(map.get($colors, disabled));
  350. }
  351. }
  352. @mixin _set-leading-icon-color($color) {
  353. .mdc-text-field__icon--leading {
  354. @include theme.property(color, $color);
  355. }
  356. }
  357. @mixin _error-leading-icon-color($color) {
  358. &.mdc-text-field--invalid {
  359. @include _leading-icon-color($color);
  360. }
  361. }
  362. @mixin _trailing-icon-color($colors) {
  363. @include if-enabled {
  364. @include _set-trailing-icon-color(map.get($colors, default));
  365. @include if-hovered {
  366. @include _set-trailing-icon-color(map.get($colors, hover));
  367. }
  368. @include if-focused {
  369. @include _set-trailing-icon-color(map.get($colors, focus));
  370. }
  371. }
  372. @include if-disabled {
  373. @include _set-trailing-icon-color(map.get($colors, disabled));
  374. }
  375. }
  376. @mixin _set-trailing-icon-color($color) {
  377. .mdc-text-field__icon--trailing {
  378. @include theme.property(color, $color);
  379. }
  380. }
  381. @mixin _error-trailing-icon-color($color) {
  382. &.mdc-text-field--invalid {
  383. @include _trailing-icon-color($color);
  384. }
  385. }
  386. @mixin _supporting-text-color($colors) {
  387. @include if-enabled {
  388. @include _set-supporting-text-color(map.get($colors, default));
  389. @include if-hovered {
  390. @include _set-supporting-text-color(map.get($colors, hover));
  391. }
  392. @include if-focused {
  393. @include _set-supporting-text-color(map.get($colors, focus));
  394. }
  395. }
  396. @include if-disabled {
  397. @include _set-supporting-text-color(map.get($colors, disabled));
  398. }
  399. }
  400. @mixin _error-supporting-text-color($color) {
  401. &.mdc-text-field--invalid {
  402. @include _supporting-text-color($color);
  403. }
  404. }
  405. @mixin _set-supporting-text-color($color) {
  406. & + .mdc-text-field-helper-line .mdc-text-field-helper-text,
  407. & + .mdc-text-field-helper-line .mdc-text-field-character-counter {
  408. @include theme.property(color, $color);
  409. }
  410. }
  411. @mixin _input-text-typography($typography-theme) {
  412. .mdc-text-field__input {
  413. @include typography.theme-styles($typography-theme);
  414. }
  415. }
  416. @mixin _label-text-typography($typography-theme) {
  417. .mdc-floating-label {
  418. @include typography.theme-styles($typography-theme);
  419. }
  420. }
  421. @mixin _label-text-populated-typography($typography-theme) {
  422. .mdc-floating-label--float-above {
  423. @include typography.theme-styles($typography-theme);
  424. }
  425. }
  426. @mixin _supporting-text-typography($typography-theme) {
  427. & + .mdc-text-field-helper-line .mdc-text-field-helper-text,
  428. & + .mdc-text-field-helper-line .mdc-text-field-character-counter {
  429. @include typography.theme-styles($typography-theme);
  430. }
  431. }
  432. /// Selector for hovered state
  433. @mixin if-hovered {
  434. &:not(.mdc-text-field--focused):hover {
  435. @content;
  436. }
  437. }
  438. /// Selector for focused state
  439. @mixin if-focused {
  440. &.mdc-text-field--focused {
  441. @content;
  442. }
  443. }
  444. /// Selector for enabled state
  445. @mixin if-enabled {
  446. &:not(.mdc-text-field--disabled) {
  447. @content;
  448. }
  449. }
  450. /// Selector for disabled state
  451. @mixin if-disabled {
  452. &.mdc-text-field--disabled {
  453. @content;
  454. }
  455. }