Ant Design Svelte

ColorPicker

Color and linear gradient selection with palettes and formats.

When to use

ColorPicker sets the design color, transparency or linear gradient. It is convenient to use in the theme, card or chart editor. For a limited set of pre-known colors, the presets palette is sufficient; For arbitrary selection, leave the channel editor.

The page contains examples of a single color, gradient and custom trigger. The selected color is shown separately from the CSS that the app uses to render the preview.

Meaning and events of

value accepts a color string, color object, or gradient points. For guided editing, save the object obtained from the callback: re-converting HEX, RGB and HSB to strings can round the channels.

Problem Setting Example
Set initial color defaultValue Basic selection
Accept every change value, onChange Controlled color
Change model after completing a gesture onChangeComplete Controlled color
Allow no color allowClear, onClear Cleaning
Edit color transition mode Linear Gradient

onChange receives the color and the finished CSS string. onChangeComplete reports the completed selection; when controlled only through it, the trigger retains the same color until the end of the gesture. Update the external value in the selected handler. defaultValue specifies the initial local state, not the subsequent reset.

The color object provides toHexString(), toRgbString(), toHsbString(), toRgb() and toHsb(). For CSS use toCssString(); isGradient() and getColors() allow you to distinguish and read gradients. When clearing, save the entire object together with cleared so that the lack of color does not turn back into normal black.

Formats, transparency and palettes

format selects hex/rgb/hsb editor; onFormatChange allows you to control this selection externally. defaultFormat sets the initial format, disabledFormat prohibits switching. The format of the fields does not change the purpose of the second argument to onChange: it remains CSS, including linear-gradient for the gradient. Example formats shows a separate format status.

disabledAlpha removes transparency controls. It does not overwrite the passed value immediately: the opacity condition is applied on the next change. Example without transparency uses this setting separately from disabled, which disables the entire component.

mode selects single, gradient or allows you to switch between them. Gradient points are specified by color and percentage; store the result as color, and get the string for preview or saving. Ready-made palettes combine single colors and gradients into labeled groups. If the lower swatches are below the viewport, scroll the page to reach them. When you use Tab, the page scrolls the focused control into view. Press Enter to expand a group or select a color.

Panel and accessibility

The usual trigger is a button. showText=true adds color text, and snippet showText gets color for its own caption. trigger=hover changes the opening method; open/onOpenChange allow you to control the panel from outside. placement and arrow specify the placement and arrow.

children - Svelte snippet of your own trigger. Keep the accessible name and native semantics of your button; if necessary, control open and aria-expanded explicitly, as in example of your button. Escape closes the panel and returns focus.

panelRender receives the original panel and extra.components.Picker/Presets - also snippets. Panel composition shows the wrapper of the original panel and the composition of its parts. Save the desired Picker so that editing and events remain.

destroyOnHidden recreates the panel after closing; without it, the hidden panel retains local state. The local getPopupContainer overrides the ConfigProvider and is called when opened. classNames and styles accept objects or functions of the component props. styles values can be CSS strings or objects. For your own trigger, its root design is set directly on your button. Semantic styles shows available slots.

ColorPicker CSS prefixes show the global ConfigProvider.prefixCls and the full local prefixCls. An empty string/undefined returns provider inheritance. The local prefix changes the ColorPicker, and Popover, input fields, Slider, Divider, Segmented and Collapse use their own common namespaces. theme.cssVar.prefix sets theme variables separately from classes. Changing the prefix preserves the color, open popup, incomplete HEX, selection, focus and expansion of presets groups. The example also shows an adjacent area with the same local class, its own button and popup container.

ColorPicker uses design tokens from the current theme for its appearance.

Examples

Basic selection

Single color with local state. defaultValue specifies the initial value.

#52c41a
#fa8c16; changes: 0

Controlled color

The first picker takes the color at each onChange, the second - after onChangeComplete. color objects from callbacks are saved without being converted to a string.

Color label

Normal showText, Svelte snippet with color and controlled open panel indicator.

Without transparency

disabledAlpha hides Alpha and makes the next change opaque. For an already defined translucent value, the alpha does not change until edited.

