Six Zero-Dependency UI Components That Grew Out of a Color Picker
A Tiny Color Picker Nearly Led Me to Build a Component Library
Author: 夕水 Tags: Frontend, JavaScript, TypeScript
Preface
Take a look at the final result:
At first glance, it doesn't seem all that hard, but if I tell you what it's made of, you'll be taken aback.
Composition
The color picker above is split into nearly 16 plugins, as follows:
| Package | Responsibility |
|---|---|
@ew-color-picker/core |
Color state, event bus, mount points, plugin registry |
@ew-color-picker/utils |
DOM / color / animation helpers, EventEmitter, Console plugin |
@ew-color-picker/style |
CSS (index.css + input-number.css) |
@ew-color-picker/ew-color-picker |
All-in-one bundle: registers all plugins, exports the core |
@ew-color-picker/box |
Trigger swatch |
@ew-color-picker/panel |
Saturation/value panel |
@ew-color-picker/hue |
Hue slider |
@ew-color-picker/alpha |
Alpha slider |
@ew-color-picker/input |
Text color input |
@ew-color-picker/input-number |
Numeric input component and plugin |
@ew-color-picker/button |
Clear / Sure buttons |
@ew-color-picker/predefine |
Predefined swatches |
@ew-color-picker/color-mode |
Hex / RGB / HSL mode switching |
@ew-color-picker/gradient |
Multi-stop gradient editor (linear / radial / conic) |
@ew-color-picker/select |
Themed select with truncated-text tooltip |
@ew-color-picker/checkbox |
Themed checkbox used by the gradient editor |
@ew-color-picker/tooltip |
Standalone tooltip utility (also usable in your own components) |
@ew-color-picker/eyedropper |
Screen color picker via the native EyeDropper API (Chromium) |
@ew-color-picker/copy-color |
Copy the current color to the clipboard |
@ew-color-picker/vue |
Vue 3 wrapper (v-model, typed props, exposed instance API) |
@ew-color-picker/react |
React wrapper (forwardRef, typed props, exposed instance API) |
See? There are fully 21 packages and over 10 plugins. I won't go into each package's contents. Today I'll focus on the six components that can be used independently of the color picker — they weren't built for the color picker in the first place; the color picker just happened to need them.
1. input plugin
As the name suggests, it's a text input plugin. Usage is simple: first include the needed script and style files:
<link rel="stylesheet" href="https://unpkg.com/@ew-color-picker/style@2/dist/input.css">
<script src="https://unpkg.com/@ew-color-picker/utils@2/dist/utils.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/input@2/dist/input.min.js"></script>
Then write the following code in a script:
const input = new Input.Input({
placeholder: '请输入内容',
onChange: function (value) {
console.log('当前值: ' + (value || '空'));
},
});
document.body.appendChild(input.getElement())
You'll then see the text input plugin as shown:
Don't believe me? Visit this link to try it live. As shown, select example 22 to see the effect:
2. input-number plugin
This is a numeric input plugin, supporting arrow key stepping, direct typing, and min/max constraints. It can also be used independently:
<link rel="stylesheet" href="https://unpkg.com/@ew-color-picker/style@2/dist/input-number.css">
<script src="https://unpkg.com/@ew-color-picker/utils@2/dist/utils.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/input-number@2/dist/input-number.min.js"></script>
Then write:
const number = new InputNumber.InputNumber({
value: 128,
min: 0,
max: 255,
step: 1,
onChange(value) {
console.log('当前值: ' + value);
},
});
document.body.appendChild(number.getElement())
Besides min / max / step, it also supports precision to control decimal places, size to switch among three sizes, disabled to disable, and onFocus / onBlur callbacks.
The effect:
Don't believe me? Visit this link to try it live. As shown, select example 11 to see the effect:
3. button plugin
The button might be the most unremarkable plugin, but it's the only component reused both inside and outside the color picker: inside the picker it handles 'Clear' and 'OK'; standalone, it's a generic button.
<link rel="stylesheet" href="https://unpkg.com/@ew-color-picker/style@2/dist/button.css">
<script src="https://unpkg.com/@ew-color-picker/utils@2/dist/utils.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/button@2/dist/button.min.js"></script>
const button = new Button.Button({
text: '主要按钮',
type: 'primary', // default | primary | danger | text
size: 'default', // small | default | large
onClick(event) {
console.log('点击了按钮', event);
},
});
document.body.appendChild(button.getElement())
Four types stacked with three sizes give 12 combinations. There are also methods to change at runtime: setText, setType, setDisabled, isDisabled, and destroy for cleanup.
The effect:
Don't believe me? Visit this link to try it live. As shown, select example 23 to see the effect:
4. select plugin
The dropdown is probably the 'heaviest' of the batch, because it's not just a styled native <select>: it handles keyboard interaction (up/down to navigate, Enter to select, Esc to close), supports clearing, and automatically pops up a tooltip when the value or option text is truncated — and that part reuses the tooltip component described next.
Usage is as follows. Note that it depends on the utils and tooltip packages, and their styles must be included too:
<link rel="stylesheet" href="https://unpkg.com/@ew-color-picker/style@2/dist/tooltip.css">
<link rel="stylesheet" href="https://unpkg.com/@ew-color-picker/style@2/dist/select.css">
<script src="https://unpkg.com/@ew-color-picker/utils@2/dist/utils.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/tooltip@2/dist/tooltip.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/select@2/dist/select.min.js"></script>
const select = new Select.Select({
items: [
{ label: '红色', value: '#ef4444' },
{ label: '绿色', value: '#22c55e' },
{ label: '蓝色', value: '#3b82f6' },
{ label: '禁用项', value: '#0f172a', disabled: true }
],
placeholder: '请选择颜色',
clearable: true,
onChange(value, option) {
console.log('当前值: ' + value);
},
});
document.body.appendChild(select.getElement())
It supports controlled value, disabled on individual items, size, clearable, and tooltip: false to turn off the truncation tooltip. Methods include open / close / getValue / setValue / setItems / clear / isDropdownOpen.
The effect:
Don't believe me? Visit this link to try it live. As shown, select example 21 to see the effect:
5. checkbox plugin
The checkbox is the simplest of the batch, but 'simple' doesn't mean 'just use a native input'. Native checkboxes render differently across the three major browsers, so to unify the appearance you have to hide the native input and draw your own checkbox — hence it's extracted as a separate package:
<link rel="stylesheet" href="https://unpkg.com/@ew-color-picker/style@2/dist/checkbox.css">
<script src="https://unpkg.com/@ew-color-picker/utils@2/dist/utils.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/checkbox@2/dist/checkbox.min.js"></script>
const checkbox = new Checkbox.Checkbox({
label: '记住我的选择',
checked: false,
onChange(checked) {
console.log('checked: ' + checked);
},
});
document.body.appendChild(checkbox.getElement())
The native checkbox is hidden, and a custom-drawn checkmark is used on the outside. This gives three sizes, a disabled state, and a check animation that stays consistent with other components. Methods include isChecked / setChecked / setLabel / setDisabled.
The effect:
// Changing state at runtime also doesn't touch the DOM manually
checkbox.setChecked(true);
checkbox.setLabel('已启用');
checkbox.setDisabled(true);
Its usage is consistent with controlled components: checked is only the initial value; from then on the onChange callback is the source of truth, and you call setChecked to write back. This way, whether used with Vue, React, or plain JS, there's only one source of state.
The checkbox has its own example in the Playground, with exactly the same code and an extra 'Toggle checked' button to demonstrate setChecked:
Don't believe me? Visit this link to try it live. As shown, select example 25 to see the effect:
6. tooltip plugin
Earlier I said select reuses tooltip, so let's look at tooltip itself. This is the component I spent the most time on in this rewrite, because the goal is clear: fully align feature-wise with el-tooltip.
<link rel="stylesheet" href="https://unpkg.com/@ew-color-picker/style@2/dist/tooltip.css">
<script src="https://unpkg.com/@ew-color-picker/utils@2/dist/utils.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/tooltip@2/dist/tooltip.min.js"></script>
<!--the element the tooltip attaches to-->
<div id="dark" style="width:50px;height:50px;border:1px solid;"></div>
<div id="click" style="width:50px;height:50px;border:1px solid;"></div>
new Tooltip.Tooltip({
target: '#dark',
content: '这是一条 dark 主题提示',
effect: 'dark', // dark | light
placement: 'top', // 12 positions, same as el-tooltip
trigger: 'hover', // hover | click | focus | contextmenu, can be an array
offset: 8,
showAfter: 100,
hideAfter: 100,
});
new Tooltip.Tooltip({
target: '#click',
content: 'click 触发,再次点击关闭',
trigger: 'click',
});
It supports, among other things: 12 placement values and fallbackPlacements auto-flipping, showAfter / hideAfter delays, autoClose auto-close, enterable to allow the mouse into the popup, teleported / appendTo to control where it mounts, virtualTriggering for virtual anchor positioning, disabled, and a full set of events: beforeShow / show / beforeHide / hide / visibleChange.
One small detail worth mentioning: the logic for detecting whether text is truncated (isTextTruncated) is extracted into @ew-color-picker/utils, so select and tooltip use the same detection, avoiding the case where it shows a tooltip when nothing is truncated or fails to show when it is truncated.
The effect:
Don't believe me? Visit this link to try it live. As shown, select example 20 to see the effect:
Finally
That's a rundown of the six components that can be used independently of the color picker:
| Component | Package | Online example |
|---|---|---|
| Input | @ew-color-picker/input |
#22 |
| InputNumber | @ew-color-picker/input-number |
#11 |
| Button | @ew-color-picker/button |
#23 |
| Select | @ew-color-picker/select |
#21 |
| Checkbox | @ew-color-picker/checkbox |
#25 |
| Tooltip | @ew-color-picker/tooltip |
#20 |
While writing this, I kept thinking: why would a color picker need to split these into separate packages?
The reason is simple — they weren't built for the color picker at all. The picker just happens to need these six things: a text box, a number box, a button, a dropdown, a checkbox, and a tooltip. And when you build admin panels, forms, or filters, you need the same six. Instead of rewriting 'hide the native input and draw my own' in every project, it's better to extract them into packages. The picker uses one copy, everyone else gets the second.
These six components share a few common traits:
- Zero runtime dependencies: they only rely on the DOM helper functions in
@ew-color-picker/utils. No Vue, no React, no third-party libraries; - Native DOM first: nothing is mounted in the constructor;
getElement()returns a real DOM node, and where and when to mount is up to you, so they work with any framework; - Built-in type declarations: options, callback parameters, and return values all have types — no guessing in Vue, React, or plain TypeScript;
- Unified visual and API: the same CSS variables and the same
prefixclass-name prefix; you can change the prefix globally when styles conflict; - Disposable:
destroy()unbinds events and removes nodes, so no leftover listeners in SPAs.
Installation is straightforward — install what you use:
pnpm add @ew-color-picker/select @ew-color-picker/tooltip
import { Select } from '@ew-color-picker/select'
import '@ew-color-picker/style/dist/tooltip.css'
import '@ew-color-picker/style/dist/select.css'
Alternatively, skip package installation and use the UMD bundles from a CDN directly, which is how every example above is loaded:
<link rel="stylesheet" href="https://unpkg.com/@ew-color-picker/style@2/dist/select.css">
<script src="https://unpkg.com/@ew-color-picker/utils@2/dist/utils.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/tooltip@2/dist/tooltip.min.js"></script>
<script src="https://unpkg.com/@ew-color-picker/select@2/dist/select.min.js"></script>
Dependencies to note: select depends on tooltip (for truncation hints) and utils; tooltip depends on utils; the other four components only depend on utils. The same goes for styles — select must also include tooltip.css, which is the easiest step to miss. Missing it won't error, but the dropdown will look like a native control from ten years ago.
Links:
- Online Playground (you can edit the code and run all examples): Online playground.
- Chinese documentation: Documentation.
- GitHub: Repository.
If this post helps you, feel free to give a star on GitHub, and issues are welcome for suggestions on how to improve.
Top 2 of 3 from juejin.cn, machine-translated. The original thread is authoritative.
This splitting approach really hits the spot—one small component forcibly split into six general-purpose components that can each fly solo. The bit where select missed the tooltip.css import and didn't error out made me laugh; silent degradation is the worst trap.
If importing styles separately is a hassle for you, then I'll provide an all.css.
I used DeepSeek to make a mini-program: it specializes in curing couples' fights and family discord.