Skip to content

Callbacks ​

Callbacks are functions passed through options. Unlike events, they are bound to a specific plugin or the core.

Core callbacks ​

CallbackSignatureTriggered by
changeColor(color: string) => voidcore.setColor()
sure(color: string, picker) => voidSure button
clear(picker) => voidClear button
togglePicker(isOpen: boolean) => voidshowPanel() / hidePanel()
ts
const picker = new ewColorPicker({
  el: '#app',
  changeColor: (color) => console.log(color),
  sure: (color, instance) => console.log(color, instance.getColor()),
  clear: (instance) => console.log('cleared', instance.getColor()),
  togglePicker: (open) => console.log(open ? 'open' : 'closed'),
})

Callback vs event

changeColor runs inside setColor() in addition to the change event. sure / clear are invoked by the Button plugin with arguments, while core.emit('sure') invokes them without arguments.

Plugin-level callbacks ​

Box ​

CallbackSignatureNotes
onClick(boxPlugin) => voidAfter the panel toggle; blocked when disabled / readonly
onMouseEnter(boxPlugin) => voidMouse enter
onMouseLeave(boxPlugin) => voidMouse leave
ts
new ewColorPicker({
  el: '#app',
  onClick: (box) => console.log('box clicked', box.hasBox),
})

Input ​

CallbackSignatureNotes
changeColor(rgba: string) => voidValid input parsed and applied
ts
new ewColorPicker({
  el: '#app',
  hasInput: true,
  changeColor: (rgba) => console.log('input applied', rgba),
})

InputNumber ​

CallbackSignatureNotes
onChange(value: number) => voidDebounced 300ms; not called on construction
onBlur(value: number) => voidInput blurred
onFocus(event: FocusEvent) => voidInput focused
ts
import { InputNumber } from '@ew-color-picker/input-number'

new InputNumber({
  value: 50,
  min: 0,
  max: 100,
  onChange: (v) => console.log(v),
  onBlur: (v) => console.log('blur', v),
  onFocus: (e) => console.log('focus', e),
})

Predefine ​

CallbackSignatureNotes
changeColor(rgba: string) => voidSwatch clicked and applied

ColorMode ​

CallbackSignatureNotes
changeMode(mode, picker) => voidAfter switching to 'hex' | 'rgb' | 'hsl'
ts
new ewColorPicker({
  el: '#app',
  changeMode: (mode, instance) => console.log(mode, instance.getColor()),
})

Button ​

CallbackSignatureNotes
clear(picker) => voidClear clicked
sure(color, picker) => voidSure clicked

Callback vs event ​

AspectCallbackEvent
RegistrationOptions objecton() / once()
Multiple handlersOne per optionMany
RemovalupdateOptions({ changeColor: noop })off()
Return value effectIgnoredtrue stops propagation
Access to plugin instanceUsually providedOnly via payload

Prefer events for analytics or multiple consumers; prefer callbacks for a single owner of the behavior.

Next steps ​