CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-storybook-dark-mode

Toggle between light and dark mode in Storybook

Pending
Overview
Eval results
Files

internal-apis.mddocs/

Internal APIs

Advanced and internal APIs for Storybook Dark Mode that provide low-level access to theme management functionality.

Capabilities

Theme Store Management

Core store functions for managing theme state and persistence.

/**
 * Get or update the dark mode store with user configuration
 * @param userTheme - Optional partial configuration to merge with stored settings
 * @returns Complete DarkModeStore configuration object
 */
function store(userTheme?: Partial<DarkModeStore>): DarkModeStore;

/**
 * Persist dark mode settings to localStorage
 * @param newStore - Complete store configuration to persist
 */
function updateStore(newStore: DarkModeStore): void;

Usage Examples:

import { store, updateStore } from 'storybook-dark-mode';

// Get current store configuration
const currentStore = store();
console.log('Current theme:', currentStore.current);

// Update store with new configuration
const updatedStore = store({
  current: 'dark',
  darkClass: ['custom-dark'],
  stylePreview: true
});

// Manually persist configuration
updateStore(updatedStore);

OS Preference Detection

MediaQueryList for detecting user's system color scheme preference.

/**
 * MediaQueryList that matches dark color scheme preference
 * Used for automatic theme detection when user hasn't explicitly set preference
 */
const prefersDark: MediaQueryList;

Usage Examples:

import { prefersDark } from 'storybook-dark-mode';

// Check current OS preference
console.log('OS prefers dark mode:', prefersDark.matches);

// Listen for OS preference changes
const handlePreferenceChange = (event: MediaQueryListEvent) => {
  console.log('OS preference changed to:', event.matches ? 'dark' : 'light');
};

prefersDark.addListener(handlePreferenceChange);

// Cleanup listener
prefersDark.removeListener(handlePreferenceChange);

Toolbar Component

The main toolbar component that provides the dark mode toggle UI element.

/**
 * Main dark mode toolbar component
 * @param props - Component props containing Storybook API instance
 * @returns React component rendering theme toggle button
 */
function DarkMode(props: DarkModeProps): React.ReactElement;

interface DarkModeProps {
  /** Storybook manager API instance */
  api: API;
}

Note: This component is typically registered automatically by the addon and doesn't need manual usage.

Storage Configuration

Constants and types related to localStorage persistence.

/** localStorage key used for persisting theme settings */
const STORAGE_KEY: 'sb-addon-themes-3';

/** Default parameter values used when no user configuration is provided */
interface DefaultParams {
  classTarget: 'body';
  dark: ThemeVars;
  darkClass: ['dark'];
  light: ThemeVars;
  lightClass: ['light'];
  stylePreview: false;
  userHasExplicitlySetTheTheme: false;
}

Internal Utility Functions

Helper functions used internally by the addon for class and iframe management.

/**
 * Convert string or array of class names to array format
 * @param classes - Single class name or array of class names
 * @returns Array of class names
 */
function arrayify(classes: string | string[]): string[];

/**
 * Apply theme classes to specified element
 * @param el - Target element to apply classes to
 * @param store - Theme store configuration
 */
function toggleDarkClass(el: Element, store: DarkModeStore): void;

/**
 * Update preview iframe with current theme classes
 * @param store - Theme store configuration
 */
function updatePreview(store: DarkModeStore): void;

/**
 * Update manager iframe with current theme classes
 * @param store - Theme store configuration
 */
function updateManager(store: DarkModeStore): void;

Note: These internal functions are not typically needed for standard usage but may be useful for advanced customization scenarios.

Advanced Usage Patterns

Custom Store Management

import { store, updateStore, prefersDark } from 'storybook-dark-mode';

// Initialize with custom defaults
const customStore = store({
  current: prefersDark.matches ? 'dark' : 'light',
  darkClass: ['app-dark', 'high-contrast'],
  lightClass: ['app-light', 'standard-contrast'],
  stylePreview: true,
  classTarget: 'html'
});

// Update and persist
updateStore(customStore);

Dynamic Theme Configuration

import { store, updateStore } from 'storybook-dark-mode';
import { themes } from '@storybook/theming';

// Dynamically update theme based on user preferences
function applyUserTheme(userPrefs: UserThemePreferences) {
  const currentStore = store();
  
  const updatedStore = {
    ...currentStore,
    dark: {
      ...themes.dark,
      appBg: userPrefs.darkBackground,
      textColor: userPrefs.darkText
    },
    light: {
      ...themes.light,
      appBg: userPrefs.lightBackground,
      textColor: userPrefs.lightText
    }
  };
  
  updateStore(updatedStore);
}

Install with Tessl CLI

npx tessl i tessl/npm-storybook-dark-mode

docs

configuration.md

event-system.md

index.md

internal-apis.md

react-integration.md

tile.json