Hone logo
Hone
Problems

Context Pattern Implementation in Vue.js with TypeScript

This challenge focuses on implementing the Context pattern within a Vue.js application using TypeScript. The Context pattern aims to encapsulate complex algorithms and state, providing a simplified interface for clients. This promotes code reusability, maintainability, and separation of concerns, particularly beneficial in larger Vue applications.

Problem Description

You are tasked with creating a Vue.js component that utilizes the Context pattern to manage and provide a specific piece of data and associated functionality. The context will be a ThemeContext which holds the current theme (e.g., "light", "dark") and provides methods to toggle the theme. The component should:

  1. Define a ThemeContext type: This type should include a theme property (string, initial value "light") and a toggleTheme function (which changes the theme to the opposite value).
  2. Create a ThemeContextProvider component: This component will hold the actual state and logic for the ThemeContext. It will use Vue's provide functionality to make the context available to its children. The provide key should be 'themeContext'.
  3. Create a ThemeConsumer component: This component will use Vue's inject functionality to access the ThemeContext provided by the ThemeContextProvider. It should display the current theme and a button to toggle the theme.
  4. Ensure the toggleTheme function updates the theme correctly and reflects the change in the UI.

Key Requirements:

  • Use TypeScript for type safety.
  • Implement the Context pattern correctly using provide and inject.
  • The ThemeConsumer component should re-render when the theme changes.
  • The code should be well-structured and readable.

Expected Behavior:

  • The application should initially display "light" as the current theme.
  • Clicking the "Toggle Theme" button should change the theme to "dark" and update the UI accordingly.
  • Clicking the "Toggle Theme" button again should change the theme back to "light".
  • The ThemeConsumer component should only be concerned with displaying the theme and toggling it; it shouldn't manage the theme state directly.

Edge Cases to Consider:

  • What happens if a component tries to inject 'themeContext' but it hasn't been provided by an ancestor? (Handle this gracefully, perhaps with a default value or error message).
  • Consider how the Context pattern can be extended to provide more complex data and functionality.

Examples

Example 1:

Input: Initial state: Theme = "light"
Output: UI displays "light" theme.  Button label: "Toggle Theme"
Explanation: The application starts with the default light theme.

Example 2:

Input: User clicks "Toggle Theme" button.
Output: UI displays "dark" theme. Button label: "Toggle Theme"
Explanation: The toggleTheme function changes the theme to "dark" and updates the UI.

Example 3:

Input: User clicks "Toggle Theme" button (after it's already dark).
Output: UI displays "light" theme. Button label: "Toggle Theme"
Explanation: The toggleTheme function changes the theme back to "light" and updates the UI.

Constraints

  • The theme property can only be "light" or "dark".
  • The solution must be implemented using Vue 3 and TypeScript.
  • The code should be concise and efficient. Avoid unnecessary complexity.
  • The ThemeContextProvider should be a functional component.
  • The ThemeConsumer should be a functional component.

Notes

  • Think about how to structure your code to clearly separate the context provider and consumer.
  • Vue's provide and inject are key to implementing the Context pattern.
  • Consider using a reactive variable (e.g., ref from Vue) to manage the theme state within the ThemeContextProvider.
  • This is a simplified example; the Context pattern can be used to manage more complex data and functionality. Focus on understanding the core principles.
Loading editor...
typescript