area17 / blast

Storybook for Laravel Blade 🚀
https://dev.to/area17/getting-started-with-blast-storybook-for-laravel-blade-c5c
Apache License 2.0
268 stars 39 forks source link

Add Twill Transformers support #47

Closed antonioribeiro closed 2 years ago

antonioribeiro commented 2 years ago

To use this on any Twill based application:

Add the transformer to your view using the @transformer directive. The args key can still be used on the @storybook directive, but the purpose here is to inject it from a transformer:

@transformer(App\Transformers\Posts)

@storybook([
    'layout' => 'fullscreen',
    'status' => 'dev',
    'args' => [],
])

Your base Transformer must call the Blast internal transformer:

public static function blast($transformer, $bladeDefinedVars): array
{
    return app(BlastTransformer::class)->transform($transformer, $bladeDefinedVars);
}

Then your transformer must define a transformStorybookData() method that will return the fake data:

<?php

namespace App\Transformers;

class Posts extends Transformer
{
    public function transform(): array
    {
        return [
            'title' => $this->title,
        ];
    }

    public function transformStorybookData(): array
    {
        return [
            'title' => 'Fake Title to Be Displayed Inside Storybook Only',
        ];
    }
}
antonioribeiro commented 2 years ago

Actually, this doesn't really needs to be in Blast, I'm moving it to the transformers package.