npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ldrender/gradient-picker

v2.0.4

Published

A gradient picker component

Downloads

370

Readme

Features

  • 🎨 Intuitive color stop management
  • 📐 Support for linear and radial gradients
  • 🔄 Multiple direction modes (select or degree)
  • 💾 Flexible output formats (CSS string, object, or stops list)
  • 📱 Mobile-friendly with touch support
  • 🎯 Precise control with numeric inputs
  • 🖼️ Optional preview window
  • 🌈 Comprehensive color format support (Hex, RGB, HSL, Named Colors)
  • 🔍 CSS gradient string parsing
  • 💨 Performance optimized with color caching
  • ✅ Built-in validation
  • 📝 Named color support (140+ CSS colors)

Installation

npm install @ldrender/gradient-picker

Development

# Start development server
npm run dev

# Build for production
npm run build

Quick Start

import GradientPicker from '@ldrender/gradient-picker';
import '@ldrender/gradient-picker/dist/gradient-picker.css';

const gradientPicker = new GradientPicker({
  el: '#gradient-picker',
  preview: true,
  stops: [
    { color: '#ff0000', offset: 0 },
    { color: 'rgb(0, 255, 0)', offset: 33 },
    { color: 'blue', offset: 66 },
    { color: 'hsl(270, 100%, 50%)', offset: 100 }
  ]
});

Configuration

Constructor Options

interface GradientPickerProps {
  el: string;                    // Selector for the target element
  stops?: GradientStop[];        // Initial color stops
  type?: 'linear' | 'radial';    // Gradient type (default: 'linear')
  direction?: string | number;    // Gradient direction (default: 'right')
  directionType?: 'select' | 'percent'; // Direction input type (default: 'select')
  directionRadial?: boolean;     // For gradient type 'radial', select if direction input is displayed (default: true)
  returnType?: 'string' | 'object' | 'stops-list'; // Output format (default: 'string')
  preview?: boolean;             // Enable preview window (default: false)
}

interface GradientStop {
  color: string;   // CSS color value
  offset: number;  // Position in percentage (0-100)
  id: number;      // Unique identifier
}

Return Types

  • string: CSS gradient string
  • object: Gradient configuration object
  • stops-list: Array of color stops only

Methods

Core Methods

getGradient()

Returns the gradient configuration as an object.

interface GradientObject {
  type: 'linear' | 'radial';
  direction: string | number;
  stops: Array<{ color: string; offset: number; }>;
}

getStops()

Returns an array of color stops.

addColorStop(color: string, offset: number)

Adds a new color stop to the gradient.

Additional Methods

importFromCSSString(gradientStr: string)

Parses and imports a CSS gradient string.

gradientPicker.importFromCSSString('linear-gradient(to right, #ff0000 0%, #00ff00 50%)');

initDirection(directionType: 'select' | 'percent')

Changes the direction input type dynamically.

Events

Change Event

Fired whenever the gradient is modified:

document.querySelector('#gradient-picker').addEventListener('change', (event) => {
  const value = event.target.value;
  // value format depends on returnType option
});

Error Handling

The picker includes built-in validation for:

  • Color formats
  • Stop positions (0-100)
  • Minimum number of stops (2)
  • Direction values
try {
  gradientPicker.addColorStop('invalid-color', 50);
} catch (error) {
  console.error('Invalid color format');
}

Gradient Type Support

Linear Gradients

  • integer: 0 - 360
  • string: top, right, bottom, left

Radial Gradients

  • string: top, right, bottom, left, center
  • integer: 0 = at center
  • integer: 1 - 89 = at center top
  • integer: 90 - 179 = at center right,
  • integer: 180 - 269 = at center bottom,
  • integer: 270 - 359 = at center left
  • integer: 360 = at center top

Color Support

Supported Formats

  • Hexadecimal: #ff0000, #f00
  • RGB/RGBA: rgb(255, 0, 0), rgba(255, 0, 0, 0.5)
  • HSL/HSLA: hsl(0, 100%, 50%), hsla(0, 100%, 50%, 0.5)
  • Named Colors: red, blue, forestgreen, etc.

Examples

// Hexadecimal
gradientPicker.addColorStop('#ff0000', 0);    // Standard hex
gradientPicker.addColorStop('#f00', 0);       // Short hex

// RGB/RGBA
gradientPicker.addColorStop('rgb(255, 0, 0)', 0);
gradientPicker.addColorStop('rgba(255, 0, 0, 0.5)', 0);

// HSL/HSLA
gradientPicker.addColorStop('hsl(0, 100%, 50%)', 0);
gradientPicker.addColorStop('hsla(0, 100%, 50%, 0.5)', 0);

// Named Colors
gradientPicker.addColorStop('red', 0);
gradientPicker.addColorStop('blue', 50);
gradientPicker.addColorStop('green', 100);

Color Caching

The picker includes an optimized color normalization system with caching for improved performance.

Named Colors

Supports all standard CSS color names (140+ colors). Full list available in the source code. Named Colors List supporter

Browser Support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Edge (latest)
  • Mobile browsers (iOS Safari, Android Chrome)

Performance Considerations

  • Efficient color caching system
  • Debounced updates for smooth interactions
  • Optimized DOM manipulation
  • Event delegation for color stops

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.