or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

child-applications.mdcontext-utilities.mdframework-applications.mdindex.mdplugin-configuration.md
tile.json

context-utilities.mddocs/

Context Utilities

React context utilities for sharing data and state between framework and child applications.

Capabilities

Framework Context

React context for sharing data between framework and child applications.

/**
 * React context for sharing data between framework and child applications
 * Initialized with empty object by default
 */
export const FrameworkContext: React.Context<{}>;

Context Hook

Hook for accessing framework context data with type safety.

/**
 * Hook to access framework context with type safety
 * @returns Typed context data from FrameworkContext
 * @template T - Type of the context data object
 */
export function useFrameworkContext<T extends object>(): T;

Usage Examples:

import { useFrameworkContext } from '@ice/plugin-icestark/Context';

// Define the expected context type
interface AppContextData {
  theme: 'light' | 'dark';
  user: { id: string; name: string };
  permissions: string[];
}

// Use the context in a child application component
function ChildComponent() {
  const contextData = useFrameworkContext<AppContextData>();
  
  return (
    <div className={`theme-${contextData.theme}`}>
      <h1>Welcome, {contextData.user.name}</h1>
      {contextData.permissions.includes('admin') && (
        <button>Admin Panel</button>
      )}
    </div>
  );
}

// Use in a framework layout component
function FrameworkLayout() {
  const contextData = useFrameworkContext<AppContextData>();
  
  return (
    <header>
      Current theme: {contextData.theme}
      User: {contextData.user.name}
    </header>
  );
}

Context Data Flow

Framework to Child Communication

The framework application can provide context data that child applications can access:

  1. Framework Setup: Context data is injected into FrameworkContext.Provider
  2. Child Access: Child applications use useFrameworkContext() to read the data
  3. Type Safety: TypeScript ensures type safety through generic parameter

Context Provider Integration

The context is automatically provided by the child runtime:

// Automatically generated by child runtime
<FrameworkContext.Provider value={{ ...customProps }}>
  {childApplication}
</FrameworkContext.Provider>

Where customProps comes from the LifecycleOptions.customProps passed during mount.

Data Passing Mechanism

Context data flows through this chain:

  1. Framework Configuration: Framework app defines custom properties
  2. Mount Options: Properties are passed via LifecycleOptions.customProps
  3. Context Provider: Child runtime wraps app with context containing the properties
  4. Hook Access: Child components use useFrameworkContext() to read the data

Example Integration

Complete example showing framework-to-child communication:

// Framework application (src/app.ts)
import { defineFrameworkConfig } from '@ice/plugin-icestark/types';

export const icestark = defineFrameworkConfig({
  getApps: () => [
    {
      name: 'child-app',
      activePath: '/child',
      container: '#child-container',
      url: ['//localhost:3001/build/js/index.js'],
      // Custom properties passed to child
      props: {
        theme: 'dark',
        apiBaseUrl: 'https://api.example.com',
        permissions: ['read', 'write'],
      },
    },
  ],
});

// Child application component
import { useFrameworkContext } from '@ice/plugin-icestark/Context';

interface FrameworkProps {
  theme: string;
  apiBaseUrl: string;
  permissions: string[];
}

function ChildApp() {
  const { theme, apiBaseUrl, permissions } = useFrameworkContext<FrameworkProps>();
  
  // Use framework-provided data
  const apiClient = new ApiClient(apiBaseUrl);
  
  return (
    <div data-theme={theme}>
      <h1>Child Application</h1>
      {permissions.includes('write') && (
        <button>Create New Item</button>
      )}
    </div>
  );
}

Type Safety

The useFrameworkContext hook provides compile-time type safety:

  • Generic Parameter: Specify the expected context data type
  • Return Type: Hook returns the typed context data
  • IDE Support: Full IntelliSense and type checking for context properties
  • Runtime Safety: TypeScript compilation catches type mismatches

Context Limitations

  • One-Way Communication: Data flows from framework to child only
  • Mount-Time Data: Context data is set during child app mount
  • Static Properties: Context data doesn't automatically update after mount
  • Scope: Context is scoped to individual child applications

For dynamic communication between framework and child apps, consider:

  • Custom event systems
  • Shared state management libraries
  • Message passing mechanisms
  • URL parameters or hash-based communication