or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdprovider-system.mdtheming-argtypes.mdtoolbar-controls.md
tile.json

toolbar-controls.mddocs/

Toolbar Controls

Interactive toolbar controls that provide direct access to color mode and layout direction switching from Storybook's interface. These tools appear in the Storybook toolbar and communicate with stories through the addon's event system.

Capabilities

Color Mode Tool

Toolbar button for toggling between light and dark color modes with localStorage persistence and visual feedback.

/**
 * Storybook toolbar component for color mode switching
 * Renders an IconButton with sun/moon icons based on current mode
 */
const ColorModeTool: React.FC;

Features:

  • Visual Indicators: Sun icon (light mode) and Moon icon (dark mode)
  • State Persistence: Reads/writes to localStorage key "chakra-ui-color-mode"
  • Event Communication: Emits TOGGLE_COLOR_MODE events to sync with stories
  • Accessibility: Includes descriptive title attributes

Usage Examples:

// The ColorModeTool is automatically registered by the addon
// Available in Storybook toolbar once addon is configured

// Programmatic access to color mode state
const isDarkMode = localStorage.getItem("chakra-ui-color-mode") === "dark";

// The tool automatically handles:
// - Icon switching (sun/moon)
// - localStorage updates
// - Event emission to stories

Direction Tool

Toolbar button for toggling between left-to-right (LTR) and right-to-left (RTL) layout directions with global state management.

/**
 * Storybook toolbar component for layout direction switching
 * Renders an IconButton with directional arrows based on current/target direction
 */
const DirectionTool: React.FC;

Features:

  • Visual Indicators: LTR and RTL arrow icons showing target direction
  • Global State: Uses Storybook's global state and addon state management
  • Event Communication: Emits TOGGLE_DIRECTION events to sync with stories
  • State Persistence: Maintains direction preference across story navigation

Usage Examples:

// The DirectionTool is automatically registered by the addon
// Available in Storybook toolbar once addon is configured

// The tool manages direction through Storybook globals:
// - Reads from globals[DIRECTION_TOOL_ID] 
// - Updates via setGlobals({ [DIRECTION_TOOL_ID]: direction })
// - Syncs with useAddonState for persistence

Tool Registration

Both tools are automatically registered when the addon is loaded through Storybook's manager API.

/**
 * Addon registration that adds both toolbar tools
 * Tools are conditionally shown based on viewMode (story/docs)
 */
addons.register(ADDON_ID, () => {
  const match = ({ viewMode }: { viewMode?: string }) =>
    Boolean(viewMode && viewMode.match(/^(story|docs)$/));

  addons.add(DIRECTION_TOOL_ID, {
    type: types.TOOL,
    title: "Direction",
    render: DirectionTool,
    match,
  });

  addons.add(COLOR_MODE_TOOL_ID, {
    type: types.TOOL,
    title: "Color Mode", 
    render: ColorModeTool,
    match,
  });
});

Tool Implementation Details

Color Mode Tool State Management

// Internal state tracking
const [darkMode, setDarkMode] = React.useState(isDarkMode);

// Event emission on toggle
const handleToggle = () => {
  channel.emit(EVENTS.TOGGLE_COLOR_MODE, !darkMode ? "dark" : "light");
  setDarkMode((prev) => !prev);
};

Direction Tool State Management

// Multi-level state management
const [globals, setGlobals] = useGlobals();
const [direction, setDirection] = useAddonState(
  DIRECTION_TOOL_ID,
  globals[DIRECTION_TOOL_ID] || "ltr"
);

// Bidirectional sync between global and addon state
useEffect(() => {
  setGlobals({ [DIRECTION_TOOL_ID]: direction });
}, [direction, setGlobals]);

// Event emission with target direction
const toggleDirection = useCallback(() => {
  const channel = addons.getChannel();
  channel.emit(EVENTS.TOGGLE_DIRECTION, targetDirection);
  setDirection(targetDirection);
}, [setDirection, targetDirection]);

Icon Components

The tools include embedded SVG icons for visual consistency:

Color Mode Icons

/**
 * Moon icon for dark mode indication
 */
const MoonIcon: React.FC;

/**
 * Sun icon for light mode indication  
 */
const SunIcon: React.FC;

Direction Icons

/**
 * Left-to-right arrow icon
 */
const LTRIcon: React.FC<React.SVGProps<SVGSVGElement>>;

/**
 * Right-to-left arrow icon
 */
const RTLIcon: React.FC<React.SVGProps<SVGSVGElement>>;

Event System Integration

The toolbar controls integrate with the addon's event system for seamless communication:

const EVENTS = {
  TOGGLE_COLOR_MODE: "@chakra-ui/storybook-addon/toggleColorMode",
  TOGGLE_DIRECTION: "@chakra-ui/storybook-addon/toggleDirection", 
};

Event Flow:

  1. User Interaction: User clicks toolbar tool button
  2. State Update: Tool updates its internal state
  3. Event Emission: Tool emits corresponding event via Storybook channel
  4. Story Reception: Provider system and hooks listen for events
  5. Theme Update: Stories re-render with updated color mode or direction
  6. Visual Feedback: Tool icons update to reflect new state