simple-translate/src/content/components/TranslateButton.js

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-02-21 16:43:45 +00:00
import React from "react";
2019-02-23 20:08:26 +00:00
import browser from "webextension-polyfill";
2019-02-21 16:43:45 +00:00
import { getSettings } from "src/settings/settings";
import "../styles/TranslateButton.scss";
const calcPosition = () => {
const buttonSize = parseInt(getSettings("buttonSize"));
2019-02-23 08:42:46 +00:00
const offset = parseInt(getSettings("buttonOffset"));
2019-02-23 08:33:14 +00:00
switch (getSettings("buttonDirection")) {
case "top":
return { top: -buttonSize - offset, left: -buttonSize / 2 };
case "bottom":
return { top: offset, left: -buttonSize / 2 };
case "right":
return { top: -buttonSize / 2, left: offset };
case "left":
return { top: -buttonSize / 2, left: -buttonSize - offset };
case "topRight":
2019-02-21 16:43:45 +00:00
return { top: -buttonSize - offset, left: offset };
2019-02-23 08:33:14 +00:00
case "topLeft":
2019-02-21 16:43:45 +00:00
return { top: -buttonSize - offset, left: -buttonSize - offset };
2019-02-23 08:33:14 +00:00
case "bottomLeft":
2019-02-21 16:43:45 +00:00
return { top: offset, left: -buttonSize - offset };
2019-02-23 08:33:14 +00:00
case "bottomRight":
default:
return { top: offset, left: offset };
2019-02-21 16:43:45 +00:00
}
};
export default props => {
const { position, shouldShow } = props;
const buttonSize = parseInt(getSettings("buttonSize"));
const { top, left } = calcPosition();
2019-02-23 20:08:26 +00:00
const iconUrl = browser.extension.getURL("icons/512.png");
2019-02-21 16:43:45 +00:00
const buttonStyle = {
2019-02-23 20:08:26 +00:00
backgroundImage: `url(${iconUrl})`,
2019-02-21 16:43:45 +00:00
height: buttonSize,
width: buttonSize,
top: top + position.y,
left: left + position.x
};
return (
2019-03-21 08:28:08 +00:00
<div
2019-02-21 16:43:45 +00:00
style={buttonStyle}
className={`simple-translate-button ${shouldShow ? "isShow" : ""}`}
onClick={props.handleButtonClick}
/>
);
};