TypeScript API
Programmatic theme management utilities.
Installation
typescript
import {
resolveTheme,
applyTheme,
getCSSVariableValue,
setCSSVariable,
getCurrentTheme,
watchSystemTheme,
type ThemeMode,
type ResolvedTheme,
} from '@3lens/themes';1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Types
ThemeMode
typescript
type ThemeMode = 'dark' | 'light' | 'high-contrast' | 'auto';1
ResolvedTheme
typescript
type ResolvedTheme = 'dark' | 'light' | 'high-contrast';1
Functions
resolveTheme()
Resolve theme mode to actual theme. 'auto' resolves based on system preference.
typescript
function resolveTheme(mode: ThemeMode): ResolvedTheme;1
Example:
typescript
import { resolveTheme } from '@3lens/themes';
const theme = resolveTheme('auto'); // Returns 'dark' or 'light'
const darkTheme = resolveTheme('dark'); // Returns 'dark'1
2
3
4
2
3
4
applyTheme()
Apply a theme by loading the appropriate CSS.
typescript
function applyTheme(theme: ResolvedTheme): void;1
Example:
typescript
import { applyTheme } from '@3lens/themes';
applyTheme('dark');
applyTheme('light');
applyTheme('high-contrast');1
2
3
4
5
2
3
4
5
getCSSVariableValue()
Get the computed value of a CSS variable.
typescript
function getCSSVariableValue(token: string, element?: HTMLElement): string;1
Example:
typescript
import { getCSSVariableValue } from '@3lens/themes';
const primaryColor = getCSSVariableValue('bg-primary');
// Returns: '#0a0e14' (in dark theme)
const accentColor = getCSSVariableValue('accent-blue');
// Returns: '#60a5fa' (in dark theme)
// Get from specific element
const element = document.querySelector('.my-element');
const color = getCSSVariableValue('bg-primary', element);1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
setCSSVariable()
Set a CSS variable value.
typescript
function setCSSVariable(token: string, value: string, element?: HTMLElement): void;1
Example:
typescript
import { setCSSVariable } from '@3lens/themes';
// Set global variable
setCSSVariable('bg-primary', '#000000');
// Set on specific element
const element = document.querySelector('.my-element');
setCSSVariable('bg-primary', '#ff0000', element);1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
getCurrentTheme()
Get the currently applied theme.
typescript
function getCurrentTheme(): ResolvedTheme | null;1
Example:
typescript
import { getCurrentTheme } from '@3lens/themes';
const currentTheme = getCurrentTheme();
// Returns: 'dark' | 'light' | 'high-contrast' | null1
2
3
4
2
3
4
watchSystemTheme()
Watch for system theme preference changes.
typescript
function watchSystemTheme(callback: (theme: ResolvedTheme) => void): () => void;1
Example:
typescript
import { watchSystemTheme, applyTheme } from '@3lens/themes';
// Watch for system theme changes
const unwatch = watchSystemTheme((theme) => {
console.log('System theme changed to:', theme);
applyTheme(theme);
});
// Cleanup when done
unwatch();1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Complete Example
typescript
import {
resolveTheme,
applyTheme,
watchSystemTheme,
getCSSVariableValue,
type ThemeMode,
} from '@3lens/themes';
// Initialize theme
function initTheme(preference: ThemeMode = 'auto') {
const theme = resolveTheme(preference);
applyTheme(theme);
return theme;
}
// Watch for system changes
function setupThemeWatcher() {
return watchSystemTheme((theme) => {
applyTheme(theme);
console.log('Theme changed to:', theme);
});
}
// Get theme color
function getThemeColor(token: string): string {
return getCSSVariableValue(token);
}
// Usage
const initialTheme = initTheme('auto');
const unwatch = setupThemeWatcher();
// Later, cleanup
// unwatch();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
React Example
tsx
import { useEffect, useState } from 'react';
import { resolveTheme, applyTheme, watchSystemTheme, type ResolvedTheme } from '@3lens/themes';
function useTheme(preference: 'dark' | 'light' | 'auto' = 'auto') {
const [theme, setTheme] = useState<ResolvedTheme>(() => resolveTheme(preference));
useEffect(() => {
applyTheme(theme);
}, [theme]);
useEffect(() => {
if (preference === 'auto') {
const unwatch = watchSystemTheme((newTheme) => {
setTheme(newTheme);
});
return unwatch;
}
}, [preference]);
return [theme, setTheme] as const;
}
function App() {
const [theme, setTheme] = useTheme('auto');
return (
<div>
<p>Current theme: {theme}</p>
<button onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
Toggle Theme
</button>
</div>
);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Vue Example
vue
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { resolveTheme, applyTheme, watchSystemTheme, type ResolvedTheme } from '@3lens/themes';
const theme = ref<ResolvedTheme>(resolveTheme('auto'));
onMounted(() => {
applyTheme(theme.value);
const unwatch = watchSystemTheme((newTheme) => {
theme.value = newTheme;
applyTheme(newTheme);
});
onUnmounted(() => {
unwatch();
});
});
</script>
<template>
<div>
<p>Current theme: {{ theme }}</p>
<button @click="theme = theme === 'dark' ? 'light' : 'dark'">
Toggle Theme
</button>
</div>
</template>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28