Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LinkTo]: Add customization #869

Merged
merged 9 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/neat-vans-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web3uikit/core": patch
---

[LinkTo]: Add customization
20 changes: 19 additions & 1 deletion packages/core/src/lib/LinkTo/LinkTo.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ComponentStory, ComponentMeta } from '@storybook/react';
import LinkTo from './LinkTo';
import { color } from '@web3uikit/styles';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { Youtube } from '@web3uikit/icons';

export default {
title: '4.UI/LinkTo',
Expand All @@ -13,6 +14,7 @@ export default {
},
},
},
argTypes: { onClick: { action: 'clicked' } },
} as ComponentMeta<typeof LinkTo>;

const Template: ComponentStory<typeof LinkTo> = (args) => <LinkTo {...args} />;
Expand Down Expand Up @@ -112,7 +114,23 @@ NoIconLink.args = {
type: 'email',
iconLayout: 'none',
};

export const LinkWithNoUnderline = TemplateText.bind({});
LinkWithNoUnderline.args = {
address: 'https://www.youtube.com/c/MoralisWeb3/featured',
iconLayout: 'trailing',
text: 'Moralis Youtube',
type: 'external',
isUnderlined: false,
};
export const LinkWithCustomIcon = TemplateText.bind({});
LinkWithCustomIcon.args = {
icon: <Youtube fontSize={18} fill={color.navy40} />,
address: 'https://www.youtube.com/c/MoralisWeb3/featured',
iconLayout: 'trailing',
text: 'Moralis Youtube',
type: 'external',
isUnderlined: false,
};
export const InternalLink: ComponentStory<typeof LinkTo> = Template.bind({});
InternalLink.decorators = [
(Story) => (
Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/lib/LinkTo/LinkTo.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,41 @@ import type { LinkToProps } from './types';

// styles
type TStyleProps = Pick<LinkToProps, 'iconLayout'>;
interface LinkStyledProps {
isUnderlined: boolean;
textColor?: typeof color | string;
}

const LinkStyled = styled.a`
const LinkStyled = styled.a<LinkStyledProps>`
${resetCSS}
${fonts.text}
align-items: center;
color: ${color.navy40};
color: ${({ textColor }) => textColor || color.navy40};
display: inline-block;
font-weight: 600;
height: fit-content;
max-width: 100%;
width: fit-content;
text-decoration: ${({ isUnderlined }) =>
isUnderlined ? 'underline' : 'none'};

&:hover {
filter: brightness(0.7);
}
`;

const InternalLinkStyled = styled(Link)`
const InternalLinkStyled = styled(Link)<LinkStyledProps>`
${resetCSS}
${fonts.text}
align-items: center;
color: ${color.navy40};
color: ${({ textColor }) => textColor || color.navy40};
display: inline-block;
font-weight: 600;
max-width: 100%;
width: fit-content;
text-decoration: ${({ isUnderlined }) =>
isUnderlined ? 'underline' : 'none'};

&:hover {
filter: brightness(0.7);
}
Expand Down
45 changes: 34 additions & 11 deletions packages/core/src/lib/LinkTo/LinkTo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Link, Mail } from '@web3uikit/icons';
import { color } from '@web3uikit/styles';
import { color as colorStyle } from '@web3uikit/styles';
import { LinkToProps } from './types';
import styles from './LinkTo.styles';
import { Typography } from '../Typography';
import React from 'react';

const { InternalLinkStyled, LinkStyled, SpanStyledFlex, SpanStyledText } =
styles;
Expand All @@ -11,32 +13,45 @@ const LinkTo: React.FC<LinkToProps> = ({
iconLayout = 'leading',
text,
type = 'external',
isUnderlined = true,
iconColor,
color,
onClick,
icon,
...props
}) => {
const renderContent = () => (
<SpanStyledFlex iconLayout={iconLayout} data-testid="test-link-flex">
{iconLayout !== 'none' &&
type !== 'internal' &&
(type === 'email' ? (
{iconLayout !== 'none' && type !== 'internal' ? (
type === 'email' ? (
<Mail
title="mail icon"
titleId="linkto mail icon"
fill={color.navy40}
fill={iconColor || colorStyle.navy40}
fontSize={18}
style={{ marginTop: 'auto' }}
/>
) : icon ? (
<React.Fragment>{icon}</React.Fragment>
) : (
<Link
title="link icon"
titleId="linkto link icon"
fill={color.navy40}
fill={iconColor || colorStyle.navy40}
fontSize={18}
style={{ marginTop: 'auto' }}
/>
))}
<SpanStyledText data-testid="test-link-text">
)
) : (
icon && <React.Fragment>{icon}</React.Fragment>
)}
<Typography
data-testid="test-link-text"
color={color || colorStyle.navy40}
{...props}
>
{text || address}
</SpanStyledText>
</Typography>
</SpanStyledFlex>
);

Expand All @@ -45,12 +60,20 @@ const LinkTo: React.FC<LinkToProps> = ({
data-testid="test-link-to"
href={`${type === 'email' ? 'mailto:' : ''}${address}`}
target={`${type === 'email' ? '_self' : '_blank'}`}
{...props}
isUnderlined={isUnderlined}
textColor={color}
onClick={onClick}
>
{renderContent()}
</LinkStyled>
) : (
<InternalLinkStyled data-testid="test-link-to" to={address}>
<InternalLinkStyled
data-testid="test-link-to"
to={address}
isUnderlined={isUnderlined}
textColor={color}
onClick={onClick}
>
{renderContent()}
</InternalLinkStyled>
);
Expand Down
30 changes: 29 additions & 1 deletion packages/core/src/lib/LinkTo/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { TypographyProps } from '../Typography';

export const layoutState = ['leading', 'trailing', 'none'] as const;
export type TLayoutState = typeof layoutState[number];

export const typeState = ['email', 'external', 'internal'] as const;
export type TTypeState = typeof typeState[number];

export interface LinkToProps {
type TTextProps = Pick<
TypographyProps,
'color' | 'variant' | 'italic' | 'monospace' | 'weight'
>;

export interface LinkToProps extends TTextProps {
/**
* what is the address you are linking to
*/
Expand All @@ -24,4 +31,25 @@ export interface LinkToProps {
* set the position of the icon, or icon only
*/
iconLayout?: TLayoutState;

/**
* Is link underlined
*/
isUnderlined?: boolean;

/**
* set the icon color
*/
iconColor?: string;

/**
* A function that will be called if the link is clicked
*/

onClick?: (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => void;

/**
* set a custom icon
*/
icon?: JSX.Element;
}