CxJS

Rectangle

import { Rectangle } from 'cx/svg'; Copied

The Rectangle component is a CxJS version of the SVG rect element designed for responsive layouts. Unlike native rect which requires absolute x, y, width, and height values, Rectangle uses bounded object properties to automatically adapt to its container.

This example shows 16 rectangles arranged in a 4×4 grid using anchors to define their positions. Notice the rounded corners on the first row using different rx/ry values.

<Svg style="width: 400px; height: 400px; border: 1px dashed #ddd" padding={5}>
  <Rectangle anchors="0 .25 .25 0" margin={5} colorIndex={0} rx={5} />
  <Rectangle anchors="0 .5 .25 .25" margin={5} colorIndex={1} ry="5%" />
  <Rectangle
    anchors="0 .75 .25 .5"
    margin={5}
    colorIndex={2}
    rx="9px"
    ry="25"
  />
  <Rectangle anchors="0 1 .25 .75" margin={5} colorIndex={3} rx="3em" />

  <Rectangle anchors=".25 .25 .5 0" margin={5} colorIndex={7} />
  <Rectangle anchors=".25 .5 .5 .25" margin={5} colorIndex={6} />
  <Rectangle anchors=".25 .75 .5 .5" margin={5} colorIndex={5} />
  <Rectangle anchors=".25 1 .5 .75" margin={5} colorIndex={4} />

  <Rectangle anchors=".5 .25 .75 0" margin={5} colorIndex={8} />
  <Rectangle anchors=".5 .5 .75 .25" margin={5} colorIndex={9} />
  <Rectangle anchors=".5 .75 .75 .5" margin={5} colorIndex={10} />
  <Rectangle anchors=".5 1 .75 .75" margin={5} colorIndex={11} />

  <Rectangle anchors=".75 .25 1 0" margin={5} colorIndex={15} />
  <Rectangle anchors=".75 .5 1 .25" margin={5} colorIndex={14} />
  <Rectangle anchors=".75 .75 1 .5" margin={5} colorIndex={13} />
  <Rectangle anchors=".75 1 1 .75" margin={5} colorIndex={12} />
</Svg>

Rounded Corners

Use rx and ry to create rounded corners:

  • rx={5} — 5 pixel horizontal radius
  • ry="5%" — 5% of rectangle height as vertical radius
  • rx="9px" ry="25" — different horizontal and vertical radii
  • rx="3em" — CSS units are supported

If only rx is specified, ry defaults to the same value (and vice versa).

Colors

The colorIndex property assigns both fill and stroke from the default color palette. You can override this with custom fill, stroke, or style properties.

Configuration

PropertyTypeDescription
anchorsstring/numberPosition within parent bounds. Format: "t r b l". See Svg for details.
offsetstring/numberTranslate edges in pixels. Format: "t r b l". See Svg for details.
marginstring/numberCSS-like margin. See Svg for details.
paddingstring/numberPadding applied before passing bounds to children.
fillstringFill color for the rectangle.
strokestringStroke color for the outline.
colorIndexnumberIndex in the default color palette. Sets both fill and stroke.
rxstring/numberHorizontal corner radius. Defaults to ry if not specified.
rystring/numberVertical corner radius. Defaults to rx if not specified.