| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * @typechecks |
| 7 | * |
| 8 | * Example usage: |
| 9 | * <Rectangle |
| 10 | * width={50} |
| 11 | * height={50} |
| 12 | * stroke="green" |
| 13 | * fill="blue" |
| 14 | * /> |
| 15 | * |
| 16 | * Additional optional properties: |
| 17 | * (Number) radius |
| 18 | * (Number) radiusTopLeft |
| 19 | * (Number) radiusTopRight |
| 20 | * (Number) radiusBottomLeft |
| 21 | * (Number) radiusBottomRight |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | 'use strict'; |
| 26 | |
| 27 | var assign = Object.assign; |
| 28 | var React = require('react'); |
| 29 | var ReactART = require('react-art'); |
| 30 | |
| 31 | var createReactClass = require('create-react-class'); |
| 32 | |
| 33 | var Shape = ReactART.Shape; |
| 34 | var Path = ReactART.Path; |
| 35 | |
| 36 | /** |
| 37 | * Rectangle is a React component for drawing rectangles. Like other ReactART |
| 38 | * components, it must be used in a <Surface>. |
| 39 | */ |
| 40 | var Rectangle = createReactClass({ |
| 41 | displayName: 'Rectangle', |
| 42 | |
| 43 | render: function render() { |
| 44 | var width = this.props.width; |
| 45 | var height = this.props.height; |
| 46 | var radius = this.props.radius ? this.props.radius : 0; |
| 47 | |
| 48 | // if unspecified, radius(Top|Bottom)(Left|Right) defaults to the radius |
| 49 | // property |
| 50 | var tl = this.props.radiusTopLeft ? this.props.radiusTopLeft : radius; |
| 51 | var tr = this.props.radiusTopRight ? this.props.radiusTopRight : radius; |
| 52 | var br = this.props.radiusBottomRight |
| 53 | ? this.props.radiusBottomRight |
| 54 | : radius; |
| 55 | var bl = this.props.radiusBottomLeft ? this.props.radiusBottomLeft : radius; |
| 56 | |
| 57 | var path = Path(); |
| 58 | |
| 59 | // for negative width/height, offset the rectangle in the negative x/y |
| 60 | // direction. for negative radius, just default to 0. |
| 61 | if (width < 0) { |
| 62 | path.move(width, 0); |
| 63 | width = -width; |
| 64 | } |
| 65 | if (height < 0) { |
| 66 | path.move(0, height); |
| 67 | height = -height; |
| 68 | } |
| 69 | if (tl < 0) { |
| 70 | tl = 0; |
| 71 | } |
| 72 | if (tr < 0) { |
| 73 | tr = 0; |
| 74 | } |
| 75 | if (br < 0) { |
| 76 | br = 0; |
| 77 | } |
| 78 | if (bl < 0) { |
| 79 | bl = 0; |
| 80 | } |
| 81 | |
| 82 | // disable border radius if it doesn't fit within the specified |
| 83 | // width/height |
| 84 | if (tl + tr > width) { |
| 85 | tl = 0; |
| 86 | tr = 0; |
| 87 | } |
| 88 | if (bl + br > width) { |
| 89 | bl = 0; |
| 90 | br = 0; |
| 91 | } |
| 92 | if (tl + bl > height) { |
| 93 | tl = 0; |
| 94 | bl = 0; |
| 95 | } |
| 96 | if (tr + br > height) { |
| 97 | tr = 0; |
| 98 | br = 0; |
| 99 | } |
| 100 | |
| 101 | path.move(0, tl); |
| 102 | |
| 103 | if (tl > 0) { |
| 104 | path.arc(tl, -tl); |
| 105 | } |
| 106 | path.line(width - (tr + tl), 0); |
| 107 | |
| 108 | if (tr > 0) { |
| 109 | path.arc(tr, tr); |
| 110 | } |
| 111 | path.line(0, height - (tr + br)); |
| 112 | |
| 113 | if (br > 0) { |
| 114 | path.arc(-br, br); |
| 115 | } |
| 116 | path.line(-width + (br + bl), 0); |
| 117 | |
| 118 | if (bl > 0) { |
| 119 | path.arc(-bl, -bl); |
| 120 | } |
| 121 | path.line(0, -height + (bl + tl)); |
| 122 | |
| 123 | return React.createElement(Shape, assign({}, this.props, {d: path})); |
| 124 | }, |
| 125 | }); |
| 126 | |
| 127 | module.exports = Rectangle; |