version-switcher
React Functional Component: VersionSwitcher
This code defines a React functional component named VersionSwitcher that allows users to select from a list of documentation versions via a dropdown menu.
Component Overview
Purpose
The VersionSwitcher provides a user-friendly interface for switching between different documentation versions. It displays the currently selected version and updates the selection dynamically when the user chooses another version from the dropdown.
Props
versions(string[]): An array of available documentation versions (e.g.,["1.0", "2.0", "3.0"]).defaultVersion(string): The default version to display when the component is first rendered.
State Management
The component uses React's state hook:
selectedVersion: Tracks the currently selected version. It is initialized withdefaultVersionand updated whenever a user selects a different version.
Key Features
-
Sidebar Menu Integration
- The component is wrapped in a
SidebarMenustructure usingSidebarMenuandSidebarMenuItemcomponents, indicating its placement within a sidebar.
- The component is wrapped in a
-
Dropdown Menu
- Trigger (
DropdownMenuTrigger): The dropdown menu is triggered by a styled button (SidebarMenuButton) that displays the selected version and an icon (ChevronsUpDown). - Content (
DropdownMenuContent): The dropdown menu lists all available versions, allowing the user to select one.
- Trigger (
-
Dynamic Version Selection
- The list of versions (
versions) is rendered asDropdownMenuItemcomponents. - Clicking a version updates
selectedVersionvia theonSelecthandler. - The currently selected version is visually indicated with a checkmark (
Checkicon).
- The list of versions (
Detailed Explanation
Trigger Button
The dropdown trigger button is styled and displays:
- A graphical icon (
GalleryVerticalEnd). - The text "Documentation" with the current version displayed below it (
v{selectedVersion}). - An arrow icon (
ChevronsUpDown) indicating the dropdown functionality.
Dropdown Menu Items
- Each version in
versionsis rendered as aDropdownMenuItem. - Clicking an item:
- Calls
onSelect, which updates theselectedVersionstate. - Displays a checkmark (
Check) next to the currently selected version for visual feedback.
- Calls
Styling
- Classes: A variety of classes are applied to ensure responsive, accessible, and visually appealing styling:
data-[state=open]modifiers dynamically style the button when the dropdown is open.bg-sidebar-primaryand othertext-sidebar-*classes define colors and layouts consistent with a sidebar theme.w-[--radix-dropdown-menu-trigger-width]ensures the dropdown menu matches the trigger button's width.
How It Works
-
Initial State:
- The component renders with
defaultVersionas the selected version.
- The component renders with
-
User Interaction:
- When the user clicks the dropdown trigger:
- The menu expands, displaying all available versions (
versions).
- The menu expands, displaying all available versions (
- Selecting a version:
- Updates
selectedVersionwith the selected version. - Displays the checkmark next to the chosen version.
- The button updates to show the newly selected version.
- Updates
- When the user clicks the dropdown trigger:
Usage Example
<VersionSwitcher
versions={["1.0", "2.0", "3.0"]}
defaultVersion="2.0"
/>
- Initially displays "Documentation v2.0."
- When the user selects another version (e.g.,
v1.0), it updates to "Documentation v1.0."
Key Benefits
1. Enhanced User Experience
Provides an intuitive, interactive interface for selecting documentation versions, making navigation seamless.
2. Dynamic State Management
Updates the selected version dynamically, ensuring the displayed version always reflects user choices.
3. Modular Integration
The component integrates into a sidebar structure, maintaining consistency with the application's layout.