UNPKG

3.75 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = triangle;
5var _getValueAndUnit = _interopRequireDefault(require("../helpers/getValueAndUnit"));
6var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
8function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
9var getBorderWidth = function getBorderWidth(pointingDirection, height, width) {
10 var fullWidth = "" + width[0] + (width[1] || '');
11 var halfWidth = "" + width[0] / 2 + (width[1] || '');
12 var fullHeight = "" + height[0] + (height[1] || '');
13 var halfHeight = "" + height[0] / 2 + (height[1] || '');
14 switch (pointingDirection) {
15 case 'top':
16 return "0 " + halfWidth + " " + fullHeight + " " + halfWidth;
17 case 'topLeft':
18 return fullWidth + " " + fullHeight + " 0 0";
19 case 'left':
20 return halfHeight + " " + fullWidth + " " + halfHeight + " 0";
21 case 'bottomLeft':
22 return fullWidth + " 0 0 " + fullHeight;
23 case 'bottom':
24 return fullHeight + " " + halfWidth + " 0 " + halfWidth;
25 case 'bottomRight':
26 return "0 0 " + fullWidth + " " + fullHeight;
27 case 'right':
28 return halfHeight + " 0 " + halfHeight + " " + fullWidth;
29 case 'topRight':
30 default:
31 return "0 " + fullWidth + " " + fullHeight + " 0";
32 }
33};
34var getBorderColor = function getBorderColor(pointingDirection, foregroundColor) {
35 switch (pointingDirection) {
36 case 'top':
37 case 'bottomRight':
38 return {
39 borderBottomColor: foregroundColor
40 };
41 case 'right':
42 case 'bottomLeft':
43 return {
44 borderLeftColor: foregroundColor
45 };
46 case 'bottom':
47 case 'topLeft':
48 return {
49 borderTopColor: foregroundColor
50 };
51 case 'left':
52 case 'topRight':
53 return {
54 borderRightColor: foregroundColor
55 };
56 default:
57 throw new _errors["default"](59);
58 }
59};
60
61/**
62 * CSS to represent triangle with any pointing direction with an optional background color.
63 *
64 * @example
65 * // Styles as object usage
66 *
67 * const styles = {
68 * ...triangle({ pointingDirection: 'right', width: '100px', height: '100px', foregroundColor: 'red' })
69 * }
70 *
71 *
72 * // styled-components usage
73 * const div = styled.div`
74 * ${triangle({ pointingDirection: 'right', width: '100px', height: '100px', foregroundColor: 'red' })}
75 *
76 *
77 * // CSS as JS Output
78 *
79 * div: {
80 * 'borderColor': 'transparent transparent transparent red',
81 * 'borderStyle': 'solid',
82 * 'borderWidth': '50px 0 50px 100px',
83 * 'height': '0',
84 * 'width': '0',
85 * }
86 */
87function triangle(_ref) {
88 var pointingDirection = _ref.pointingDirection,
89 height = _ref.height,
90 width = _ref.width,
91 foregroundColor = _ref.foregroundColor,
92 _ref$backgroundColor = _ref.backgroundColor,
93 backgroundColor = _ref$backgroundColor === void 0 ? 'transparent' : _ref$backgroundColor;
94 var widthAndUnit = (0, _getValueAndUnit["default"])(width);
95 var heightAndUnit = (0, _getValueAndUnit["default"])(height);
96 if (isNaN(heightAndUnit[0]) || isNaN(widthAndUnit[0])) {
97 throw new _errors["default"](60);
98 }
99 return _extends({
100 width: '0',
101 height: '0',
102 borderColor: backgroundColor
103 }, getBorderColor(pointingDirection, foregroundColor), {
104 borderStyle: 'solid',
105 borderWidth: getBorderWidth(pointingDirection, heightAndUnit, widthAndUnit)
106 });
107}
108module.exports = exports.default;
\No newline at end of file