Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont merge color palettes/scales #2894

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-jars-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@evidence-dev/tailwind': patch
---

Dont merge color palettes/scales
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import defaultsDeep from 'lodash/defaultsDeep.js';
import merge from 'lodash/merge.js';
import mergeWith from 'lodash/mergeWith.js';
import { defaultThemesConfigFile } from './defaultThemesConfigFile.js';
import { computeShades } from './computeShades.js';

Expand All @@ -8,8 +8,14 @@ import { computeShades } from './computeShades.js';
* @returns {import('../../schemas/types.js').ThemesConfig}
*/
export const applyThemeDefaults = (configFile) => {
const withDefaults = defaultsDeep({}, configFile, defaultThemesConfigFile);
/** @satisfies {typeof defaultThemesConfigFile} */
const withDefaults = mergeWith({}, defaultThemesConfigFile, configFile, (_, configValue) => {
// Don't merge arrays - prevents merging users defined color palette with our defaults
if (Array.isArray(configValue) && configValue.length) {
return configValue;
}
});
const computedColors = computeShades(withDefaults.theme.colors);
merge(withDefaults.theme.colors, computedColors);
return withDefaults;
return /** @type {import('../../schemas/types.js').ThemesConfig} */ (withDefaults);
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

import { describe, it, expect } from 'vitest';
import chroma from 'chroma-js';

Expand Down Expand Up @@ -61,8 +63,8 @@ describe('applyThemeDefaults', () => {
'should have contrast >= 4.5 between $requiredColor and $computedColor in $mode mode',
({ requiredColor, computedColor, mode }) => {
const actual = applyThemeDefaults(input);
const requiredColorValue = actual.theme.colors[requiredColor][mode];
const computedColorValue = actual.theme.colors[computedColor][mode];
const requiredColorValue = actual.theme.colors[requiredColor]?.[mode];
const computedColorValue = actual.theme.colors[computedColor]?.[mode];
expect(
chroma.contrast(requiredColorValue, computedColorValue),
`Expected contrast between ${requiredColor} (${requiredColorValue}) and ${computedColor} (${computedColorValue}) to be greater than 4.5 in ${mode} mode`
Expand All @@ -81,8 +83,8 @@ describe('applyThemeDefaults', () => {
'should have contrast >= 4.5 between $bgColor and $fgColor in $mode mode',
({ bgColor, fgColor, mode }) => {
const actual = applyThemeDefaults(input);
const bgColorValue = actual.theme.colors[bgColor][mode];
const fgColorValue = actual.theme.colors[fgColor][mode];
const bgColorValue = actual.theme.colors[bgColor]?.[mode];
const fgColorValue = actual.theme.colors[fgColor]?.[mode];
expect(
chroma.contrast(bgColorValue, fgColorValue),
`Expected contrast between ${bgColor} (${bgColorValue}) and ${fgColor} (${fgColorValue}) to be greater than 7 in ${mode} mode`
Expand All @@ -107,4 +109,40 @@ describe('applyThemeDefaults', () => {
expect(actual.theme.colors.myCustomColor?.light).toBe('#abcdef');
expect(actual.theme.colors.myCustomColor?.dark).toBe('#fedcba');
});

it('should not merge default color palette into user configured default color palette', () => {
/** @satisfies {import('../../schemas/types.js').ThemesConfigFile} */
const config = {
theme: {
colorPalettes: {
default: {
light: ['colorPalettes_default_light_1', 'colorPalettes_default_light_2'],
dark: ['colorPalettes_default_dark_1', 'colorPalettes_default_dark_2']
}
}
}
};

const withDefaults = applyThemeDefaults(config);

expect(withDefaults.theme.colorPalettes.default).toEqual(config.theme.colorPalettes.default);
});

it('should not merge default color scale into user configured default color scale', () => {
/** @satisfies {import('../../schemas/types.js').ThemesConfigFile} */
const config = {
theme: {
colorScales: {
default: {
light: ['colorScales_default_light_1', 'colorScales_default_light_2'],
dark: ['colorScales_default_dark_1', 'colorScales_default_dark_2']
}
}
}
};

const withDefaults = applyThemeDefaults(config);

expect(withDefaults.theme.colorScales.default).toEqual(config.theme.colorScales.default);
});
});
Loading