Chroma Panel

搜索文档

查找页面或章节

EN

ChromaPanel

查看 Markdown

完整的颜色选择器,直接渲染在页面中。

ChromaPanel 就是 React 颜色选择器组件本身,直接渲染在原位,而不是藏在按钮后面。它包含标题栏、每个模式各自的标签页、当前模式的内容,以及底栏。底栏里有当前颜色预览、最近使用的颜色、复制按钮,浏览器支持时还有取色器。如果希望选择器一直留在屏幕上,而不是按需打开,就用它。

默认显示全部五种模式,依次是色轮、滑块、调色板、图片和预设色。通过 modes 可以选择显示哪些标签页,以及它们的顺序。只有一种模式时,不显示标签栏。ColorInput 在弹出层中打开的正是这个面板,所以你在这里做的配置对它同样有效。如果要用同一个值驱动多个面板,就给它们传入同一个颜色 store

结构

import { ChromaPanel } from "chroma-panel";
 
<ChromaPanel defaultValue="#3366cc" modes={["wheel"]} showTitleBar={false} />;

演示

正在加载在线示例,代码在下方。

chroma-panel-demo.tsx
"use client";
 
import { ChromaPanel } from "chroma-panel";
 
export default function ChromaPanelDemo() {
  return <ChromaPanel defaultValue="#3366cc" injectStyles={false} />;
}

API 参考

v1 新增的元数据回调是对原有回调的补充。onValueChangeonValueCommit 的第二个参数是 { phase, source }。source 标明了变更的来源:指针、键盘、输入框、色块、图片、取色器、最近使用的颜色,或是代码调用。

<ChromaPanel onValueCommit={(color, meta) => save(color.css, meta.source)} />

Props

PropTypeDefaultDescription
valuestring | HsvaThe color, when you keep it in your own state.
defaultValuestring | Hsva'#3366cc'The starting color, when you want the panel to keep it.
onChange(color: ColorChangeResult) => voidFires continuously while a drag is in progress.
onChangeComplete(color: ColorChangeResult) => voidFires once when the drag ends. Use it for saving, undo entries and network calls.
onValueChange(color: ColorChangeResult, meta: ColorChangeMeta) => voidFires continuously with interaction metadata, including the change source.
onValueCommit(color: ColorChangeResult, meta: ColorChangeMeta) => voidFires once per completed interaction with phase and source metadata.
modesreadonly (ModeId | string | PickerMode)[]all fiveWhich tabs appear, in the order you list them.
modestringThe open tab, when you control it.
defaultModestringfirst modeThe tab to open on.
onModeChange(mode: string) => voidFires when the reader switches tabs.
formatColorFormat'hex'Sets the css string on the change result, and the value a form submits.
showAlphabooleantrueTurn off when opacity is not allowed.
showEyedropperbooleantrueTurn off to hide the eyedropper even where it is supported.
showCopyButtonbooleantrueShow a button that copies the color in the configured format.
showRecentColorsbooleantrueTurn off in a one-shot picker.
recentColorsstring[]The recent-color history, when you keep it. Persist it yourself to carry the list between sessions.
defaultRecentColorsstring[][]The starting history, when you want the panel to keep it.
onRecentColorsChange(colors: string[]) => voidFires with the whole list whenever a color is added to it.
palettesColorPalette[]built-in setReplaces the palette swatches with your own.
pencilsstring[]built-in 120-color gridReplaces the pencil grid.
modePropsRecord<string, Record<string, unknown>>Props spread onto one mode's panel, keyed by mode id. Lets a custom mode take props without the panel knowing about it.
imageOptionsExtractOptionsShorthand for modeProps.image.extractOptions.
disabledbooleanfalseMakes the panel read-only.
theme"dark" | "light"systemForces one theme instead of following the OS.
showTitleBarbooleantrueTurn off for an inline panel with no chrome.
titlestring'Colors'The title bar text.
onClose() => voidFires when the red window control is used. An inline panel has nothing to close, so that control is dimmed until you pass this.
collapsedbooleanWhether the panel is collapsed to its title bar, when you control it.
defaultCollapsedbooleanfalseWhether it starts collapsed. Collapsing hides the body with CSS rather than unmounting, so nothing is lost.
onCollapsedChange(collapsed: boolean) => voidFires when the yellow window control is used.
sizePanelSizeThe panel size, when you control it.
defaultSizePanelSize'default'The starting size. 'expanded' widens the panel, and the wheel with it.
onSizeChange(size: PanelSize) => voidFires when the green window control is used.
injectStylesbooleantrueTurn off when you import chroma-panel/style.css yourself, as this site does.
classNamestringClass applied to the panel root.
classNamesChromaClassNamesClasses applied per part.
styleReact.CSSPropertiesInline styles on the panel root. Setting the --cp-* custom properties here themes a single panel.
storeColorStoreAn external color store, for driving several panels from one color.
  • ColorInput:同一个面板,放在色块按钮后面
  • 主题:用 CSS 变量设置面板的尺寸和颜色
  • 受控与非受控:决定哪些 prop 由你自己管理
  • 入口:只导入你列出的模式