storybook_rails 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d9e73c7b0bf6d8f82291a937ef7d37f74621b5e1803d561a7396829ca4f61c3
4
- data.tar.gz: ce0f60e3dd44c77e3678287d74ac340b3d5dd3233b1f7d52e31f083d734acb8d
3
+ metadata.gz: d93e71f5673db4150c93133e0fefb5a8fe052add9b78e4e59d5efbfb90a9fac0
4
+ data.tar.gz: 73496d3de905209e3c55ec87a09458c51f3d90b6b2145531924ecf4da2ed4e52
5
5
  SHA512:
6
- metadata.gz: 6a7b37fc8c986e55f9926c307e8966b40cc855f0174f2cf9c9fd7b4e2458abe3b634471edcfc007b85b1822f4ca2a231e149cfe995228fa499a7a280f54794b7
7
- data.tar.gz: 8f6e2d0188d83131473477881735d314c3f118f3dbe08c6028e5650fe4b58e6b9b804b0df66d3f022043a1f262a5deb5d43e92d2c5db26ff09ed0cfc2340c772
6
+ metadata.gz: a31bec0636ea50f973151b068724f4d689a7218c062f5816206e2c8714e7cfd58f5ee84f10bd1d0cfa0a02cc8afc488ce7f11fb3f6509ae1b11a293c5bad6cad
7
+ data.tar.gz: cbfce6773d0fe32ef5f96c062d78ad79d6145b9b6882e94be88dbf730a4e1db8f1a3dac29efa5866d6083af7ef7d30a1153adb8c3a80e97faa730c0326bf5b25
data/README.md CHANGED
@@ -66,12 +66,15 @@ Equivalent configuration will be necessary in `config/production.rb` or `applica
66
66
  ```
67
67
 
68
68
  ### Webpacker
