react-github-buttons
Version:
React Components for dynamic buttons for github repo's star, fork etc.
278 lines (253 loc) • 13.3 kB
JavaScript
import { number, string, oneOf, bool } from 'prop-types';
import { createElement, Component } from 'react';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;
if (!css || typeof document === 'undefined') { return; }
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
var css_248z = "/* add css styles here (optional) */\n\n.styles_root__3WDXs {\n color: #24292e;\n font-size: 11px;\n margin: 0 5px;\n font-family: \"-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol\";\n}\n\n.styles_button__3Vugn {\n color: inherit;\n font-size: 12px;\n line-height: 20px;\n padding: 3px 10px;\n border: 1px solid rgba(27,31,35,.2);\n border-radius: .25em;\n border-bottom-right-radius: 0;\n border-top-right-radius: 0;\n float: left;\n background-color: #eff3f6;\n background-image: linear-gradient(-180deg,#fafbfc,#eff3f6 90%);\n background-position: -1px -1px;\n background-repeat: repeat-x;\n background-size: 110% 110%;\n cursor: pointer;\n font-weight: 600;\n position: relative;\n user-select: none;\n vertical-align: middle;\n white-space: nowrap;\n}\n\n.styles_count__3ceOG {\n background-color: #fff;\n border: 1px solid rgba(27,31,35,.2);\n border-bottom-right-radius: 3px;\n border-left: 0;\n border-top-right-radius: 3px;\n color: #24292e;\n float: left;\n font-size: 12px;\n font-weight: 600;\n line-height: 20px;\n padding: 3px 10px;\n vertical-align: middle;\n text-decoration: none;\n}\n\n.styles_svgicon__3Mp65 {\n vertical-align: text-bottom;\n}\n";
var classes = {"root":"styles_root__3WDXs","button":"styles_button__3Vugn","count":"styles_count__3ceOG","svgicon":"styles_svgicon__3Mp65"};
styleInject(css_248z);
function ForkIcon() {
return (createElement("svg", { className: classes.svgicon, viewBox: "0 0 10 16", version: "1.1", width: "10", height: "16" },
createElement("path", { fillRule: "evenodd",
// tslint:disable-next-line:max-line-length
d: "M8 1a1.993 1.993 0 0 0-1 3.72V6L5 8 3 6V4.72A1.993 1.993 0 0 0 2 1a1.993 1.993 0 0 0-1 3.72V6.5l3 3v1.78A1.993 1.993 0 0 0 5 15a1.993 1.993 0 0 0 1-3.72V9.5l3-3V4.72A1.993 1.993 0 0 0 8 1zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm3 10c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm3-10c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z" })));
}
function StarIcon() {
return (createElement("svg", { className: classes.svgicon, viewBox: "0 0 14 16", version: "1.1", width: "14", height: "16" },
createElement("path", { fillRule: "evenodd", d: "M14 6l-4.9-.64L7 1 4.9 5.36 0 6l3.6 3.26L2.67 14 7 11.67 11.33 14l-.93-4.74L14 6z" })));
}
function StarIcon$1() {
return (createElement("svg", { className: classes.svgicon, viewBox: "0 0 16 16", version: "1.1", width: "16", height: "16" },
createElement("path", { fillRule: "evenodd",
// tslint:disable-next-line:max-line-length
d: "M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z" })));
}
function UsedByIcon() {
return (createElement("svg", { className: classes.svgicon, viewBox: "0 0 16 16", version: "1.1", width: "16", height: "16" },
createElement("path", { fillRule: "evenodd",
// tslint:disable-next-line:max-line-length
d: "M1 4.27v7.47c0 .45.3.84.75.97l6.5 1.73c.16.05.34.05.5 0l6.5-1.73c.45-.13.75-.52.75-.97V4.27c0-.45-.3-.84-.75-.97l-6.5-1.74a1.4 1.4 0 0 0-.5 0L1.75 3.3c-.45.13-.75.52-.75.97zm7 9.09l-6-1.59V5l6 1.61v6.75zM2 4l2.5-.67L11 5.06l-2.5.67L2 4zm13 7.77l-6 1.59V6.61l2-.55V8.5l2-.53V5.53L15 5v6.77zm-2-7.24L6.5 2.8l2-.53L15 4l-2 .53z" })));
}
function SponsorIcon() {
return (createElement("svg", { className: classes.svgicon, viewBox: "0 0 12 16", version: "1.1", width: "12", height: "16", style: { fill: '#ea4aaa' } },
createElement("path", { fillRule: "evenodd",
// tslint:disable-next-line:max-line-length
d: "M9 2c-.97 0-1.69.42-2.2 1-.51.58-.78.92-.8 1-.02-.08-.28-.42-.8-1-.52-.58-1.17-1-2.2-1-1.632.086-2.954 1.333-3 3 0 .52.09 1.52.67 2.67C1.25 8.82 3.01 10.61 6 13c2.98-2.39 4.77-4.17 5.34-5.33C11.91 6.51 12 5.5 12 5c-.047-1.69-1.342-2.913-3-3z" })));
}
/**
* @function GithubButton
*/
function getDataForVariant(variant, count, owner, repo) {
var title;
var btnTitle;
var label;
var countUrl;
if (variant === 'star') {
title = 'Star';
btnTitle = "Star " + owner + "/" + repo;
label = count + " users starred this repository";
countUrl = "https://github.com/" + owner + "/" + repo + "/stargazers";
return { title: title, btnTitle: btnTitle, label: label, countUrl: countUrl, Icon: StarIcon };
}
else if (variant === 'fork') {
title = 'Fork';
btnTitle = "Fork your own copy of " + owner + "/" + repo + " to your account";
label = count + " users forked this repository";
countUrl = "https://github.com/" + owner + "/" + repo + "/network/members";
return { title: title, btnTitle: btnTitle, label: label, countUrl: countUrl, Icon: ForkIcon };
}
else if (variant === 'watch') {
title = 'Watch';
btnTitle = '';
label = count + " users are watching this repository";
countUrl = "https://github.com/" + owner + "/" + repo + "/watchers";
return { title: title, btnTitle: btnTitle, label: label, countUrl: countUrl, Icon: StarIcon$1 };
}
else if (variant === 'usedby') {
title = 'Used by';
btnTitle = '';
label = count + " repositories depend on this package";
countUrl = "https://github.com/" + owner + "/" + repo + "/network/dependents";
return { title: title, btnTitle: btnTitle, label: label, countUrl: countUrl, Icon: UsedByIcon };
}
else if (variant === 'sponsor') {
title = 'Sponsor';
btnTitle = "Sponsor " + owner + "/" + repo;
label = '';
countUrl = '';
return { title: title, btnTitle: btnTitle, label: label, countUrl: countUrl, Icon: SponsorIcon };
}
throw new Error('Invalid Variant, supply one of [star, fork, watch, usedby, sponsor]');
}
function GithubButton(props) {
var owner = props.owner, repo = props.repo, variant = props.variant, count = props.count, showCount = props.showCount;
var _a = getDataForVariant(variant, count, owner, repo), title = _a.title, btnTitle = _a.btnTitle, label = _a.label, countUrl = _a.countUrl, Icon = _a.Icon;
var inlineStyle = showCount ? {} : { borderRadius: '.25em' };
return (createElement("div", { className: classes.root },
createElement("button", { title: btnTitle, className: classes.button, style: inlineStyle },
createElement(Icon, null), " " + title),
showCount && createElement("a", { className: classes.count, href: countUrl, "aria-label": label, target: "_blank", rel: "noreferrer noopener" }, count)));
}
GithubButton.propTypes = {
count: number,
owner: string.isRequired,
repo: string.isRequired,
variant: oneOf(['star', 'fork', 'watch', 'usedby', 'sponsor']),
showCount: bool,
};
GithubButton.defaultProps = {
count: 0,
showCount: true,
};
var Fork = /** @class */ (function (_super) {
__extends(Fork, _super);
function Fork(props) {
var _this = _super.call(this, props) || this;
_this.state = {
forks_count: 0,
};
return _this;
}
Fork.prototype.componentDidMount = function () {
var _this = this;
var _a = this.props, owner = _a.owner, repo = _a.repo;
fetch("https://api.github.com/repos/" + owner + "/" + repo)
.then(function (res) { return res.json(); })
.then(function (res) {
_this.setState({ forks_count: res.forks_count });
});
};
Fork.prototype.render = function () {
// const { owner, repo } = this.props;
var forks_count = this.state.forks_count;
return createElement(GithubButton, __assign({ variant: "fork", count: forks_count }, this.props));
};
Fork.propTypes = {
owner: string.isRequired,
repo: string.isRequired,
};
return Fork;
}(Component));
function Sponsor(props) {
return createElement(GithubButton, __assign({ variant: "sponsor", showCount: false }, props));
}
Sponsor.propTypes = {
owner: string.isRequired,
repo: string.isRequired,
};
var Star = /** @class */ (function (_super) {
__extends(Star, _super);
function Star(props) {
var _this = _super.call(this, props) || this;
_this.state = {
stargazers_count: 0,
};
return _this;
}
Star.prototype.componentDidMount = function () {
var _this = this;
var _a = this.props, owner = _a.owner, repo = _a.repo;
fetch("https://api.github.com/repos/" + owner + "/" + repo).then(function (res) { return res.json(); }).then(function (res) {
_this.setState({ stargazers_count: res.stargazers_count });
});
};
Star.prototype.render = function () {
var stargazers_count = this.state.stargazers_count;
return createElement(GithubButton, __assign({ variant: "star", count: stargazers_count }, this.props));
};
Star.propTypes = {
owner: string.isRequired,
repo: string.isRequired,
};
return Star;
}(Component));
function UsedBy(props) {
return createElement(GithubButton, __assign({ variant: "usedby" }, props));
}
UsedBy.propTypes = {
count: number.isRequired,
owner: string.isRequired,
repo: string.isRequired,
};
var Watch = /** @class */ (function (_super) {
__extends(Watch, _super);
function Watch(props) {
var _this = _super.call(this, props) || this;
_this.state = {
watchers_count: 0,
};
return _this;
}
Watch.prototype.componentDidMount = function () {
var _this = this;
var _a = this.props, owner = _a.owner, repo = _a.repo;
fetch("https://api.github.com/repos/" + owner + "/" + repo).then(function (res) { return res.json(); }).then(function (res) {
_this.setState({ watchers_count: res.watchers_count });
});
};
Watch.prototype.render = function () {
// const { owner, repo } = this.props;
var watchers_count = this.state.watchers_count;
return createElement(GithubButton, __assign({ variant: "watch", count: watchers_count }, this.props));
};
Watch.propTypes = {
owner: string.isRequired,
repo: string.isRequired,
};
return Watch;
}(Component));
export { Fork, Sponsor, Star, UsedBy, Watch };
//# sourceMappingURL=index.es.js.map