Here's some detail around how the new @trycampsite switch component was crafted and built. Will share some of the shadow/lighting detail as well as a little code used for the toggle animation:
This 2x zoom better exposes the smaller highlights as well as the finer contrast shadows that create more environmental detail. White highlights on white backgrounds are difficult to show, but there are some little tricks you can use to make them visible. I'll call some out next:
There's the component gray/blue background and the background gradient on the switch thumb and then just box shadows for the rest. My personal fav trick is #1.
Next I'll show with and without this added detail so you can compare a more traditional switch style to these.
Here are flat switches next to the detailed ones. The side by side really shows off how the subtle little details create a stronger presence. Let's check out dark mode:
Once again, like with our buttons, dark mode gives us even more opportunity for details. With the switch thumb being white next to dark we can have it emit reflections on the sides of it too 💅
Next, lets animate.
Rather than a single movement, this toggle uses a sequenced animation. When sequencing, you write the code steps just how it happens.
1. Idle state. 2. Thumb goes full width. 3. State changes that tells the toggle it's new state. 4. Thumb goes to final state.
You want the code?
In motion you can write async functions to sequence animation.
The magic happens with a middle state swap. After going full width, the state change tells the parent to either flex-start or flex-end based on state, and once that's done, motion animates from `100%` to `auto`.
Lastly: I don't name my layers 😅
Hope you enjoyed 🤙
PS: In case you missed it, here's the the animation in action:
Yesterday, @jamesm called out our @trycampsite UI bits and how we utilize box shadows as borders to get better border contrast on elements with shadows. Lets talk about how we actually implement this and work around the gotchas:
To recap - use a 1px slightly transparent box shadow as a border instead of a traditional css border on elements with shadows to get a gradient border on the element for free. The box shadow blends with the element's shadow as it gets darker towards the bottom. Why not borders?
These days devs use the css magic of box-sizing: border-box for all elements. This makes elements include their padding and borders inside of their element size. Shadows render outside of this boundary. But outside shadows as borders will create size and alignment issues like...