Own button

Own trigger - Button with its own native semantics. Managed open synchronizes aria-expanded.

RGB

HEX, RGB and HSB

The editor format can be local, managed or fixed. The CSS from onChange is always RGB or linear-gradient.

Panel composition

panelRender receives the original panel and snippets Picker/Presets. Here the standard parts are collected with a header and a finish button.

Dimensions

Three trigger sizes: large, medium and small. Without size, the size from the ConfigProvider is used.

linear-gradient(90deg, #fa8c16 0%, #eb2f96 100%)

Linear Gradient

Color points and percentages define the gradient. mode allows switching between single color and gradient; the preview uses the CSS from the callback.

Unavailable state

disabled blocks the opening of the panel for both a simple trigger and a trigger with a label.

#fa8c16

Cleaning

allowClear adds cleaning to the panel. Save the entire color object to preserve the cleared attribute.

Opening by guidance

trigger="hover" opens the palette with an animation. Moving the pointer away closes it after a short delay.

rgb(82, 196, 26)

Ready-made palettes

Groups presets have a stable key and an initial defaultOpen. Selecting a sample calls the usual onChange.

Semantic styles

ConfigProvider supplies shared CSS objects, and the local styles function extends them based on component props.

Color with a ready-made palette
#52c41a
Linear Gradient
linear-gradient(90deg, rgb(68,50,30) 0%, rgb(235,47,150) 100%)

Project color and gradient

Applied example from the review saved: green palette, warm gradient and result CSS.

#52c41a
#722ed1
General: suite; local: project-color

Local prefixCls changes ColorPicker, and Popover, fields, Slider, Divider, Segmented and Collapse separately inherit the common namespace. The neighboring picker uses the same local class, a different common prefix, its own button and popup container. Theme variables are set independently.

CSS prefixes ColorPicker

General and local prefixCls, independent namespaces of nested fields and an adjacent area with its own button and popup container.

Coverage Examples

14 main themes of ant.design live page collated. React rendering functions have been replaced by Svelte snippets; the exact types are determined by the installed package.

Topics of official examples
ThemeExample Svelte
Basic UsageBasic selection
controlled modeControlled color
Rendering Trigger TextColor label
Disabled AlphaWithout transparency
Custom TriggerOwn button
Color FormatHEX, RGB and HSB
Custom Render PanelPanel composition
Trigger sizeDimensions
Line GradientLinear Gradient
DisableUnavailable state
Clear ColorCleaning
Custom Trigger EventOpening by guidance
Preset ColorsReady-made palettes
Custom semantic dom stylingSemantic styles

API

The main parameters were checked against the public ColorPickerProps. The managed value, open and format are updated via the corresponding callbacks.

Basic parameters of ColorPicker
PropertyDescriptionTypeDefault
prefixClsFull local prefix of ColorPicker and its parts. The empty string/undefined inherit ConfigProvider. Popover, Input, InputNumber, Select, Slider, Divider, Segmented and Collapse use an independent common prefix; The CSS namespace of the theme is set separately.stringConfigProvider.prefixCls + "-color-picker"
valueString, color object from callback, array of points, or null. Control via onChange; bind:value is not supported.ColorValueundefined
defaultValueInitial local color, read when created.ColorValueundefined
modeOne mode or allowed modes with a switch.single | gradient | readonly ColorMode[]single
formatManaged format. defaultFormat specifies the initial local format.hex | rgb | hsbhex
onChangeColor change; CSS, regardless of editor format, is returned as RGB or linear-gradient.(color: Color, css: string) => void—
onChangeCompleteDrag completion or discrete change.(color: Color) => void—
onFormatChangeFor managed format, save the new format externally.(format?: ColorFormat) => void—
openControlled panel visibility. onOpenChange reports a request to change it.booleanLocal
showTexttrigger label: standard text or snippet with the current color.boolean | Snippet<[Color]>false
childrenOwn trigger, for example Button. Its disabled and available attributes are set inside the snippet.SnippetStandard trigger
presetsGroups with label, colors, optional key and defaultOpen; colors can be gradients.readonly ColorPickerPreset[]undefined
panelRenderGets panel and extra.components.Picker/Presets - Svelte snippets.Snippet<[Snippet, { components }]>Standard panel
allowClearCleaning inside the panel; onClear is called separately.booleanfalse
disabledAlphaRemoves alpha editing. The next change makes the original translucent color opaque.booleanfalse
disabledFormatHides the format selection, preserving its current fields.booleanfalse
disabledBlocks standard trigger and panel opening.booleanFrom context
sizelarge, medium and small.ComponentSizeFrom context
triggerPanel opening event.click | hoverclick
placementPreferred placement; autoAdjustOverflow by default true adjusts it at the edge.ColorPickerPlacementbottomLeft
arrowPanel arrow; can be inherited from ConfigProvider.colorPicker.boolean | { pointAtCenter?: boolean }true
getPopupContainerThe local container takes precedence. Getter is called after the first open.(trigger: HTMLElement) => HTMLElementFrom ConfigProvider
destroyOnHiddenDestroys the contents of a hidden panel.booleanfalse
classNamesClasses root, body, content, description and popup.root.ColorPickerSlots | ((info) => ColorPickerSlots)undefined
stylesCSS strings and objects. popupOverlayInner styles the inside of the palette.ColorPickerStyles | ((info) => ColorPickerStyles)undefined

Color and events

Callback returns an object of public type Color. Keep it in a state for editing without loss of accuracy. Available toHexString(), toRgbString(), toHsbString(), toRgb() and toHsb().

toCssString() returns the finished CSS; isGradient() features a gradient, getColors() returns its points. When cleaning, also maintain the property cleared, leaving the entire object.

onChange reports intermediate changes when dragging, onChangeComplete is a completed selection. In the second case, the controlled button remains the same color until the gesture is completed, as in control example.

Semantic DOM

Standard trigger - native button with aria-haspopup and aria-expanded; the panel has the role dialog. Give your trigger an accessible name and state yourself.

Real slots ColorPicker
SlotPurpose
rootStandard opening button.
bodyColor sample or cleaning mark.
contentPouring the sample.
descriptionStandard button label.
popup.rootObject with root: root dialog of the panel.

Example semantic styles shows object and function. Root slots belong to the standard trigger; For children, design your own button directly. Escape closes the panel and returns focus.

Design

There are no separate ColorPicker component tokens in the public map of the package; There is also no separate table of tokens on the verified official page. Use a common theme, ConfigProvider.colorPicker and semantic styles.

Below are some common current theme tokens via theme.useToken(). These are design guidelines and not a complete list of style dependencies.

Common tokens of the current topic
TokenPurposeMeaning
colorBgContainerBackground trigger.#ffffff
colorBgElevatedPanel background.#ffffff
colorBordertrigger frame.#d9d9d9
colorTextMain text.rgba(0,0,0,0.88)
colorPrimaryFocus and active elements.#2f54eb
controlHeightNormal trigger height.32
controlHeightSMLow height trigger.24
controlHeightLGHigh height trigger.40

ConfigProvider.colorPicker

The provider passes class, style, arrow, classNames and styles. Local semantic styles take precedence.

<script lang="ts">
  import { ColorPicker, ConfigProvider } from '@woodger/antd';
</script>

<ConfigProvider colorPicker={{
  arrow: false, styles: {
    root: 'border:2px solid #722ed1',
    description: 'font-weight:600'
  }
}}>
  <ColorPicker
    aria-label={("Color from ConfigProvider")}
    defaultValue="#722ed1"
    showText
  />
</ConfigProvider>

FAQ

Why is it better to store a color object?

Converting strings of different formats can round off channels. Save the object from the callback when editing, and receive the string at the display or send border.

Why is the RGB format but the CSS gradient does not change?

format changes the editor fields and signature. ToCssString() and the second argument to onChange provide ready-made CSS regardless of the selected editor.

How to replace React panelRender?

Pass Svelte snippet with the panel and extra arguments. Draw the original panel or extra.components.Picker and presets, as in the panel composition example.