<%= link_to path, class: "flex items-center space-x-3 px-3 py-2 text-sm font-medium text-gray-700 dark:text-neutral-200 rounded-lg #{'bg-gray-100 dark:bg-neutral-700' if current} hover:bg-gray-200 dark:hover:bg-neutral-600 transition-colors duration-200" do %>
<%= heroicon icon, class: "h-5 w-5" %>
<%= text %>
<% end %>
```
This partial accepts four parameters: `path`, `icon`, `text`, and `current`.
### 2. Create the Sidebar Partial
Next, create a sidebar partial that uses the link partial for adding navigation links.
Create the file `app/views/layouts/_sidebar.html.erb` with the following content:
```erb
<%= link_to root_path, class: "flex items-center space-x-2" do %>
<%= image_tag "logo.svg", alt: "Logo", class: "h-10 w-10" %>
App Name
<% end %>
```
This partial dynamically creates a sidebar with links by using the `_sidebar_link.html.erb` partial.
### 3. Render the Sidebar in Your Layout
Include the sidebar partial in your main layout file, usually located at `app/views/layouts/application.html.erb`:
```erb
<%= render 'layouts/sidebar' %>
<%= yield %>
```
## Benefits of This Approach
- Reusability: The sidebar link partial makes it easy to add or modify links without repeating code.
- DRY Principle: By using partials, the code remains organized and avoids unnecessary repetition.
- Responsive Design: TailwindCSS provides utility classes that ensure the sidebar is responsive and visually appealing.
- Consistent Icon Styling: Heroicons integrate smoothly with TailwindCSS classes for consistent icon styling.