|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 6 | + <title> |
| 7 | + Creating A Tri-State Switch In Alpine.js |
| 8 | + </title> |
| 9 | + <link rel="stylesheet" type="text/css" href="./main.css" /> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | + |
| 13 | + <h1> |
| 14 | + Creating A Tri-State Switch In Alpine.js |
| 15 | + </h1> |
| 16 | + |
| 17 | + <div x-data="Demo"> |
| 18 | + <p> |
| 19 | + <button |
| 20 | + @click="setMode( 'None' )" |
| 21 | + :class="{ active: ( selected === 'None' ) }"> |
| 22 | + None |
| 23 | + </button> |
| 24 | + <button |
| 25 | + @click="setMode( 'Some' )" |
| 26 | + :class="{ active: ( selected === 'Some' ) }"> |
| 27 | + Some |
| 28 | + </button> |
| 29 | + <button |
| 30 | + @click="setMode( 'All' )" |
| 31 | + :class="{ active: ( selected === 'All' ) }"> |
| 32 | + All |
| 33 | + </button> |
| 34 | + </p> |
| 35 | + |
| 36 | + <!-- |
| 37 | + The x-effect directive works by re-evaluating the overall expression every |
| 38 | + time one of the embedded dependencies changes. As such, any time either the |
| 39 | + "options" or the "selected" parent state is updated, the tri-switch |
| 40 | + component's reconcile() method will be invoked and the updated state will be |
| 41 | + passed-in, allowing the internal tri-switch state to be mapped from the parent |
| 42 | + scope state (ie, one-way data binding). |
| 43 | + --> |
| 44 | + <div |
| 45 | + x-data="TriSwitch()" |
| 46 | + x-effect="reconcile({ |
| 47 | + states: options, |
| 48 | + state: selected |
| 49 | + })" |
| 50 | + @click="cycleMode()" |
| 51 | + class="tri-switch" |
| 52 | + :class="{ |
| 53 | + on: ( phase === 'on' ), |
| 54 | + off: ( phase === 'off' ), |
| 55 | + partial: ( phase === 'partial' ) |
| 56 | + }"> |
| 57 | + <div class="tri-switch__track"> |
| 58 | + <div class="tri-switch__thumb"></div> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + |
| 62 | + </div> |
| 63 | + |
| 64 | + <script type="text/javascript" src="../../vendor/alpine/3.13.5/alpine.3.13.5.min.js" defer></script> |
| 65 | + <script type="text/javascript"> |
| 66 | + |
| 67 | + function Demo() { |
| 68 | + |
| 69 | + return { |
| 70 | + options: [ "None", "Some", "All" ], |
| 71 | + selected: "Some", |
| 72 | + |
| 73 | + // Public methods. |
| 74 | + cycleMode, |
| 75 | + setMode, |
| 76 | + }; |
| 77 | + |
| 78 | + // --- |
| 79 | + // PUBLIC METHODS. |
| 80 | + // --- |
| 81 | + |
| 82 | + /** |
| 83 | + * I cycle to the next selectable mode. |
| 84 | + */ |
| 85 | + function cycleMode() { |
| 86 | + |
| 87 | + var selectedIndex = this.options.indexOf( this.selected ); |
| 88 | + |
| 89 | + // If the NEXT index is undefined, circle back to the front of the modes. |
| 90 | + if ( this.options[ ++selectedIndex ] === undefined ) { |
| 91 | + |
| 92 | + selectedIndex = 0; |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + this.selected = this.options[ selectedIndex ]; |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * I set the selected mode. |
| 102 | + */ |
| 103 | + function setMode( newMode ) { |
| 104 | + |
| 105 | + this.selected = newMode; |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + // --------------------------------------------------------------------------- // |
| 112 | + // --------------------------------------------------------------------------- // |
| 113 | + |
| 114 | + function TriSwitch() { |
| 115 | + |
| 116 | + return { |
| 117 | + phases: [ "off", "partial", "on" ], |
| 118 | + phase: "off", |
| 119 | + |
| 120 | + // Public methods. |
| 121 | + reconcile, |
| 122 | + }; |
| 123 | + |
| 124 | + // --- |
| 125 | + // PUBLIC METHODS. |
| 126 | + // --- |
| 127 | + |
| 128 | + /** |
| 129 | + * I reconcile the parent scope state with the local input bindings. |
| 130 | + */ |
| 131 | + function reconcile( inputs ) { |
| 132 | + |
| 133 | + this.phase = ( |
| 134 | + this.phases[ inputs.states.indexOf( inputs.state ) ] || |
| 135 | + this.phases[ 0 ] |
| 136 | + ); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + </script> |
| 143 | + |
| 144 | +</body> |
| 145 | +</html> |
0 commit comments