Skip to content

Commit c5debb1

Browse files
authored
Custom Viewports (#90)
* Add support for custom viewports and generating them from a tailwind config * Update defaults and remove console log * tidy up * Update to use tailwind config by default. Falls back to false if no tailwind config found
1 parent 30f00d9 commit c5debb1

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

Diff for: .storybook/preview.js

+5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ let setDocsTheme = (configDocsTheme) => {
1212
}
1313
};
1414

15+
const customViewports = JSON.parse(process.env.STORYBOOK_VIEWPORTS);
16+
1517
const preview = {
1618
parameters: {
19+
viewport: {
20+
viewports: customViewports
21+
},
1722
controls: {
1823
expanded: JSON.parse(process.env.STORYBOOK_EXPANDED_CONTROLS)
1924
},

Diff for: README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ An array of custom components used by Blast.
262262

263263
Default: `[ 'docs-page' => Components\DocsPages\DocsPage::class ]`
264264

265+
#### `storybook_viewports`
266+
267+
Configure custom viewports in the Storybook preview toolbar.
268+
269+
It supports an array with the structure found [in the Storybook docs](https://storybook.js.org/docs/react/essentials/viewport#add-new-devices) and it can also use your Tailwind config to generate the viewports for you by setting the value to `'tailwind'`. Defaults to `'tailwind'` but fails silently if blast can't find a Tailwind config. The viewports can be disabled by setting to `false`.
270+
271+
It supports the various ways you can define breakpoints in Tailwind using these rules:
272+
273+
- If the value is a string, it uses that
274+
- If the value is an array with only a `min` **or** only a `max` it will use that value
275+
- If the value is an array with both a `min` **and** `max` value it will use the `min` value
276+
- `raw` values will be ignored
277+
278+
Default: `'tailwind'`
279+
265280
## Story Configuration
266281

267282
There are certain Storybook elements you can configure from within your story blade files. You can do this by adding the `@storybook` directive to the top of your files:
@@ -346,7 +361,7 @@ It can be run alongside the `php artisan blast:launch` task or you can run the d
346361

347362
## Presetting story options
348363

349-
You can create preset options for components to reuse throughtout your storybook instance.
364+
You can create preset options for components to reuse throughout your storybook instance.
350365

351366
The preset options use the same structure as Laravel config files:
352367

Diff for: config/blast.php

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
// More info - https://storybook.js.org/docs/react/writing-stories/naming-components-and-hierarchy#sorting-stories
8989
'storybook_sort_order' => [],
9090

91+
'storybook_viewports' => 'tailwind',
92+
9193
// set the background color of the storybook canvas area
9294
'canvas_bg_color' => '',
9395

Diff for: src/Commands/Launch.php

+52-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace A17\Blast\Commands;
44

5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
7+
use A17\Blast\Traits\Helpers;
58
use Illuminate\Console\Command;
69
use Illuminate\Filesystem\Filesystem;
7-
use A17\Blast\Traits\Helpers;
810

911
class Launch extends Command
1012
{
@@ -53,6 +55,7 @@ public function __construct(Filesystem $filesystem)
5355
[],
5456
);
5557
$this->storybookSortOrder = config('blast.storybook_sort_order', []);
58+
$this->storybookViewports = config('blast.storybook_viewports', false);
5659
}
5760

5861
/*
@@ -140,9 +143,57 @@ public function handle()
140143
$this->storybookGlobalTypes,
141144
),
142145
'STORYBOOK_SORT_ORDER' => json_encode($this->storybookSortOrder),
146+
'STORYBOOK_VIEWPORTS' => json_encode(
147+
$this->buildTailwindViewports(),
148+
),
143149
'LIBSTORYPATH' => $this->vendorPath . '/stories',
144150
'PROJECTPATH' => base_path(),
145151
'COMPONENTPATH' => base_path('resources/views/stories'),
146152
]);
147153
}
154+
155+
private function buildTailwindViewports()
156+
{
157+
if ($this->storybookViewports === 'tailwind') {
158+
$parsedConfig = $this->vendorPath . '/tmp/tailwind.config.php';
159+
160+
if (!$this->filesystem->exists($parsedConfig)) {
161+
return false;
162+
}
163+
164+
$config = include $parsedConfig;
165+
166+
$viewports = [];
167+
168+
foreach ($config['theme']['screens'] as $key => $value) {
169+
$width = false;
170+
171+
if (is_array($value) && !empty($value)) {
172+
if (Arr::has($value, 'min')) {
173+
$width = $value['min'];
174+
} elseif (Arr::has($value, 'max')) {
175+
$width = $value['max'];
176+
}
177+
} elseif (is_string($value) && $value !== '0') {
178+
$width = $value;
179+
}
180+
181+
if ($width) {
182+
$viewports[$key] = [
183+
'name' => $key,
184+
'styles' => [
185+
'width' => $width,
186+
'height' => '100%',
187+
],
188+
];
189+
}
190+
}
191+
192+
return $viewports;
193+
} else {
194+
return $this->storybookViewports;
195+
}
196+
197+
return false;
198+
}
148199
}

0 commit comments

Comments
 (0)