How it works
A textbook figure of set operations on areas. The outlines of a circle A and a triangle or square B cut the plane into three regions: only in A, in both, only in B. An operation is a choice of those regions. The left figure is the construction: the chosen regions are tinted, and a sweep across regions adds or removes them; a knob at each shape's centre moves it. The right figure shows the result alone, filled. The head names the operation and its area identity, such as |A ∪ B| = |A| + |B| − |A ∩ B|; the chips under the operations give each region's area, and the readout the area of the result. Areas are exact for B and use a 360-sided polygon for the circle.
Use it for
- Teach set operations with something you can measure: the union, intersection and differences of two shapes and the inclusion–exclusion identity for their areas.
- Explain what union, subtract and intersect do in a vector drawing tool before learners use them.
Look elsewhere when
- For editing real vector paths, use a drawing tool: the shapes here are fixed in size.
Usage
import React, { useState } from 'react'
import { CampBadgePress } from './CampBadgePress.jsx'
export function SetLesson() {
const [operation, setOperation] = useState('intersection')
const [area, setArea] = useState(null)
return (
<CampBadgePress
value={operation}
onChange={(next, details) => {
setOperation(next)
setArea(details.area)
}}
shape="square"
labels={{ title: 'Where two shapes overlap' }}
/>
)
}Props
| Prop | Type | Default | What it does |
|---|---|---|---|
value | 'union' | 'intersection' | 'aMinusB' | 'bMinusA' | 'xor' | 'a' | 'b' | 'none' | — | Controlled result: the operation, or the set that the chosen regions make ('a', 'b' and 'none' are reached by sweeping or the region chips). |
defaultValue | string | 'union' | Starting result when uncontrolled, and what Reset returns to. |
onChange | (value, { reason, regions, area }) => void | — | Called with the new result and { reason ('operation', 'region', 'sweep', 'cancel' or 'reset'), regions ([A − B, A ∩ B, B − A] as 0 or 1), area (square units) }. |
positions | { a: [x, y], b: [x, y] } | — | Controlled centres of the circle A and of shape B, in units on the 20 × 16 grid. |
defaultPositions | { a: [x, y], b: [x, y] } | { a: [7.5, 8], b: [12.5, 8.6] } | Starting centres when uncontrolled. |
onPositionsChange | (positions, { reason }) => void | — | Called while a shape is dragged or moved with the keys; reason is 'drag', 'key', 'cancel' or 'reset'. |
shape | 'triangle' | 'square' | 'triangle' | Shape B: a triangle with base 10 and height 8, or a square with side 8. A is a circle of radius 5. |
labels | object | — | Replaces any built-in text: title, construction, result, operation, regions, names and formulas (objects keyed by result), regionNames (three), readout ('{set}'), empty, unit, moveA, moveB, help, regionText ('{name}', '{area}'), movedText ('{shape}', '{x}', '{y}', '{area}') and reset. |
locale | string | 'en' | Language tag for the decimal readouts. |
className | string | — | Extra class on the card, for example to set its width (30rem by default, never wider than its container). |
Accessibility
- The operations are a radio group labelled with their names (the visible text is the symbol): Tab reaches the chosen one, arrows move and select.
- Each region is a toggle button (aria-pressed) whose name includes its area; sweeping on the figure does the same with a pointer.
- Each shape's knob is a focusable button: arrows move the shape by 0.5 units, Shift with an arrow by 2. A polite status announces the new position and the overlap.
- The area of the result is a polite live region; the result figure is an image labelled with the result's name and area. Reset is a button, marked unavailable while nothing has changed.
- Dragging uses pointer capture; a cancelled gesture restores the regions or the position it started from. The construction figure sets touch-action none so a sweep does not scroll the page.
Motion
Region fills fade for --ds-duration-base; a shape moved with the keys glides for --ds-duration-fast and follows the pointer directly while dragged. With reduced motion every change lands at once.
Add it to your project
Pro members copy a prompt and their coding agent installs the source, the same way as the free components.
CampBadgePress.jsxcamp-badge-press.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.





