|
| 1 | +package components |
| 2 | + |
| 3 | +import androidx.compose.runtime.Composable |
| 4 | +import androidx.compose.runtime.MutableState |
| 5 | +import androidx.compose.runtime.mutableStateOf |
| 6 | +import androidx.compose.runtime.remember |
| 7 | +import common.ThemeVariables |
| 8 | +import kotlinx.uuid.UUID |
| 9 | +import kotlinx.uuid.generateUUID |
| 10 | +import org.jetbrains.compose.web.ExperimentalComposeWebApi |
| 11 | +import org.jetbrains.compose.web.attributes.InputType |
| 12 | +import org.jetbrains.compose.web.attributes.disabled |
| 13 | +import org.jetbrains.compose.web.attributes.forId |
| 14 | +import org.jetbrains.compose.web.css.* |
| 15 | +import org.jetbrains.compose.web.dom.Input |
| 16 | +import org.jetbrains.compose.web.dom.Label |
| 17 | +import org.jetbrains.compose.web.dom.Span |
| 18 | +import org.jetbrains.compose.web.dom.Text |
| 19 | + |
| 20 | +@OptIn(ExperimentalComposeWebApi::class) |
| 21 | +internal class SwitchStyles( |
| 22 | + invertedColors: Boolean, |
| 23 | +) : StyleSheet() { |
| 24 | + val switch by style { |
| 25 | + position(Position.Relative) |
| 26 | + display(DisplayStyle.InlineBlock) |
| 27 | + width(60.px) |
| 28 | + height(34.px) |
| 29 | + margin(10.px) |
| 30 | + } |
| 31 | + |
| 32 | + val switchInput by style { |
| 33 | + opacity(0) |
| 34 | + width(0.px) |
| 35 | + height(0.px) |
| 36 | + } |
| 37 | + |
| 38 | + val switchSlider by style { |
| 39 | + position(Position.Absolute) |
| 40 | + cursor("pointer") |
| 41 | + top(0.px) |
| 42 | + left(0.px) |
| 43 | + right(0.px) |
| 44 | + bottom(0.px) |
| 45 | + backgroundColor(Color.transparent) |
| 46 | + border( |
| 47 | + 2.px, |
| 48 | + LineStyle.Solid, |
| 49 | + if (invertedColors) { |
| 50 | + ThemeVariables.backgroundColor.value() |
| 51 | + } else { |
| 52 | + ThemeVariables.mainColor.value() |
| 53 | + }, |
| 54 | + ) |
| 55 | + transitions { |
| 56 | + defaultDuration(0.4.s) |
| 57 | + } |
| 58 | + borderRadius(34.px) |
| 59 | + |
| 60 | + self + "::before" style { |
| 61 | + position(Position.Absolute) |
| 62 | + property("content", "\"\"") |
| 63 | + height(20.px) |
| 64 | + width(20.px) |
| 65 | + left(4.px) |
| 66 | + top(50.percent) // Центрируем по вертикали |
| 67 | + transform { translateY((-50).percent) } // Корректируем смещение |
| 68 | + backgroundColor( |
| 69 | + if (invertedColors) { |
| 70 | + ThemeVariables.backgroundColor.value() |
| 71 | + } else { |
| 72 | + ThemeVariables.mainColor.value() |
| 73 | + }, |
| 74 | + ) |
| 75 | + border( |
| 76 | + 2.px, |
| 77 | + LineStyle.Solid, |
| 78 | + if (invertedColors) { |
| 79 | + ThemeVariables.backgroundColor.value() |
| 80 | + } else { |
| 81 | + ThemeVariables.mainColor.value() |
| 82 | + }, |
| 83 | + ) |
| 84 | + transitions { |
| 85 | + defaultDuration(0.4.s) |
| 86 | + } |
| 87 | + borderRadius(50.percent) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + val switchLabel by style { |
| 92 | + display(DisplayStyle.InlineBlock) |
| 93 | + marginLeft(10.px) |
| 94 | + attr("vertical-align", "middle") |
| 95 | + fontSize(16.px) |
| 96 | + } |
| 97 | + |
| 98 | + init { |
| 99 | + (".$switchInput:checked + .$switchSlider") style { |
| 100 | + property( |
| 101 | + "border-color", |
| 102 | + if (invertedColors) { |
| 103 | + ThemeVariables.backgroundColor.value() |
| 104 | + } else { |
| 105 | + ThemeVariables.mainColor.value() |
| 106 | + }, |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + (".$switchInput:checked + .$switchSlider::before") style { |
| 111 | + transform { |
| 112 | + translateX(24.px) |
| 113 | + translateY((-50).percent) |
| 114 | + } |
| 115 | + property( |
| 116 | + "border-color", |
| 117 | + if (invertedColors) { |
| 118 | + ThemeVariables.backgroundColor.value() |
| 119 | + } else { |
| 120 | + ThemeVariables.mainColor.value() |
| 121 | + }, |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + (".$switchInput:disabled + .$switchSlider") style { |
| 126 | + property("borderColor", (rgb(224, 224, 224))) |
| 127 | + cursor("not-allowed") |
| 128 | + } |
| 129 | + |
| 130 | + (".$switchInput:disabled + .$switchSlider::before") style { |
| 131 | + backgroundColor(rgb(189, 189, 189)) |
| 132 | + property("borderColor", (rgb(189, 189, 189))) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +@Composable |
| 138 | +fun Switch( |
| 139 | + label: String, |
| 140 | + disabled: Boolean = false, |
| 141 | + invertedColors: Boolean = false, |
| 142 | + state: MutableState<Boolean> = mutableStateOf(false), |
| 143 | + onSwitchChanged: (newValue: Boolean) -> Unit = {}, |
| 144 | +) { |
| 145 | + val styles = remember { SwitchStyles(invertedColors = invertedColors) } |
| 146 | + Style(styles) |
| 147 | + |
| 148 | + val inputId = UUID.generateUUID().toString() |
| 149 | + Label( |
| 150 | + attrs = { |
| 151 | + classes(styles.switch) |
| 152 | + }, |
| 153 | + ) { |
| 154 | + Input(type = InputType.Checkbox, attrs = { |
| 155 | + id(inputId) |
| 156 | + if (disabled) { |
| 157 | + disabled() |
| 158 | + } |
| 159 | + classes(styles.switchInput) |
| 160 | + checked(state.value) |
| 161 | + onChange { |
| 162 | + state.value = it.value |
| 163 | + onSwitchChanged(state.value) |
| 164 | + } |
| 165 | + }) |
| 166 | + Span( |
| 167 | + attrs = { |
| 168 | + classes(styles.switchSlider) |
| 169 | + }, |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + Label( |
| 174 | + attrs = { |
| 175 | + forId(inputId) |
| 176 | + classes(styles.switchLabel) |
| 177 | + }, |
| 178 | + ) { |
| 179 | + Text(label) |
| 180 | + } |
| 181 | +} |
0 commit comments