@salesforce/b2c-dx-mcp 0.4.4 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -370
- package/content/pwav3/components.md +400 -0
- package/content/pwav3/config.md +124 -0
- package/content/pwav3/data-fetching.md +213 -0
- package/content/pwav3/extensibility.md +167 -0
- package/content/pwav3/i18n.md +214 -0
- package/content/pwav3/quick-reference.md +169 -0
- package/content/pwav3/routing.md +107 -0
- package/content/pwav3/state-management.md +193 -0
- package/content/pwav3/styling.md +248 -0
- package/content/pwav3/testing.md +124 -0
- package/content/site-theming/theming-accessibility.md +126 -0
- package/content/site-theming/theming-questions.md +208 -0
- package/content/site-theming/theming-validation.md +174 -0
- package/dist/commands/mcp.d.ts +3 -3
- package/dist/commands/mcp.js +7 -7
- package/dist/registry.js +1 -1
- package/dist/services.d.ts +15 -15
- package/dist/services.js +21 -14
- package/dist/tools/adapter.d.ts +2 -2
- package/dist/tools/adapter.js +2 -2
- package/dist/tools/cartridges/index.js +1 -6
- package/dist/tools/index.d.ts +1 -4
- package/dist/tools/index.js +1 -4
- package/dist/tools/mrt/index.js +4 -9
- package/dist/tools/pwav3/index.d.ts +12 -3
- package/dist/tools/pwav3/index.js +5 -63
- package/dist/tools/pwav3/pwa-kit-development-guidelines.d.ts +9 -0
- package/dist/tools/pwav3/pwa-kit-development-guidelines.js +151 -0
- package/dist/tools/scapi/index.d.ts +1 -1
- package/dist/tools/scapi/index.js +6 -1
- package/dist/tools/scapi/scapi-custom-api-scaffold.d.ts +60 -0
- package/dist/tools/scapi/scapi-custom-api-scaffold.js +175 -0
- package/dist/tools/storefrontnext/figma/figma-to-component/figma-url-parser.d.ts +24 -0
- package/dist/tools/storefrontnext/figma/figma-to-component/figma-url-parser.js +53 -0
- package/dist/tools/storefrontnext/figma/figma-to-component/index.d.ts +42 -0
- package/dist/tools/storefrontnext/figma/figma-to-component/index.js +325 -0
- package/dist/tools/storefrontnext/figma/generate-component/decision.d.ts +40 -0
- package/dist/tools/storefrontnext/figma/generate-component/decision.js +312 -0
- package/dist/tools/storefrontnext/figma/generate-component/formatter.d.ts +9 -0
- package/dist/tools/storefrontnext/figma/generate-component/formatter.js +92 -0
- package/dist/tools/storefrontnext/figma/generate-component/index.d.ts +114 -0
- package/dist/tools/storefrontnext/figma/generate-component/index.js +98 -0
- package/dist/tools/storefrontnext/figma/map-tokens/css-parser.d.ts +71 -0
- package/dist/tools/storefrontnext/figma/map-tokens/css-parser.js +260 -0
- package/dist/tools/storefrontnext/figma/map-tokens/index.d.ts +61 -0
- package/dist/tools/storefrontnext/figma/map-tokens/index.js +234 -0
- package/dist/tools/storefrontnext/figma/map-tokens/token-matcher.d.ts +65 -0
- package/dist/tools/storefrontnext/figma/map-tokens/token-matcher.js +268 -0
- package/dist/tools/storefrontnext/index.d.ts +17 -0
- package/dist/tools/storefrontnext/index.js +10 -60
- package/dist/tools/storefrontnext/page-designer-decorator/analyzer.js +15 -0
- package/dist/tools/storefrontnext/page-designer-decorator/index.js +3 -3
- package/dist/tools/storefrontnext/{developer-guidelines.js → sfnext-development-guidelines.js} +3 -3
- package/dist/tools/storefrontnext/site-theming/color-contrast.d.ts +92 -0
- package/dist/tools/storefrontnext/site-theming/color-contrast.js +186 -0
- package/dist/tools/storefrontnext/site-theming/color-mapping.d.ts +16 -0
- package/dist/tools/storefrontnext/site-theming/color-mapping.js +131 -0
- package/dist/tools/storefrontnext/site-theming/guidance-merger.d.ts +11 -0
- package/dist/tools/storefrontnext/site-theming/guidance-merger.js +78 -0
- package/dist/tools/storefrontnext/site-theming/index.d.ts +14 -0
- package/dist/tools/storefrontnext/site-theming/index.js +122 -0
- package/dist/tools/storefrontnext/site-theming/response-builder.d.ts +16 -0
- package/dist/tools/storefrontnext/site-theming/response-builder.js +316 -0
- package/dist/tools/storefrontnext/site-theming/theming-store.d.ts +62 -0
- package/dist/tools/storefrontnext/site-theming/theming-store.js +410 -0
- package/dist/tools/storefrontnext/site-theming/types.d.ts +35 -0
- package/dist/tools/storefrontnext/site-theming/types.js +7 -0
- package/oclif.manifest.json +8 -5
- package/package.json +9 -6
- /package/content/{auth.md → sfnext/auth.md} +0 -0
- /package/content/{components.md → sfnext/components.md} +0 -0
- /package/content/{config.md → sfnext/config.md} +0 -0
- /package/content/{data-fetching.md → sfnext/data-fetching.md} +0 -0
- /package/content/{extensions.md → sfnext/extensions.md} +0 -0
- /package/content/{i18n.md → sfnext/i18n.md} +0 -0
- /package/content/{page-designer.md → sfnext/page-designer.md} +0 -0
- /package/content/{performance.md → sfnext/performance.md} +0 -0
- /package/content/{pitfalls.md → sfnext/pitfalls.md} +0 -0
- /package/content/{quick-reference.md → sfnext/quick-reference.md} +0 -0
- /package/content/{state-management.md → sfnext/state-management.md} +0 -0
- /package/content/{styling.md → sfnext/styling.md} +0 -0
- /package/content/{testing.md → sfnext/testing.md} +0 -0
- /package/dist/tools/storefrontnext/{developer-guidelines.d.ts → sfnext-development-guidelines.d.ts} +0 -0
|
@@ -3,62 +3,12 @@
|
|
|
3
3
|
* SPDX-License-Identifier: Apache-2
|
|
4
4
|
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
* Storefront Next toolset for B2C Commerce.
|
|
8
|
-
*
|
|
9
|
-
* This toolset provides MCP tools for Storefront Next development.
|
|
10
|
-
*
|
|
11
|
-
* **Implemented Tools:**
|
|
12
|
-
* - `storefront_next_development_guidelines` - Get development guidelines and best practices (GA)
|
|
13
|
-
*
|
|
14
|
-
* **Placeholder Tools (Use `--allow-non-ga-tools` flag to enable):**
|
|
15
|
-
* - `storefront_next_site_theming` - Configure site theming
|
|
16
|
-
* - `storefront_next_figma_to_component_workflow` - Convert Figma to components
|
|
17
|
-
* - `storefront_next_generate_component` - Generate new components
|
|
18
|
-
* - `storefront_next_map_tokens_to_theme` - Map design tokens
|
|
19
|
-
* - `storefront_next_generate_page_designer_metadata` - Generate Page Designer metadata
|
|
20
|
-
*
|
|
21
|
-
* @module tools/storefrontnext
|
|
22
|
-
*/
|
|
23
|
-
import { z } from 'zod';
|
|
24
|
-
import { createToolAdapter, jsonResult } from '../adapter.js';
|
|
25
|
-
import { createDeveloperGuidelinesTool } from './developer-guidelines.js';
|
|
6
|
+
import { createDeveloperGuidelinesTool } from './sfnext-development-guidelines.js';
|
|
26
7
|
import { createPageDesignerDecoratorTool } from './page-designer-decorator/index.js';
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
* the actual implementation is available.
|
|
32
|
-
*
|
|
33
|
-
* @param name - Tool name
|
|
34
|
-
* @param description - Tool description
|
|
35
|
-
* @param loadServices - Function that loads configuration and returns Services instance
|
|
36
|
-
* @returns The configured MCP tool
|
|
37
|
-
*/
|
|
38
|
-
function createPlaceholderTool(name, description, loadServices) {
|
|
39
|
-
return createToolAdapter({
|
|
40
|
-
name,
|
|
41
|
-
description: `[PLACEHOLDER] ${description}`,
|
|
42
|
-
toolsets: ['STOREFRONTNEXT'],
|
|
43
|
-
isGA: false,
|
|
44
|
-
requiresInstance: false,
|
|
45
|
-
inputSchema: {
|
|
46
|
-
message: z.string().optional().describe('Optional message to echo'),
|
|
47
|
-
},
|
|
48
|
-
async execute(args) {
|
|
49
|
-
// Placeholder implementation
|
|
50
|
-
const timestamp = new Date().toISOString();
|
|
51
|
-
return {
|
|
52
|
-
tool: name,
|
|
53
|
-
status: 'placeholder',
|
|
54
|
-
message: `This is a placeholder implementation for '${name}'. The actual implementation is coming soon.`,
|
|
55
|
-
input: args,
|
|
56
|
-
timestamp,
|
|
57
|
-
};
|
|
58
|
-
},
|
|
59
|
-
formatOutput: (output) => jsonResult(output),
|
|
60
|
-
}, loadServices);
|
|
61
|
-
}
|
|
8
|
+
import { createSiteThemingTool } from './site-theming/index.js';
|
|
9
|
+
import { createFigmaToComponentTool } from './figma/figma-to-component/index.js';
|
|
10
|
+
import { createGenerateComponentTool } from './figma/generate-component/index.js';
|
|
11
|
+
import { createMapTokensToThemeTool } from './figma/map-tokens/index.js';
|
|
62
12
|
/**
|
|
63
13
|
* Creates all tools for the STOREFRONTNEXT toolset.
|
|
64
14
|
*
|
|
@@ -73,11 +23,11 @@ export function createStorefrontNextTools(loadServices) {
|
|
|
73
23
|
return [
|
|
74
24
|
createDeveloperGuidelinesTool(loadServices),
|
|
75
25
|
createPageDesignerDecoratorTool(loadServices),
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
26
|
+
createSiteThemingTool(loadServices),
|
|
27
|
+
createPageDesignerDecoratorTool(loadServices),
|
|
28
|
+
createFigmaToComponentTool(loadServices),
|
|
29
|
+
createGenerateComponentTool(loadServices),
|
|
30
|
+
createMapTokensToThemeTool(loadServices),
|
|
81
31
|
];
|
|
82
32
|
}
|
|
83
33
|
//# sourceMappingURL=index.js.map
|
|
@@ -226,12 +226,27 @@ export function generateTypeSuggestions(propName, tsType) {
|
|
|
226
226
|
// ============================================================================
|
|
227
227
|
/**
|
|
228
228
|
* Extract component name from file content
|
|
229
|
+
*
|
|
230
|
+
* Priority order:
|
|
231
|
+
* 1. export default function X (inline default function)
|
|
232
|
+
* 2. export default X (default export of named identifier, e.g. export default ProductItem)
|
|
233
|
+
* 3. export function X (first named function export)
|
|
234
|
+
* 4. export const X =
|
|
235
|
+
* 5. fallback: 'Component'
|
|
236
|
+
*
|
|
237
|
+
* Note: (2) must be checked before (3) because files may have both "export function Foo"
|
|
238
|
+
* and "export default Bar" — the default export is the primary component.
|
|
229
239
|
*/
|
|
230
240
|
function extractComponentName(content) {
|
|
231
241
|
const defaultFunctionMatch = content.match(/export\s+default\s+function\s+(\w+)/);
|
|
232
242
|
if (defaultFunctionMatch) {
|
|
233
243
|
return defaultFunctionMatch[1];
|
|
234
244
|
}
|
|
245
|
+
// export default X where X is a named identifier (not "function")
|
|
246
|
+
const defaultNamedMatch = content.match(/export\s+default\s+(?!function\s)(\w+)/);
|
|
247
|
+
if (defaultNamedMatch) {
|
|
248
|
+
return defaultNamedMatch[1];
|
|
249
|
+
}
|
|
235
250
|
const namedFunctionMatch = content.match(/export\s+function\s+(\w+)/);
|
|
236
251
|
if (namedFunctionMatch) {
|
|
237
252
|
return namedFunctionMatch[1];
|
|
@@ -14,7 +14,7 @@ export const pageDesignerDecoratorSchema = z
|
|
|
14
14
|
.object({
|
|
15
15
|
component: z
|
|
16
16
|
.string()
|
|
17
|
-
.describe('Component name (e.g., "
|
|
17
|
+
.describe('Component name (e.g., "ProductItem", "ProductTile") or file path (e.g., "src/components/ProductItem.tsx"). ' +
|
|
18
18
|
'When a name is provided, the tool automatically searches common component directories. ' +
|
|
19
19
|
'For backward compatibility, file paths are also supported.'),
|
|
20
20
|
searchPaths: z
|
|
@@ -530,7 +530,7 @@ export function createPageDesignerDecoratorTool(loadServices) {
|
|
|
530
530
|
name: 'storefront_next_page_designer_decorator',
|
|
531
531
|
description: 'Adds Page Designer decorators (@Component, @AttributeDefinition, @RegionDefinition) to React components. ' +
|
|
532
532
|
'Two modes: autoMode=true for quick setup with defaults, or interactive mode via conversationContext.step. ' +
|
|
533
|
-
'Component discovery uses
|
|
533
|
+
'Component discovery uses --project-directory flag or SFCC_PROJECT_DIRECTORY env var. ' +
|
|
534
534
|
'Auto mode: selects suitable props, infers types, generates code immediately. ' +
|
|
535
535
|
'Interactive mode: multi-step workflow (analyze → select_props → configure_attrs → configure_regions → confirm_generation).',
|
|
536
536
|
inputSchema: pageDesignerDecoratorSchema.shape,
|
|
@@ -543,7 +543,7 @@ export function createPageDesignerDecoratorTool(loadServices) {
|
|
|
543
543
|
// Use projectDirectory from services to ensure we search in the correct project directory
|
|
544
544
|
// This prevents searches in the home folder when MCP clients spawn servers from ~
|
|
545
545
|
const services = loadServices();
|
|
546
|
-
const workspaceRoot = services.
|
|
546
|
+
const workspaceRoot = services.resolveWithProjectDirectory();
|
|
547
547
|
if (validatedArgs.autoMode === undefined && !validatedArgs.conversationContext) {
|
|
548
548
|
const fullPath = resolveComponent(validatedArgs.component, workspaceRoot, validatedArgs.searchPaths);
|
|
549
549
|
const componentInfo = componentAnalyzer.analyzeComponent(fullPath);
|
package/dist/tools/storefrontnext/{developer-guidelines.js → sfnext-development-guidelines.js}
RENAMED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* Provides critical development guidelines and best practices for building
|
|
10
10
|
* Storefront Next applications with React Server Components.
|
|
11
11
|
*
|
|
12
|
-
* @module tools/storefrontnext/
|
|
12
|
+
* @module tools/storefrontnext/sfnext-development-guidelines
|
|
13
13
|
*/
|
|
14
14
|
import { readFileSync } from 'node:fs';
|
|
15
15
|
import { createRequire } from 'node:module';
|
|
@@ -21,7 +21,7 @@ import { createToolAdapter, textResult } from '../adapter.js';
|
|
|
21
21
|
// regardless of where this module is located in the build output
|
|
22
22
|
const require = createRequire(import.meta.url);
|
|
23
23
|
const packageRoot = path.dirname(require.resolve('@salesforce/b2c-dx-mcp/package.json'));
|
|
24
|
-
const CONTENT_DIR = path.join(packageRoot, 'content');
|
|
24
|
+
const CONTENT_DIR = path.join(packageRoot, 'content', 'sfnext');
|
|
25
25
|
/**
|
|
26
26
|
* Section metadata with key and optional description.
|
|
27
27
|
* Single source of truth for all available sections.
|
|
@@ -137,4 +137,4 @@ export function createDeveloperGuidelinesTool(loadServices) {
|
|
|
137
137
|
formatOutput: (output) => textResult(output),
|
|
138
138
|
}, loadServices);
|
|
139
139
|
}
|
|
140
|
-
//# sourceMappingURL=
|
|
140
|
+
//# sourceMappingURL=sfnext-development-guidelines.js.map
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates that a string is a valid 6-digit hex color.
|
|
3
|
+
* @param hex - Hex color string to validate
|
|
4
|
+
* @returns true if valid
|
|
5
|
+
*/
|
|
6
|
+
export declare function isValidHex(hex: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Calculates the relative luminance of a color according to WCAG 2.1
|
|
9
|
+
* @param hex - Hex color string (e.g., "#635BFF")
|
|
10
|
+
* @returns Relative luminance value between 0 and 1
|
|
11
|
+
* @throws Error if hex format is invalid
|
|
12
|
+
*/
|
|
13
|
+
export declare function getLuminance(hex: string): number;
|
|
14
|
+
/**
|
|
15
|
+
* Calculates the contrast ratio between two colors according to WCAG 2.1
|
|
16
|
+
* @param color1 - First hex color string
|
|
17
|
+
* @param color2 - Second hex color string
|
|
18
|
+
* @returns Contrast ratio (1:1 to 21:1)
|
|
19
|
+
*/
|
|
20
|
+
export declare function getContrastRatio(color1: string, color2: string): number;
|
|
21
|
+
/**
|
|
22
|
+
* WCAG compliance levels
|
|
23
|
+
*/
|
|
24
|
+
export declare enum WCAGLevel {
|
|
25
|
+
AA = "AA",// 4.5:1 for normal text
|
|
26
|
+
AA_LARGE = "AA_LARGE",// 3:1 for large text
|
|
27
|
+
AAA = "AAA",// 7:1 for normal text
|
|
28
|
+
AAA_LARGE = "AAA_LARGE",// 4.5:1 for large text
|
|
29
|
+
FAIL = "FAIL"
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Determines WCAG compliance level for a contrast ratio
|
|
33
|
+
* @param ratio - Contrast ratio
|
|
34
|
+
* @param isLargeText - Whether this is for large text (18pt+ or 14pt+ bold)
|
|
35
|
+
* @returns WCAG compliance level
|
|
36
|
+
*/
|
|
37
|
+
export declare function getWCAGLevel(ratio: number, isLargeText?: boolean): WCAGLevel;
|
|
38
|
+
/**
|
|
39
|
+
* Result of color contrast validation for a single foreground/background pair.
|
|
40
|
+
*
|
|
41
|
+
* @property {string} color1 - First hex color (typically foreground)
|
|
42
|
+
* @property {string} color2 - Second hex color (typically background)
|
|
43
|
+
* @property {number} ratio - Contrast ratio (1:1 to 21:1)
|
|
44
|
+
* @property {WCAGLevel} wcagLevel - WCAG compliance level
|
|
45
|
+
* @property {boolean} passesAA - Whether the combination meets WCAG AA
|
|
46
|
+
* @property {boolean} passesAAA - Whether the combination meets WCAG AAA
|
|
47
|
+
* @property {boolean} isLargeText - Whether validation used large-text thresholds
|
|
48
|
+
* @property {string} visualAssessment - Readability assessment (excellent, good, acceptable, poor)
|
|
49
|
+
* @property {string} [recommendation] - Optional suggestion when contrast is suboptimal
|
|
50
|
+
*/
|
|
51
|
+
export interface ContrastValidationResult {
|
|
52
|
+
color1: string;
|
|
53
|
+
color2: string;
|
|
54
|
+
ratio: number;
|
|
55
|
+
wcagLevel: WCAGLevel;
|
|
56
|
+
passesAA: boolean;
|
|
57
|
+
passesAAA: boolean;
|
|
58
|
+
isLargeText: boolean;
|
|
59
|
+
visualAssessment: 'acceptable' | 'excellent' | 'good' | 'poor';
|
|
60
|
+
recommendation?: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Validates contrast between two colors
|
|
64
|
+
* @param color1 - First hex color string
|
|
65
|
+
* @param color2 - Second hex color string
|
|
66
|
+
* @param isLargeText - Whether this is for large text
|
|
67
|
+
* @returns Validation result with contrast ratio and compliance info
|
|
68
|
+
*/
|
|
69
|
+
export declare function validateContrast(color1: string, color2: string, isLargeText?: boolean): ContrastValidationResult;
|
|
70
|
+
/**
|
|
71
|
+
* Validates multiple color combinations for WCAG compliance.
|
|
72
|
+
*
|
|
73
|
+
* @param combinations - Array of foreground/background pairs with optional label and large-text flag
|
|
74
|
+
* @returns Array of validation results, each including the input label if provided
|
|
75
|
+
*/
|
|
76
|
+
export declare function validateColorCombinations(combinations: Array<{
|
|
77
|
+
foreground: string;
|
|
78
|
+
background: string;
|
|
79
|
+
isLargeText?: boolean;
|
|
80
|
+
label?: string;
|
|
81
|
+
}>): Array<ContrastValidationResult & {
|
|
82
|
+
label?: string;
|
|
83
|
+
}>;
|
|
84
|
+
/**
|
|
85
|
+
* Formats a validation result as a human-readable string for display to users.
|
|
86
|
+
*
|
|
87
|
+
* @param result - Validation result, optionally with a label for the color combination
|
|
88
|
+
* @returns Multi-line string with contrast ratio, WCAG status, and recommendation (if any)
|
|
89
|
+
*/
|
|
90
|
+
export declare function formatValidationResult(result: ContrastValidationResult & {
|
|
91
|
+
label?: string;
|
|
92
|
+
}): string;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025, Salesforce, Inc.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2
|
|
4
|
+
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* WCAG 2.1 color contrast utilities for accessibility validation.
|
|
8
|
+
*
|
|
9
|
+
* Provides luminance calculation, contrast ratio computation, and WCAG compliance
|
|
10
|
+
* checking for theming and color validation in Storefront Next.
|
|
11
|
+
*
|
|
12
|
+
* @module tools/storefrontnext/site-theming/color-contrast
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* WCAG 2.1 constants for contrast ratio calculation
|
|
16
|
+
* These values are specified in the WCAG 2.1 standard
|
|
17
|
+
*/
|
|
18
|
+
const WCAG_CONTRAST_OFFSET = 0.05; // Offset added to luminance values in contrast ratio formula
|
|
19
|
+
// Linear RGB conversion constants (sRGB to linear RGB)
|
|
20
|
+
const LINEAR_RGB_THRESHOLD = 0.039_28; // Threshold for linear RGB conversion
|
|
21
|
+
const LINEAR_RGB_DIVISOR = 12.92; // Divisor for values below threshold
|
|
22
|
+
const GAMMA_CORRECTION_OFFSET = 0.055; // Offset for gamma correction
|
|
23
|
+
const GAMMA_CORRECTION_DIVISOR = 1.055; // Divisor for gamma correction
|
|
24
|
+
const GAMMA_EXPONENT = 2.4; // Gamma exponent for sRGB
|
|
25
|
+
// Relative luminance weights (WCAG 2.1 standard)
|
|
26
|
+
const LUMINANCE_RED_WEIGHT = 0.2126;
|
|
27
|
+
const LUMINANCE_GREEN_WEIGHT = 0.7152;
|
|
28
|
+
const LUMINANCE_BLUE_WEIGHT = 0.0722;
|
|
29
|
+
/** Valid 6-digit hex color pattern (with optional # prefix) */
|
|
30
|
+
const HEX_PATTERN = /^#?[0-9A-Fa-f]{6}$/;
|
|
31
|
+
/**
|
|
32
|
+
* Validates that a string is a valid 6-digit hex color.
|
|
33
|
+
* @param hex - Hex color string to validate
|
|
34
|
+
* @returns true if valid
|
|
35
|
+
*/
|
|
36
|
+
export function isValidHex(hex) {
|
|
37
|
+
return typeof hex === 'string' && HEX_PATTERN.test(hex.trim());
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Calculates the relative luminance of a color according to WCAG 2.1
|
|
41
|
+
* @param hex - Hex color string (e.g., "#635BFF")
|
|
42
|
+
* @returns Relative luminance value between 0 and 1
|
|
43
|
+
* @throws Error if hex format is invalid
|
|
44
|
+
*/
|
|
45
|
+
export function getLuminance(hex) {
|
|
46
|
+
const trimmed = hex.trim();
|
|
47
|
+
if (!HEX_PATTERN.test(trimmed)) {
|
|
48
|
+
throw new Error(`Invalid hex color: "${hex}". Expected 6-digit hex (e.g., #635BFF).`);
|
|
49
|
+
}
|
|
50
|
+
const cleanHex = trimmed.replace('#', '');
|
|
51
|
+
// Parse RGB values
|
|
52
|
+
const r = Number.parseInt(cleanHex.slice(0, 2), 16) / 255;
|
|
53
|
+
const g = Number.parseInt(cleanHex.slice(2, 4), 16) / 255;
|
|
54
|
+
const b = Number.parseInt(cleanHex.slice(4, 6), 16) / 255;
|
|
55
|
+
// Convert to linear RGB
|
|
56
|
+
const [rs, gs, bs] = [r, g, b].map((c) => {
|
|
57
|
+
return c <= LINEAR_RGB_THRESHOLD
|
|
58
|
+
? c / LINEAR_RGB_DIVISOR
|
|
59
|
+
: ((c + GAMMA_CORRECTION_OFFSET) / GAMMA_CORRECTION_DIVISOR) ** GAMMA_EXPONENT;
|
|
60
|
+
});
|
|
61
|
+
// Calculate relative luminance
|
|
62
|
+
return LUMINANCE_RED_WEIGHT * rs + LUMINANCE_GREEN_WEIGHT * gs + LUMINANCE_BLUE_WEIGHT * bs;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Calculates the contrast ratio between two colors according to WCAG 2.1
|
|
66
|
+
* @param color1 - First hex color string
|
|
67
|
+
* @param color2 - Second hex color string
|
|
68
|
+
* @returns Contrast ratio (1:1 to 21:1)
|
|
69
|
+
*/
|
|
70
|
+
export function getContrastRatio(color1, color2) {
|
|
71
|
+
const l1 = getLuminance(color1);
|
|
72
|
+
const l2 = getLuminance(color2);
|
|
73
|
+
const lighter = Math.max(l1, l2);
|
|
74
|
+
const darker = Math.min(l1, l2);
|
|
75
|
+
return (lighter + WCAG_CONTRAST_OFFSET) / (darker + WCAG_CONTRAST_OFFSET);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* WCAG compliance levels
|
|
79
|
+
*/
|
|
80
|
+
export var WCAGLevel;
|
|
81
|
+
(function (WCAGLevel) {
|
|
82
|
+
WCAGLevel["AA"] = "AA";
|
|
83
|
+
WCAGLevel["AA_LARGE"] = "AA_LARGE";
|
|
84
|
+
WCAGLevel["AAA"] = "AAA";
|
|
85
|
+
WCAGLevel["AAA_LARGE"] = "AAA_LARGE";
|
|
86
|
+
WCAGLevel["FAIL"] = "FAIL";
|
|
87
|
+
})(WCAGLevel || (WCAGLevel = {}));
|
|
88
|
+
/**
|
|
89
|
+
* Determines WCAG compliance level for a contrast ratio
|
|
90
|
+
* @param ratio - Contrast ratio
|
|
91
|
+
* @param isLargeText - Whether this is for large text (18pt+ or 14pt+ bold)
|
|
92
|
+
* @returns WCAG compliance level
|
|
93
|
+
*/
|
|
94
|
+
export function getWCAGLevel(ratio, isLargeText = false) {
|
|
95
|
+
if (isLargeText) {
|
|
96
|
+
if (ratio >= 4.5) {
|
|
97
|
+
return WCAGLevel.AAA_LARGE;
|
|
98
|
+
}
|
|
99
|
+
if (ratio >= 3) {
|
|
100
|
+
return WCAGLevel.AA_LARGE;
|
|
101
|
+
}
|
|
102
|
+
return WCAGLevel.FAIL;
|
|
103
|
+
}
|
|
104
|
+
if (ratio >= 7) {
|
|
105
|
+
return WCAGLevel.AAA;
|
|
106
|
+
}
|
|
107
|
+
if (ratio >= 4.5) {
|
|
108
|
+
return WCAGLevel.AA;
|
|
109
|
+
}
|
|
110
|
+
return WCAGLevel.FAIL;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Validates contrast between two colors
|
|
114
|
+
* @param color1 - First hex color string
|
|
115
|
+
* @param color2 - Second hex color string
|
|
116
|
+
* @param isLargeText - Whether this is for large text
|
|
117
|
+
* @returns Validation result with contrast ratio and compliance info
|
|
118
|
+
*/
|
|
119
|
+
export function validateContrast(color1, color2, isLargeText = false) {
|
|
120
|
+
const ratio = getContrastRatio(color1, color2);
|
|
121
|
+
const wcagLevel = getWCAGLevel(ratio, isLargeText);
|
|
122
|
+
const passesAA = ratio >= (isLargeText ? 3 : 4.5);
|
|
123
|
+
const passesAAA = ratio >= (isLargeText ? 4.5 : 7);
|
|
124
|
+
// Visual assessment based on ratio
|
|
125
|
+
let visualAssessment;
|
|
126
|
+
let recommendation;
|
|
127
|
+
if (ratio >= 7) {
|
|
128
|
+
visualAssessment = 'excellent';
|
|
129
|
+
}
|
|
130
|
+
else if (ratio >= 5) {
|
|
131
|
+
visualAssessment = 'good';
|
|
132
|
+
}
|
|
133
|
+
else if (ratio >= 4.5) {
|
|
134
|
+
visualAssessment = 'acceptable';
|
|
135
|
+
recommendation =
|
|
136
|
+
'Meets minimum WCAG AA but may be difficult to read, especially for body text. Consider using a darker/lighter color for better readability.';
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
visualAssessment = 'poor';
|
|
140
|
+
recommendation =
|
|
141
|
+
'Does not meet WCAG AA standards. Text will be difficult to read. Strongly recommend using a color with better contrast.';
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
color1,
|
|
145
|
+
color2,
|
|
146
|
+
ratio,
|
|
147
|
+
wcagLevel,
|
|
148
|
+
passesAA,
|
|
149
|
+
passesAAA,
|
|
150
|
+
isLargeText,
|
|
151
|
+
visualAssessment,
|
|
152
|
+
recommendation,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Validates multiple color combinations for WCAG compliance.
|
|
157
|
+
*
|
|
158
|
+
* @param combinations - Array of foreground/background pairs with optional label and large-text flag
|
|
159
|
+
* @returns Array of validation results, each including the input label if provided
|
|
160
|
+
*/
|
|
161
|
+
export function validateColorCombinations(combinations) {
|
|
162
|
+
return combinations.map((combo) => ({
|
|
163
|
+
...validateContrast(combo.foreground, combo.background, combo.isLargeText ?? false),
|
|
164
|
+
label: combo.label,
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Formats a validation result as a human-readable string for display to users.
|
|
169
|
+
*
|
|
170
|
+
* @param result - Validation result, optionally with a label for the color combination
|
|
171
|
+
* @returns Multi-line string with contrast ratio, WCAG status, and recommendation (if any)
|
|
172
|
+
*/
|
|
173
|
+
export function formatValidationResult(result) {
|
|
174
|
+
const label = result.label ? `${result.label}: ` : '';
|
|
175
|
+
const textType = result.isLargeText ? 'large text' : 'normal text';
|
|
176
|
+
const wcagStatus = result.passesAAA ? '✅ AAA' : result.passesAA ? '✅ AA' : '❌ FAIL';
|
|
177
|
+
let output = `${label}${result.color1} on ${result.color2}\n`;
|
|
178
|
+
output += ` Contrast Ratio: ${result.ratio.toFixed(2)}:1\n`;
|
|
179
|
+
output += ` WCAG ${textType}: ${wcagStatus}\n`;
|
|
180
|
+
output += ` Visual Assessment: ${result.visualAssessment.toUpperCase()}\n`;
|
|
181
|
+
if (result.recommendation) {
|
|
182
|
+
output += ` ⚠️ ${result.recommendation}\n`;
|
|
183
|
+
}
|
|
184
|
+
return output;
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=color-contrast.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** A foreground/background color pair for contrast validation */
|
|
2
|
+
export type ColorCombination = {
|
|
3
|
+
foreground: string;
|
|
4
|
+
background: string;
|
|
5
|
+
label: string;
|
|
6
|
+
isLargeText?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Builds foreground/background color combinations from a semantic color mapping.
|
|
10
|
+
* Derives pairs for text-on-background, button text, links, etc.
|
|
11
|
+
*/
|
|
12
|
+
export declare function buildColorCombinations(colorMapping: Record<string, string>): ColorCombination[];
|
|
13
|
+
/**
|
|
14
|
+
* Appends WCAG color contrast validation results to the given instructions string.
|
|
15
|
+
*/
|
|
16
|
+
export declare function appendValidationSection(internalInstructions: string, combinations: ColorCombination[]): string;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025, Salesforce, Inc.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2
|
|
4
|
+
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Derives foreground/background color combinations from a color mapping and
|
|
8
|
+
* appends WCAG validation results to response text.
|
|
9
|
+
*
|
|
10
|
+
* @module tools/storefrontnext/site-theming/color-mapping
|
|
11
|
+
*/
|
|
12
|
+
import { validateColorCombinations, formatValidationResult, isValidHex } from './color-contrast.js';
|
|
13
|
+
function tryTextCombo(key, color, keyLower, ctx) {
|
|
14
|
+
if (keyLower.includes('text') && keyLower.includes('light') && isValidHex(ctx.lightBg)) {
|
|
15
|
+
return { foreground: color, background: ctx.lightBg, label: `${key}: ${color} on light background (${ctx.lightBg})` };
|
|
16
|
+
}
|
|
17
|
+
if (keyLower.includes('text') && keyLower.includes('dark') && isValidHex(ctx.darkBg)) {
|
|
18
|
+
return { foreground: color, background: ctx.darkBg, label: `${key}: ${color} on dark background (${ctx.darkBg})` };
|
|
19
|
+
}
|
|
20
|
+
const isButtonText = keyLower === 'buttontext' || (keyLower.includes('button') && keyLower.includes('text'));
|
|
21
|
+
if (isButtonText && isValidHex(ctx.buttonBg)) {
|
|
22
|
+
return {
|
|
23
|
+
foreground: color,
|
|
24
|
+
background: ctx.buttonBg,
|
|
25
|
+
label: `${key}: ${color} on button background (${ctx.buttonBg})`,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if (keyLower.includes('link') && isValidHex(ctx.lightBg)) {
|
|
29
|
+
return { foreground: color, background: ctx.lightBg, label: `${key}: ${color} on light background (${ctx.lightBg})` };
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
function tryBackgroundCombo(key, color, ctx) {
|
|
34
|
+
const foregroundKey = key.replace(/Background|Bg/i, 'Text') || key.replace(/Background|Bg/i, 'Foreground');
|
|
35
|
+
const foreground = ctx.colorMapping[foregroundKey] || ctx.colorMapping[`${key.replace(/Background|Bg/i, '')}Text`];
|
|
36
|
+
if (foreground?.startsWith('#') && isValidHex(foreground)) {
|
|
37
|
+
return { foreground, background: color, label: `${foregroundKey || 'text'} (${foreground}) on ${key} (${color})` };
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
function tryTextForegroundCombo(key, color, keyLower, ctx) {
|
|
42
|
+
const backgroundKey = key.replace(/Text|Foreground/i, 'Background') || key.replace(/Text|Foreground/i, 'Bg');
|
|
43
|
+
let background = ctx.colorMapping[backgroundKey];
|
|
44
|
+
let backgroundLabel = backgroundKey;
|
|
45
|
+
if (!background) {
|
|
46
|
+
background = keyLower.includes('button') ? ctx.buttonBg : keyLower.includes('dark') ? ctx.darkBg : ctx.lightBg;
|
|
47
|
+
backgroundLabel = keyLower.includes('button')
|
|
48
|
+
? 'button background'
|
|
49
|
+
: keyLower.includes('dark')
|
|
50
|
+
? 'dark background'
|
|
51
|
+
: 'light background';
|
|
52
|
+
}
|
|
53
|
+
if (background?.startsWith('#') && isValidHex(background)) {
|
|
54
|
+
return { foreground: color, background, label: `${key} (${color}) on ${backgroundLabel} (${background})` };
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
function tryComboForEntry(key, color, ctx) {
|
|
59
|
+
const keyLower = key.toLowerCase();
|
|
60
|
+
const textCombo = tryTextCombo(key, color, keyLower, ctx);
|
|
61
|
+
if (textCombo)
|
|
62
|
+
return textCombo;
|
|
63
|
+
if (keyLower.includes('background') || keyLower.includes('bg'))
|
|
64
|
+
return tryBackgroundCombo(key, color, ctx);
|
|
65
|
+
if (keyLower.includes('text') || keyLower.includes('foreground'))
|
|
66
|
+
return tryTextForegroundCombo(key, color, keyLower, ctx);
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Builds foreground/background color combinations from a semantic color mapping.
|
|
71
|
+
* Derives pairs for text-on-background, button text, links, etc.
|
|
72
|
+
*/
|
|
73
|
+
export function buildColorCombinations(colorMapping) {
|
|
74
|
+
const ctx = {
|
|
75
|
+
colorMapping,
|
|
76
|
+
lightBg: colorMapping.lightBackground || colorMapping.background || '#FFFFFF',
|
|
77
|
+
darkBg: colorMapping.darkBackground || '#18181B',
|
|
78
|
+
buttonBg: colorMapping.buttonBackground || colorMapping.primary || '#0A2540',
|
|
79
|
+
};
|
|
80
|
+
const combinations = [];
|
|
81
|
+
for (const [key, color] of Object.entries(colorMapping)) {
|
|
82
|
+
if (!color || !color.startsWith('#') || !isValidHex(color))
|
|
83
|
+
continue;
|
|
84
|
+
const combo = tryComboForEntry(key, color, ctx);
|
|
85
|
+
if (combo)
|
|
86
|
+
combinations.push(combo);
|
|
87
|
+
}
|
|
88
|
+
if (combinations.length === 0) {
|
|
89
|
+
const whiteBg = '#FFFFFF';
|
|
90
|
+
const darkBgFallback = '#18181B';
|
|
91
|
+
for (const [key, color] of Object.entries(colorMapping)) {
|
|
92
|
+
if (!color || !color.startsWith('#') || !isValidHex(color))
|
|
93
|
+
continue;
|
|
94
|
+
if (key.toLowerCase().includes('background') || key.toLowerCase().includes('bg'))
|
|
95
|
+
continue;
|
|
96
|
+
combinations.push({ foreground: color, background: whiteBg, label: `${key} (${color}) on white background` }, { foreground: color, background: darkBgFallback, label: `${key} (${color}) on dark background` });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return combinations;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Appends WCAG color contrast validation results to the given instructions string.
|
|
103
|
+
*/
|
|
104
|
+
export function appendValidationSection(internalInstructions, combinations) {
|
|
105
|
+
if (combinations.length === 0) {
|
|
106
|
+
return internalInstructions;
|
|
107
|
+
}
|
|
108
|
+
const results = validateColorCombinations(combinations);
|
|
109
|
+
let output = internalInstructions;
|
|
110
|
+
for (const result of results) {
|
|
111
|
+
output += formatValidationResult(result);
|
|
112
|
+
output += '\n';
|
|
113
|
+
}
|
|
114
|
+
const hasIssues = results.some((r) => !r.passesAA || r.visualAssessment === 'poor' || r.visualAssessment === 'acceptable');
|
|
115
|
+
if (hasIssues) {
|
|
116
|
+
output += '### ⚠️ VALIDATION SUMMARY\n\n';
|
|
117
|
+
output += '**Issues found that should be addressed:**\n\n';
|
|
118
|
+
for (const result of results.filter((r) => !r.passesAA || r.visualAssessment === 'poor' || r.visualAssessment === 'acceptable')) {
|
|
119
|
+
output += `- ${result.label || 'Color combination'}: ${result.recommendation || 'Needs improvement'}\n`;
|
|
120
|
+
}
|
|
121
|
+
output += '\n';
|
|
122
|
+
output +=
|
|
123
|
+
'**You MUST present these findings to the user BEFORE implementing and wait for their confirmation.**\n\n';
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
output += '### ✅ VALIDATION SUMMARY\n\n';
|
|
127
|
+
output += 'All color combinations meet WCAG AA standards and have good visual assessment.\n\n';
|
|
128
|
+
}
|
|
129
|
+
return output;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=color-mapping.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merges multiple ThemingGuidance objects from different theming files.
|
|
3
|
+
*
|
|
4
|
+
* @module tools/storefrontnext/site-theming/guidance-merger
|
|
5
|
+
*/
|
|
6
|
+
import type { ThemingGuidance } from './theming-store.js';
|
|
7
|
+
/**
|
|
8
|
+
* Merges multiple ThemingGuidance objects into one.
|
|
9
|
+
* Questions are deduplicated by ID; guidelines, rules, workflows, and validations are combined.
|
|
10
|
+
*/
|
|
11
|
+
export declare function mergeGuidance(guidanceArray: ThemingGuidance[]): ThemingGuidance;
|