@@ -560,64 +560,65 @@ function _selection_to_jsselection(options, selection)
560
560
end
561
561
end
562
562
563
- struct ToggleSwitch
564
- value:: Observable{Bool}
563
+ @kwdef struct ToggleSwitch
564
+ value:: Observable{Bool} = Observable {Bool} ()
565
+ height:: Int = 24
566
+ width:: Int = 35
567
+ inset:: Int = 3
568
+ checked_color:: String = " #2196F3"
569
+ background_color:: String = " #ccc"
570
+ thumb_color:: String = " white"
571
+ transition_time:: Float64 = 0.4
572
+ label:: String = " "
565
573
end
566
574
567
- function Bonito. jsrender (session:: Session , slider:: ToggleSwitch )
568
- # main properties
569
- height = 24
570
- width = 35
571
- inset = 3
572
- checked_color = " #2196F3"
573
- background_color = " #ccc"
574
- thumb_color = " white"
575
- transition_time = 0.4
576
- label = " rel to u0"
575
+ function Bonito. jsrender (session:: Session , toggle:: ToggleSwitch )
576
+ thumb_diameter = toggle. height - 2 * toggle. inset
577
+ translate = toggle. width - thumb_diameter - 2 * toggle. inset
577
578
578
- thumb_diameter = height - 2 * inset
579
- translate = width - thumb_diameter - 2 * inset
580
-
581
-
582
- switch_style = Styles (
579
+ domlabel_style = Styles (
583
580
" position" => " relative" ,
584
581
" display" => " inline-block" ,
585
- " width" => " $(width) px" ,
586
- " height" => " $(height) px" ,
582
+ " cursor" => " pointer" ,
587
583
)
588
584
589
585
input_style = Styles (
590
586
CSS (
591
587
" opacity" => " 0" ,
592
588
" width" => " 0" ,
593
589
" height" => " 0" ,
590
+ " position" => " absolute" ,
594
591
),
595
- CSS (" :checked + .slider" ,
596
- " background-color" => checked_color,
592
+ CSS (" :checked + .switch-label-grid . slider" ,
593
+ " background-color" => toggle . checked_color,
597
594
),
598
- CSS (" :focus + .slider" ,
599
- " box-shadow" => " 0 0 1px $(checked_color) " ,
595
+ CSS (" :focus + .switch-label-grid . slider" ,
596
+ " box-shadow" => " 0 0 1px $(toggle . checked_color) " ,
600
597
),
601
- CSS (" :checked + .slider:before" ,
598
+ CSS (" :checked + .switch-label-grid . slider:before" ,
602
599
" -webkit-transform" => " translateX($(translate) px)" ,
603
600
" -ms-transform" => " translateX($(translate) px)" ,
604
601
" transform" => " translateX($(translate) px)" ,
605
602
),
606
603
)
607
604
605
+ slider_grid_style = Styles (
606
+ " position" => " relative" ,
607
+ " display" => " inline-grid" ,
608
+ " grid-template-columns" => " auto auto" ,
609
+ " align-items" => " center" ,
610
+ " grid-gap" => " 10px" ,
611
+ )
612
+
608
613
slider_style = Styles (
609
614
CSS (
610
- " position" => " absolute" ,
611
- " cursor" => " pointer" ,
612
- " top" => " 0" ,
613
- " left" => " 0" ,
614
- " right" => " 0" ,
615
- # "width" => "$(width)px",
615
+ " width" => " $(toggle. width) px" ,
616
+ " height" => " $(toggle. height) px" ,
616
617
" bottom" => " 0" ,
617
- " background-color" => background_color,
618
- " -webkit-transition" => " $(transition_time) s" ,
619
- " transition" => " $(transition_time) s" ,
620
- " border-radius" => " $(height/ 2 ) px" ,
618
+ " background-color" => toggle . background_color,
619
+ " -webkit-transition" => " $(toggle . transition_time) s" ,
620
+ " transition" => " $(toggle . transition_time) s" ,
621
+ " border-radius" => " $(toggle . height/ 2 ) px" ,
621
622
),
622
623
CSS (" :hover" ,
623
624
# "background-color" => "#2196F3",
@@ -627,31 +628,41 @@ function Bonito.jsrender(session::Session, slider::ToggleSwitch)
627
628
" content" => " ''" ,
628
629
" height" => " $(thumb_diameter) px" ,
629
630
" width" => " $(thumb_diameter) px" ,
630
- " left" => " $(inset) px" ,
631
- " bottom" => " $(inset) px" ,
631
+ " left" => " $(toggle . inset) px" ,
632
+ " bottom" => " $(toggle . inset) px" ,
632
633
" background-color" => " white" ,
633
- " -webkit-transition" => " $(transition_time) s" ,
634
- " transition" => " $(transition_time) s" ,
634
+ " -webkit-transition" => " $(toggle . transition_time) s" ,
635
+ " transition" => " $(toggle . transition_time) s" ,
635
636
" border-radius" => " 50%" ,
636
637
)
637
638
)
638
639
639
- container = DOM. div (
640
- DOM. label (
641
- DOM. input (
642
- type= " checkbox" ,
643
- checked= slider. value[],
644
- onchange= js """
645
- (e ) => {
646
- $ (slider .value ).notify (e .target .checked );
647
- }
648
- """ ;
649
- style= input_style
650
- ),
651
- DOM. span (; class= " slider" , style= slider_style),
652
- DOM. span (label; class= " sliederlabel" );
653
- class= " switch" , style= switch_style
654
- )
640
+ jscode = js """
641
+ (input ) => {
642
+ input .onchange = function () {
643
+ $ (toggle .value ).notify (this .checked );
644
+ }
645
+ $ (toggle .value ).on (val => {
646
+ input .checked = val
647
+ });
648
+ }
649
+ """
650
+
651
+ inputdom = DOM. input (
652
+ type= " checkbox" ,
653
+ checked= toggle. value[],
654
+ style= input_style
655
+ )
656
+ onload (session, inputdom, jscode)
657
+
658
+ container = DOM. label (
659
+ inputdom,
660
+ DOM. div (
661
+ DOM. div (; class= " slider" , style= slider_style),
662
+ DOM. div (toggle. label; class= " sliederlabel" );
663
+ class= " switch-label-grid" , style= slider_grid_style
664
+ );
665
+ style= domlabel_style
655
666
)
656
667
jsrender (session, container)
657
668
end
0 commit comments