Skip to content

Commit fcb7882

Browse files
authored
πŸ’„ Improve color labels with strings instead of shades (#7)
* βœ… Add test for `theme.json` color labels
1 parent d9f2a40 commit fcb7882

File tree

2 files changed

+91
-16
lines changed

2 files changed

+91
-16
lines changed

β€Žsrc/index.tsβ€Ž

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -821,17 +821,32 @@ export function wordpressThemeJson(config: ThemeJsonConfig = {}): VitePlugin {
821821
...extractVariables(patterns.COLOR, themeContent)
822822
.filter(([name]) => !name.endsWith('-*'))
823823
.map(([name, value]) => {
824-
const [colorName, shade] = name.split('-');
824+
const parts = name.split('-');
825+
const colorName = parts[0];
826+
const shade =
827+
parts.length > 1
828+
? parts.slice(1).join(' ')
829+
: undefined;
825830
const capitalizedColor =
826831
colorName.charAt(0).toUpperCase() +
827832
colorName.slice(1);
828-
const displayName =
829-
shade && !Number.isNaN(Number(shade))
830-
? config.shadeLabels &&
831-
shade in config.shadeLabels
832-
? `${config.shadeLabels[shade]} ${capitalizedColor}`
833-
: `${capitalizedColor} (${shade})`
834-
: capitalizedColor;
833+
const displayName = shade
834+
? config.shadeLabels &&
835+
shade in config.shadeLabels
836+
? `${config.shadeLabels[shade]} ${capitalizedColor}`
837+
: Number.isNaN(Number(shade))
838+
? `${capitalizedColor} (${shade
839+
.split(' ')
840+
.map(
841+
(word) =>
842+
word
843+
.charAt(0)
844+
.toUpperCase() +
845+
word.slice(1)
846+
)
847+
.join(' ')})`
848+
: `${capitalizedColor} (${shade})`
849+
: capitalizedColor;
835850

836851
return {
837852
name: displayName,
@@ -844,17 +859,32 @@ export function wordpressThemeJson(config: ThemeJsonConfig = {}): VitePlugin {
844859
? flattenColors(
845860
resolvedTailwindConfig.theme.colors
846861
).map(([name, value]) => {
847-
const [colorName, shade] = name.split('-');
862+
const parts = name.split('-');
863+
const colorName = parts[0];
864+
const shade =
865+
parts.length > 1
866+
? parts.slice(1).join(' ')
867+
: undefined;
848868
const capitalizedColor =
849869
colorName.charAt(0).toUpperCase() +
850870
colorName.slice(1);
851-
const displayName =
852-
shade && !Number.isNaN(Number(shade))
853-
? config.shadeLabels &&
854-
shade in config.shadeLabels
855-
? `${config.shadeLabels[shade]} ${capitalizedColor}`
856-
: `${capitalizedColor} (${shade})`
857-
: capitalizedColor;
871+
const displayName = shade
872+
? config.shadeLabels &&
873+
shade in config.shadeLabels
874+
? `${config.shadeLabels[shade]} ${capitalizedColor}`
875+
: Number.isNaN(Number(shade))
876+
? `${capitalizedColor} (${shade
877+
.split(' ')
878+
.map(
879+
(word) =>
880+
word
881+
.charAt(0)
882+
.toUpperCase() +
883+
word.slice(1)
884+
)
885+
.join(' ')})`
886+
: `${capitalizedColor} (${shade})`
887+
: capitalizedColor;
858888

859889
return {
860890
name: displayName,

β€Žtests/index.test.tsβ€Ž

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,51 @@ describe('wordpressThemeJson', () => {
580580
});
581581
});
582582

583+
it('should handle multi-hyphen color names', () => {
584+
const plugin = wordpressThemeJson({
585+
tailwindConfig: mockTailwindConfigPath,
586+
});
587+
588+
const cssContent = `
589+
@theme {
590+
--color-fancy-test-example: #123456;
591+
--color-button-hover-state: #234567;
592+
--color-social-twitter-blue: #1DA1F2;
593+
--color-primary: #000000;
594+
}
595+
`;
596+
597+
(plugin.transform as any)(cssContent, 'app.css');
598+
const emitFile = vi.fn();
599+
(plugin.generateBundle as any).call({ emitFile });
600+
601+
const themeJson = JSON.parse(emitFile.mock.calls[0][0].source);
602+
603+
expect(themeJson.settings.color.palette).toContainEqual({
604+
name: 'Fancy (Test Example)',
605+
slug: 'fancy-test-example',
606+
color: '#123456',
607+
});
608+
609+
expect(themeJson.settings.color.palette).toContainEqual({
610+
name: 'Button (Hover State)',
611+
slug: 'button-hover-state',
612+
color: '#234567',
613+
});
614+
615+
expect(themeJson.settings.color.palette).toContainEqual({
616+
name: 'Social (Twitter Blue)',
617+
slug: 'social-twitter-blue',
618+
color: '#1DA1F2',
619+
});
620+
621+
expect(themeJson.settings.color.palette).toContainEqual({
622+
name: 'Primary',
623+
slug: 'primary',
624+
color: '#000000',
625+
});
626+
});
627+
583628
it('should preserve existing theme.json settings', () => {
584629
const existingThemeJson = {
585630
settings: {

0 commit comments

Comments
Β (0)