Skip to content

Commit

Permalink
document & add tests for color shorthand order issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredh159 committed Feb 12, 2024
1 parent 02cb50d commit d3716f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/__tests__/colors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe(`colors`, () => {
test(`color with opacity shorthand`, () => {
expect(tw`bg-black/50`).toEqual({ backgroundColor: `rgba(0, 0, 0, 0.5)` });
expect(tw`text-red-300/75`).toEqual({ color: `rgba(252, 165, 165, 0.75)` });
// shorthand preferred
expect(tw`bg-black/50 bg-opacity-75`).toEqual({
backgroundColor: `rgba(0, 0, 0, 0.5)`,
});
});

test(`bg colors with customized configs`, () => {
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/dark-mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ describe(`dark mode`, () => {
backgroundColor: `#fff`,
});

// ignores dark:bg-opacity-25 when merging, not dark mode
expect(tw`bg-white dark:bg-white/50 dark:bg-opacity-25`).toEqual({
backgroundColor: `#fff`,
});

// merges bg-opacity-50
expect(tw`bg-white dark:bg-white/75 bg-opacity-50`).toEqual({
backgroundColor: `rgba(255, 255, 255, 0.5)`,
});

tw.setColorScheme(`dark`);

expect(tw`bg-gray-100/50 dark:bg-gray-800/50`).toEqual({
Expand All @@ -48,5 +58,10 @@ describe(`dark mode`, () => {
expect(tw`bg-white dark:bg-white/50`).toEqual({
backgroundColor: `rgba(255, 255, 255, 0.5)`,
});

// shorthand opacity wins over "merged" bg-opacity-X
expect(tw`bg-white dark:bg-white/50 bg-opacity-75 dark:bg-opacity-25`).toEqual({
backgroundColor: `rgba(255, 255, 255, 0.5)`,
});
});
});
3 changes: 3 additions & 0 deletions src/resolve/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export function color(
if (!Number.isNaN(opacity)) {
color = addOpacity(color, opacity / 100);
return {
// even though we know the bg opacity, return `dependent` to work around
// subtle dark-mode ordering issue when combining shorthand & non-shorthand
// @see https://github.com/jaredh159/tailwind-react-native-classnames/pull/269
kind: `dependent`,
complete(style) {
style[STYLE_PROPS[type].color] = color;
Expand Down

0 comments on commit d3716f6

Please sign in to comment.