Skip to content

Commit

Permalink
Merge pull request #12 from vnphanquang/main
Browse files Browse the repository at this point in the history
Add support for Tailwind v4 CSS theme override
  • Loading branch information
11bit authored Nov 25, 2024
2 parents c73c661 + 3e20f1a commit bb2dd2d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ npm install @evilmartians/harmony

Harmony can work as drop-in replacement for the Tailwind color palette:

### Tailwind v4

Simply import `@evilmartians/harmony/tailwind.css`:

```css
/* app.css, or anywhere within Tailwind-aware context */
@import 'tailwindcss';
@import "@evilmartians/harmony/tailwind.css";
```

### Tailwind v3

```js
// tailwind.config.js

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"require": "./dist/tailwind/index.mjs",
"types": "./dist/tailwind/index.d.ts"
},
"./tailwind.css": "./dist/tailwind/index.css",
"./base": {
"import": "./dist/base/index.mjs",
"require": "./dist/base/index.js",
Expand Down
5 changes: 5 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { buildTailwindPalette } from "./targets/tailwind.ts";
import { buildBasicPalette } from "./targets/base.ts";
import { ExportTarget, PaletteWithFallback } from "./types.ts";
import { buildCssVars } from "./targets/cssVariables.ts";
import { buildTailwindv4Palette } from "./targets/tailwind-v4.ts";

//
// Config
Expand All @@ -19,6 +20,10 @@ const EXPORT_TARGETS = [
targetDir: "tailwind",
target: buildTailwindPalette,
},
{
targetDir: "tailwind",
target: buildTailwindv4Palette,
},
{
targetDir: "css",
target: buildCssVars,
Expand Down
17 changes: 17 additions & 0 deletions scripts/targets/__snapshots__/tailwind-v4_test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const snapshot = {};

snapshot[`Tailwind v4 palette theme override 1`] = `
"
@theme {
--color-*: initial;
--color-white: #fff;
--color-black: #000;
--color-red-100:oklch(0.966797 0.0171875 20);
--color-red-500:oklch(0.742188 0.151562 20);
--color-orange-100:oklch(0.966797 0.0171875 43.3333);
}"
`;
29 changes: 29 additions & 0 deletions scripts/targets/tailwind-v4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { path } from "../deps.ts";
import type { ExportTarget } from "../types.ts";

const css = String.raw;

export const buildTailwindv4Palette: ExportTarget = async (
{ palette, targetDir },
) => {
const vars: string[] = [];

for (const [color, shades] of Object.entries(palette)) {
for (const [shade, value] of Object.entries(shades)) {
vars.push(`--color-${color}-${shade}:${value.oklch};`);
}
vars.push("");
}

const template = css`
@theme {
--color-*: initial;
--color-white: #fff;
--color-black: #000;
${vars.join("\n ")}
}`;

await Deno.writeTextFile(path.join(targetDir, "index.css"), template);
};
9 changes: 9 additions & 0 deletions scripts/targets/tailwind-v4_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { buildTailwindv4Palette } from "./tailwind-v4.ts";
import { testExportTarget } from "./testUtils.ts";

Deno.test("Tailwind v4 palette theme override", async (t) => {
await testExportTarget(t, buildTailwindv4Palette, [
"index.css",
]);
});

0 comments on commit bb2dd2d

Please sign in to comment.