Skip to content

Commit 4d93823

Browse files
authored
Add a new component generator (#19)
1 parent dd9c3f5 commit 4d93823

File tree

8 files changed

+42
-1
lines changed

8 files changed

+42
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ We have no idea yet. We are still working on this.
5151

5252
Bug reports and pull requests are very much welcome. Please check our [plan](https://github.com/orgs/renuo/projects/7) to see what components we would like to add.
5353

54+
If you want to add a new component, run `bin/rails g ui_component my_component_name` and follow the instructions.
5455

5556
## Development
5657

bin/fastcheck

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ echo "Executing: yarn eslint app/javascripts spec/javascripts"
2323
yarn eslint 'app/javascript/**/*.js'
2424

2525

26+
bin/rails zeitwerk:check

config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Application < Rails::Application
2828
# Please, add to the `ignore` list any other `lib` subdirectories that do
2929
# not contain `.rb` files, or that should not be reloaded or eager loaded.
3030
# Common ones are `templates`, `generators`, or `middleware`, for example.
31-
config.autoload_lib(ignore: %w[assets tasks])
31+
config.autoload_lib(ignore: %w[assets tasks generators])
3232

3333
# Configuration for the application, engines, and railties goes here.
3434
#

lib/generators/ui_component/USAGE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Description:
2+
Generates all necessary files to start coding your new UI component.
3+
4+
Example:
5+
bin/rails generate ui_component modal
6+
7+
This will create al necessary files to start coding a modal component.
8+

lib/generators/ui_component/templates/component.scss

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_literal_string: true
2+
3+
class <%= class_name %>ComponentPreview < Lookbook::Preview
4+
include LookbookHelper
5+
6+
# @source ../../../app/views/components/<%= plural_name %>/_themes.html.erb
7+
def themes
8+
render 'components/<%= plural_name %>/themes'
9+
end
10+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Your new <%= name %> component implementation.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
class UiComponentGenerator < Rails::Generators::NamedBase
4+
source_root File.expand_path('templates', __dir__)
5+
6+
def create_assets_file
7+
template 'component.scss', "app/assets/stylesheets/components/_#{file_name}.scss"
8+
9+
import_string = "@import 'components/#{file_name}';"
10+
application_stylesheet = 'app/assets/stylesheets/application.scss'
11+
return unless File.exist?(application_stylesheet) && File.read(application_stylesheet).exclude?(import_string)
12+
13+
append_to_file application_stylesheet, "#{import_string}\n"
14+
end
15+
16+
def create_preview_files
17+
template 'component_preview.rb', "app/components/previews/#{file_name}_component_preview.rb"
18+
template 'themes.html.erb', "app/views/components/#{plural_name}/_themes.html.erb"
19+
end
20+
end

0 commit comments

Comments
 (0)