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

refactor(theme): to make the link underline style configurable via a prop #253

Merged
merged 1 commit into from
Jan 29, 2020
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
3 changes: 1 addition & 2 deletions packages/gatsby-theme-docs/src/components/beta-flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const getStyles = props => {
box-shadow: ${designSystem.tokens.shadowForBetaFlag};
color: ${designSystem.colors.light.textInfo} !important;
font-size: ${designSystem.typography.fontSizes.small};
text-decoration: none !important;

:active,
:focus,
Expand All @@ -39,7 +38,7 @@ const getStyles = props => {
const BetaFlag = props => {
if (props.href) {
return (
<Link href={props.href} css={getStyles(props)}>
<Link href={props.href} noUnderline={true} css={getStyles(props)}>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: the prop is called noUnderline on purpose, because the default behavior should be with underline and you can decide to explicitly opt-out.

{'BETA'}
</Link>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import styled from '@emotion/styled';
import React from 'react';
import { css } from '@emotion/core';
import { designSystem } from '@commercetools-docs/ui-kit';
import Link from './link';

const GlobalNavigationLink = styled(Link)`
const linkStyles = css`
font-size: ${designSystem.typography.fontSizes.extraSmall};
line-height: 1.75;
color: ${designSystem.colors.light.textPrimary} !important;
text-decoration: none !important;

svg {
* {
Expand All @@ -25,4 +25,8 @@ const GlobalNavigationLink = styled(Link)`
}
`;

const GlobalNavigationLink = props => (
<Link {...props} css={linkStyles} noUnderline={true} />
);

export default GlobalNavigationLink;
23 changes: 19 additions & 4 deletions packages/gatsby-theme-docs/src/components/link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';
import { Location } from '@reach/router';
import { Link as GatsbyLink, withPrefix } from 'gatsby';
import styled from '@emotion/styled';
Expand All @@ -20,6 +21,10 @@ const withoutPrefix = (value, pathPrefix) =>

const trimTrailingSlash = url => url.replace(/(\/?)$/, '');

const getStylesFromProps = ({ noUnderline }) => css`
text-decoration: ${noUnderline ? 'none' : 'underline'};
`;

const AnchorLink = styled(StyledLink)``;
const InternalSiteLink = styled(StyledLink)``;
const GatsbyRouterLink = StyledLink.withComponent(GatsbyLink);
Expand Down Expand Up @@ -91,7 +96,7 @@ export const ExternalSiteLink = props => (
*/
const PureLink = extendedProps => {
const siteData = useSiteData();
const { location, ...props } = extendedProps;
const { location, noUnderline, ...props } = extendedProps;
// For image links, return the link as-is.
if (props.href.startsWith(withPrefix('/static'))) {
return <a {...props} role="image-link" />;
Expand Down Expand Up @@ -127,19 +132,25 @@ const PureLink = extendedProps => {
React.cloneElement(props.children, {
children: (
<InlineLink>
<span>{props.children.props.children}</span>
<span css={getStylesFromProps({ noUnderline })}>
{props.children.props.children}
</span>
<ExternalLinkIcon size="small" />
</InlineLink>
),
})
) : (
<InlineLink>
<span>{props.children}</span>
<span css={getStylesFromProps({ noUnderline })}>{props.children}</span>
<ExternalLinkIcon size="small" />
</InlineLink>
);
return (
<ExternalSiteLink {...props} role="external-link">
<ExternalSiteLink
{...props}
role="external-link"
css={getStylesFromProps({ noUnderline })}
>
{linkWithIcon}
</ExternalSiteLink>
);
Expand All @@ -155,6 +166,7 @@ const PureLink = extendedProps => {
role="anchor-link"
href={trimTrailingSlash(hrefObject.hash)}
className={props.className}
css={getStylesFromProps({ noUnderline })}
>
{props.children}
</AnchorLink>
Expand All @@ -176,6 +188,7 @@ const PureLink = extendedProps => {
role="gatsby-link"
to={trimTrailingSlash(hrefObject.pathname) + hrefObject.hash}
className={props.className}
css={getStylesFromProps({ noUnderline })}
>
{props.children}
</GatsbyRouterLink>
Expand All @@ -196,6 +209,7 @@ const PureLink = extendedProps => {
role="internal-link"
href={internalHref}
className={props.className}
css={getStylesFromProps({ noUnderline })}
>
{props.children}
</InternalSiteLink>
Expand All @@ -205,6 +219,7 @@ PureLink.propTypes = {
href: PropTypes.string.isRequired,
target: PropTypes.string,
className: PropTypes.string,
noUnderline: PropTypes.bool,
children: PropTypes.node,
// from @react/router
location: PropTypes.object.isRequired,
Expand Down
1 change: 0 additions & 1 deletion packages/ui-kit/src/components/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import styled from '@emotion/styled';
import { colors } from '../design-system';

const Link = styled.a`
text-decoration: underline;
&,
> code {
color: ${colors.light.link};
Expand Down