Chroma Panel

搜索文档

查找页面或章节

EN

ColorInput

查看 Markdown

点击色块按钮,在响应式弹出层中打开选择器。

ColorInput 是一个 React 颜色输入框:一个显示当前颜色的色块按钮,点击后在弹出层中打开完整的选择器。当颜色只是众多字段之一时就适合用它,比如表单里的一项设置、工具栏里的一个按钮。选择器平时不占地方,有人需要时才打开。

弹出层里放的是 ChromaPanel,所以那一页讲的内容在这里同样适用。它打开的浮层就是导出的 Popover,在小屏幕上会变成底部面板。给 ColorInput 设置 name,它还会渲染一个隐藏的 input,让颜色按你设置的 format 随所在的表单一起提交。

结构

import { ColorInput } from "chroma-panel";
 
<ColorInput defaultValue="#3366cc" onChangeComplete={(c) => save(c.hex)} />;

ColorInput 支持 ChromaPanel 的全部 prop,另外还有下面列出的弹出层和表单相关 prop。它也可以像 <input> 一样直接放进表单,详见表单

演示

#3366cc
color-input-demo.tsx
"use client";
 
import { ColorInput } from "chroma-panel";
import { useState } from "react";
 
export default function ColorInputDemo() {
  const [color, setColor] = useState("#3366cc");
 
  return (
    <div className="flex items-center gap-3">
      <ColorInput
        value={color}
        onChange={(c) => setColor(c.hex)}
        injectStyles={false}
      />
      <span className="font-mono text-sm text-muted-foreground">{color}</span>
    </div>
  );
}

API 参考

Props

PropTypeDefaultDescription
openbooleanWhether the popover is open, when you control it.
defaultOpenbooleanfalseWhether the popover starts open.
onOpenChange(open: boolean) => voidFires when the popover opens or closes, however it was triggered.
namestringSubmits with the surrounding form under this name. The submitted value is the string in whichever format you set.
formstringAssociates the control with a form by id, for when it cannot be nested inside one.
requiredbooleanfalseMarks the control invalid while it has no value.
readOnlybooleanfalseShows the color but does not allow changing it. Unlike disabled, the value still submits.
autoCompletestringPassed through to the hidden input backing the control.
validationBehavior"native" | "aria"'native''native' uses the browser's own validation bubble. 'aria' reports the message through ARIA only, for when you render your own.
idstringId on the trigger, so your own <label htmlFor> can point at it.
aria-labelstring'Choose a color'Accessible name for the trigger, used when there is no visible label.
triggerClassNamestringClass on the swatch button only. Equivalent to classNames.trigger.
valuestring | HsvaThe color, when you keep it in your own state.Inherited from ChromaPanel.
defaultValuestring | Hsva'#3366cc'The starting color, when you want the panel to keep it.Inherited from ChromaPanel.
onChange(color: ColorChangeResult) => voidFires continuously while a drag is in progress.Inherited from ChromaPanel.
onChangeComplete(color: ColorChangeResult) => voidFires once when the drag ends. Use it for saving, undo entries and network calls.Inherited from ChromaPanel.
onValueChange(color: ColorChangeResult, meta: ColorChangeMeta) => voidFires continuously with interaction metadata, including the change source.Inherited from ChromaPanel.
onValueCommit(color: ColorChangeResult, meta: ColorChangeMeta) => voidFires once per completed interaction with phase and source metadata.Inherited from ChromaPanel.
modesreadonly (ModeId | string | PickerMode)[]all fiveWhich tabs appear, in the order you list them.Inherited from ChromaPanel.
modestringThe open tab, when you control it.Inherited from ChromaPanel.
defaultModestringfirst modeThe tab to open on.Inherited from ChromaPanel.
onModeChange(mode: string) => voidFires when the reader switches tabs.Inherited from ChromaPanel.
formatColorFormat'hex'Sets the css string on the change result, and the value a form submits.Inherited from ChromaPanel.
showAlphabooleantrueTurn off when opacity is not allowed.Inherited from ChromaPanel.
showEyedropperbooleantrueTurn off to hide the eyedropper even where it is supported.Inherited from ChromaPanel.
showCopyButtonbooleantrueShow a button that copies the color in the configured format.Inherited from ChromaPanel.
showRecentColorsbooleantrueTurn off in a one-shot picker.Inherited from ChromaPanel.
recentColorsstring[]The recent-color history, when you keep it. Persist it yourself to carry the list between sessions.Inherited from ChromaPanel.
defaultRecentColorsstring[][]The starting history, when you want the panel to keep it.Inherited from ChromaPanel.
onRecentColorsChange(colors: string[]) => voidFires with the whole list whenever a color is added to it.Inherited from ChromaPanel.
palettesColorPalette[]built-in setReplaces the palette swatches with your own.Inherited from ChromaPanel.
pencilsstring[]built-in 120-color gridReplaces the pencil grid.Inherited from ChromaPanel.
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.Inherited from ChromaPanel.
imageOptionsExtractOptionsShorthand for modeProps.image.extractOptions.Inherited from ChromaPanel.
disabledbooleanfalseMakes the panel read-only.Inherited from ChromaPanel.
theme"dark" | "light"systemForces one theme instead of following the OS.Inherited from ChromaPanel.
showTitleBarbooleantrueTurn off for an inline panel with no chrome.Inherited from ChromaPanel.
titlestring'Colors'The title bar text.Inherited from ChromaPanel.
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.Inherited from ChromaPanel.
collapsedbooleanWhether the panel is collapsed to its title bar, when you control it.Inherited from ChromaPanel.
defaultCollapsedbooleanfalseWhether it starts collapsed. Collapsing hides the body with CSS rather than unmounting, so nothing is lost.Inherited from ChromaPanel.
onCollapsedChange(collapsed: boolean) => voidFires when the yellow window control is used.Inherited from ChromaPanel.
sizePanelSizeThe panel size, when you control it.Inherited from ChromaPanel.
defaultSizePanelSize'default'The starting size. 'expanded' widens the panel, and the wheel with it.Inherited from ChromaPanel.
onSizeChange(size: PanelSize) => voidFires when the green window control is used.Inherited from ChromaPanel.
injectStylesbooleantrueTurn off when you import chroma-panel/style.css yourself, as this site does.Inherited from ChromaPanel.
classNamestringClass applied to the panel root.Inherited from ChromaPanel.
classNamesChromaClassNamesClasses applied per part.Inherited from ChromaPanel.
styleReact.CSSPropertiesInline styles on the panel root. Setting the --cp-* custom properties here themes a single panel.Inherited from ChromaPanel.
storeColorStoreAn external color store, for driving several panels from one color.Inherited from ChromaPanel.