Skip to main content
rule CX Codex

state-context-interface — Claude Code rule: purpose and installation

The state-context-interface rule separates a component’s UI from its state implementation with a context split into state, actions, and meta. Use it when the same UI needs to work with different providers.

MIT ★ 7,191 updated 2026-10-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/state-context-interface.md
Open on GitHub

What state-context-interface does

The rule recommends a shared context interface with three parts: state, actions, and meta. UI components consume that contract instead of depending on a particular hook or store. The same interface can then connect to local form state or to globally synchronized channel state.

A provider supplies the state, actions, and metadata, such as a reference to an input. Components can read context or invoke actions anywhere inside the provider boundary, including outside Composer.Frame. This is an architectural guideline for React components; the UI still needs a provider that implements the interface to receive its context.

What it helps with

  • decouple a UI component from a specific state hook
  • connect local and global state providers to the same interface
  • read composer state from a preview outside Composer.Frame
  • trigger submission from a button outside the composer

Example prompts for the agent

  • Design a component context with state, actions, and meta so the UI does not depend on a specific hook.
  • Refactor this component so local and global providers implement the same interface.
  • Add a message preview outside Composer.Frame that can still read the provider’s state.

How to install state-context-interface

Manually: put the files in place

.claude/rules/state-context-interface.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about state-context-interface

How do I install state-context-interface in Claude Code?

Add the rule file at .claude/rules/state-context-interface.md in your project so Claude Code can apply it.

What does state-context-interface do?

It recommends separating UI components from a particular state implementation through a context contract containing state, actions, and meta. Different providers can implement that contract.

Can components outside Composer.Frame access the context?

Yes. They can read state or call actions as long as they remain inside the provider boundary; visual placement inside Composer.Frame is unnecessary.

Source text

state-context-interface.md · MIT
---
title: Define Generic Context Interfaces for Dependency Injection
impact: HIGH
impactDescription: enables dependency-injectable state across use-cases
tags: composition, context, state, typescript, dependency-injection
---

## Define Generic Context Interfaces for Dependency Injection

Define a **generic interface** for your component context with three parts:
`state`, `actions`, and `meta`. This interface is a contract that any provider
can implement—enabling the same UI components to work with completely different
state implementations.

**Core principle:** Lift state, compose internals, make state
dependency-injectable.

**Incorrect (UI coupled to specific state implementation):**

```tsx
function ComposerInput() {
  // Tightly coupled to a specific hook
  const { input, setInput } = useChannelComposerState()
  return <TextInput value={input} onChangeText={setInput} />
}
```

**Correct (generic interface enables dependency injection):**

```tsx
// Define a GENERIC interface that any provider can implement
interface ComposerState {
  input: string
  attachments: Attachment[]
  isSubmitting: boolean
}

interface ComposerActions {
  update: (updater: (state: ComposerState) => ComposerState) => void
  submit: () => void
}

interface ComposerMeta {
  inputRef: React.RefObject<TextInput>
}

interface ComposerContextValue {
  state: ComposerState
  actions: ComposerActions
  meta: ComposerMeta
}

const ComposerContext = createContext<ComposerContextValue | null>(null)
```

**UI components consume the interface, not the implementation:**

```tsx
function ComposerInput() {
  const {
    state,
    actions: { update },
    meta,
  } = use(ComposerContext)

  // This component works with ANY provider that implements the interface
  return (
    <TextInput
      ref={meta.inputRef}
      value={state.input}
      onChangeText={(text) => update((s) => ({ ...s, input: text }))}
    />
  )
}
```

**Different providers implement the same interface:**

```tsx
// Provider A: Local state for ephemeral forms
function ForwardMessageProvider({ children }: { children: React.ReactNode }) {
  const [state, setState] = useState(initialState)
  const inputRef = useRef(null)
  const submit = useForwardMessage()

  return (
    <ComposerContext
      value={{
        state,
        actions: { update: setState, submit },
        meta: { inputRef },
      }}
    >
      {children}
    </ComposerContext>
  )
}

// Provider B: Global synced state for channels
function ChannelProvider({ channelId, children }: Props) {
  const { state, update, submit } = useGlobalChannel(channelId)
  const inputRef = useRef(null)

  return (
    <ComposerContext
      value={{
        state,
        actions: { update, submit },
        meta: { inputRef },
      }}
    >
      {children}
    </ComposerContext>
  )
}
```

