Skip to content

RectangularPushButton

RectangularPushButton (from scenerystack/sun) is the workhorse momentary-action button: it fires a listener once per press (or once per release, depending on fireOnDown) and holds no state of its own. Use it for one-shot actions like "step forward" or "randomize" — for a button that tracks on/off state, see Toggle Buttons instead.

ts
import { RectangularPushButton } from 'scenerystack/sun';
import { Text } from 'scenerystack/scenery';
import { Tandem } from 'scenerystack/tandem';

const stepButton = new RectangularPushButton( {
  content: new Text( 'Step' ),
  baseColor: 'yellow',
  listener: () => {
    model.step( 0.1 );
  },
  tandem: Tandem.REQUIRED
} );

content accepts any Node, so an icon (e.g. a Path built from a shape) works just as well as text. If your button's content is specifically a text label, TextPushButton saves you the boilerplate of constructing a Text node yourself.

Options

RectangularPushButtonOptions combines options from three layers: the button's own RectangularButton shape options, the shared ButtonNode appearance options, and PushButtonModel's firing behavior. The most commonly used:

OptionEffect
contentThe Node shown on the button (icon, text, etc.); null requires an explicit size
listenerConvenience for adding a single fire listener, () => void
baseColorBackground color the 3D appearance gradients are derived from
xMargin / yMarginMargin between content and the button's edge
cornerRadiusCorner radius, applied to all four corners unless overridden per-corner
sizeFixed Dimension2 for the button; content is scaled down to fit
fireOnDowntrue fires on pointer-down instead of pointer-up (default false)
fireOnHold / fireOnHoldDelay / fireOnHoldIntervalEnables press-and-hold repeat firing
enabledPropertyExternally control whether the button can be pressed
touchAreaXDilation / touchAreaYDilationExpand the touch-friendly hit area beyond the visual bounds

Methods

MethodEffect
addListener( listener: () => void )Adds another fire listener
removeListener( listener: () => void )Removes a previously added listener

Prefer listener for the common case

addListener/removeListener exist for adding or removing listeners after construction. If you just need one listener and never remove it, pass listener in the options object — it's equivalent and shorter.

tandem is required

Every sun component in a real PhET-iO-instrumented sim needs a tandemRectangularPushButton will assert if one isn't supplied. Even outside PhET-iO builds, it's good practice to always pass one.