Tool picker
ToolPicker is a PencilKit-style palette: undo and redo, drawn instruments that rise when selected, the ruler toggle, colour swatches and a custom colour button.
- Tap the selected instrument to choose its width and opacity, or the eraser mode.
- Tap the rainbow button after the swatches to pick any colour. On iOS it opens the system colour picker (grid, spectrum, sliders, eyedropper and opacity); on Android it shows hue, saturation, brightness and opacity sliders.
- The rainbow button shows the current colour when it is not one of the swatches.
- Each instrument remembers its own colour and width.
import { useState } from 'react'
import { Drawing, SketchCanvas, ToolPicker, toolPickerThemes, type Tool } from 'nitro-ui-sketch'
import { View } from 'nitro-ui-renderer'
export function Notes() {
const [drawing] = useState(() => new Drawing())
const [tool, setTool] = useState<Tool>({ type: 'ink', ink: 'pen', color: 0x000000ff, width: 4 })
return (
<View layout={{ flex: 1 }}>
<SketchCanvas layout={{ flex: 1 }} drawing={drawing} tool={tool} />
<ToolPicker
tool={tool}
onToolChange={setTool}
drawing={drawing}
items={['undo', 'redo', 'pen', 'pencil', 'eraser', 'lasso', 'swatches', 'colorPicker']}
theme={{ ...toolPickerThemes.dark, cornerRadius: 24 }}
layout={{ position: 'absolute', left: 0, right: 0, bottom: 32 }}
/>
</View>
)
}
Items
items picks what the bar shows, in order, each as its own control in one row. The default, defaultToolPickerItems, is all of them:
| Item | Control | Needs |
|---|---|---|
'undo', 'redo' | Undo and redo buttons | drawing |
'pen', 'monoline', 'fountainPen', 'marker', 'pencil', 'crayon', 'watercolor' | Drawn ink instruments | |
'eraser' | Eraser | |
'lasso' | Lasso | |
'ruler' | Ruler toggle | onRulerActiveChange |
'swatches' | Colour swatches | |
'colorPicker' | Rainbow custom colour button |
Items whose requirement is missing are hidden. Without 'colorPicker', the swatches stay in the bar at every density.
To place undo and redo elsewhere, leave them out of items and use UndoRedoControls.
Density
| Density | Bar height | Swatches |
|---|---|---|
compact | 64 pt | In the colour card |
comfortable | 80 pt | In the bar |
spacious | 96 pt | In the bar, larger |
Without density, the picker uses 'compact' in windows narrower than 500 points and 'comfortable' otherwise.
Colours, widths and labels
| Prop | Default | Contents |
|---|---|---|
colors | defaultPaletteColors | Swatch colours as 0xRRGGBBAA. |
widths | defaultInstrumentWidths | Widths per ink and pixel-eraser diameters, thinnest first. |
labels | defaultToolPickerLabels | Eraser mode titles, for localization ('Object', 'Pixel'). |
<ToolPicker
tool={tool}
onToolChange={setTool}
colors={[0x000000ff, 0xffffffff, 0xff3b30ff]}
widths={{ ...defaultInstrumentWidths, pen: [2, 4, 8] }}
labels={{ strokeEraser: 'Objet', pixelEraser: 'Pixel' }}
/>
Accessory
accessory adds content at the end of the bar, such as a ToolPickerButton. It scrolls with the controls.
<ToolPicker
tool={tool}
onToolChange={setTool}
accessory={
<ToolPickerButton onPress={() => canvas.current?.setViewport({ zoom: 1, x: 0, y: 0 })}>
1×
</ToolPickerButton>
}
/>
Closing the options card
To close the options card when the canvas is touched, as PencilKit does, pass the picker's handle to the canvas:
const picker = useRef<ToolPickerHandle | null>(null)
<SketchCanvas tool={tool} onTouchDown={() => picker.current?.dismissOptions()} layout={{ flex: 1 }} />
<ToolPicker ref={picker} tool={tool} onToolChange={setTool} />
Custom layouts
The picker's parts are exported: InstrumentButton, RulerButton, ColorPalette, ColorPicker, StrokeWidthPicker, EraserModePicker, ToolPickerButton and UndoRedoControls, each taking the same theme. See Tool picker parts.
:::warning Bar scrolling with NitroUI 0.3.0 With NitroUI 0.3.0, the bar doesn't scroll when its items are wider than the bar. See Troubleshooting. :::