Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

💄 Animate the switch thumb with a double-bounce travel (#3305)

Luke Vellac2c32b15d26f
diff --git a/packages/tailwind-config/shared-styles.css b/packages/tailwind-config/shared-styles.css
index fd5a247a7..a36dbae5c 100644
--- a/packages/tailwind-config/shared-styles.css
+++ b/packages/tailwind-config/shared-styles.css
@@ -215,6 +215,36 @@
--ease-out-expo: cubic-bezier(0.19, 1, 0.22, 1);
--animate-caret-blink: caret-blink 1.25s ease-out infinite;
+ --animate-switch-thumb-on: switch-thumb-on 350ms
+ cubic-bezier(0.34, 1.35, 0.64, 1) both;
+ --animate-switch-thumb-off: switch-thumb-off 350ms
+ cubic-bezier(0.34, 1.35, 0.64, 1) both;
+
+ @keyframes switch-thumb-on {
+ 0% {
+ translate: 0 0;
+ }
+ 55% {
+ translate: calc(var(--switch-travel) + 1px) 0;
+ }
+ 80%,
+ 100% {
+ translate: var(--switch-travel) 0;
+ }
+ }
+
+ @keyframes switch-thumb-off {
+ 0% {
+ translate: var(--switch-travel) 0;
+ }
+ 55% {
+ translate: -1px 0;
+ }
+ 80%,
+ 100% {
+ translate: 0 0;
+ }
+ }
@keyframes caret-blink {
0%,
diff --git a/packages/ui/src/switch.tsx b/packages/ui/src/switch.tsx
index 16dec21d8..c711459bd 100644
--- a/packages/ui/src/switch.tsx
+++ b/packages/ui/src/switch.tsx
@@ -1,17 +1,22 @@
"use client";
import { Switch as SwitchPrimitive } from "@base-ui/react/switch";
-import type * as React from "react";
+import * as React from "react";
import { cn } from "./lib/utils";
function Switch({
className,
icon,
+ onCheckedChange,
...props
}: SwitchPrimitive.Root.Props & {
icon?: React.ReactNode;
}) {
+ // The thumb keyframes only run once the user has toggled the switch, so the
+ // "off" bounce does not play on mount for switches that render unchecked.
+ const [interacted, setInteracted] = React.useState(false);
+
return (
// Rendered as a native <button> (Base UI defaults to a span) so the
// switch is a labelable element: with nativeButton, a caller-supplied
@@ -22,15 +27,20 @@ function Switch({
render={<button type="button" />}
nativeButton
data-slot="switch"
+ data-interacted={interacted ? "" : undefined}
className={cn(
- "peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full p-[3px] ring-1 ring-button-outline ring-inset transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-offset-2 data-disabled:cursor-not-allowed data-checked:bg-primary data-unchecked:bg-foreground/10 data-disabled:opacity-50",
+ "peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full p-[3px] ring-1 ring-button-outline ring-inset focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-offset-2 data-disabled:cursor-not-allowed data-checked:bg-primary data-unchecked:bg-foreground/10 data-disabled:opacity-50",
className,
)}
+ onCheckedChange={(checked, eventDetails) => {
+ setInteracted(true);
+ onCheckedChange?.(checked, eventDetails);
+ }}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
- className="pointer-events-none flex h-full w-3.5 items-center justify-center rounded-full border border-black/10 bg-white shadow-sm ring-0 transition-transform duration-100 data-checked:translate-x-4 data-unchecked:translate-x-0"
+ className="pointer-events-none flex h-full w-3.5 items-center justify-center rounded-full border border-black/10 bg-white shadow-sm ring-0 will-change-[translate] [--switch-travel:1rem] data-checked:translate-x-(--switch-travel) data-unchecked:translate-x-0 in-data-interacted:data-checked:animate-switch-thumb-on in-data-interacted:data-unchecked:animate-switch-thumb-off motion-reduce:animate-none"
>
{icon}
</SwitchPrimitive.Thumb>