Replies: 10 comments 3 replies
-
@rosendi a couple folks have brought up this same concern, both publicly and privately. With the convention to organize class files in the For now, we've been pretty aggressive about using modules to namespace / group our components. We have app-specific components in I'm hoping we can share some of these components with the public soon. |
Beta Was this translation helpful? Give feedback.
-
We do exactly the same in our React app. We have a UI component library which we extracted into a npm package and the app-specific components which build on top the UI library. In our For example our UI library:
In js we create an
Ideally a component should be self-packed, i.e. all the assets, js, css related to the component live together. I would say the component itself is a combination of that files. We found it very useful for growing apps. It would be messy to put all the files at the same level. You mentioned before that you organize components by a feature, e.g PullRequest::Item. In our React app we use Fractal Project Structure - https://github.com/davezuko/react-redux-starter-kit/wiki/Fractal-Project-Structure but it doesn't suit Rails apps, because Rails has a flat structure. I'm curious about that questions because currently I’m working on a new rails app I want 1) keep my components aligned as much as possible with the official gem and migrate to ActionView::Component later 2) bring best practices from the modern frontend development to Rails development. Do you recommend to unwrap components to the same level folder? But if a component has images, svgs, where would you put them? In my example for each component I have a special folder called |
Beta Was this translation helpful? Give feedback.
-
@rosendi that's all super interesting. What I like about the approach you're suggesting is how it leverages encapsulation to limit the scope of CSS/JS to the component. I'd love to see a proof of concept for how this might be done in practice. Until we can provide encapsulation of sibling assets, I'm wary of including them in the same folder, as doing so would imply a relationship to the component that isn't enforced programmatically. |
Beta Was this translation helpful? Give feedback.
-
I think one approach that might work - although maybe a bit heavy handed - for encapsulating the sidecar CSS/JS would be:
|
Beta Was this translation helpful? Give feedback.
-
👋 I'm currently doing some tries with a proof of concept application. I initially worked on a custom I currently have the following structure.
|
Beta Was this translation helpful? Give feedback.
-
I'd love to see a config option to use directory based components...
I think that is much cleaner, otherwise we end up with fat directories. :( I'd be happy to submit a PR if this is something that would be accepted. |
Beta Was this translation helpful? Give feedback.
-
Here's a way to dynamically have components with and without directories, while maintaining short namespaces.
Form::FormFieldComponent.new
Form::FormComponent.new Updated!
to_prepare, used eg here
config.to_prepare do
require_dependency(Rails.root.join('lib', 'models', 'view_component_namespace_delegator.rb'))
Dir.glob(Rails.root.join('app', 'components', '**', '*_component', '*_component.rb')).each do |file|
ViewComponentNamespaceDelegator.apply_shorthand(file)
end
end
Form::FormComponent.new => Form::FormComponent::FormComponent.new
module ViewComponentNamespaceDelegator
def self.apply_shorthand(file)
component_class_name = File.basename(file, ".rb").classify
folder_path = File.dirname(file)
folder_rel_path = Pathname.new( folder_path ).relative_path_from( Pathname.new( Rails.root.join('app', 'components') ) ).to_s
folder_namespace = folder_rel_path.classify
folder_module = folder_namespace.constantize
if !folder_module.method_defined?(:new) && component_class_name == folder_namespace.split('::').last
folder_module.define_singleton_method(:new) do |*args|
"#{folder_namespace}::#{component_class_name}".constantize.new(*args)
end
end
end
end |
Beta Was this translation helpful? Give feedback.
-
Since my comment, I've changed how I organise my components...
Which is supported out of the box. |
Beta Was this translation helpful? Give feedback.
-
We're organizing our components in the same way as @joelmoss is and it's been working great. We even adjusted the generators to spit out that structure by default. |
Beta Was this translation helpful? Give feedback.
-
I'll throw in my two cents, as well as my current approach: I would very much prefer, like @danturu seems to approach it - for a parent component, and all it's associated child components - to be placed in the same folder. ./components
├── /feed_component
│ ├── line_item_invoice_state_component.html.erb
│ ├── line_item_invoice_state_component.rb
│ ├── line_item_issue_state_component.html.erb
│ ├── line_item_issue_state_component.rb
│ ├── project_state_component.html.erb
│ └── project_state_component.rb
├── feed_component.html.erb # outside the `feed_component` folder :'( - not preferred.
├── feed_component.rb # outside the `feed_component` folder :'( # namespacing can get messy if we go more than one level deep
class FeedComponent::LineItemInvoiceStateComponent < ViewComponent::Base
def initialize(line_item:)
# etc
end
end # feed_component.html.erb
<%= render(FeedComponent::LineItemIssueStateComponent.new(line_item: nil)) %>
<%= render(FeedComponent::LineItemInvoiceStateComponent.new(line_item: nil)) %>
<%= render(FeedComponent::ProjectStateComponent.new(line_item: nil)) %> Is there a canonical way to organise child components (into sidecars)? |
Beta Was this translation helpful? Give feedback.
-
Could you please tell us how do organize components in GitHub?
In React development there is a common rule - one component, one folder. For example in my daily work we have about 1000 components and it works great. In my recent Rails project I do the same:
If I follow the rule above I repeat component name twice
I don't do
because we found what it's simplify searching using
ctrl+p
(jump to file).Any tips? Should we add some documentation?
Beta Was this translation helpful? Give feedback.
All reactions