Skip to content

Commit

Permalink
add first tailwind theme draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Makisuo committed May 10, 2024
1 parent c7b6171 commit 3ecf797
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 251 deletions.
15 changes: 8 additions & 7 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
"@pixelshades/ui": "workspace:*",
"@pixelshades/utils": "workspace:*",
"@radix-ui/colors": "^3.0.0",
"@tanstack/react-router": "1.31.8",
"@tanstack/router-devtools": "1.31.8",
"@tanstack/router-vite-plugin": "^1.30.0",
"@tanstack/react-router": "1.31.26",
"@tanstack/router-devtools": "1.31.26",
"@tanstack/router-vite-plugin": "^1.31.18",
"bezier-easing": "^2.1.0",
"colorjs.io": "^0.5.0",
"fuse.js": "^7.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"shiki": "^1.4.0",
"zod": "^3.23.6"
"shiki": "^1.5.1",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/react": "^18.3.1",
Expand All @@ -43,8 +44,8 @@
"typescript": "^5.4.5",
"unist-builder": "^4.0.0",
"unist-util-visit": "^5.0.0",
"velite": "^0.1.0-beta.14",
"vite": "^5.2.10",
"velite": "^0.1.0-rc.3",
"vite": "^5.2.11",
"vite-tsconfig-paths": "^4.3.2"
}
}
4 changes: 2 additions & 2 deletions apps/docs/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@layer base {
:root {
--primary-scale-1: 345.75 42.453% 3.3945%;
--primary-scale-1: 345.75 42.453% 3.3945%;
--primary-scale-2: 342.74 31.057% 9.279%;
--primary-scale-3: 338.72 53.878% 14.856%;
--primary-scale-4: 334.05 80.379% 17.76%;
Expand Down Expand Up @@ -102,7 +102,7 @@
--radius: 0.5rem;

--info: var(--info-scale-9);
--info-foreground: var(--info-scale-12);
--info-foreground: var(--info-scale-12);
}

.dark {
Expand Down
62 changes: 62 additions & 0 deletions apps/docs/src/lib/colors/tailwind-themes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Color from "colorjs.io"

export interface Shade {
name: string | number
lightness: number
}

export interface Palette {
name: string | number
color: Color
}

const baseShades: Shade[] = [
{ name: "50", lightness: 98 },
{ name: "100", lightness: 95 },
{ name: "200", lightness: 90 },
{ name: "300", lightness: 82 },
{ name: "400", lightness: 64 },
{ name: "500", lightness: 46 },
{ name: "600", lightness: 33 },
{ name: "700", lightness: 24 },
{ name: "800", lightness: 14 },
{ name: "900", lightness: 7 },
{ name: "950", lightness: 4 },
]

// This generates a color palette based on the provided color and shades.
// It takes a color string and an array of shades as input, and returns an array of palette objects.
export const generateColorPalette = ({
color,
preserve,
shades = baseShades,
}: { color: string; preserve?: boolean; shades?: Shade[] }) => {
const startColor = new Color(color).to("oklch")

const lightnessDelta: { [key: string]: number } = {}

const palette: Palette[] = shades.map((shade) => {
const color = new Color(startColor)

color.set({
"hsl.l": shade.lightness,
})

if (preserve) {
lightnessDelta[shade.name] = color.get("hsl.l") - startColor.get("hsl.l")
}

return {
name: shade.name,
color: color,
}
})

if (preserve) {
const [closestShade] = Object.keys(lightnessDelta).sort((a, b) => lightnessDelta[a] - lightnessDelta[b])

palette[palette.findIndex((shade) => shade.name === closestShade)].color = startColor
}

return palette
}
77 changes: 55 additions & 22 deletions apps/docs/src/routes/themes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
yellow,
zinc,
} from "tailwindcss/colors"
import { type Palette, type Shade, generateColorPalette } from "~/lib/colors/tailwind-themes"

export const Route = createFileRoute("/themes")({
component: Index,
Expand Down Expand Up @@ -77,17 +78,7 @@ function Index() {
background: neutralColor.value[950],
})

// console.log(
// exportTheme({
// primaryScale: darkModeResult.primaryScale,
// neutralScale: darkModeResult.neutralScale,
// infoScale: darkModeResult.infoScale,
// successScale: darkModeResult.sucessScale,
// destructiveScale: darkModeResult.destructiveScale,
// background: darkModeResult.background,
// foreground: darkModeResult.foreground,
// }),
// )
const test = generateColorPalette({ color: neutralColor.value[500] })

return (
<div className="container flex min-h-screen w-full flex-col items-center gap-layout-md py-layout-lg">
Expand Down Expand Up @@ -147,8 +138,15 @@ function Index() {
<div className="flex w-full flex-row items-center gap-md">
<Typography className="flex-1">Primary</Typography>

{darkModeResult.primaryScale.map((color, i) => (
{/* {darkModeResult.primaryScale.map((color, i) => (
<ColorPreview key={i} style={{ backgroundColor: color }} />
))} */}

{test.map((palette, i) => (
<ColorPreview
key={i}
style={{ backgroundColor: palette.color.to("hsl").toString({ format: "hsl" }) }}
/>
))}
</div>

Expand Down Expand Up @@ -197,32 +195,67 @@ const exportTheme = ({
background,
foreground,
}: {
primaryScale: string[]
neutralScale: string[]
successScale: string[]
infoScale: string[]
destructiveScale: string[]
primaryScale: Palette[]
neutralScale: Palette[]
successScale: Palette[]
infoScale: Palette[]
destructiveScale: Palette[]
background: string
foreground: string
}) => {
const primaryScaleString = primaryScale
.map((color, index) => `--primary-scale-${index + 1}: ${color.replace("hsl(", "").replace(")", "")};`)
.map(
(shade, index) =>
`--primary-scale-${index + 1}: ${shade.color
.to("hsl")
.toString({ format: "hsl" })
.replace("hsl(", "")
.replace(")", "")};`,
)
.join("\n\t\t")

const neutralScaleString = neutralScale
.map((color, index) => `--neutral-scale-${index + 1}: ${color.replace("hsl(", "").replace(")", "")};`)
.map(
(shade, index) =>
`--neutral-scale-${index + 1}: ${shade.color
.to("hsl")
.toString({ format: "hsl" })
.replace("hsl(", "")
.replace(")", "")};`,
)
.join("\n\t\t")

const successScaleString = successScale
.map((color, index) => `--success-scale-${index + 1}: ${color.replace("hsl(", "").replace(")", "")};`)
.map(
(shade, index) =>
`--success-scale-${index + 1}: ${shade.color
.to("hsl")
.toString({ format: "hsl" })
.replace("hsl(", "")
.replace(")", "")};`,
)
.join("\n\t\t")

const infoScaleString = infoScale
.map((color, index) => `--info-scale-${index + 1}: ${color.replace("hsl(", "").replace(")", "")};`)
.map(
(shade, index) =>
`--info-scale-${index + 1}: ${shade.color
.to("hsl")
.toString({ format: "hsl" })
.replace("hsl(", "")
.replace(")", "")};`,
)
.join("\n\t\t")

const destructiveScaleString = destructiveScale
.map((color, index) => `--destructive-scale-${index + 1}: ${color.replace("hsl(", "").replace(")", "")};`)
.map(
(shade, index) =>
`--destructive-scale-${index + 1}: ${shade.color
.to("hsl")
.toString({ format: "hsl" })
.replace("hsl(", "")
.replace(")", "")};`,
)
.join("\n\t\t")

const base = `
Expand Down
Binary file modified bun.lockb
Binary file not shown.
Loading

0 comments on commit 3ecf797

Please sign in to comment.