Theme
Design tokens and theme utilities for consistent styling
Theme
The @mdk/core package provides a theme system with design tokens and utilities for consistent styling across your application.
Import
// Import tokens
import { colors, spacing, borderRadius, fontSize } from '@mdk/core'
// Import utilities
import {
getSystemTheme,
applyTheme,
getStoredTheme,
setStoredTheme,
} from '@mdk/core'
// Import types
import type { Theme, ThemeConfig } from '@mdk/core'Design tokens
Colors
Primary and gray color scales with 10 shades each (50-900).
import { colors } from '@mdk/core'
colors.primary[500] // 'hsl(222, 47%, 50%)'
colors.gray[900] // 'hsl(222, 47%, 11%)'Primary scale
| Token | Value | Usage |
|---|---|---|
primary.50 | hsl(222, 47%, 95%) | Lightest background |
primary.100 | hsl(222, 47%, 90%) | Light background |
primary.200 | hsl(222, 47%, 80%) | Hover states |
primary.300 | hsl(222, 47%, 70%) | Borders |
primary.400 | hsl(222, 47%, 60%) | Secondary text |
primary.500 | hsl(222, 47%, 50%) | Primary color |
primary.600 | hsl(222, 47%, 40%) | Hover on primary |
primary.700 | hsl(222, 47%, 30%) | Active states |
primary.800 | hsl(222, 47%, 20%) | Dark backgrounds |
primary.900 | hsl(222, 47%, 11.2%) | Darkest |
Gray scale
| Token | Value | Usage |
|---|---|---|
gray.50 | hsl(210, 40%, 98%) | Page background |
gray.100 | hsl(210, 40%, 96.1%) | Card background |
gray.200 | hsl(214, 32%, 91.4%) | Borders |
gray.300 | hsl(213, 27%, 84.1%) | Dividers |
gray.400 | hsl(215, 20%, 65.1%) | Placeholder text |
gray.500 | hsl(215, 16%, 47%) | Secondary text |
gray.600 | hsl(215, 19%, 35%) | Body text |
gray.700 | hsl(215, 25%, 27%) | Headings |
gray.800 | hsl(217, 33%, 17%) | Dark mode card |
gray.900 | hsl(222, 47%, 11%) | Dark mode bg |
Spacing
Consistent spacing scale for margins, padding, and gaps.
import { spacing } from '@mdk/core'
spacing[4] // '1rem' (16px)
spacing[8] // '2rem' (32px)| Token | Value | Pixels |
|---|---|---|
0 | 0 | 0px |
1 | 0.25rem | 4px |
2 | 0.5rem | 8px |
3 | 0.75rem | 12px |
4 | 1rem | 16px |
5 | 1.25rem | 20px |
6 | 1.5rem | 24px |
8 | 2rem | 32px |
10 | 2.5rem | 40px |
12 | 3rem | 48px |
16 | 4rem | 64px |
20 | 5rem | 80px |
24 | 6rem | 96px |
Border radius
Border radius scale for rounded corners.
import { borderRadius } from '@mdk/core'
borderRadius.md // '0.375rem'
borderRadius.full // '9999px'| Token | Value |
|---|---|
none | 0 |
sm | 0.125rem |
DEFAULT | 0.25rem |
md | 0.375rem |
lg | 0.5rem |
xl | 0.75rem |
2xl | 1rem |
full | 9999px |
Font size
Font size scale for typography.
import { fontSize } from '@mdk/core'
fontSize.base // '1rem'
fontSize.lg // '1.125rem'| Token | Value |
|---|---|
xs | 0.75rem |
sm | 0.875rem |
base | 1rem |
lg | 1.125rem |
xl | 1.25rem |
2xl | 1.5rem |
3xl | 1.875rem |
4xl | 2.25rem |
5xl | 3rem |
Theme utilities
Theme type
type Theme = 'light' | 'dark' | 'system'getSystemTheme
Get the user's system theme preference.
import { getSystemTheme } from '@mdk/core'
const systemTheme = getSystemTheme() // 'light' or 'dark'applyTheme
Apply a theme to the document root.
import { applyTheme } from '@mdk/core'
applyTheme('dark') // Sets dark mode
applyTheme('light') // Sets light mode
applyTheme('system') // Uses system preferenceThis function:
- Removes existing
light/darkclasses from<html> - Adds the resolved theme class
- Sets the
color-schemeCSS property
getStoredTheme
Get the stored theme from localStorage.
import { getStoredTheme } from '@mdk/core'
const storedTheme = getStoredTheme() // Theme | null
const customKey = getStoredTheme('my-theme-key')setStoredTheme
Store the theme in localStorage.
import { setStoredTheme } from '@mdk/core'
setStoredTheme('dark')
setStoredTheme('light', 'my-theme-key') // Custom storage keyUsage example
import { useEffect, useState } from 'react'
import {
applyTheme,
getStoredTheme,
setStoredTheme,
getSystemTheme,
} from '@mdk/core'
import type { Theme } from '@mdk/core'
function ThemeProvider({ children }) {
const [theme, setTheme] = useState<Theme>(() => {
return getStoredTheme() || 'system'
})
useEffect(() => {
applyTheme(theme)
setStoredTheme(theme)
}, [theme])
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
{children}
</ThemeContext.Provider>
)
}CSS custom properties
The theme system uses CSS custom properties that can be overridden:
:root {
--mdk-color-primary: hsl(222, 47%, 50%);
--mdk-color-background: hsl(210, 40%, 98%);
--mdk-color-foreground: hsl(222, 47%, 11%);
/* ... more properties */
}
.dark {
--mdk-color-background: hsl(222, 47%, 11%);
--mdk-color-foreground: hsl(210, 40%, 98%);
}
