Skip to content

Commit 2bceaf4

Browse files
committed
feat(ui-link): add variants to <Link>
INSTUI-4506
1 parent 52dbd71 commit 2bceaf4

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

packages/ui-link/src/Link/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,41 @@ type: example
2222
</View>
2323
```
2424

25+
### Variant
26+
27+
In order to make it easy to get the most commonly used links, we have the variant prop. It will set all the necessary styles (fontSize, lineHeight, and textDecoration).
28+
29+
```js
30+
---
31+
type: example
32+
---
33+
<div>
34+
<div>
35+
In a line of text you should use the <Link variant="inline" renderIcon={<IconUserLine />} href="https://instructure.github.io/instructure-ui/">inline</Link> link variant.
36+
</div>
37+
38+
<br></br>
39+
<div>
40+
<Text variant="contentSmall">In a line of text, where the text is smaller, use the
41+
<Link variant="inline-small" renderIcon={<IconUserLine />} href="https://instructure.github.io/instructure-ui/">inline-small</Link> link variant
42+
</Text>
43+
</div>
44+
45+
<br></br>
46+
<div>
47+
If the link is standalone (not in a text), use the `standalone`
48+
49+
<Link display="block" variant="standalone" renderIcon={<IconUserLine />} href="https://instructure.github.io/instructure-ui/">standalone</Link>
50+
</div>
51+
52+
<br></br>
53+
<div>
54+
If the link is standalone (not in a text), but smaller, use the `standalone-small`
55+
<Link display="block" variant="standalone-small" renderIcon={<IconUserLine />} href="https://instructure.github.io/instructure-ui/">standalone-small</Link>
56+
</div>
57+
</div>
58+
```
59+
2560
### Adding margin
2661

2762
Use the `margin` prop to add space to the left or right of the Link. Because
@@ -76,6 +111,8 @@ Use the `renderIcon` property to put an [icon](#icons) inside a Link. To positio
76111
icon _after_ the link text, change the `iconPlacement` property to `end`. You can also
77112
render a Link with just an icon. Don't forget to add text for screen readers, though.
78113

114+
NOTE: if you want the icon to have the same `font-size` as the link, do not specify its `size`!
115+
79116
```js
80117
---
81118
type: example

packages/ui-link/src/Link/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,23 @@ class Link extends Component<LinkProps, LinkState> {
7979
}
8080
ref: Element | null = null
8181

82+
checkProps = () => {
83+
const { variant, isWithinText } = this.props
84+
if (variant && isWithinText) {
85+
console.warn(
86+
'<Link> when `variant` is used, `isWithinText` is ignored. Please remove it!'
87+
)
88+
}
89+
}
90+
8291
componentDidMount() {
8392
this.props.makeStyles?.(this.makeStyleProps())
93+
this.checkProps()
8494
}
8595

8696
componentDidUpdate() {
8797
this.props.makeStyles?.(this.makeStyleProps())
98+
this.checkProps()
8899
}
89100

90101
makeStyleProps = (): LinkStyleProps => {
@@ -252,9 +263,9 @@ class Link extends Component<LinkProps, LinkState> {
252263
tabIndex={tabIndex}
253264
css={this.props.styles?.link}
254265
>
255-
{renderIcon && iconPlacement === 'start' && this.renderIcon()}
266+
{renderIcon && iconPlacement === 'start' ? this.renderIcon() : null}
256267
{children}
257-
{renderIcon && iconPlacement === 'end' && this.renderIcon()}
268+
{renderIcon && iconPlacement === 'end' ? this.renderIcon() : null}
258269
</View>
259270
)
260271
}

packages/ui-link/src/Link/props.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ type LinkOwnProps = {
129129
* Fires when the Link is hovered
130130
*/
131131
onMouseEnter?: (event: React.MouseEvent<ViewOwnProps>) => void
132+
133+
/**
134+
* Sets pre-defined values for the component to achieve specific roles for the component
135+
*/
136+
variant?: 'inline' | 'inline-small' | 'standalone' | 'standalone-small'
132137
}
133138

134139
export type LinkStyleProps = {
@@ -174,7 +179,13 @@ const propTypes: PropValidators<PropKeys> = {
174179
onBlur: PropTypes.func,
175180
onClick: PropTypes.func,
176181
onFocus: PropTypes.func,
177-
onMouseEnter: PropTypes.func
182+
onMouseEnter: PropTypes.func,
183+
variant: PropTypes.oneOf([
184+
'inline',
185+
'inline-small',
186+
'standalone',
187+
'standalone-small'
188+
])
178189
}
179190

180191
const allowedProps: AllowedPropKeys = [
@@ -194,7 +205,8 @@ const allowedProps: AllowedPropKeys = [
194205
'onBlur',
195206
'onClick',
196207
'onFocus',
197-
'onMouseEnter'
208+
'onMouseEnter',
209+
'variant'
198210
]
199211

200212
export type { LinkProps, LinkState, LinkStyle }

packages/ui-link/src/Link/styles.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,33 @@ const generateStyle = (
4040
props: LinkProps,
4141
state: LinkStyleProps
4242
): LinkStyle => {
43-
const { isWithinText, renderIcon, iconPlacement, color } = props
43+
const { isWithinText, renderIcon, iconPlacement, color, variant } = props
4444
const { containsTruncateText, hasVisibleChildren } = state
4545
const inverseStyle = color === 'link-inverse'
4646

47+
const variantStyles = {
48+
inline: {
49+
fontSize: '1rem',
50+
lineHeight: '1.5rem',
51+
textDecoration: 'underline'
52+
},
53+
'inline-small': {
54+
fontSize: '0.875rem',
55+
lineHeight: '1.3125rem',
56+
textDecoration: 'underline'
57+
},
58+
standalone: {
59+
fontSize: '1rem',
60+
lineHeight: '1.5rem',
61+
textDecoration: 'none'
62+
},
63+
'standalone-small': {
64+
fontSize: '0.875rem',
65+
lineHeight: '1.3125rem',
66+
textDecoration: 'none'
67+
}
68+
}
69+
4770
const baseStyles = {
4871
boxSizing: 'border-box',
4972
fontFamily: componentTheme.fontFamily,
@@ -96,7 +119,8 @@ const generateStyle = (
96119
textDecoration: isWithinText
97120
? componentTheme.hoverTextDecorationWithinText
98121
: componentTheme.hoverTextDecorationOutsideText
99-
}
122+
},
123+
...(variant ? variantStyles[variant] : {})
100124
}
101125

102126
const buttonStyle = {
@@ -108,7 +132,8 @@ const generateStyle = (
108132
fontSize: '1em',
109133
margin: 0,
110134
padding: 0,
111-
textAlign: 'inherit'
135+
textAlign: 'inherit',
136+
...(variant ? variantStyles[variant] : {})
112137
}
113138

114139
const inverseStyles = {

0 commit comments

Comments
 (0)