跪拜 Guibai
← All articles
Frontend · JavaScript · TypeScript

Six Zero-Dependency UI Components That Grew Out of a Color Picker

By 夕水 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams building form-heavy admin interfaces can reuse these six battle-tested-style components directly, cutting out the usual framework lock-in and dependency bloat. The package structure also serves as a practical model for extracting shared UI primitives from a domain-specific widget.

Summary

The author of the open-source color picker `@ew-color-picker` found that the picker needed a text input, number input, button, dropdown, checkbox, and tooltip — and realized these are the same building blocks that almost any admin interface or form needs. Rather than reimplement them in every project, each was extracted into its own npm package with a tiny dependency graph: only `utils` for most, and `tooltip` additionally for the dropdown's truncation hint.

These components are not tied to any framework. They return native DOM nodes via `getElement()`, expose typed options and callbacks, include a `destroy()` method for clean disposal, and share a consistent CSS-variable-based theme. They can be consumed as ES modules or loaded from a CDN as UMD bundles. The tooltip is the most feature-packed, aiming for parity with Element Plus's `el-tooltip` — supporting 12 placements, fallback placement flipping, delays, multiple triggers, teleporting, virtual triggering, and a full event lifecycle.

An interactive playground with numbered examples lets you try each component live, and the project is available on GitHub and npm under the `@ew-color-picker` scope.

Takeaways
— The color picker project is modularized into 21 packages, with six components intentionally reusable outside the picker: input, input-number, button, select, checkbox, and tooltip.
— All six components have zero runtime dependencies — they rely only on `@ew-color-picker/utils`, which provides DOM/color/animation helpers and is framework-agnostic.
— Plugins expose `getElement()` to return a real DOM node, letting you mount them anywhere with any framework, and `destroy()` unbinds events to prevent SPA memory leaks.
— Input supports placeholder and onChange; InputNumber adds min/max/step, precision, size, disabled, and focus/blur events.
— Button offers four types (default, primary, danger, text) and three sizes, with runtime setters like setText, setType, setDisabled.
— Select implements full keyboard navigation (arrows, Enter, Esc), clearable, truncation detection, and a `tooltip: false` option to disable the hint.
— Checkbox hides the native input and draws its own checkmark for cross-browser consistency, with methods like setChecked, setLabel, setDisabled.
— Tooltip matches el-tooltip in functionality: 12 placements and fallback flipping, delays, multiple triggers, autoClose, enterable, teleport, virtualTriggering, and lifecycle events.
— The truncation-detection logic (`isTextTruncated`) is shared between Select and Tooltip via the utils package, ensuring consistent behavior.
— Install via pnpm or individual packages; the easiest miss is forgetting to include tooltip.css when using Select — it won't error, but styling looks like a native old dropdown.
— Live examples are available in an online playground, with numbered demos for each of the six components.
Conclusions

Extracting generic UI primitives from a single-purpose widget is a sound architectural move, but it only pays off when the primitives are genuinely identical across contexts; here the shared truncation logic shows that even small cross-cutting concerns benefit from a single implementation.

The tooltip's explicit goal of matching el-tooltip feature-for-feature raises expectations — it's not just a simple bubble but a full-featured popup system, which suggests the author values drop-in compatibility over minimalism.

The zero-dependency, native-DOM approach trades convenience for portability: you get no framework integration out of the box, but you can wrap it in Vue/React with custom bindings (the project already ships wrappers), making the core stable across ecosystems.

From the discussion
Featured comments
去吧波波鸟

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.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