triangle.js 3.7 KB

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