Skip to content

Commit

Permalink
support arbitrary named colors
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredh159 committed Jul 9, 2024
1 parent 8011d31 commit 3d6459d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/__tests__/borders.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe(`borders`, () => {
[`border-white`, { borderColor: `#fff` }],
[`border-t-white`, { borderTopColor: `#fff` }],
[`border-t-[#e9c46a]`, { borderTopColor: `#e9c46a` }],
[`border-t-[steelblue]`, { borderTopColor: `steelblue` }],
[`border-blue-200`, { borderColor: `#bfdbfe` }],
[`border-black border-opacity-50`, { borderColor: `rgba(0, 0, 0, 0.5)` }],
[`border-dashed`, { borderStyle: `dashed` }],
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/colors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe(`colors`, () => {

test(`arbitrary color values`, () => {
expect(tw`bg-[#012]`).toEqual({ backgroundColor: `#012` });
expect(tw`bg-[rebeccapurple]`).toEqual({ backgroundColor: `rebeccapurple` });
expect(tw`bg-[rgba(3,4,5,0.1)]`).toEqual({
backgroundColor: `rgba(3,4,5,0.1)`,
});
Expand Down
7 changes: 4 additions & 3 deletions src/resolve/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export function color(

let color = ``;

// for arbitrary support: `bg-[#eaeaea]`, `text-[rgba(1, 1, 1, 0.5)]`
// arbitrary hex/rgb(a) support: `bg-[#eaeaea]`, `text-[rgba(1, 1, 1, 0.5)]`
if (value.startsWith(`[#`) || value.startsWith(`[rgb`)) {
color = value.slice(1, -1);

// search for color in config
// arbitrary named colors: `bg-[lemonchiffon]`
} else if (value.startsWith(`[`) && value.slice(1, -1).match(/^[a-z]{3,}$/)) {
color = value.slice(1, -1);
} else {
color = configColor(value, config) ?? ``;
}
Expand Down

0 comments on commit 3d6459d

Please sign in to comment.