I thought about making it possible to pass a Button for the left and right sides of the Slider, but then I realised I don’t really want custom functionality.
I want the Button
on the left to set the value to the range’s lower bound, while the Button on the right should set it to the upper bound. Where I do want some customisability is the label for each Button.
By only requiring that the LeftButton
and RightButton
types conform to the View
protocol, I am allowing labels that are images as well. In fact each Button
could have a label that has a layout such as an HStack
inside of it if necessary.
If you want to add a closure that could run when a Button
is pressed, simply add a property like this:
let leftAction: () -> Void
let rightAction: () -> Void
Then make sure to call them like this:
Button {
withAnimation { value = range.lowerBound }
leftAction()
} label: { leftButton() }
Whether you put them inside the withAnimation
block depends upon whether you think the action
closure will require animations to take place. You can always use withAnimation
blocks inside your closure in that case though. I try to keep them as short as possible.