Skip to content
staritPublic

About

A powerful, framework-agnostic theme management library with AI-powered theme generation.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

Themed.js

npm version License: MIT

Demo →

AI-generated themes for modern frontends.

Themed.js lets users generate personalized frontend themes from natural language prompts.

Features

  • Framework Agnostic — vanilla JS, React, Vue
  • AI Generation — describe a theme in plain text; supports OpenAI, Claude, Gemini, Groq, DeepSeek, Moonshot, and a Chrome extension proxy
  • Custom Structured Data — attach arbitrary JSON to any theme; AI can generate it alongside tokens
  • CSS Variables — all tokens injected as --themed-* custom properties
  • Built-in Themes — 8 themes included
  • TypeScript — full type definitions across all packages
  • Persistent — localStorage and IndexedDB, configurable
  • WCAG — color contrast utilities

Packages

Package Description
@themed.js/core Core library with theme management, AI integration, and storage
@themed.js/react React bindings with hooks and context provider
@themed.js/vue Vue 3 bindings with composables and plugin

Quick Start

Installation

# Core only (vanilla JS)
pnpm add @themed.js/core

# With React
pnpm add @themed.js/core @themed.js/react

# With Vue
pnpm add @themed.js/core @themed.js/vue

Vanilla JavaScript

import { createThemed } from '@themed.js/core';

const themed = createThemed({
  defaultTheme: 'light',
  ai: {
    provider: 'openai',
    apiKey: 'sk-xxx',
  },
});

await themed.init();

// Switch themes
themed.apply('dark');

// Generate AI theme
const theme = await themed.generate('A warm autumn sunset theme');

// Generate with custom structured data
const theme2 = await themed.generate('A corporate blue theme', {
  customSchema: 'Brand guidelines with name, tone of voice, and target audience',
});
// theme2.custom → { "brandName": "...", "tone": "...", "audience": "..." }

React

import { ThemeProvider, useTheme, useAITheme } from '@themed.js/react';

function App() {
  return (
    <ThemeProvider
      defaultTheme="light"
      ai={{ provider: 'openai', apiKey: 'sk-xxx' }}
    >
      <MyApp />
    </ThemeProvider>
  );
}

function ThemeSwitcher() {
  const { themes, apply } = useTheme();
  const { generate, isGenerating } = useAITheme();

  return (
    <div>
      {themes.map(t => (
        <button key={t.id} onClick={() => apply(t.id)}>
          {t.name}
        </button>
      ))}
      <button onClick={() => generate('Ocean sunset')}>
        AI Generate
      </button>
    </div>
  );
}

Vue 3

<script setup>
import { useTheme, useAITheme } from '@themed.js/vue';

const { themes, apply } = useTheme();
const { generate, isGenerating } = useAITheme();
</script>

<template>
  <button
    v-for="t in themes"
    :key="t.id"
    @click="apply(t.id)"
  >
    {{ t.name }}
  </button>
</template>
// main.ts
import { createApp } from 'vue';
import { themedPlugin } from '@themed.js/vue';

const app = createApp(App);
app.use(themedPlugin, {
  defaultTheme: 'light',
  ai: { provider: 'openai', apiKey: 'sk-xxx' },
});

Using CSS Variables

.button {
  background-color: var(--themed-color-primary);
  color: var(--themed-color-text-inverse);
  font-family: var(--themed-font-family-sans);
  font-size: var(--themed-font-size-base);
}

.card {
  background-color: var(--themed-color-surface);
  border: 1px solid var(--themed-color-border);
}

Custom Structured Data

Themes have an optional custom field — an arbitrary JSON object persisted through storage, export/import, and AI generation.

Attach custom data to an existing theme

// Vanilla
themed.updateThemeCustom('my-theme', {
  brandName: 'Acme Corp',
  tone: 'professional',
  audience: 'enterprise',
});
themed.apply('my-theme'); // triggers re-render in reactive frameworks
// React
const { theme, updateThemeCustom, apply } = useTheme();

updateThemeCustom(theme.id, { brandName: 'Acme', tone: 'friendly' });
apply(theme.id);
<!-- Vue -->
<script setup>
const { theme, updateThemeCustom, apply } = useTheme();

function attach(custom) {
  updateThemeCustom(theme.value.id, custom);
  apply(theme.value.id);
}
</script>

Create a theme with custom data

import { createTheme } from '@themed.js/core';

const theme = createTheme({
  id: 'brand',
  name: 'Brand Theme',
  tokens: { /* ... */ },
  custom: {
    brandName: 'Acme Corp',
    primaryUsage: 'Marketing site',
  },
});

Generate custom data with AI

Pass a customSchema when generating a theme. It can be a natural-language description or a JSON skeleton with placeholder values:

// Natural language
const theme = await themed.generate('A warm startup theme', {
  customSchema: 'Brand guidelines with company name, tagline, tone of voice, and target audience',
});

// JSON skeleton — AI fills in the values to match the theme
const theme2 = await themed.generate('A dark fintech theme', {
  customSchema: '{ "brandName": "...", "tone": "...", "audience": "..." }',
});

console.log(theme.custom);
// { "brandName": "...", "tone": "...", "audience": "...", ... }

The custom field is included in export/import and persisted to storage automatically.

Theme Export / Import

Export

// Single theme → JSON string
const json = themed.exportTheme('midnight-ocean');

// Multiple themes → bundle JSON ({ version, exportedAt, themes })
const bundle = themed.exportThemes(['light', 'dark', 'midnight-ocean']);

// All registered themes
const all = themed.exportThemes();

In the browser you can trigger a file download:

const blob = new Blob([themed.exportTheme('midnight-ocean')], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = Object.assign(document.createElement('a'), { href: url, download: 'midnight-ocean.json' });
a.click();
URL.revokeObjectURL(url);

Import

// Single theme from a JSON string (or file contents)
const theme = themed.importTheme(json);

// Bundle produced by exportThemes(), or a plain JSON array of themes
const themes = themed.importThemes(bundleJson);

// File input example
input.addEventListener('change', async () => {
  const text = await input.files[0].text();
  themed.importThemes(text);
});

importTheme / importThemes validate the structure before registering and throw a descriptive error if the data is invalid or malformed.


Server-Side Rendering (SSR)

CSS injection is a no-op on the server (no document). Use the SSR utilities to inject initial styles into the HTML response and prevent a flash of unstyled content (FOUC).

React — ThemeScript

// app/layout.tsx (Next.js App Router)
import { ThemeScript, ThemeProvider } from '@themed.js/react';

export default function RootLayout({ children }) {
  return (
    <html>
      <head>
        <ThemeScript defaultTheme="light" />
      </head>
      <body>
        <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
      </body>
    </html>
  );
}

Vue / Nuxt — getSSRStyles

// plugins/themed.server.ts
import { getSSRStyles, builtinThemes } from '@themed.js/vue';

export default defineNuxtPlugin(() => {
  useHead({
    style: [{ id: 'themed-js-styles', innerHTML: getSSRStyles('light', builtinThemes) }],
  });
});

Vanilla SSR

import {