**The same composed UI works with both:**

```tsx
// Works with ForwardMessageProvider (local state)
<ForwardMessageProvider>
  <Composer.Frame>
    <Composer.Input />
    <Composer.Submit />
  </Composer.Frame>
</ForwardMessageProvider>

// Works with ChannelProvider (global synced state)
<ChannelProvider channelId="abc">
  <Composer.Frame>
    <Composer.Input />
    <Composer.Submit />
  </Composer.Frame>
</ChannelProvider>
```

**Custom UI outside the component can access state and actions:**

The provider boundary is what matters—not the visual nesting. Components that
need shared state don't have to be inside the `Composer.Frame`. They just need
to be within the provider.

```tsx
function ForwardMessageDialog() {
  return (
    <ForwardMessageProvider>
      <Dialog>
        {/* The composer UI */}
        <Composer.Frame>
          <Composer.Input placeholder="Add a message, if you'd like." />
          <Composer.Footer>
            <Composer.Formatting />
            <Composer.Emojis />
          </Composer.Footer>
        </Composer.Frame>

        {/* Custom UI OUTSIDE the composer, but INSIDE the provider */}
        <MessagePreview />

        {/* Actions at the bottom of the dialog */}
        <DialogActions>
          <CancelButton />
          <ForwardButton />
        </DialogActions>
      </Dialog>
    </ForwardMessageProvider>
  )
}

// This button lives OUTSIDE Composer.Frame but can still submit based on its context!
function ForwardButton() {
  const {
    actions: { submit },
  } = use(ComposerContext)
  return <Button onPress={submit}>Forward</Button>
}

// This preview lives OUTSIDE Composer.Frame but can read composer's state!
function MessagePreview() {
  const { state } = use(ComposerContext)
  return <Preview message={state.input} attachments={state.attachments} />
}
```

The `ForwardButton` and `MessagePreview` are not visually inside the composer
box, but they can still access its state and actions. This is the power of
lifting state into providers.

The UI is reusable bits you compose together. The state is dependency-injected
by the provider. Swap the provider, keep the UI.

© tech-leads-club · MIT · tech-leads-club/agent-skills

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
perf-astro The perf-astro Claude Code skill tunes Astro builds with inlined critical CSS, minification, non-blocking fonts and an LCP preload, aiming for 95+ Lighthouse scores. CX Codex +1 ★ 7,191repo stars tactical-ddd Audits domain models for anemia and refactors them into rich aggregates using tactical DDD patterns in Claude Code. CX Codex +1 ★ 7,191repo stars nx-workspace nx-workspace — a skill for exploring an Nx monorepo from the CLI: list projects, read resolved config, find affected projects and wire affected commands into CI. CX Codex +1 ★ 7,191repo stars perf-web-optimization perf-web-optimization guides front-end performance tuning by shrinking JS bundles, deferring heavy scripts, and configuring cache headers. Best when pages load slowly or fail speed benchmarks. CX Codex +1 ★ 7,191repo stars learning-opportunities learning-opportunities adds optional 10–15 minute exercises to a Claude session, so the person who shipped the code can still explain how it works. CX Codex +1 ★ 7,191repo stars confluence-assistant confluence-assistant operates Confluence spaces through Atlassian MCP tools. Helps Claude Code search team documentation, create structured pages, and update existing entries using Markdown. CX Codex +1 ★ 7,191repo stars security-threat-model security-threat-model reads your repository and writes a threat model: trust boundaries, assets, realistic abuse paths and mitigations, saved as a Markdown file. CX Codex +1 ★ 7,191repo stars modular-decomposition modular-decomposition walks a monolith through five analysis steps before splitting it into modules: sizing and listing components, finding duplicate domain logic, hierarchy, coupling, and domain grouping. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open