File: switch.scss

package info (click to toggle)
cockpit 354-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 308,956 kB
  • sloc: javascript: 775,606; python: 40,351; ansic: 35,655; cpp: 11,117; sh: 3,511; makefile: 580; xml: 261
file content (191 lines) | stat: -rw-r--r-- 8,487 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
@use '../../sass-utilities' as *;

@include pf-root($switch) {
  // Switch - the vars for this file don't follow our normal order so that the IE11 script can convert the vars correctly
  --#{$switch}--FontSize: var(--pf-t--global--font--size--sm);
  --#{$switch}--ColumnGap: var(--pf-t--global--spacer--sm);

  // Switch icon
  --#{$switch}__toggle-icon--FontSize: calc(var(--#{$switch}--FontSize) * .625); // We don't use an icon font-size here because this value allows the icon to scale relative to the switch's font-size.
  --#{$switch}__toggle-icon--Color: var(--pf-t--global--icon--color--on-brand--default);
  --#{$switch}__toggle-icon--InsetInlineStart: calc(var(--#{$switch}--FontSize) * .4);
  --#{$switch}__toggle-icon--Offset: #{pf-size-prem(2px)}; // this value is created to handle sizing of the toggle and therefore the width of the toggle and its transform value.
  --#{$switch}__input--disabled__toggle-icon--Color: var(--pf-t--global--icon--color--disabled);

  // Switch
  --#{$switch}--LineHeight: var(--pf-t--global--font--line-height--body);
  --#{$switch}--Height: auto;

  // Switch input
  --#{$switch}__input--checked__toggle--BackgroundColor: var(--pf-t--global--color--brand--default);
  --#{$switch}__input--checked__toggle--before--TranslateX: calc(100% + var(--#{$switch}__toggle-icon--Offset));
  --#{$switch}__input--checked__toggle--BorderColor: transparent;
  --#{$switch}__input--checked__toggle--BorderWidth: var(--pf-t--global--border--width--high-contrast--regular);
  --#{$switch}__input--checked__label--Color: var(--pf-t--global--text--color--regular);
  --#{$switch}__input--not-checked__label--Color: var(--pf-t--global--text--color--subtle);
  --#{$switch}__input--disabled__label--Color: var(--pf-t--global--text--color--disabled);
  --#{$switch}__input--disabled__toggle--BackgroundColor: var(--pf-t--global--background--color--disabled--default);
  --#{$switch}__input--disabled__toggle--BorderColor: var(--pf-t--global--border--color--high-contrast);
  --#{$switch}__input--checked__toggle--before--BackgroundColor: var(--pf-t--global--icon--color--inverse);
  --#{$switch}__input--not-checked__toggle--before--BackgroundColor: var(--pf-t--global--icon--color--subtle);
  --#{$switch}__input--disabled__toggle--before--BackgroundColor: var(--pf-t--global--icon--color--on-disabled);
  --#{$switch}__input--focus__toggle--OutlineWidth: var(--pf-t--global--border--width--strong);
  --#{$switch}__input--focus__toggle--OutlineOffset: var(--pf-t--global--spacer--xs);
  --#{$switch}__input--focus__toggle--OutlineColor: var(--pf-t--global--color--brand--default);

  // Switch toggle
  --#{$switch}__toggle--Height: calc(var(--#{$switch}--FontSize) * var(--#{$switch}--LineHeight));
  --#{$switch}__toggle--BackgroundColor: var(--pf-t--global--background--color--control--default);
  --#{$switch}__toggle--BorderColor: var(--pf-t--global--border--color--default);
  --#{$switch}__toggle--BorderWidth: var(--pf-t--global--border--width--control--default);
  --#{$switch}__toggle--BorderRadius: var(--pf-t--global--border--radius--pill);
  --#{$switch}__toggle--before--Width: calc(var(--#{$switch}--FontSize) - var(--#{$switch}__toggle-icon--Offset));
  --#{$switch}__toggle--before--Height: var(--#{$switch}__toggle--before--Width);
  --#{$switch}__toggle--before--InsetInlineStart: calc((var(--#{$switch}__toggle--Height) - var(--#{$switch}__toggle--before--Height)) / 2);
  --#{$switch}__toggle--before--BorderWidth: var(--pf-t--global--border--width--high-contrast--regular);
  --#{$switch}__toggle--before--BorderRadius: var(--pf-t--global--border--radius--large);
  --#{$switch}__toggle--before--TransitionTimingFunction: var(--pf-t--global--motion--timing-function--default);
  --#{$switch}__toggle--before--TransitionDuration: var(--pf-t--global--motion--duration--md);
  --#{$switch}__toggle--before--Transition: transform var(--#{$switch}__toggle--before--TransitionTimingFunction) var(--#{$switch}__toggle--before--TransitionDuration), background-color var(--#{$switch}__toggle--before--TransitionTimingFunction) var(--#{$switch}__toggle--before--TransitionDuration); // TODO remove in breaking change along with shorthand property
  --#{$switch}__toggle--Width: calc(var(--#{$switch}__toggle--Height) + var(--#{$switch}__toggle-icon--Offset) + var(--#{$switch}__toggle--before--Width));
}

