-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextLinkAction.jsx
108 lines (100 loc) · 3.5 KB
/
TextLinkAction.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* TEAM: frontend_infra
* @flow strict
*/
import * as React from "react";
import {css, StyleSheet} from "aphrodite";
import {fontWeights, typeScale} from "./styles/typography";
import linkStyles, {type LinkStyle} from "./styles/linkStyles";
import ThemeNameContext, {type Theme} from "./context/ThemeNameContext";
import {sharedStyles} from "./button/styles";
import TextLinkContext from "./TextLinkContext";
/* eslint-disable react/prefer-stateless-function */
type Props = {|
/** TextLinkAction tags can only wrap strings. If you find this restrictive, contact @theseus. The thought is we want to avoid the temptation for folks to wrap entire apps or components in Link tags, since that usually means a different component should be built. */
+children: string | React.ChildrenArray<string>,
+onClick: (e: SyntheticInputEvent<HTMLButtonElement>) => void,
/** One of three standard link color schemes. */
+linkStyle: LinkStyle,
/** The size of the link which is is a subset of TypeScale's sizes. */
+scale?: "base" | "subtext" | "title",
/** The boldness of the link. */
+weight?: "regular" | "bold",
/** Select a custom display property depending on how you intend to use the link. */
+display:
| "inline"
| "inline-block"
| "flex"
| "block"
| "inline-flex"
| "none",
+disabled: boolean,
|};
/**
* @short In general, try and use a TextLink, which takes an href not an onClick. TextLinks are more accessible (hovering over them shows the link preview in the browser), and don't require JavaScript. Sometimes, however, you need to have something that looks and feels like a TextLink but takes an onClick. This is rendered as a button, but can be nested in <Text> like a TextLink
* @brandStatus V2
* @status Stable
* @category Interaction
* @extends React.Component */
class TextLinkAction extends React.PureComponent<Props> {
static defaultProps = {
linkStyle: "default",
display: "inline-block",
// onClick is noop by default
onClick: () => {},
disabled: false,
};
static contextType = ThemeNameContext;
context: Theme;
render() {
const {children, onClick, linkStyle, disabled, display} = this.props;
return (
<TextLinkContext.Consumer>
{parentStyles => {
let {scale, weight} = this.props;
// If we are not provided with a style prop and we will use the one from context
scale = scale || parentStyles.scale;
weight = weight || parentStyles.weight;
return (
// eslint-disable-next-line react/forbid-elements
<button
type="button"
className={css(
sharedStyles.resetButton,
styles.link,
styles.standardOverride,
linkStyles(this.context)[linkStyle]
)}
style={{
display,
weight: fontWeights[weight],
...typeScale[scale],
}}
onClick={onClick}
disabled={disabled}
>
{children}
</button>
);
}}
</TextLinkContext.Consumer>
);
}
}
export default TextLinkAction;
const styles = StyleSheet.create({
link: {
background: "none",
color: "inherit",
border: "none",
padding: "0",
font: "inherit",
cursor: "pointer",
},
standardOverride: {
// override for our questionable OOCSS bottom margins on all semantic type tags
marginBottom: 0,
marginTop: 0,
cursor: "pointer",
},
});