Skip to content

Commit e4e6878

Browse files
committed
fix: added a guard to the toggle button
1 parent fc068c1 commit e4e6878

1 file changed

Lines changed: 26 additions & 20 deletions

File tree

apps/ascent/components/ui/VersionToggle.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,21 @@ export default function VersionToggle({ className }: VersionToggleProps) {
4040

4141
const handleVersionChange = useCallback(
4242
(newVersion: Version) => {
43+
if (newVersion === version) return
4344
setVersion(newVersion)
44-
// Only attempt navigation if we're on a component page
4545
const slugMatch = pathname.match(/^\/docs\/components\/([^/]+)/)
4646
if (!slugMatch) return
4747

4848
const currentSlug = slugMatch[1]
49-
// Look up the peer slug from the map — works for any naming scheme
5049
const targetSlug = peerMap.get(currentSlug)
51-
if (!targetSlug) return // no peer exists for this component, stay put
50+
if (!targetSlug) return
5251

5352
router.push(pathname.replace(currentSlug, targetSlug))
5453
},
55-
[pathname, peerMap, setVersion, router]
54+
[pathname, peerMap, setVersion, router, version]
5655
)
5756

58-
// Only show on docs pages
5957
if (!pathname?.startsWith('/docs')) return null
60-
6158
if (!mounted) return <VersionToggleSkeleton className={className} />
6259

6360
return (
@@ -69,6 +66,27 @@ export default function VersionToggle({ className }: VersionToggleProps) {
6966
className
7067
)}
7168
>
69+
{/* Sliding indicator layer — sits behind buttons, animates via layoutId */}
70+
<div className="absolute inset-1 flex gap-0.5 pointer-events-none">
71+
{VERSIONS.map((v) =>
72+
version === v ? (
73+
<motion.div
74+
key={v}
75+
layoutId={`activeVersion-${layoutId}`}
76+
initial={false}
77+
transition={{
78+
type: 'spring',
79+
stiffness: 400,
80+
damping: 30,
81+
}}
82+
className="flex-1 bg-primary/90 border-[0.25px] border-primary rounded-lg"
83+
/>
84+
) : (
85+
<div key={v} className="flex-1" />
86+
)
87+
)}
88+
</div>
89+
7290
{VERSIONS.map((v) => (
7391
<button
7492
key={v}
@@ -77,24 +95,12 @@ export default function VersionToggle({ className }: VersionToggleProps) {
7795
aria-pressed={version === v}
7896
aria-label={`Switch to version ${v}`}
7997
className={cn(
80-
'relative px-2.5 py-1 text-xs font-medium rounded-lg text-shadow-2xs/5 transition-colors',
98+
'relative flex-1 px-2.5 py-1 text-xs font-medium rounded-lg text-shadow-2xs/5 transition-colors z-20',
8199
version === v
82100
? 'text-primary-foreground'
83-
: 'bg-background text-muted-foreground hover:text-foreground hover:bg-muted'
101+
: 'text-muted-foreground hover:text-foreground'
84102
)}
85103
>
86-
{version === v && (
87-
<motion.div
88-
layoutId={`activeVersion-${layoutId}`}
89-
initial={false}
90-
transition={{
91-
type: 'spring',
92-
stiffness: 400,
93-
damping: 30,
94-
}}
95-
className="absolute inset-0 bg-primary/90 border-[0.25px] border-primary rounded-lg z-10"
96-
/>
97-
)}
98104
<span className="relative z-10">V{v}</span>
99105
</button>
100106
))}

0 commit comments

Comments
 (0)