node-wrapper.tsx
Overview
node-wrapper.tsx defines a reusable React functional component called NodeWrapper. Its primary purpose is to provide a styled container (<section> element) that wraps arbitrary child elements, with optional visual styling to indicate a "selected" state. This component is typically used in UI contexts where nodes or items need consistent styling and an optional highlight effect when selected.
The component leverages utility CSS classes (likely Tailwind CSS) to apply padding, background color, rounded corners, width, and font size. It also conditionally applies a border when the selected prop is true. The component supports passing additional CSS classes and standard HTML div attributes via props.
Detailed Explanation
Imports
cnfrom@/lib/utils: A utility function (commonly a classNames concatenator) used to conditionally join CSS class strings.HTMLAttributesfromreact: TypeScript type for standard HTML attributes applicable to a<div>element, used to type props.
Types
type IProps = HTMLAttributes<HTMLDivElement> & { selected?: boolean };
IPropsextends all valid HTML attributes of a<div>element.Adds an optional
selectedboolean prop to indicate if the node wrapper is selected.
Component: NodeWrapper
export function NodeWrapper({ children, className, selected }: IProps) {
return (
<section
className={cn(
'bg-text-title-invert p-2.5 rounded-sm w-[200px] text-xs',
{ 'border border-accent-primary': selected },
className,
)}
>
{children}
</section>
);
}
Parameters
children: ReactNode - The content to be wrapped inside theNodeWrappercomponent.className:string | undefined- Additional CSS class names to customize or extend styling.selected:boolean | undefined- Whentrue, applies a visible border to indicate selection.
Returns
A React element of type
<section>that wraps the children with styled classes.
Usage Example
import { NodeWrapper } from './node-wrapper';
function Example() {
return (
<NodeWrapper selected className="custom-class">
<p>This node is selected and has custom styling.</p>
</NodeWrapper>
);
}
Behavior and Styling
Always applies:
bg-text-title-invert: Background color (likely inverted text title color).p-2.5: Padding 2.5 units.rounded-sm: Small rounded corners.w-[200px]: Fixed width of 200 pixels.text-xs: Extra small text size.
Conditionally applies:
border border-accent-primaryifselectedistrue, showing a highlighted border.
Merges any additional classes passed via the
classNameprop.
Implementation Details
Uses a utility function
cnto concatenate CSS classes conditionally. This is a common pattern to avoid manual string concatenation bugs and improve readability.The component uses a
<section>element semantically appropriate for grouping related content.Extends the standard HTML div attributes to allow wide flexibility in usage (e.g., event handlers, aria attributes).
Interaction with Other Parts of the System
Depends on
cnutility from'@/lib/utils'for CSS class merging.Likely used as a building block in a larger UI system that renders nodes or elements that can be selected/highlighted.
The styling classes (
bg-text-title-invert,border-accent-primary) imply a design system or CSS framework (e.g., Tailwind CSS) is configured in the project.Its simplicity and extensibility make it suitable for wrapping various node content components in lists, trees, or graphical node editors.
Mermaid Component Diagram
componentDiagram
component NodeWrapper {
+props: IProps
+children: ReactNode
+className?: string
+selected?: boolean
+returns: ReactElement (<section>)
}
component cn {
<<utility>>
+(...classes: any[]): string
}
NodeWrapper --> cn : uses
Summary
The node-wrapper.tsx file provides a simple, flexible React component for wrapping content nodes with consistent styling and optional selection highlighting. It is designed to integrate smoothly with Tailwind CSS and a broader UI framework, emphasizing reusability and adaptability in rendering selectable nodes or items.