({isSelected, direction}: SwitchRenderProps & {direction: Direction})
| 178 | |
| 179 | // Use an inline style to calculate the transform so we can combine it with the press scale. |
| 180 | const transformStyle = ({isSelected, direction}: SwitchRenderProps & {direction: Direction}) => { |
| 181 | // In the default state, the handle is 8px smaller than the track. When selected it grows to 6px smaller than the track. |
| 182 | // Normally this could be calculated as a scale transform with (trackHeight - 8px) / trackHeight, however, |
| 183 | // CSS does not allow division with units. To solve this we use a 3d perspective transform. Perspective is the |
| 184 | // distance from the Z=0 plane to the viewer. Since we want to scale the handle by a fixed amount and we cannot divide |
| 185 | // by a value with units, we can set the Z translation to a fixed amount and change the perspective in order to achieve |
| 186 | // the desired effect. Given the following formula: |
| 187 | // |
| 188 | // scale = perspective / (perspective - translateZ) |
| 189 | // |
| 190 | // and desired scale factors (accounting for the 2px border on each side of the track): |
| 191 | // |
| 192 | // defaultScale = (trackHeight - 8px) / (trackHeight - 4px) |
| 193 | // selectedScale = (trackHeight - 6px) / (trackHeight - 4px) |
| 194 | // |
| 195 | // we can solve for the perspective needed in each case where translateZ is hard coded to -4px: |
| 196 | // |
| 197 | // defaultPerspective = trackHeight - 8px |
| 198 | // selectedPerspective = 2 * (trackHeight - 6px) |
| 199 | const placement = |
| 200 | direction === 'ltr' |
| 201 | ? // The selected state also translates the X position to the end of the track (minus the borders). |
| 202 | 'translateX(calc(var(--trackWidth) - 100% - 4px)) perspective(calc(2 * (var(--trackHeight) - 6px))) translateZ(-4px)' |
| 203 | : 'translateX(calc(100% - var(--trackWidth) + 4px)) perspective(calc(2 * (var(--trackHeight) - 6px))) translateZ(-4px)'; |
| 204 | return { |
| 205 | transform: isSelected |
| 206 | ? placement |
| 207 | : 'perspective(calc(var(--trackHeight) - 8px)) translateZ(-4px)' |
| 208 | }; |
| 209 | }; |
| 210 | |
| 211 | /** |
| 212 | * Switches allow users to turn an individual option on or off. |
nothing calls this directly
no outgoing calls
no test coverage detected