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
Provide Defaults: Start with a set of useful default colors
Limit Count: Cap custom colors at 10-20 to avoid clutter
Validate Input: Ensure colors are valid before adding
Persist Data: Save colors to localStorage or backend
Avoid Duplicates: Check for duplicate colors before adding
Visual Feedback: Show color swatches clearly in the UI
Accessibility: Ensure sufficient contrast for readability
Troubleshooting
Colors Not Appearing
Verify the
userDefinedColorsarray is properly formattedCheck 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
Setto ensure uniqueness if needed
Last updated
Was this helpful?