69
- If your application uses Webpacker to compile your JavaScript and/or CSS, you will need to modify the default Storybook webpack configuration. Please see the [Storybook Webpack config](https://storybook.js.org/docs/react/configure/webpack) for more information on how to do that. Here's an example:
69
+ If your application uses Webpacker to compile your JavaScript and/or CSS, you will need to modify the default Storybook webpack configuration. Please see the [Storybook Webpack config](https://storybook.js.org/docs/react/configure/webpack) for more information on how to do that.
70
+
71
+ Here's an example of what that _might_ look like:
70
72
 
71
73
  ```javascript
72
74
  // .storybook/main.js
73
75
 
74
76
  const path = require('path');
77
+ // Import the Webpacker webpack environment
75
78
  const environment = require('../config/webpack/environment');
76
79
  const { merge } = require('webpack-merge');
77
80
 
@@ -81,7 +84,8 @@ module.exports = {
81
84
  // You can change the configuration based on that.
82
85
  // 'PRODUCTION' is used when building the static version of storybook.
83
86
  let envConfig = environment.toWebpackConfig();
84
-
87
+
88
+ // Tell Webpack to compile an additional entry point, pointing to your Webpacker pack file(s)
85
89
  let entries = {
86
90
  main: config.entry,
87
91
  application: path.resolve(__dirname, '../app/javascript/packs/application.js')
@@ -89,7 +93,8 @@ module.exports = {
89
93
 
90
94
  config.entry = entries
91
95
 
92
- // Storybook doesn't support .scss out of the box
96
+ // Storybook doesn't support .scss out of the box, and we like scss, so...
97
+ // Add a rule to tell Webpack to process .scss files using these loaders
93
98
  config.module.rules.push({
94
99
  test: /\.scss$/,
95
100
  use: ['style-loader', 'css-loader', 'sass-loader'],
@@ -116,7 +121,8 @@ In package.json:
116
121
  {
117
122
  ...
118
123
  "scripts": {
119
- "storybook:start": "rm -rf node_modules/.cache/storybook && start-storybook",
124
+ "storybook:clean": "find ./test/components/stories -name '*.stories.json' -delete && rm -rf node_modules/.cache/storybook",
125
+ "storybook:start": "yarn storybook:clean && yarn storybook:write-json && start-storybook -p 8081",
120
126
  "storybook:write-json": "bundle exec rake storybook_rails:write_stories_json",
121
127
  "storybook:watch": "chokidar '**/*_stories.rb' -c 'yarn storybook:write-json'"
122
128
  },
@@ -146,7 +152,7 @@ Suppose our app has a shared `app/views/shared/_button.html.erb` partial:
146
152
  We can write a stories describing the `_button.html.erb` partial:
147
153
 
148
154
  ```ruby
149
- # buttons/button_stories.rb
155
+ # test/components/stories/buttons/button_stories.rb
150
156
 
151
157
  class Buttons::ButtonStories < ActionView::Storybook::Stories
152
158
  self.title = "Buttons"
@@ -173,7 +179,7 @@ end
173
179
 
174
180
  And a story template to render individual stories:
175
181
  ```erb
176
- # buttons/button_stories.html.erb
182
+ # test/components/stories/buttons/button_stories.html.erb
177
183
 
178
184
  <% story_name_class_map = {
179
185
  primary: "button",
@@ -187,6 +193,13 @@ And a story template to render individual stories:
187
193
 
188
194
  It's up to you how handle rendering your partials in Storybook, but `storybook_rails` will look for a view template that matches the story name (`buttons/button_stories.html.erb` in the example above. In addition, `storybook_rails` provides a `story_params` helper which provides quick access to the params and args specified in the story config. You can use these parameters in your view template to render each story dynamically. Or not. It's up to you.
189
195
 
196
+ ### Generating Storybook Files
197
+ `storybook_rails` includes a Rails generator to make it easy to generate the files outlined in the section above.
198
+
199
+ To generate the files above, we could have done this: `bin/rails generate storybook_rails:stories Button primary secondary outline`.
200
+
201
+ For more detail, `bin/rails storybook_rails:stories --help`.
202
+
190
203
  ### Generating Storybook Stories JSON
191
204
 
192
205
  Generate the Storybook JSON stories by running the rake task:
@@ -226,13 +239,79 @@ If Storybook fails to load with a `cannot get /` error, it could be related to [
226
239
  }
227
240
  ```
228
241
 
242
+ #### Storybook fails to fetch due to CORS
243
+ If the Storybook UI is rendering, but not fetching stories from your Rails server and you see an error in the browser console like this:
244
+ ```bash
245
+ Access to fetch at 'http://localhost:3000/storybook/button/primary' from origin 'http://localhost:8081' has been blocked by
246
+ CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs,
247
+ set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
248
+ ```
249
+ Then you probably need to setup CORS. Please see [rack-cors](https://github.com/cyu/rack-cors) for examples.
250
+
229
251
  ### The Story DSL
230
252
 
231
- Coming Soon
253
+ 🚧 Work in progress... 🚧
232
254
 
233
255
  #### Parameters
256
+ From the [Storybook CSF docs](https://storybook.js.org/docs/html/writing-stories/parameters):
257
+ > Parameters are a set of static, named metadata about a story, typically used to control the behavior of Storybook features and addons.
258
+
259
+ Using the example parameters [configuration](https://storybook.js.org/docs/html/essentials/backgrounds#configuration) for the Backgrounds addon, this is how you could accomplish the same parameters using the `parameters` method:
260
+
261
+ ```ruby
262
+ class ButtonStories < ActionView::Storybook::Stories
263
+ background_data = {
264
+ default: 'twitter',
265
+ values: [
266
+ { name: 'twitter', value: '#00aced' },
267
+ { name: 'facebook', value: '#3b5998' }
268
+ ]
269
+ }
270
+
271
+ parameters( { backgrounds: background_data })
272
+
273
+ story(:primary) do
274
+ parameters({ backgrounds: { default: 'facebook'} })
275
+
276
+ controls do
277
+ text(:button_text, "Primary")
278
+ end
279
+ end
280
+ end
281
+ ```
282
+
283
+
234
284
  #### Layout
285
+ By default, your stories will render without a layout. This can be customized just like you would normally in a Rails controller, by calling `layout "application"`, at either the class level, the story level, or both.
286
+
287
+ ```ruby
288
+ class Buttons::ButtonStories < ActionView::Storybook::Stories
289
+ self.title = "Buttons"
290
+ layout "application"
291
+
292
+ story(:primary) do
293
+ controls do
294
+ text(:button_text, "Primary")
295
+ end
296
+ end
297
+
298
+ story(:secondary) do
299
+ controls do
300
+ text(:button_text, "Secondary")
301
+ end
302
+ end
303
+
304
+ story(:custom) do
305
+ controls do
306
+ text(:button_text, "Custom")
307
+ end
308
+
309
+ layout "custom"
310
+ end
311
+ end
312
+ ```
235
313
  #### Controls
314
+ Coming soon... in the meantime, see https://github.com/danieldpence/storybook_rails/tree/main/lib/action_view/storybook/controls.
236
315
 
237
316
 
238
317
  ## Development
@@ -39,7 +39,9 @@ module ActionView
39
39
  end
40
40
 
41
41
  rake_tasks do
42
+ # :nocov:
42
43
  load File.join(__dir__, "tasks/write_stories_json.rake")
44
+ # :nocov:
43
45
  end
44
46
  end
45
47
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActionView
4
4
  module Storybook
5
- VERSION = "1.1.0"
5
+ VERSION = "1.2.0"
6
6
  end
7
7
  end
@@ -0,0 +1,12 @@
1
+ Description:
2
+ ============
3
+ Generates a [NAME]_stories.rb file and matching view template with
4
+ the given NAME (if one does not exist) and optional [story_names].
5
+
6
+ Example:
7
+ ========
8
+ bin/rails generate storybook_rails:stories Button default primary
9
+
10
+ creates a Button story file and view template:
11
+ Story File: test/components/button_stories.rb
12
+ Template: test/components/button_component.html.erb
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/named_base"
4
+
5
+ module StorybookRails
6
+ class StoriesGenerator < Rails::Generators::NamedBase
7
+ source_root File.expand_path("templates", __dir__)
8
+ argument :stories, type: :array, default: [], banner: "stories"
9
+ check_class_collision suffix: "Stories"
10
+
11
+ def generate_stories_files
12
+ template "stories.rb", File.join(stories_path.to_s, "#{file_path}_stories.rb")
13
+ template "stories.html.erb", File.join(stories_path.to_s, "#{file_path}_stories.html.erb")
14
+ end
15
+
16
+ private
17
+
18
+ def stories_path
19
+ Rails.application.config.storybook_rails.stories_path
20
+ end
21
+ end
22
+ end
@@ -0,0 +1 @@
1
+ <div>Render the <%= class_name %> partial here.</div>
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= class_name %>Stories < ActionView::Storybook::Stories
4
+ <%- stories.each do |story_name| -%>
5
+ story(:<%= story_name %>) do
6
+ controls do
7
+ end
8
+ end
9
+
10
+ <%- end -%>
11
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storybook_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pence
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-27 00:00:00.000000000 Z
11
+ date: 2021-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -196,6 +196,10 @@ files:
196
196
  - lib/action_view/storybook/story_config.rb
197
197
  - lib/action_view/storybook/tasks/write_stories_json.rake
198
198
  - lib/action_view/storybook/version.rb
199
+ - lib/generators/storybook_rails/USAGE
200
+ - lib/generators/storybook_rails/stories_generator.rb
201
+ - lib/generators/storybook_rails/templates/stories.html.erb.tt
202
+ - lib/generators/storybook_rails/templates/stories.rb.tt
199
203
  homepage: https://github.com/danieldpence/storybook_rails
200
204
  licenses:
201
205
  - MIT