-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Description
Implement a React-like context API for dsl-lite to allow component authors to share values without explicit prop passing. This should include both createContext and useContext functions.
Tasks
- Implement a context stack mechanism to manage context values
- Create
createContextfunction to create context objects - Implement
useContexthook to consume context values - Ensure proper typing for context values
- Add provider functionality to inject context values
- Create tests for context creation and consumption
Technical Details
// Context implementation
const contextStack: Array<{ context: any, value: any }> = [];
// createContext function
export function createContext<T>(defaultValue: T) {
const context = {
$$typeof: Symbol.for('jsx.context'),
_defaultValue: defaultValue,
Provider: null as any
};
context.Provider = {
$$typeof: Symbol.for('jsx.provider'),
_context: context
};
return context;
}
// useContext hook
export function useContext<T>(context: any): T {
hookIndex++; // Increment hook index to maintain order
// Search for context in stack
for (let i = contextStack.length - 1; i >= 0; i--) {
if (contextStack[i].context === context) {
return contextStack[i].value;
}
}
// Return default if not found
return context._defaultValue;
}
// Push context to stack
export function pushContext(context: any, value: any) {
contextStack.push({ context, value });
}
// Pop context from stack
export function popContext() {
contextStack.pop();
}Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request