Keys
A responsive and interactive piano keyboard component with configurable number of keys, note highlighting, and multiple styling modes.
The Keys component provides a responsive and interactive piano keyboard visualization. It shares the same layout and accessibility as other vector components; see the Vector introduction.
- Flexible configuration: Variable number of keys (1-128), different starting positions, octave transposition
- Note highlighting: Visual feedback for active notes via
notesOnprop - Multiple styling modes: theme-based, classic ivory/ebony, and inverted classic
- Interactive control: Optional
onChangecallback for key press/release events - Full theming support: color, roundness customization (in theme mode)
Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
nbKeys | number | 61 | No | Number of keys on the keyboard (1-128) |
startKey | "A" | "B" | "C" | "D" | "E" | "F" | "G" | — | No | Starting note name (A-G). Default 'C' for 61 keys, 'A' for 88 keys |
octaveShift | number | 0 | No | Octave transpose index. Positive values shift notes up by that many octaves, negative values shift down |
notesOn | (string | number)[] | [] | No | Array of notes that should be highlighted. Notes can be specified as strings (e.g., 'C4', 'F#5') or MIDI note numbers (e.g., 60 for C4) |
keyStyle | "theme" | "classic" | "classic-inverted" | theme | No | Key styling mode. 'theme' uses theme colors, 'classic' uses ivory/ebony colors, 'classic-inverted' inverts the classic colors |
onChange | (event: AudioControlEvent<{ note: number; active: boolean }>) => void | — | No | Callback triggered when a key is pressed or released. Only active if this prop is provided. |
color | string | — | No | Component primary color - any valid CSS color value (used in theme mode) |
roundness | number | — | No | Roundness for component corners (normalized 0.0-1.0) |
adaptiveSize | boolean | false | No | Whether the component stretches to fill its container |
size | "xsmall" | "small" | "normal" | "large" | "xlarge" | normal | No | Size of the component |
displayMode | "scaleToFit" | "fill" | scaleToFit | No | Layout mode for the control |
onClick | React.MouseEventHandler | — | No | Click event handler |
className | string | — | No | Additional CSS classes |
style | React.CSSProperties | — | No | Additional inline styles |
Basic Usage
The Keys component can be used as a simple visualization or as an interactive control:
Number of Keys
Configure the number of keys on the keyboard (1-128):
Starting Position
Control which note the keyboard starts with:
Octave Transposition
Use the octaveShift prop to transpose the keyboard up or down by octaves:
Note Highlighting
Highlight active notes using the notesOn prop. Notes can be specified as strings (note names) or MIDI note numbers:
Using Note Names
Using MIDI Note Numbers
Mixing Note Names and MIDI Numbers
Interactive Note Highlighting
Update the highlighted notes based on user interaction:
Key Styling Modes
The Keys component supports three styling modes:
Theme Mode
The default mode uses theme colors (from the color prop or global theme):
Classic Mode
Classic piano style with ivory white keys and ebony black keys:
Classic Inverted Mode
Inverted classic style with ebony white keys and ivory black keys:
Note:
In classic and classic-inverted modes, active keys always use the theme color for visual feedback, regardless of the key style.
Theming
Vector components share common theming features; see Theming Features in the Vector introduction for an overview. The Keys component supports theming through the color and roundness props:
Color Customization
In theme mode, customize the color of active keys:
Roundness
Control the roundness of the key corners:
Adaptive Sizing
The Keys component supports both fixed sizes and adaptive sizing. By default, the size of the component is driven by the Size System and their size attribute:
Adaptive sizing allows the component to adapt to the size of its parent container, adjusting its dimensions intelligently without causing distortion (scaleToFit display mode). The fill display mode makes the component fill the entire container, potentially distorting it.
Interaction
The Keys component becomes interactive when you provide the onChange prop:
The onChange callback receives an event with:
value.note: The MIDI note number (0-127)value.active: Whether the note is being pressed (true) or released (false)normalizedValue: The normalized value (0.0-1.0)midiValue: The MIDI note number
Common Patterns
Full Piano Keyboard
A standard 88-key piano keyboard:
Compact Keyboard
A compact 25-key keyboard for mobile or space-constrained interfaces:
MIDI Monitor
A keyboard that highlights notes received from MIDI input. In a real app you would connect this to MIDI input; here it shows a static example:
Chord Display
Display a chord by highlighting specific notes:
Layout Considerations
The Keys component maintains proper piano key layout and proportions regardless of the number of keys or starting position. The component:
- Automatically calculates white and black key positions
- Maintains correct spacing between keys
- Handles partial octaves correctly
- Supports any number of keys from 1 to 128
The component uses SVG for rendering, ensuring crisp display at any size and proper scaling.
Common Use Cases
MIDI Keyboard Visualization
Piano Roll Display
Compact Keyboard Control
Next Steps
- Explore other vector components like Knob and Slider
- Learn about raster components for bitmap-based controls
- Visit the playground for interactive examples