Last active
October 16, 2024 17:26
-
-
Save nderscore/6c5bdd04b844414c73b9e41a738556dc to your computer and use it in GitHub Desktop.
Example Tamagui Solito Text Link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { forwardRef } from 'react'; | |
import { LinkCore, LinkCoreProps } from 'solito/link'; | |
import { AnchorProps, styled, Anchor } from 'tamagui'; | |
const StyledTextLink = styled(Anchor, { | |
name: 'TextLink', | |
}); | |
export type TextLinkProps = Pick<LinkCoreProps, 'href' | 'target'> & | |
AnchorProps; | |
export const TextLink = StyledTextLink.styleable( | |
({ children, href, target, ...restProps }, ref) => { | |
return ( | |
<LinkCore | |
Component={StyledTextLink} | |
componentProps={restProps} | |
href={href} | |
ref={ref} | |
target={target} | |
> | |
{children} | |
</LinkCore> | |
); | |
} | |
); |
@nandorojo Ah, I haven't updated this in a while, but this is what my text link looks like these days...
It's very similar to your version, except with a workaround for external links on web (the addition of extra wrapper elements by Tamagui breaks the default click handler from next.js's Link component)
const StyledTextLink = styled(Text, {
name: 'TextLink',
tag: 'a',
accessibilityRole: 'link',
});
export const TextLink = StyledTextLink.styleable<
Pick<LinkProps, 'href' | 'target'>
>(
(
{ href, target, children, ...props },
ref: Ref<HTMLAnchorElement>
) => {
if (isWeb && isExternalUrl(href)) {
// special case for external links on web - render it without LinkCore
// tamagui _dsp-contents wrapper breaks next.js external link behavior
return (
<StyledTextLink
href={href}
ref={ref}
target={target}
{...componentProps}
>
{children}
</StyledTextLink>
);
}
return (
<LinkCore
Component={StyledTextLink}
href={href}
ref={ref}
target={target}
componentProps={componentProps}
>
{children}
</LinkCore>
);
}
);
const isExternalUrl = (url: {}): url is string => {
return (
typeof url === 'string' &&
(url.startsWith('http://') || url.startsWith('https://'))
);
};
Cool! Good to know. Wondering about solito/image
next
I've been using the solito image component raw with its default styles 🤐
For most styling, a wrapper view along with the fill
+ resizeMode
props usually suffices.
Yeah agreed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing! Here's an updated version: