Skip to content

Gradients

@ripl/core

CSS gradient string parsing and serialization.

Overview

Interfaces: GradientColorStop · LinearGradient · RadialGradient · ConicGradient

Type Aliases: Gradient · GradientType

Functions: parseGradient · isGradientString · serialiseGradient

GradientColorStop interface

A single color stop within a gradient, consisting of a CSS color and an optional offset position.

ts
interface GradientColorStop {
    color: string;
    offset?: number;
}

Properties:

PropertyTypeDescription
colorstring
offset?number | undefined

LinearGradient interface

A parsed linear gradient with angle, color stops, and optional repeating flag.

ts
interface LinearGradient {
    type: 'linear';
    repeating: boolean;
    angle: number;
    stops: GradientColorStop[];
}

Properties:

PropertyTypeDescription
type"linear"
repeatingboolean
anglenumber
stopsGradientColorStop[]

RadialGradient interface

A parsed radial gradient with shape, position, color stops, and optional repeating flag.

ts
interface RadialGradient {
    type: 'radial';
    repeating: boolean;
    shape: string;
    position: [number, number];
    stops: GradientColorStop[];
}

Properties:

PropertyTypeDescription
type"radial"
repeatingboolean
shapestring
position[number, number]
stopsGradientColorStop[]

ConicGradient interface

A parsed conic gradient with angle, position, color stops, and optional repeating flag.

ts
interface ConicGradient {
    type: 'conic';
    repeating: boolean;
    angle: number;
    position: [number, number];
    stops: GradientColorStop[];
}

Properties:

PropertyTypeDescription
type"conic"
repeatingboolean
anglenumber
position[number, number]
stopsGradientColorStop[]

Gradient type

Union of all supported gradient types.

ts
type Gradient = LinearGradient | RadialGradient | ConicGradient;

GradientType type

ts
type GradientType = Gradient['type'];

parseGradient function

Parses a CSS gradient string (linear, radial, or conic) into a structured Gradient object, or returns undefined if the string is not a recognised gradient.

ts
function parseGradient(value: string): Gradient | undefined

Parameters:

NameTypeDescription
valuestring

Returns: Gradient \| undefined


isGradientString function

Tests whether a string looks like a CSS gradient (starts with a recognised gradient function name).

ts
function isGradientString(value: string): boolean

Parameters:

NameTypeDescription
valuestring

Returns: boolean


serialiseGradient function

Serialises a structured Gradient object back into a CSS gradient string.

ts
function serialiseGradient(gradient: Gradient): string

Parameters:

NameTypeDescription
gradientGradient

Returns: string