Skip to content

ColorPicker

Rules

Basic Example

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "Color",
    value: '#ff7271',
}

Props Configuration Examples

Show Color Preview

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "Theme Color",
    value: '#ff7271',
    props: {
        showPreview: true,
        showAlpha: true,
    }
}

Custom Color Swatches

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "Color",
    value: '#ff7271',
    props: {
        swatches: [
            '#FF6B6B',
            '#4ECDC4',
            '#45B7D1',
            '#FFA07A',
            '#98D8C8',
        ],
    }
}

Disable Alpha Channel

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "Color",
    value: '#ff7271',
    props: {
        showAlpha: false,
    }
}

Different Sizes

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "Color",
    value: '#ff7271',
    props: {
        size: "large",
    }
}

Events Examples

Handle Color Changes

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "Color",
    value: '#ff7271',
    props: {
        showPreview: true,
    },
    on: {
        'update:value': (value) => {
            console.log('Color changed:', value);
        },
        complete: (value) => {
            console.log('Color selection completed:', value);
        },
    },
}

Linkage Update After Color Selection

js
const rule = [
    {
        type: "ColorPicker",
        field: "primaryColor",
        title: "Primary Color",
        value: '#ff7271',
        props: {
            showPreview: true,
        },
        inject: true,
        on: {
            complete: ($inject, value) => {
                // Generate secondary color from primary color
                const secondaryColor = lightenColor(value, 0.2);
                $inject.api.setValue('secondaryColor', secondaryColor);
            },
        },
    },
    {
        type: "ColorPicker",
        field: "secondaryColor",
        title: "Secondary Color",
        value: '#ff9999',
        props: {
            showPreview: true,
            disabled: true,
        },
    },
]

Complete configuration items: naive-ui_ColorPicker

value :String

Props

NameTypeDefault ValueDescriptionVersion
default-showbooleanundefinedDefault whether to show popup layer
default-valuestring | nullBlack value corresponding to first modeDefault color value
modesArray<'rgb' | 'hex' | 'hsl' | 'hsv'>['rgb', 'hex', 'hsl']Color formats supported by color picker, note that once you select a value in a mode, the color picker value format will follow this format
placement'top-start' | 'top' | 'top-end' | 'right-start' | 'right' | 'right-end' | 'bottom-start' | 'bottom' | 'bottom-end' | 'left-start' | 'left' | 'left-end' | 'bottom-start'Popup position of panel2.25.0
render-label(color: string | null) => VNodeChildundefinedTrigger content2.24.0
showbooleanundefinedWhether to show panel
show-alphabooleantrueWhether alpha channel can be adjusted
show-previewbooleanfalseWhether to show color preview block
size'small' | 'medium' | 'large''medium'Size of color picker
disabledbooleanfalseWhether disabled2.24.5
swatchesstring[]undefinedColor swatch values
tostring | HTMLElement'body'Unmount position of panel
valuestring | nullundefinedValue of color picker
on-complete(value: string) => voidundefinedCallback after color change is completed (not called during mouse movement)
on-update:show(value: boolean) => voidundefinedCallback when panel visibility changes
on-update:value(value: string) => voidundefinedCallback when color changes
actionsArray<'confirm'> | nullnullDisplay buttons

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.