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

Add support for Tailwind v4 CSS theme override #12

Merged
merged 2 commits into from
Nov 25, 2024
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
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",
]);
});

Loading