@@ -71,32 +71,34 @@ rawComponent =
71 buttonRef <- useRef null
71 buttonRef <- useRef null
72 toggleRef <- useRef null
72 toggleRef <- useRef null
73 tapState <- useRef TapNotAllowed
73 tapState <- useRef TapNotAllowed
74− dragState /\ setDragState <- React.useState' NotDragging
74+ dragStateRef <- useRef NotDragging
75− useEffect dragState do
75+ let
76+ setDragState new = do
77+ writeRef dragStateRef new
78+ dragState <- readRef dragStateRef
76− case dragState of
79+ case dragState of
77− NotDragging -> mempty
80+ NotDragging -> mempty
78− Dragging _ -> do
81+ Dragging _ -> do
79− writeRef tapState TapNotAllowed
82+ writeRef tapState TapNotAllowed
80− DragDone { startX, endX } -> do
83+ DragDone { startX, endX } -> do
81− maybeBbox <- getBoundingBoxFromRef buttonRef
84+ maybeBbox <- getBoundingBoxFromRef buttonRef
82− for_ maybeBbox \bbox -> do
85+ for_ maybeBbox \bbox -> do
83− if endX - startX <= (bbox.left - startX) + (bbox.width / 2.0) then do
86+ if endX - startX <= (bbox.left - startX) + (bbox.width / 2.0) then do
84− setTogglePosition ToggleIsLeft
87+ setTogglePosition ToggleIsLeft
86− setTogglePosition ToggleIsRight
89+ setTogglePosition ToggleIsRight
89 buttonVariants =
91 buttonVariants =
91− { backgroundColor:
93+ { backgroundColor:
92− (Emotion.str <<< Color.cssStringRGBA <$> props.backgroundLeft)
94+ (Emotion.str <<< Color.cssStringRGBA <$> props.backgroundLeft)
93− ?|| (Emotion.str colour.backgroundLayer3)
95+ ?|| (Emotion.str colour.backgroundLayer3)
96− { backgroundColor:
98+ { backgroundColor:
97− (Emotion.str <<< Color.cssStringRGBA <$> props.backgroundRight)
99+ (Emotion.str <<< Color.cssStringRGBA <$> props.backgroundRight)
98− ?|| (Emotion.str colour.success)
100+ ?|| (Emotion.str colour.success)
101 buttonVariant = Motion.makeVariantLabels buttonVariants
103 buttonVariant = Motion.makeVariantLabels buttonVariants
102 -- components
104 -- components
@@ -108,40 +110,43 @@ rawComponent =
108 , css: Style.button <>? props.css
110 , css: Style.button <>? props.css
109 , variants: Motion.variants buttonVariants
111 , variants: Motion.variants buttonVariants
110 , initial:
112 , initial:
111− Motion.initial case togglePosition of
113+ Motion.initial case togglePosition of
112− ToggleIsRight -> buttonVariant.right
114+ ToggleIsRight -> buttonVariant.right
113− ToggleIsLeft -> buttonVariant.left
115+ ToggleIsLeft -> buttonVariant.left
114 , animate:
116 , animate:
115− Motion.animate case togglePosition of
117+ Motion.animate case togglePosition of
116− ToggleIsRight -> buttonVariant.right
118+ ToggleIsRight -> buttonVariant.right
117− ToggleIsLeft -> buttonVariant.left
119+ ToggleIsLeft -> buttonVariant.left
118 , value: show togglePosition
120 , value: show togglePosition
119 , onClick: handler preventDefault \_ -> setTogglePosition (flipToggle togglePosition)
121 , onClick: handler preventDefault \_ -> setTogglePosition (flipToggle togglePosition)
120 , style: props.style
122 , style: props.style
121 , _data: Object.singleton "testid" "toggle-testid"
123 , _data: Object.singleton "testid" "toggle-testid"
122 , role: "switch"
124 , role: "switch"
124− Object.fromHomogeneous
126+ Object.fromHomogeneous
125− { checked:
127+ { checked:
126− case togglePosition of
128+ case togglePosition of
127− ToggleIsLeft -> "false"
129+ ToggleIsLeft -> "false"
128− ToggleIsRight -> "true"
130+ ToggleIsRight -> "true"
130− <>? (props.ariaLabel <#> Object.singleton "label")
132+ <>? (props.ariaLabel <#> Object.singleton "label")
131 , ref: buttonRef
133 , ref: buttonRef
132 , children
134 , children
134 textContainer =
136 textContainer =
136− </* { className: "ry-toggle-text"
139+ { className: "ry-toggle-text"
137 , css: Style.toggleTextContainer
140 , css: Style.toggleTextContainer
139 textOnContainer =
142 textOnContainer =
141− </* { className: "ry-toggle-text-on"
145+ { className: "ry-toggle-text-on"
142 , css: Style.toggleText
146 , css: Style.toggleText
144− /> [ guard (togglePosition == ToggleIsRight)
149+ [ guard (togglePosition == ToggleIsRight)
146 [ props.left
151 [ props.left
147 ?|| (Icon.component </> { icon: SVGIcon.on, stroke: Style.successTextColour })
152 ?|| (Icon.component </> { icon: SVGIcon.on, stroke: Style.successTextColour })
@@ -149,10 +154,12 @@ rawComponent =
150 textOffContainer =
155 textOffContainer =
152− </* { className: "ry-toggle-text-off"
158+ { className: "ry-toggle-text-off"
153 , css: Style.toggleText
159 , css: Style.toggleText
155− /> [ guard (togglePosition == ToggleIsLeft)
162+ [ guard (togglePosition == ToggleIsLeft)
156 $ textOff
163 $ textOff
157 [ props.right
164 [ props.right
158 ?|| (Icon.component </> { icon: SVGIcon.off, stroke: Style.disabledTextColour })
165 ?|| (Icon.component </> { icon: SVGIcon.off, stroke: Style.disabledTextColour })
@@ -160,7 +167,8 @@ rawComponent =
162 Motion.div
169 Motion.div
163− </ { className: "ry-toggle-text-container"
171+ { className: "ry-toggle-text-container"
164 , key: "ry-toggle-text-on-container"
172 , key: "ry-toggle-text-on-container"
165 , initial: Motion.initial $ false
173 , initial: Motion.initial $ false
166 , animate: Motion.animate $ css { scale: 1, opacity: 1 }
174 , animate: Motion.animate $ css { scale: 1, opacity: 1 }
@@ -168,7 +176,8 @@ rawComponent =
169 textOff =
177 textOff =
170 Motion.div
178 Motion.div
171− </ { className: "ry-toggle-text-container"
180+ { className: "ry-toggle-text-container"
172 , key: "ry-toggle-text-off-container"
181 , key: "ry-toggle-text-off-container"
173 , initial: Motion.initial $ false
182 , initial: Motion.initial $ false
174 , animate: Motion.animate $ css { scale: 1, opacity: 1 }
183 , animate: Motion.animate $ css { scale: 1, opacity: 1 }
@@ -177,11 +186,12 @@ rawComponent =
177 animateTextPresence = Motion.animatePresence </ {}
186 animateTextPresence = Motion.animatePresence </ {}
179 toggleCircle
188 toggleCircle
180− </> { buttonRef
190+ { buttonRef
181 , toggleRef
191 , toggleRef
182 , togglePosition
192 , togglePosition
183 , onChange: setTogglePosition
193 , onChange: setTogglePosition
184− , dragState
194+ , dragStateRef
185 , setDragState
195 , setDragState
186 , tapState
196 , tapState
@@ -198,7 +208,7 @@ toggleCircle ∷
198 , toggleRef ∷ NodeRef
208 , toggleRef ∷ NodeRef
199 , buttonRef ∷ NodeRef
209 , buttonRef ∷ NodeRef
200 , togglePosition ∷ TogglePosition
210 , togglePosition ∷ TogglePosition
201− , dragState ∷ DragState
211+ , dragStateRef ∷ Ref DragState
202 , setDragState ∷ DragState -> Effect Unit
212 , setDragState ∷ DragState -> Effect Unit
203 , tapState ∷ Ref TappingState
213 , tapState ∷ Ref TappingState
@@ -206,14 +216,14 @@ toggleCircle =
206 unsafePerformEffect
216 unsafePerformEffect
207 $ reactComponent "ToggleCircle" do
217 $ reactComponent "ToggleCircle" do
208 \( { togglePosition
218 \( { togglePosition
209− , onChange
219+ , onChange
210− , toggleRef
220+ , toggleRef
211− , buttonRef
221+ , buttonRef
212− , dragState
222+ , dragStateRef
213− , setDragState
223+ , setDragState
214− , tapState
224+ , tapState
216− ) -> React.do
226+ ) -> React.do
217 maxLeft /\ setMaxLeft <- useState' Nothing
227 maxLeft /\ setMaxLeft <- useState' Nothing
218 yPos /\ setYPos <- useState' Nothing
228 yPos /\ setYPos <- useState' Nothing
219 useEffectAlways do
229 useEffectAlways do
@@ -236,7 +246,8 @@ toggleCircle =
236 toggleVariant = Motion.makeVariantLabels toggleVariants
246 toggleVariant = Motion.makeVariantLabels toggleVariants
238 $ Motion.div
248 $ Motion.div
239− </*> { className: "ry-toggle-toggle"
250+ { className: "ry-toggle-toggle"
240 , layout: Motion.layout true
251 , layout: Motion.layout true
241 , onClick: handler stopPropagation mempty
252 , onClick: handler stopPropagation mempty
242 , onTouchStart: handler stopPropagation mempty
253 , onTouchStart: handler stopPropagation mempty
@@ -247,67 +258,70 @@ toggleCircle =
247 , key: if isNothing maxLeft then "initialising" else show yPos
258 , key: if isNothing maxLeft then "initialising" else show yPos
248 , dragElastic: Motion.dragElastic false
259 , dragElastic: Motion.dragElastic false
249 , dragConstraints:
260 , dragConstraints:
250− Motion.dragConstraintsBoundingBox
261+ Motion.dragConstraintsBoundingBox
252− case togglePosition of
263+ case togglePosition of
253− ToggleIsLeft -> zero
264+ ToggleIsLeft -> zero
254− ToggleIsRight -> -(maxLeft <#> (_ - Style.dragWidthDelta) # fromMaybe 0.0)
265+ ToggleIsRight -> 10.0 -- [WARN] Maybe this breaks down
255− , right:
266+ , right:
256− case togglePosition of
267+ case togglePosition of
257− ToggleIsRight -> zero
268+ ToggleIsRight -> 26.0 -- [WARN] Maybe this breaks down
258− ToggleIsLeft -> maxLeft <#> (_ - Style.dragWidthDelta) # fromMaybe 0.0
269+ ToggleIsLeft -> maxLeft <#> (_ - Style.dragWidthDelta) # fromMaybe 0.0
260 , variants: Motion.variants toggleVariants
271 , variants: Motion.variants toggleVariants
261 , transition: Motion.transition { type: "tween", duration: 0.33, ease: "easeOut" }
272 , transition: Motion.transition { type: "tween", duration: 0.33, ease: "easeOut" }
262 , whileTap:
273 , whileTap:
263− Motion.prop
274+ Motion.prop
265− { width: "calc(var(--s2)*0.85 + 10px)"
276+ { width: "calc(var(--s2)*0.85 + 10px)"
267− case togglePosition of
278+ case togglePosition of
268− ToggleIsLeft -> Style.toggleLeft
279+ ToggleIsLeft -> Style.toggleLeft
269− ToggleIsRight -> "calc(" <> Style.toggleLeft <> " - 10px)"
280+ ToggleIsRight -> "calc(" <> Style.toggleLeft <> " - 10px)"
270− , transition: { type: "tween", duration: 0.10, ease: "easeInOut" }
281+ , transition: { type: "tween", duration: 0.10, ease: "easeInOut" }
272 , onTapStart:
283 , onTapStart:
273− Motion.onTapStart \_ _ -> writeRef tapState TapAllowed
284+ Motion.onTapStart \_ _ -> writeRef tapState TapAllowed
275− Motion.onTap \_ _ -> do
286+ Motion.onTap \_ _ -> do
276− ts <- readRef tapState
287+ ts <- readRef tapState
277− case ts of
288+ case ts of
278− TapAllowed -> onChange (flipToggle togglePosition)
289+ TapAllowed -> onChange (flipToggle togglePosition)
279− _ -> mempty
290+ _ -> mempty
281 , onTapCancel:
292 , onTapCancel:
282− Motion.onTapCancel \_ _ -> do
293+ Motion.onTapCancel \_ _ -> do
283− writeRef tapState TapNotAllowed
294+ writeRef tapState TapNotAllowed
285 , initial: Motion.initial false
296 , initial: Motion.initial false
286 , animate:
297 , animate:
287− Motion.animate case togglePosition of
298+ Motion.animate case togglePosition of
288− ToggleIsLeft -> toggleVariant.left
299+ ToggleIsLeft -> toggleVariant.left
289− ToggleIsRight -> toggleVariant.right
300+ ToggleIsRight -> toggleVariant.right
290 , onDragStart:
301 , onDragStart:
291− Motion.onDragStart \_ pi -> do
302+ Motion.onDragStart \_ pi -> do
292− maybeBBox <- getBoundingBoxFromRef toggleRef
303+ maybeBBox <- getBoundingBoxFromRef toggleRef
293− let x = maybeBBox <#> \bbox -> bbox.left + (bbox.width / 2.0)
304+ let x = maybeBBox <#> \bbox -> bbox.left + (bbox.width / 2.0)
294− setDragState
305+ setDragState
295− $ Dragging
306+ $ Dragging
296− { startX: x # fromMaybe pi.point.x, currentX: pi.point.x
307+ { startX: x # fromMaybe pi.point.x
308+ , currentX: pi.point.x
298 , onDrag:
310 , onDrag:
299− Motion.onDrag \_ pi -> do
311+ Motion.onDrag \_ pi -> do
312+ dragState <- readRef dragStateRef
300− case dragState of
313+ case dragState of
301− Dragging { startX } -> do
314+ Dragging { startX } -> do
302− setDragState $ Dragging { startX, currentX: pi.point.x }
315+ setDragState $ Dragging { startX, currentX: pi.point.x }
303− other -> Console.warn $ i "Unexpected drag state " (show other) " in onDragEvent"
316+ other -> Console.warn $ i "Unexpected drag state " (show other) " in onDragEvent"
304 , onDragEnd:
317 , onDragEnd:
305− Motion.onDragEnd \_ _ -> do
318+ Motion.onDragEnd \_ _ -> do
319+ dragState <- readRef dragStateRef
306− case dragState of
320+ case dragState of
307− Dragging { startX } -> do
321+ Dragging { startX } -> do
308− maybeBBox <- getBoundingBoxFromRef toggleRef
322+ maybeBBox <- getBoundingBoxFromRef toggleRef
309− let x = maybeBBox <#> \bbox -> bbox.left + (bbox.width / 2.0)
323+ let x = maybeBBox <#> \bbox -> bbox.left + (bbox.width / 2.0)
310− setDragState (DragDone { startX, endX: x # fromMaybe' \_ -> unsafeCrashWith "shit" })
324+ setDragState (DragDone { startX, endX: x # fromMaybe' \_ -> unsafeCrashWith "shit" })
311− other -> Console.warn $ i "Unexpected drag state " (show other) " in onDragEndEvent"
325+ other -> Console.warn $ i "Unexpected drag state " (show other) " in onDragEndEvent"
312 , ref: toggleRef
326 , ref: toggleRef