| 21 | } |
| 22 | |
| 23 | export class Switch extends Node { |
| 24 | @initial(false) |
| 25 | @signal() |
| 26 | public declare readonly initialState: SimpleSignal<boolean, this>; |
| 27 | |
| 28 | @initial('#68ABDF') |
| 29 | @colorSignal() |
| 30 | public declare readonly accent: ColorSignal<this>; |
| 31 | |
| 32 | private isOn: boolean; |
| 33 | private readonly indicatorPosition = createSignal(0); |
| 34 | private readonly offColor = new Color('#242424'); |
| 35 | private readonly indicator = createRef<Circle>(); |
| 36 | private readonly container = createRef<Rect>(); |
| 37 | |
| 38 | public constructor(props?: SwitchProps) { |
| 39 | super({ |
| 40 | ...props, |
| 41 | }); |
| 42 | |
| 43 | this.isOn = this.initialState(); |
| 44 | this.indicatorPosition(this.isOn ? 50 : -50); |
| 45 | |
| 46 | this.add( |
| 47 | <Rect |
| 48 | ref={this.container} |
| 49 | fill={this.isOn ? this.accent() : this.offColor} |
| 50 | size={[200, 100]} |
| 51 | radius={100} |
| 52 | > |
| 53 | <Circle |
| 54 | x={() => this.indicatorPosition()} |
| 55 | ref={this.indicator} |
| 56 | size={[80, 80]} |
| 57 | fill="#ffffff" |
| 58 | /> |
| 59 | </Rect>, |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | public *toggle(duration: number) { |
| 64 | yield* all( |
| 65 | tween(duration, value => { |
| 66 | const oldColor = this.isOn ? this.accent() : this.offColor; |
| 67 | const newColor = this.isOn ? this.offColor : this.accent(); |
| 68 | |
| 69 | this.container().fill( |
| 70 | Color.lerp(oldColor, newColor, easeInOutCubic(value)), |
| 71 | ); |
| 72 | }), |
| 73 | |
| 74 | tween(duration, value => { |
| 75 | const currentPos = this.indicator().position(); |
| 76 | |
| 77 | this.indicatorPosition( |
| 78 | easeInOutCubic(value, currentPos.x, this.isOn ? -50 : 50), |
| 79 | ); |
| 80 | }), |
nothing calls this directly
no test coverage detected