_form-field-native-select.scss 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. @use 'sass:math';
  2. @use '../core/tokens/m2/mat/form-field' as tokens-mat-form-field;
  3. @use '../core/tokens/token-utils';
  4. // Width of the Material Design form-field select arrow.
  5. $mat-form-field-select-arrow-width: 10px;
  6. // Height of the Material Design form-field select arrow.
  7. $mat-form-field-select-arrow-height: 5px;
  8. // Horizontal padding that needs to be applied to the native select in a form-field so
  9. // that the absolute positioned arrow does not overlap the select content.
  10. $mat-form-field-select-horizontal-end-padding: $mat-form-field-select-arrow-width + 5px;
  11. $_tokens: tokens-mat-form-field.$prefix, tokens-mat-form-field.get-token-slots();
  12. // Mixin that creates styles for native select controls in a form-field.
  13. @mixin private-form-field-native-select() {
  14. // Remove the native select down arrow and ensure that the native appearance
  15. // does not conflict with the form-field. e.g. Focus indication of the native
  16. // select is undesired since we handle focus as part of the form-field.
  17. select.mat-mdc-form-field-input-control {
  18. -moz-appearance: none;
  19. -webkit-appearance: none;
  20. background-color: transparent;
  21. display: inline-flex;
  22. box-sizing: border-box;
  23. // By default the cursor does not change when hovering over a select. We want to
  24. // change this for non-disabled select elements so that it's more obvious that the
  25. // control can be clicked.
  26. &:not(:disabled) {
  27. cursor: pointer;
  28. }
  29. &:not(.mat-mdc-native-select-inline) {
  30. @include token-utils.use-tokens($_tokens...) {
  31. option {
  32. @include token-utils.create-token-slot(color, select-option-text-color);
  33. }
  34. option:disabled {
  35. @include token-utils.create-token-slot(color, select-disabled-option-text-color);
  36. }
  37. }
  38. }
  39. }
  40. // Native select elements with the `matInput` directive should have Material Design
  41. // styling. This means that we add an arrow to the form-field that is based on the
  42. // Material Design specification. We achieve this by adding a pseudo element to the
  43. // form-field infix.
  44. .mat-mdc-form-field-type-mat-native-select {
  45. .mat-mdc-form-field-infix::after {
  46. content: '';
  47. width: 0;
  48. height: 0;
  49. border-left: math.div($mat-form-field-select-arrow-width, 2) solid transparent;
  50. border-right: math.div($mat-form-field-select-arrow-width, 2) solid transparent;
  51. border-top: $mat-form-field-select-arrow-height solid;
  52. position: absolute;
  53. right: 0;
  54. top: 50%;
  55. margin-top: -#{math.div($mat-form-field-select-arrow-height, 2)};
  56. // Make the arrow non-clickable so the user can click on the form control under it.
  57. pointer-events: none;
  58. @include token-utils.use-tokens($_tokens...) {
  59. @include token-utils.create-token-slot(color, enabled-select-arrow-color);
  60. }
  61. [dir='rtl'] & {
  62. right: auto;
  63. left: 0;
  64. }
  65. }
  66. @include token-utils.use-tokens($_tokens...) {
  67. &.mat-focused .mat-mdc-form-field-infix::after {
  68. @include token-utils.create-token-slot(color, focus-select-arrow-color);
  69. }
  70. &.mat-form-field-disabled .mat-mdc-form-field-infix::after {
  71. @include token-utils.create-token-slot(color, disabled-select-arrow-color);
  72. }
  73. }
  74. // Add padding on the end of the native select so that the content does not
  75. // overlap with the Material Design arrow.
  76. .mat-mdc-form-field-input-control {
  77. padding-right: $mat-form-field-select-horizontal-end-padding;
  78. [dir='rtl'] & {
  79. padding-right: 0;
  80. padding-left: $mat-form-field-select-horizontal-end-padding;
  81. }
  82. }
  83. }
  84. }