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

convert link component to typescript #1011

Merged
merged 5 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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/yellow-mice-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Migrate `Link` to TypeScript
56 changes: 0 additions & 56 deletions src/Link.js

This file was deleted.

76 changes: 76 additions & 0 deletions src/Link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import {system} from 'styled-system'
import {COMMON, TYPOGRAPHY, get, SystemCommonProps, SystemTypographyProps} from './constants'
import sx, {SxProp} from './sx'
import theme from './theme'
import {ComponentProps} from './utils/types'

type StyledLinkProps = {
hoverColor?: string
muted?: boolean
underline?: boolean
} & SystemCommonProps &
SxProp &
SystemTypographyProps

const buttonStyles = `
display: inline-block;
padding: 0;
font-size: inherit;
white-space: nowrap;
cursor: pointer;
user-select: none;
background-color: transparent;
border: 0;
appearance: none;
`

const hoverColor = system({
hoverColor: {
property: 'color',
scale: 'colors'
}
})

const Link = styled.a<StyledLinkProps>`
color: ${props => (props.muted ? get('colors.gray.6')(props) : get('colors.blue.5')(props))};
text-decoration: ${props => (props.underline ? 'underline' : 'none')};
&:hover {
text-decoration: ${props => (props.muted ? 'none' : 'underline')};
${props => (props.hoverColor ? hoverColor : props.muted ? `color: ${get('colors.blue.5')(props)}` : '')};
}
&:is(button) {
display: inline-block;
padding: 0;
font-size: inherit;
white-space: nowrap;
cursor: pointer;
user-select: none;
background-color: transparent;
border: 0;
appearance: none;
}
${TYPOGRAPHY};
${COMMON};
${sx};
`

Link.defaultProps = {
theme
}

Link.propTypes = {
hoverColor: PropTypes.string,
href: PropTypes.string,
muted: PropTypes.bool,
theme: PropTypes.object,
underline: PropTypes.bool,
...TYPOGRAPHY.propTypes,
...COMMON.propTypes,
...sx.propTypes
}

export type LinkProps = ComponentProps<typeof Link>
export default Link
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

exports[`Link applies button styles when rendering a button element 1`] = `
.c0 {
color: #0366d6;
-webkit-text-decoration: none;
text-decoration: none;
}

.c0:hover {
-webkit-text-decoration: underline;
text-decoration: underline;
}

.c0:is(button) {
display: inline-block;
padding: 0;
font-size: inherit;
Expand All @@ -18,59 +27,85 @@ exports[`Link applies button styles when rendering a button element 1`] = `
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
color: #0366d6;
}

.c0:hover {
-webkit-text-decoration: underline;
text-decoration: underline;
}

<button
className="c0"
color="blue.5"
/>
`;

exports[`Link passes href down to link element 1`] = `
.c0 {
color: #0366d6;
-webkit-text-decoration: none;
text-decoration: none;
color: #0366d6;
}

.c0:hover {
-webkit-text-decoration: underline;
text-decoration: underline;
}

.c0:is(button) {
display: inline-block;
padding: 0;
font-size: inherit;
white-space: nowrap;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}

<a
className="c0"
color="blue.5"
href="https://github.com"
/>
`;

exports[`Link renders consistently 1`] = `
.c0 {
color: #0366d6;
-webkit-text-decoration: none;
text-decoration: none;
color: #0366d6;
}

.c0:hover {
-webkit-text-decoration: underline;
text-decoration: underline;
}

.c0:is(button) {
display: inline-block;
padding: 0;
font-size: inherit;
white-space: nowrap;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}

<a
className="c0"
color="blue.5"
/>
`;

exports[`Link respectes the "color" prop when "muted" prop is also passed 1`] = `
.c0 {
color: #586069;
-webkit-text-decoration: none;
text-decoration: none;
color: #1b1f23;
Expand All @@ -82,6 +117,23 @@ exports[`Link respectes the "color" prop when "muted" prop is also passed 1`] =
color: #0366d6;
}

.c0:is(button) {
display: inline-block;
padding: 0;
font-size: inherit;
white-space: nowrap;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}

<a
className="c0"
color="black"
Expand All @@ -91,9 +143,9 @@ exports[`Link respectes the "color" prop when "muted" prop is also passed 1`] =

exports[`Link respectes the "muted" prop 1`] = `
.c0 {
color: #586069;
-webkit-text-decoration: none;
text-decoration: none;
color: #586069;
}

.c0:hover {
Expand All @@ -102,18 +154,34 @@ exports[`Link respectes the "muted" prop 1`] = `
color: #0366d6;
}

.c0:is(button) {
display: inline-block;
padding: 0;
font-size: inherit;
white-space: nowrap;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}

<a
className="c0"
color="gray.6"
muted={true}
/>
`;

exports[`Link respects hoverColor prop 1`] = `
.c0 {
color: #0366d6;
-webkit-text-decoration: none;
text-decoration: none;
color: #0366d6;
}

.c0:hover {
Expand All @@ -122,8 +190,24 @@ exports[`Link respects hoverColor prop 1`] = `
color: #2188ff;
}

.c0:is(button) {
display: inline-block;
padding: 0;
font-size: inherit;
white-space: nowrap;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}

<a
className="c0"
color="blue.5"
/>
`;
20 changes: 18 additions & 2 deletions src/__tests__/__snapshots__/SideNav.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

exports[`SideNav SideNav.Link renders consistently 1`] = `
.c0 {
color: #0366d6;
-webkit-text-decoration: none;
text-decoration: none;
color: #0366d6;
position: relative;
display: block;
width: 100%;
Expand All @@ -17,6 +17,23 @@ exports[`SideNav SideNav.Link renders consistently 1`] = `
text-decoration: underline;
}

.c0:is(button) {
display: inline-block;
padding: 0;
font-size: inherit;
white-space: nowrap;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}

.c0 > .c1 {
border-bottom: none;
}
Expand Down Expand Up @@ -94,7 +111,6 @@ exports[`SideNav SideNav.Link renders consistently 1`] = `

<a
className="c0"
color="blue.5"
/>
`;

Expand Down