Skip to content

Commit dec5347

Browse files
committed
added an example of how to sync the picker's value with an input's value
1 parent e9c4364 commit dec5347

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ observerCallback()
166166
resizeObserver.observe(document.body)
167167
```
168168

169+
### Listen to input external `value` changes
170+
171+
If your <input> element's value property might change by means other than this color picker component,
172+
then it is advisable to periodically check and match the color picker's internal value to the input's value.
173+
174+
```js
175+
import ColorPicker, {any_to_hex} from '@yaireo/color-picker'
176+
177+
const colorInput = document.querySelector('input')
178+
const cPicker = new ColorPicker(...)
179+
const VALUE_CHANGE_SAMPLING_DURATION = 1000
180+
181+
setInterval(() => {
182+
const inputValue = any_to_hex(colorInput.value)
183+
const pickerValue = any_to_hex(cPicker.CSSColor)
184+
185+
if(pickerValue != inputValue)
186+
cPicker.setColor(inputValue)
187+
}, VALUE_CHANGE_SAMPLING_DURATION);
188+
```
189+
169190
### Helper methods exported alongside the *default* `ColorPicker`
170191

171192

0 commit comments

Comments
 (0)