Skip to content

Commit

Permalink
Support components in nested directories
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Sep 24, 2024
1 parent 4ba6ef5 commit 430429f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Gem Version](https://badge.fury.io/rb/turbo-mount.svg)](https://rubygems.org/gems/turbo-mount)

`TurboMount` is a simple library that allows you to add highly interactive components from React, Vue, Svelte, and other gframeworks to your Hotwire application.
`TurboMount` is a simple library that allows you to add highly interactive components from React, Vue, Svelte, and other frameworks to your Hotwire application.

### Learn more

Expand All @@ -21,6 +21,7 @@
- [Supported Frameworks](#supported-frameworks)
- [Custom Controllers](#custom-controllers)
- [Auto-Loading Components](#auto-loading-components)
- [Components in Nested Directories](#components-in-nested-directories)
- [Vite Integration](#vite-integration)
- [ESBuild Integration](#esbuild-integration)
- [Mount Target](#mount-target)
Expand Down Expand Up @@ -185,6 +186,28 @@ The `registerComponents` helpers search for controllers in the following paths:
- `controllers/turbo-mount/${controllerName}`
- `controllers/turbo-mount-${controllerName}`

#### Components in Nested Directories

Turbo Mount supports components located in nested directories. For example, if you have a component structure like:

```
components/
├── Dashboard/
│ └── WeatherWidget.tsx
└── ...
```

You can use the following helper to mount the component:

```erb
<%= turbo_mount("Dashboard/WeatherWidget") %>
```

For nested components, controllers are searched in these paths:

- `controllers/turbo_mount/dashboard/weather_widget_controller.js`
- `controllers/turbo_mount_dashboard__weather_widget_controller.js`

#### Vite Integration

Vite helper requires the `stimulus-vite-helpers` package to load components and controllers. Here's how to set it up:
Expand Down
2 changes: 1 addition & 1 deletion lib/turbo/mount/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Helpers
def turbo_mount(component_name, props: {}, tag: "div", **attrs, &block)
raise TypeError, "Component name expected" unless component_name.is_a? String

controller_name = "turbo-mount-#{component_name.underscore.dasherize}"
controller_name = "turbo-mount-#{component_name.underscore.dasherize.gsub("/", "--")}"
attrs["data-controller"] = controller_name
prefix = "data-#{controller_name}"
attrs["#{prefix}-component-value"] = component_name
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-mount/src/registerComponentsBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RegisterComponentsProps<T> = {
};

const identifierNames = (name: string) => {
const controllerName = camelToKebabCase(name);
const controllerName = camelToKebabCase(name).replace("/", "--");

return [`turbo-mount--${controllerName}`, `turbo-mount-${controllerName}`];
};
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-mount/src/turbo-mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class TurboMount {
this.components.set(name, { component, plugin });

if (controller) {
const controllerName = `turbo-mount-${camelToKebabCase(name)}`;
const controllerName = `turbo-mount-${camelToKebabCase(name).replace("/", "--")}`;
this.application.register(controllerName, controller);
}
}
Expand Down

0 comments on commit 430429f

Please sign in to comment.