.#{$switch} {
  position: relative;
  display: inline-grid;
  grid-template-columns: auto;
  grid-auto-columns: 1fr;
  column-gap: var(--#{$switch}--ColumnGap);
  height: var(--#{$switch}--Height);
  font-size: var(--#{$switch}--FontSize);
  line-height: var(--#{$switch}--LineHeight);
  vertical-align: middle;
  cursor: pointer;

  &.pf-m-reverse {
    .#{$switch}__label,
    .#{$switch}__toggle {
      grid-row: 1;
    }

    .#{$switch}__label {
      grid-column: 1;
    }
  }
}

.#{$switch}__input {
  position: absolute;
  cursor: pointer;
  opacity: 0;

  &:focus-visible ~ .#{$switch}__toggle {
    outline: var(--#{$switch}__input--focus__toggle--OutlineWidth) solid var(--#{$switch}__input--focus__toggle--OutlineColor);
    outline-offset: var(--#{$switch}__input--focus__toggle--OutlineOffset);
  }

  &:checked {
    ~ .#{$switch}__label {
      color: var(--#{$switch}__input--checked__label--Color);
    }

    ~ .#{$switch}__toggle {
      --#{$switch}__toggle--BorderWidth: var(--#{$switch}__input--checked__toggle--BorderWidth);
      --#{$switch}__toggle--BorderColor: var(--#{$switch}__input--checked__toggle--BorderColor);

      background-color: var(--#{$switch}__input--checked__toggle--BackgroundColor);

      &::before {
        @include pf-v6-bidirectional-style(
          $prop: transform,
          $ltr-val: translate(var(--#{$switch}__input--checked__toggle--before--TranslateX), -50%),
          $rtl-val: translate(#{pf-v6-calc-inverse(var(--#{$switch}__input--checked__toggle--before--TranslateX))}, -50%)
        );

        background-color: var(--#{$switch}__input--checked__toggle--before--BackgroundColor);
      }
    }
  }

  &:not(:checked) {
    ~ .#{$switch}__label {
      color: var(--#{$switch}__input--not-checked__label--Color);
    }

    ~ .#{$switch}__toggle {
      .#{$switch}__toggle-icon {
        display: none;
      }
    }
  }

  &:disabled {
    cursor: not-allowed;

    ~ .#{$switch}__label {
      color: var(--#{$switch}__input--disabled__label--Color);
      cursor: not-allowed;
    }

    ~ .#{$switch}__toggle {
      --#{$switch}__toggle-icon--Color: var(--#{$switch}__input--disabled__toggle-icon--Color);
      --#{$switch}__toggle--BorderColor: var(--#{$switch}__input--disabled__toggle--BorderColor);

      cursor: not-allowed;
      background-color: var(--#{$switch}__input--disabled__toggle--BackgroundColor);

      &::before {
        background-color: var(--#{$switch}__input--disabled__toggle--before--BackgroundColor);
      }
    }
  }
}

.#{$switch}__toggle {
  position: relative;
  display: inline-block;
  width: var(--#{$switch}__toggle--Width);
  height: var(--#{$switch}__toggle--Height);
  background-color: var(--#{$switch}__toggle--BackgroundColor);
  border-radius: var(--#{$switch}__toggle--BorderRadius);

  &::before {
    position: absolute;
    inset-block-start: 50%;
    inset-inline-start: var(--#{$switch}__toggle--before--InsetInlineStart);
    display: block;
    width: var(--#{$switch}__toggle--before--Width);
    height: var(--#{$switch}__toggle--before--Height);
    content: "";
    background-color: var(--#{$switch}__input--not-checked__toggle--before--BackgroundColor);
    border: var(--#{$switch}__toggle--before--BorderWidth) solid transparent; // for forced color mode
    border-radius: var(--#{$switch}__toggle--before--BorderRadius);
    transition: var(--#{$switch}__toggle--before--Transition); // TODO remove shorthand in breaking change
    transform: translateY(-50%);
  }

  &::after {
    position: absolute;
    inset: 0;
    content: "";
    border: var(--#{$switch}__toggle--BorderWidth) solid var(--#{$switch}__toggle--BorderColor);
    border-radius: var(--#{$switch}__toggle--BorderRadius);
  }
}

.#{$switch}__toggle-icon {
  position: absolute;
  inset-block-start: 0;
  inset-block-end: 0;
  inset-inline-start: var(--#{$switch}__toggle-icon--InsetInlineStart);
  display: flex;
  align-items: center;
  font-size: var(--#{$switch}__toggle-icon--FontSize);
  color: var(--#{$switch}__toggle-icon--Color);
}

.#{$switch}__label {
  display: inline-block;
  grid-column: 2;
  vertical-align: top;
}