Skip to content
Star

Gradient API Reference

Everything on this page is exported from @vuelor/gradient. For the color-picker components used alongside it, see the picker API Reference.

Components

NameDescription
GradientPickerRoot.vueRoot wrapper that owns the gradient state (stops, selection, type, angle) and provides context to all child components.
GradientPickerSlider.vueMulti-thumb stops slider with crossing-safe selection — dragging a stop past a neighbour keeps its color and selection attached.
GradientPickerPositionInput.vueText input editing a stop's position as a percentage.
GradientPickerAngleInput.vueText input editing the gradient angle in degrees. Disables itself for radial gradients.
GradientPickerAddStop.vueButton that splits the selected segment at its position/color midpoint.
GradientPickerRemoveStop.vueButton that removes a stop and reselects its left neighbour.
GradientPickerReverse.vueButton that mirrors the gradient — positions and colors reverse together.
GradientPickerRotate.vueButton that rotates the angle by a step (90° by default). Disables itself for radial gradients.
GradientPickerPreview.vueElement whose background renders the current gradient (or the flat stop strip).

GradientPickerRoot.vue

The root component. Must wrap all other GradientPicker components. It creates the useGradient() engine and broadcasts it via Vue's provide/inject.

PropDefaultDescription
modelValue-Current value as a CSS gradient string, or null. See The model for the grammar.
defaultValuelinear-gradient(90deg, #FF98C2FF 0%, #FFFA7AFF 100%)Applied when modelValue is null. Warns in dev if it doesn't parse.
stylingtailwindcsstailwindcss — built-in Tailwind classes. vanillacss — plain vuelor-gradient-* class names. unstyled — strips all base styles.
disabledfalseDisables all interaction across every child component.
minStops2Read once at mount. Bounds the remove-stop interaction only; never below 2 (the CSS minimum).
maxStops8Read once at mount. Bounds the add-stop interaction only — a parsed modelValue keeps however many stops it declares.
class-Extra classes applied to the root <div>.
ui-Override styles for any slot across all child components. See GradientThemeSlots.
EmitDescription
update:modelValueFires on every gradient change — including each tick of a thumb drag.
valueCommitFires once the user finishes an interaction (pointer up, input blur, button click). Identical re-commits are deduplicated; a parent write resets the baseline.

An unsupported modelValue warns in dev and leaves the editor state untouched — a bad write can never wipe stops mid-edit. Writing null resets to defaultValue, mirroring ColorPickerRoot.

Slot props

The default slot exposes the live engine, so custom controls need no extra wiring:

NameTypeDescription
gradientUseGradientReturnThe full useGradient() instance backing this root.
commitValue() => voidSignals the end of a custom interaction, emitting valueCommit.

Both are also available on a template ref (defineExpose), and child components can reach them with injectGradientPickerContext().

GradientThemeSlots

Passed to the ui prop of GradientPickerRoot to override styles globally. Any slot can also be overridden locally on the child component's own ui prop.

ts
type GradientThemeSlots = {
  root: {
    root: string         // The root <div> of GradientPickerRoot
  },
  slider: {
    root: string         // The stops-slider wrapper
    track: string        // The slider track (renders the flat gradient strip)
    thumb: string        // Each stop thumb (selected state via [data-selected])
    thumbSwatch: string  // The color chip inside the default thumb
  },
  input: {
    group: string        // The outer wrapper of the position/angle inputs
    item: string         // The inner cell
    field: string        // The <input> element
  },
  button: {
    root: string         // Add / Remove / Reverse / Rotate buttons
  },
  preview: {
    root: string         // The Preview element
  }
}

GradientPickerSlider.vue

The multi-thumb stops slider. Dragging or arrowing a thumb moves its stop; crossing a neighbour re-sorts the stops while colors and the selection stay attached to the moved stop. Clicking or Tab-focusing a thumb selects its stop. Keyboard: arrows move by 1, PageUp/PageDown by 10, Home/End to the ends.

PropDefaultDescription
class-Extra classes applied to the slider root.
ui-Partial<{ root, track, thumb, thumbSwatch }> — overrides slider slots.

Slots:

NameDescription
thumbReplaces the default swatch inside each thumb. Scoped props: { stop, index, selected }.
html
<GradientPickerSlider>
  <template #thumb="{ stop, selected }">
    <span :class="selected ? 'ring-2' : ''">{{ stop.position }}</span>
  </template>
</GradientPickerSlider>

GradientPickerPositionInput.vue

Edits a stop's position as NN%. Accepts bare numbers or %-suffixed text, clamps to 0–100, and commits on blur or Enter — only when the value genuinely changed. Typing a position past a neighbour re-sorts the stops.

PropDefaultDescription
stopId-The stop to edit. Omitted, the input follows the selected stop. A provided id that no longer resolves renders the input inert (disabled and empty) instead of retargeting.
labelStop positionThe aria-label of the field.
class-Extra classes applied to the input group.
ui-Partial<{ group, item, field }>

GradientPickerAngleInput.vue

Edits the gradient angle as NN°. Accepts bare numbers, ° or deg suffixes, normalizes into 0–359, and commits on blur or Enter when changed. Disables itself while the type is radial (the radial serializer has no angle).

PropDefaultDescription
labelGradient angleThe aria-label of the field.
class-Extra classes applied to the input group.
ui-Partial<{ group, item, field }>

Action buttons

GradientPickerAddStop, GradientPickerRemoveStop, GradientPickerReverse, and GradientPickerRotate share the same shape: a <button> wired to the engine with a built-in icon, aria-label, and disabled logic. Each accepts the default slot to replace the icon.

PropDefaultDescription
labelper componentOverrides the aria-label.
class-Extra classes applied to the button.
ui-Partial<{ root }>

Component specifics:

ComponentExtra propsBehavior
GradientPickerAddStop-Splits the segment right of the selected stop (left when it's the last) at its position/color midpoint; selects the new stop. Disabled at maxStops.
GradientPickerRemoveStopstopId (selected)Removes the stop and reselects its left neighbour. Disabled at minStops.
GradientPickerReverse-Mirrors positions and colors together — unevenly spaced stops reverse correctly.
GradientPickerRotatedegrees (90)Adds degrees to the angle, wrapping into 0–359. Disabled for radial gradients.

GradientPickerPreview.vue

An element whose background is bound to the current gradient. Useful as a live preview or a trigger surface.

PropDefaultDescription
asdivThe HTML element or component to render as.
trackfalseWhen true, renders the flat left-to-right stop strip (the slider-track view) instead of the real gradient.
class-Extra classes applied to the element.
ui-Partial<{ root }>

Slots: Default slot — render any content on top of the gradient.

useGradient()

The reactive engine behind GradientPickerRoot — usable standalone for fully custom UIs.

ts
import { useGradient } from '@vuelor/gradient'

const gradient = useGradient({
  type: 'linear',
  angle: 90,
  stops: [
    { color: '#FF98C2FF', position: 0 },
    { color: '#FFFA7AFF', position: 100 }
  ]
})

Options

All optional. Invalid initial stops (fewer than 2, or non-hex colors) warn in dev and fall back to the default pink→yellow pair.

OptionDefaultDescription
typelinear'linear' | 'radial' | 'conic'
angle90Degrees; normalized into 0–359.
stops2-stop defaultInitial stops as { color, position } — sorted and canonicalized on load.
minStops2Interaction floor for removeStop; never below 2.
maxStops8Interaction ceiling for addStop; setFromCSS may load more.

State and derived values

PropertyTypeDescription
typeRef<GradientType>Gradient type. Settable.
angleRef<number>Angle in degrees. Settable; used by linear and conic.
stopsRef<ManagedGradientStop[]>The stops, always sorted by position. Each carries a stable id.
selectedStopIdRef<number>Id of the selected stop.
selectedStopComputedRef<ManagedGradientStop>The selected stop; falls back to the first stop.
selectedColorWritableComputedRef<string>The selected stop's color — bind it to a ColorPickerRoot.
cssComputedRef<string>The serialized gradient — what GradientPickerRoot emits.
trackCSSComputedRef<string>Flat left-to-right strip of the stops, for tracks and previews.
stopsCSSComputedRef<string>Just the serialized stop list (#… 0%, #… 100%).
canAddStopComputedRef<boolean>stops.length < maxStops
canRemoveStopComputedRef<boolean>stops.length > minStops
minStops / maxStopsnumberThe resolved bounds.

Actions

MethodDescription
select(id)Selects a stop by id; unknown ids are ignored.
selectAt(index)Selects by current index.
addStop(position?, color?)No arguments: splits the selected segment at its midpoint. With a position: interpolates the color at that point unless one is given. Rejects invalid colors (dev-warn) and returns the new stop or null.
removeStop(id?)Removes the stop (selected by default) and reselects its left neighbour. Returns success.
moveStop(id, position)Clamps to 0–100, moves, and re-sorts; the selection follows ids, not indices.
setStopColor(id, color)Normalizes to #RRGGBBAA; invalid colors are dropped with a dev warning.
reverse()Mirrors positions and colors together.
rotate(degrees = 90)Adds to the angle with wrap-around.
setFromCSS(css)Parses and replaces the whole state; returns false (state untouched) for unsupported input. The parsed model is authoritative over the stop bounds.

Core functions

The framework-agnostic layer under the composable — importable without touching any component.

FunctionDescription
parseGradient(input)Parses a supported gradient string into { type, angle?, stops }, or null.
serializeGradient(gradient)Emits exactly the grammar parseGradient accepts, so values round-trip.
parseGradientStops(list)Parses a stop list with CSS position-defaulting rules; all-or-nothing, null on any bad segment.
serializeGradientStops(stops)'#RRGGBBAA 0%, #RRGGBBAA 100%'
stopsToTrackCSS(stops, direction?)Flat strip (linear-gradient(to right, …)) for tracks and previews.
isGradient(input)Loose shape check for dispatching a color-or-gradient model; parseGradient stays the authority.
normalizeHexa(raw)Canonicalizes 3/4/6/8-digit hex (optional #, whitespace-tolerant) to #RRGGBBAA, or null. Shares the picker's parseHex grammar.
normalizeAngle(raw)Wraps any number or numeric string into 0–359, rounded.
mixHexa(a, b, t?)Linear RGBA mix of two hexa colors (t defaults to 0.5).
midpointStop(a, b)The stop halfway between two stops in both position and color.
colorAt(stops, position)Piecewise-linear color of a sorted stop list at any position.
reverseStops(stops)Mirrored copy; extra properties (like editor ids) survive.
clampPosition(position)Rounds and clamps into 0–100.
MIN_GRADIENT_STOPS / DEFAULT_MAX_STOPSThe 2 / 8 constants behind the defaults.

Types

ts
import type {
  GradientType,          // 'linear' | 'radial' | 'conic'
  GradientStop,          // { color: string, position: number }
  ParsedGradient,        // { type: GradientType, angle?: number, stops: GradientStop[] }
  ManagedGradientStop,   // GradientStop & { id: number }
  UseGradientOptions,
  UseGradientReturn,
  GradientThemeSlots,
  GradientPickerRootProps,
  GradientPickerRootEmits,
  GradientPickerRootContext
} from '@vuelor/gradient'

injectGradientPickerContext() is also exported for building custom child components — it returns the full engine plus uiSlots, disabled, and commitValue, exactly what the built-in components consume.

Released under the MIT License.