Skip to content

Commit 4658f44

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

File tree

5 files changed

+77
-27
lines changed

5 files changed

+77
-27
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,37 @@ 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 <Link variant="inline-small" renderIcon={<IconUserLine />} href="https://instructure.github.io/instructure-ui/">inline-small</Link> link variant
41+
</Text>
42+
</div>
43+
44+
<br></br>
45+
<div>
46+
If the link is standalone (not in a text), use the <code>standalone</code> <Link display="block" variant="standalone" renderIcon={<IconUserLine />} href="https://instructure.github.io/instructure-ui/">standalone</Link>
47+
</div>
48+
49+
<br></br>
50+
<div>
51+
If the link is standalone (not in a text), but smaller, use the <code>standalone-small</code> <Link display="block" variant="standalone-small" renderIcon={<IconUserLine />} href="https://instructure.github.io/instructure-ui/">standalone-small</Link>
52+
</div>
53+
</div>
54+
```
55+
2556
### Adding margin
2657

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

110+
NOTE: if you want the icon to have the same `font-size` as the link, do not specify its `size`!
111+
79112
```js
80113
---
81114
type: example

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ class Link extends Component<LinkProps, LinkState> {
252252
tabIndex={tabIndex}
253253
css={this.props.styles?.link}
254254
>
255-
{renderIcon && iconPlacement === 'start' && this.renderIcon()}
255+
{renderIcon && iconPlacement === 'start' ? this.renderIcon() : null}
256256
{children}
257-
{renderIcon && iconPlacement === 'end' && this.renderIcon()}
257+
{renderIcon && iconPlacement === 'end' ? this.renderIcon() : null}
258258
</View>
259259
)
260260
}

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 = {

packages/ui-text/src/Text/index.tsx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,12 @@ class Text extends Component<TextProps> {
5353
children: null
5454
} as const
5555

56-
checkProps() {
57-
const { variant, size, lineHeight, weight, fontStyle } = this.props
58-
if (variant) {
59-
if (size) {
60-
console.warn("[Text]: Don't use 'size' with 'variant' ")
61-
}
62-
if (lineHeight) {
63-
console.warn("[Text]: Don't use 'lineHeight' with 'variant' ")
64-
}
65-
if (weight) {
66-
console.warn("[Text]: Don't use 'weight' with 'variant' ")
67-
}
68-
if (fontStyle) {
69-
console.warn("[Text]: Don't use 'fontStyle' with 'variant' ")
70-
}
71-
}
72-
}
73-
7456
componentDidMount() {
7557
this.props.makeStyles?.()
76-
this.checkProps()
7758
}
7859

7960
componentDidUpdate() {
8061
this.props.makeStyles?.()
81-
this.checkProps()
8262
}
8363

8464
render() {

0 commit comments

Comments
 (0)