Broken relative links when using <Routes>
#10404
-
My I have a custom component called import { Tab, type TabProps } from 'ariakit/tab';
import cn from 'classnames';
import { useLayoutEffect, useRef } from 'react';
import { useHref, useLinkClickHandler } from 'react-router-dom';
import { useRect } from '@hooks/useRect';
import { useAnimatedTabs } from './context/useAnimatedTabs';
import TabStyles from './TabLink.module.css';
type TabLinkProps = TabProps<'a'> & {
to: string;
};
export function TabLink({ to, ...props }: TabLinkProps) {
const href = useHref(to);
const onClick = useLinkClickHandler(to);
// get the state and style changing function from context
const { state, setActiveRect } = useAnimatedTabs();
// measure the size of our element, only listen to rect if active
const ref = useRef<HTMLAnchorElement>(null);
const isSelected = !!state.selectedId && ref.current?.href.includes(state.selectedId);
const rect = useRect(ref, { observe: isSelected });
// callback to set styles whenever we're active
useLayoutEffect(() => {
if (isSelected) {
setActiveRect(rect);
}
}, [isSelected, rect, setActiveRect]);
return (
<Tab
ref={ref}
as="a"
href={href}
onClick={onClick}
className={cn(TabStyles.TabLink, { [TabStyles.TabLink___isActive]: isSelected })}
{...props}
/>
);
} I would expect |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
cc @brophdawg11 since you've been helpful sorting out Routes bugs recently I can provide a complete reproduction if necessary, just wanted to post an initial discussion |
Beta Was this translation helpful? Give feedback.
-
Here's my reproduction: https://stackblitz.com/edit/react-ts-7xqchk?file=HostsRoot.tsx I guess I have two bugs, one that the Link should either crash or warn if I pass a non-string to it or it should call the toString method of it The other is that when I construct the |
Beta Was this translation helpful? Give feedback.
Looks like your example works as expected using strings and
6.11.1-pre.0
: https://stackblitz.com/edit/react-ts-dknpd3?file=App.tsx