This repository was archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.babel.js
More file actions
143 lines (124 loc) · 3.56 KB
/
index.babel.js
File metadata and controls
143 lines (124 loc) · 3.56 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React from 'react';
import {getSpec, getHref} from 'link-to-inbox';
import template from 'lodash-template';
import styled from 'styled-components';
class LinkToInbox extends React.Component {
render() {
let {template: templ, style, subject, sender, email, tag, guessUnknownDomain, className} = this.props;
if (!email) {
throw new Error(`email is required`);
}
if (!email.includes('@')) {
throw new Error(`Invalid email address ${email}`);
}
tag = tag || 'a';
templ = templ || 'Open in <%- name %>';
const filter = {subject, sender};
let spec = getSpec(email, filter, true);
let href = getHref(email, filter, true);
if (!spec) {
if (!guessUnknownDomain) {
return null;
}
const domain = email.split('@')[1];
spec = {
name: domain,
protocol: 'https',
domain,
path: ''
};
}
if (!href) {
href = spec.protocol + '://' + spec.domain + spec.path;
}
const msg = template(templ)({subject, email, sender, href, ...spec});
function clickHandler() {
window.open(href);
}
switch (tag) {
case 'a':
return (<a href={href} className={className} style={style}>{msg}</a>);
case 'button':
return (<button onClick={clickHandler} className={className} style={style}>{msg}</button>);
case 'input':
return (<input type="button" onClick={clickHandler} value={msg} className={className} style={style}/>);
default:
console.error(`unrecognized tag ${tag}`);
throw new Error(`unrecognized tag ${tag}`);
}
}
}
LinkToInbox.propTypes = {
template: React.PropTypes.string,
subject: React.PropTypes.string,
sender: React.PropTypes.string,
tag: React.PropTypes.oneOf(['a', 'button', 'input']),
guessUnknownDomain: React.PropTypes.boolean,
email: (props, propName) => {
if (!props[propName]) {
return new Error(`email is required`);
}
if (!props[propName].includes('@')) {
return new Error(`email must be a valid email address, got ${props[propName]}`);
}
},
style: React.PropTypes.string,
className: React.PropTypes.string
};
export default LinkToInbox;
export const InboxButton = props => <LinkToInbox tag="button" {...props}/>;
export const InboxInput = props => <LinkToInbox tag="input" {...props}/>;
const commonStyles = `
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
padding: 10px 24px;
cursor: pointer;
`;
const buttonStyles = `
${commonStyles}
border: 1px solid #9768BA;
background-color:#9768BA;
color:white;
border-radius: 20px;
transition: all .05s ease-in-out;
box-sizing: border-box;
letter-spacing: .01em;
text-transform: capitalize;
&:focus {
background-color:#8751AF;
border-color: #8751AF;
box-shadow: 0 0 0 1px #fff, 0 0 0 3px rgba(80, 40, 120, 0.75);
outline: none;
}
&:hover {
background-color:#8751AF;
border-color: #8751AF;
}
&:active {
box-shadow: 0 0 0 1px #fff, 0 0 0 3px rgba(80, 40, 120, 0.75);
border-color: #8751AF;
background-image: linear-gradient(rgba(0,0,0,0),rgba(0,0,0,0.15));
/*outline: none;*/
}`;
const linkStyles = `
${commonStyles}
text-decoration: none;
&:hover {
text-decoration: underline;
color:#8751AF;
}
&:focus {
text-decoration: underline;
color:#8751AF;
outline: none;
}
`;
export const StyledInboxLink = styled(LinkToInbox)`
${linkStyles}
`;
export const StyledInboxButton = styled(InboxButton)`
${buttonStyles}
`;
export const StyledInboxInput = styled(InboxInput)`
${buttonStyles}
`;