context.tsx

Overview

context.tsx is a React context module that provides a centralized state management solution for controlling dropdown visibility within a React component tree. It defines a DropdownProvider component that encapsulates dropdown state logic and exposes a context with functions to manage which dropdown (if any) is currently active. The file ensures that only one dropdown can be active at a time, preventing UI conflicts by using a React context and hooks.

This file is designed to be used in scenarios where multiple dropdown components coexist, such as dropdown handles or draggable dropdowns, and there is a need to coordinate their open/closed states to avoid overlapping or conflicting dropdowns.


Detailed Explanation

Types and Interfaces

DropdownContextType

interface DropdownContextType {
  canShowDropdown: () => boolean;
  setActiveDropdown: (type: 'handle' | 'drag') => void;
  clearActiveDropdown: () => void;
}

Defines the shape of the context value. It includes:


Context Creation

const DropdownContext = createContext<DropdownContextType | null>(null);

Creates a React context initialized with null. This context will hold the dropdown management functions and state.


Hook: useDropdownManager

export const useDropdownManager = () => {
  const context = useContext(DropdownContext);
  if (!context) {
    throw new Error('useDropdownManager must be used within DropdownProvider');
  }
  return context;
};

A custom hook to consume the dropdown context. It throws an error if used outside the DropdownProvider, ensuring proper context usage.

Parameters: None

Returns: The current context object of type DropdownContextType.

Usage example:

const { canShowDropdown, setActiveDropdown, clearActiveDropdown } = useDropdownManager();

if (canShowDropdown()) {
  setActiveDropdown('handle');
}

Component: DropdownProvider

interface DropdownProviderProps {
  children: ReactNode;
}

export const DropdownProvider = ({ children }: DropdownProviderProps) => {
  ...
  return (
    <DropdownContext.Provider value={value}>
      {children}
    </DropdownContext.Provider>
  );
};

This component provides the dropdown context to its descendant components.

Props:

Internal Implementation Details:

Usage example:

<DropdownProvider>
  <YourComponent />
</DropdownProvider>

Inside YourComponent, you can use useDropdownManager to control dropdown visibility.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class DropdownProvider {
        -activeDropdownRef: Ref<'handle' | 'drag' | null>
        +canShowDropdown(): boolean
        +setActiveDropdown(type: 'handle' | 'drag'): void
        +clearActiveDropdown(): void
        +render()
    }

    class DropdownContext {
        <<context>>
    }

    class useDropdownManager {
        +(): DropdownContextType
    }

    DropdownProvider --> DropdownContext : provides
    useDropdownManager ..> DropdownContext : consumes

Summary

context.tsx provides a clean and performant way to manage mutually exclusive dropdown visibility states in a React app. By wrapping components in DropdownProvider and using useDropdownManager, developers gain simple APIs to coordinate dropdown UI elements and ensure a smooth user experience without conflicting dropdowns.