Skip to content

Commit

Permalink
feat: add support for fallback, accent and bg muted colors
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjavi committed Sep 9, 2024
1 parent 4bf1d66 commit e312f14
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
51 changes: 41 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,50 @@ export default {

@layer base {
:root {
--background: theme("colors.background.DEFAULT");
--foreground: theme("colors.foreground.DEFAULT");
--foreground-muted: theme("colors.foreground.muted.DEFAULT");
--border: theme("colors.border.DEFAULT");
--border-muted: theme("colors.border.muted.DEFAULT");
--background: theme('colors.background.light');
--background-muted: theme('colors.background.muted.light');
--foreground: theme('colors.foreground.light');
--foreground-muted: theme('colors.foreground.muted.light');
--border: theme('colors.border.light');
--border-muted: theme('colors.border.muted.light');
}

.dark {
--background: theme("colors.background.dark");
--foreground: theme("colors.foreground.dark");
--foreground-muted: theme("colors.foreground.muted.dark");
--border: theme("colors.border.dark");
--border-muted: theme("colors.border.muted.dark");
--background: theme('colors.background.dark');
--background-muted: theme('colors.background.muted.dark');
--foreground: theme('colors.foreground.dark');
--foreground-muted: theme('colors.foreground.muted.dark');
--border: theme('colors.border.dark');
--border-muted: theme('colors.border.muted.dark');
}

*,
::before,
::after {
border-color: var(--border);
}

* {
box-sizing: border-box;
position: relative;
}

[hidden] {
display: none;
}

[inert] {
pointer-events: none;
user-select: none;
}

html {
font-size: 16px;
}

body {
background-color: var(--background);
color: var(--foreground);
}
}
```
Expand Down
2 changes: 2 additions & 0 deletions src/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@layer base {
:root {
--background: theme('colors.background.light');
--background-muted: theme('colors.background.muted.light');
--foreground: theme('colors.foreground.light');
--foreground-muted: theme('colors.foreground.muted.light');
--border: theme('colors.border.light');
Expand All @@ -13,6 +14,7 @@

.dark {
--background: theme('colors.background.dark');
--background-muted: theme('colors.background.muted.dark');
--foreground: theme('colors.foreground.dark');
--foreground-muted: theme('colors.foreground.muted.dark');
--border: theme('colors.border.dark');
Expand Down
43 changes: 42 additions & 1 deletion src/lib/create-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ type PresetConfigColors = {
* @default 'fuchsia'
*/
secondary: TailwindColor | CustomColor;
/**
* Accent color name or custom color palette object
* @default 'fuchsia'
*/
accent: TailwindColor | CustomColor;
/**
* Gray color name or custom color palette object
* @default 'neutral'
Expand All @@ -87,6 +92,11 @@ type PresetConfigColors = {
* @default 'bg-white' and 'bg-gray-950'
*/
background: DarkLightColor;
/**
* Default muted background color in light and dark modes
* @default 'bg-gray-100' and 'bg-gray-900'
*/
backgroundMuted: DarkLightColor;
/**
* Default foreground color in light and dark modes
* @default 'text-gray-950' and 'text-white'
Expand Down Expand Up @@ -130,13 +140,18 @@ const defaultConfig: PresetConfig = {
colors: {
primary: "blue",
secondary: "fuchsia",
accent: "fuchsia",
gray: "neutral",
danger: "red",
warn: "yellow",
background: {
light: "#ffffff",
dark: safeTwColors.gray[950],
},
backgroundMuted: {
light: safeTwColors.gray[100],
dark: safeTwColors.gray[900],
},
foreground: {
light: safeTwColors.gray[950],
dark: "#ffffff",
Expand Down Expand Up @@ -177,6 +192,7 @@ function resolveConfig(config: PartialPresetConfig): PresetConfig {

resolvedConfig.colors.primary = resolveColor(colors.primary);
resolvedConfig.colors.secondary = resolveColor(colors.secondary);
resolvedConfig.colors.accent = resolveColor(colors.accent);
resolvedConfig.colors.gray = resolveColor(colors.gray);
resolvedConfig.colors.danger = resolveColor(colors.danger);
resolvedConfig.colors.warn = resolveColor(colors.warn);
Expand All @@ -203,7 +219,11 @@ export function createTailwindPreset(
DEFAULT: getColorMix("var(--background)"),
light: resolvedConfig.colors.background.light,
dark: resolvedConfig.colors.background.dark,
// muted: resolvedConfig.colors.backgroundMuted,
muted: {
DEFAULT: getColorMix("var(--background-muted)"),
light: resolvedConfig.colors.backgroundMuted.light,
dark: resolvedConfig.colors.backgroundMuted.dark,
},
},
foreground: {
DEFAULT: getColorMix("var(--foreground)"),
Expand All @@ -227,9 +247,30 @@ export function createTailwindPreset(
},
primary: resolvedConfig.colors.primary,
secondary: resolvedConfig.colors.secondary,
accent: resolvedConfig.colors.accent,
gray: resolvedConfig.colors.gray,
danger: resolvedConfig.colors.danger,
warn: resolvedConfig.colors.warn,

// Just kept for original shadcn/ui compatibility, but not used by the glasscn components:
input: getColorMix("var(--border)"),
ring: "currentColor",
destructive: {
DEFAULT: resolvedConfig.colors.danger[500],
foreground: getColorMix("var(--foreground)"),
},
muted: {
DEFAULT: getColorMix("var(--background-muted)"),
foreground: getColorMix("var(--foreground-muted)"),
},
popover: {
DEFAULT: getColorMix("var(--background-muted)"),
foreground: getColorMix("var(--foreground-muted)"),
},
card: {
DEFAULT: getColorMix("var(--background-muted)"),
foreground: getColorMix("var(--foreground-muted)"),
},
},
fontWeight: {
base: "400",
Expand Down

0 comments on commit e312f14

Please sign in to comment.