User-Defined Colors

Create custom color palettes for your spreadsheet

User-defined colors allow you to extend the default color palette with custom colors that persist across your application. Users can add their own colors to color pickers for text, backgrounds, and borders.

Overview

By default, color selectors in the toolbar (background color, text color, border color) provide a standard palette. User-defined colors extend this palette with:

  • Custom brand colors: Add your organization's brand colors

  • Project-specific palettes: Create color schemes for different projects

  • User preferences: Allow users to save frequently used colors

  • Persistent colors: Colors that persist across sessions

Basic Usage

import React, { useState } from "react";
import {
  SpreadsheetProvider,
  CanvasGrid,
  Toolbar,
  BackgroundColorSelector,
  TextColorSelector,
  BorderSelector,
} from "@rowsncolumns/spreadsheet";

function SpreadsheetWithCustomColors() {
  const [userDefinedColors, setUserDefinedColors] = useState<string[]>([
    "#FF6B6B", // Coral Red
    "#4ECDC4", // Turquoise
    "#45B7D1", // Sky Blue
    "#FFA07A", // Light Salmon
  ]);

  const handleAddColor = (color: string) => {
    setUserDefinedColors((prev) => [...prev, color]);
  };

  return (
    <SpreadsheetProvider>
      <Toolbar>
        <BackgroundColorSelector
          color={currentCellFormat?.backgroundColor}
          theme={theme}
          userDefinedColors={userDefinedColors}
          onAddUserDefinedColor={handleAddColor}
          onChange={onChangeBackgroundColor}
        />

        <TextColorSelector
          color={currentCellFormat?.textFormat?.color}
          theme={theme}
          isDarkMode={isDarkMode}
          userDefinedColors={userDefinedColors}
          onAddUserDefinedColor={handleAddColor}
          onChange={onChangeTextColor}
        />

        <BorderSelector
          borders={currentCellFormat?.borders}
          theme={theme}
          isDarkMode={isDarkMode}
          userDefinedColors={userDefinedColors}
          onAddUserDefinedColor={handleAddColor}
          onChange={onChangeBorder}
        />
      </Toolbar>

      <CanvasGrid
        // ... props
      />
    </SpreadsheetProvider>
  );
}

State Management

Simple State

Use React state for basic color management:

Persistent Storage

Save colors to localStorage for persistence:

Server-Side Persistence

Save colors to your backend:

Color Format

User-defined colors support various formats:

Complete Example

Advanced Features

Color Validation

Validate colors before adding them:

Color Limits

Limit the number of custom colors:

Color Organization

Organize colors by category:

Remove Colors

Allow users to remove custom colors:

Integration with Theme Colors

User-defined colors work alongside theme colors:

Use Cases

Brand Colors

Project-Specific Palettes

User Preferences

Best Practices

  1. Provide Defaults: Start with a set of useful default colors

  2. Limit Count: Cap custom colors at 10-20 to avoid clutter

  3. Validate Input: Ensure colors are valid before adding

  4. Persist Data: Save colors to localStorage or backend

  5. Avoid Duplicates: Check for duplicate colors before adding

  6. Visual Feedback: Show color swatches clearly in the UI

  7. Accessibility: Ensure sufficient contrast for readability

Troubleshooting

Colors Not Appearing

  • Verify the userDefinedColors array is properly formatted

  • Check that color values are valid CSS colors

  • Ensure the array is passed to all color selector components

Colors Not Persisting

  • Confirm localStorage save logic is working

  • Check for browser storage limits

  • Verify localStorage keys are consistent

Duplicate Colors

  • Implement duplicate checking before adding colors

  • Use Set to ensure uniqueness if needed

Last updated

Was this helpful?