Skip to content

Commit 894ac3d

Browse files
authored
Merge pull request #5 from qossmic/iframe
Add resizable iframe
2 parents a496dfc + f49b4e8 commit 894ac3d

11 files changed

Lines changed: 206 additions & 46 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ twig_doc:
3838
# or for localized: prefix: /{_locale}/twig/doc/
3939
```
4040

41+
### Template
42+
43+
To use your design in the documentation, you have to override the component template.
44+
45+
Create a template in your project: templates/bundles/TwigDocBundle/component.html.twig
46+
47+
```twig
48+
{% extends '@!TwigDoc/component.html.twig' %}
49+
50+
{% block stylesheets %}
51+
<link rel="stylesheet" href="{{ asset('css/your-styles.css') }}">
52+
{% endblock %}
53+
```
54+
55+
#### Customizing the documentation
56+
57+
If you want to customize the documentation, you can override the template.
58+
59+
Create a template in your project: templates/bundles/TwigDocBundle/documentation.html.twig
60+
61+
```twig
62+
{% extends '@!TwigDoc/documentation.html.twig' %}
63+
64+
{% block stylesheets %}
65+
<link rel="stylesheet" href="{{ asset('css/your-styles.css') }}">
66+
{% endblock %}
67+
```
68+
4169
### Configuration
4270

4371
Create a config file: configs/packages/twig_doc.yaml

config/documentation.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,30 @@
1010
use Qossmic\TwigDocBundle\Twig\TwigDocExtension;
1111

1212
return static function (ContainerConfigurator $container) {
13-
$container->services()
14-
->set('twig_doc.controller.documentation', TwigDocController::class)
15-
->public()
16-
->args([
17-
service('twig'),
18-
service('twig_doc.service.component')
19-
])
13+
$container->services() ->set('twig_doc.controller.documentation', TwigDocController::class)
14+
->public()
15+
->autoconfigure()
16+
->autowire()
17+
->arg('$profiler', service('profiler')->nullOnInvalid())
2018
->set('twig_doc.service.category', CategoryService::class)
19+
->alias(CategoryService::class, 'twig_doc.service.category')
2120

2221
->set('twig_doc.service.component_factory', ComponentItemFactory::class)
23-
->public()
24-
->args([service('validator'), service('twig_doc.service.category')])
22+
->public()
23+
->autoconfigure()
24+
->autowire()
25+
->alias(ComponentItemFactory::class, 'twig_doc.service.component_factory')
2526

2627
->set('twig_doc.service.component', ComponentService::class)
27-
->public()
28-
->args([service('twig_doc.service.component_factory'), service('twig_doc.service.category')])
28+
->public()
29+
->autoconfigure()
30+
->autowire()
31+
->alias(ComponentService::class, 'twig_doc.service.component')
2932

3033
->set('twig_doc.twig.extension', TwigDocExtension::class)
31-
->args([
32-
service('ux.twig_component.component_renderer')->nullOnInvalid(),
33-
service('twig_doc.service.component'),
34-
service('twig_doc.service.category'),
35-
service('twig')]
36-
)
37-
->tag('twig.extension');
34+
->autoconfigure()
35+
->autowire()
36+
->tag('twig.extension')
37+
->alias(TwigDocExtension::class, 'twig_doc.twig.extension')
38+
;
3839
};

config/routing/documentation.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
<route id="_documentation_invalid_components" path="/invalid">
1111
<default key="_controller">twig_doc.controller.documentation::invalidComponents</default>
1212
</route>
13+
<route id="_documentation_component_view" path="/component-view">
14+
<default key="_controller">twig_doc.controller.documentation::componentView</default>
15+
</route>
1316

1417
</routes>

src/Controller/TwigDocController.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
use Symfony\Component\HttpFoundation\Request;
77
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9+
use Symfony\Component\HttpKernel\Profiler\Profiler;
810
use Twig\Environment;
911
use Qossmic\TwigDocBundle\Component\ComponentItem;
1012
use Qossmic\TwigDocBundle\Service\ComponentService;
@@ -13,7 +15,8 @@ class TwigDocController
1315
{
1416
public function __construct(
1517
private readonly Environment $twig,
16-
private readonly ComponentService $componentService
18+
private readonly ComponentService $componentService,
19+
private readonly ?Profiler $profiler = null
1720
)
1821
{
1922
}
@@ -28,7 +31,7 @@ public function index(Request $request): Response
2831
}
2932

3033
return new Response(
31-
$this->twig->render('@TwigDoc/pages/index.html.twig', [
34+
$this->twig->render('@TwigDoc/documentation.html.twig', [
3235
'components' => $components,
3336
'filterQuery' => $filterQuery,
3437
'filterType' => $filterType ?? null,
@@ -42,4 +45,25 @@ public function invalidComponents(): Response
4245
$this->twig->render('@TwigDoc/pages/invalid_components.html.twig')
4346
);
4447
}
48+
49+
public function componentView(Request $request): Response
50+
{
51+
$name = $request->query->get('name');
52+
$component = $this->componentService->getComponent($name);
53+
if (!$component) {
54+
throw new NotFoundHttpException(sprintf('Component %s is unknown', $name));
55+
}
56+
$breakpoint = $request->query->get('breakpoint');
57+
// disable profiler to get rid of toolbar in dev
58+
if($this->profiler) {
59+
$this->profiler->disable();
60+
}
61+
return new Response(
62+
$this->twig->render('@TwigDoc/component.html.twig', [
63+
'component' => $component,
64+
'componentData' => $request->query->all('data'),
65+
'quantity' => $request->query->get('quantity')
66+
])
67+
);
68+
}
4569
}

src/Service/ComponentService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,9 @@ public function getInvalidComponents(): array
131131
{
132132
return $this->invalidComponents;
133133
}
134+
135+
public function getComponent(string $name): ?ComponentItem
136+
{
137+
return array_values($this->filterComponents($name, 'name'))[0] ?? null;
138+
}
134139
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<html lang="en">
22
{% block head %}
33
{% block stylesheets %}
4-
<style>
5-
{{ include('@TwigDoc/style/style.css.twig') }}
6-
</style>
74
{% endblock %}
85
<title>{% block title %}Twig Component Documentation{% endblock %}</title>
96
{% block javascripts %}
107
{% endblock %}
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
119
{% endblock %}
1210
</html>
1311

1412
<body>
15-
{% block body '' %}
13+
{% for i in 1..quantity %}
14+
{{ renderComponent(component, componentData ?? [])|raw }}
15+
{% endfor %}
1616
</body>

templates/component/_item.html.twig

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,30 @@
4545
</div>
4646
</div>
4747
<div class="twig-doc-component-examples">
48-
<h4>Variations</h4>
49-
{% for variation in component.variations %}
50-
<div class="twig-doc-variation">
51-
<div class="twig-doc-component-variation">
52-
{{ renderComponent(component, variation ?? [])|raw }}
53-
</div>
54-
<div class="twig-doc-variation-description">
55-
<ul>
56-
{% for key, value in variation %}
57-
<li>
58-
<b>{{ key }}:</b>
59-
{% include '@TwigDoc/component/_parameter.html.twig' with {parameter: value} %}
60-
</li>
61-
{% endfor %}
62-
</ul>
48+
<h4>Previews</h4>
49+
<div class="twig-doc-variation">
50+
{% for name, variation in component.variations %}
51+
<h5{% if loop.first %} class="active"{% endif %} data-variation="{{ name }}">{{ name }}</h5>
52+
{% endfor %}
53+
{% for name, variation in component.variations %}
54+
<div class="twig-doc-variation-body{% if loop.first %} active{% endif %}" data-variation="{{ name }}">
55+
56+
<div class="twig-doc-viewport-container">
57+
{% include '@TwigDoc/component/_viewport.html.twig' with {component: component, componentData: variation ?? [], width: '1280px'} %}
58+
</div>
59+
<div class="twig-doc-variation-description">
60+
<ul>
61+
{% for key, value in variation %}
62+
<li>
63+
<b>{{ key }}:</b>
64+
{% include '@TwigDoc/component/_parameter.html.twig' with {parameter: value} %}
65+
</li>
66+
{% endfor %}
67+
</ul>
68+
</div>
6369
</div>
64-
</div>
65-
{% endfor %}
70+
{% endfor %}
71+
</div>
6672
</div>
6773
</div>
6874
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="twig-doc-viewport" style="width: {{ width }}px">
2+
<iframe class="twig-doc-iframe"
3+
src="{{ path('_documentation_component_view', {name: component.name, data: componentData, quantity: 1}) }}"
4+
>
5+
</iframe>
6+
</div>
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1-
{% extends '@TwigDoc/base.html.twig' %}
1+
<html lang="en">
2+
{% block head %}
3+
{% block stylesheets %}
4+
<style>
5+
{{ include('@TwigDoc/style/style.css.twig') }}
6+
</style>
7+
{% endblock %}
8+
<title>{% block title %}Twig Component Documentation{% endblock %}</title>
9+
{% block javascripts %}
10+
<script type="application/javascript">
11+
window.addEventListener('load', () => {
12+
document.querySelectorAll('.twig-doc-variation h5').forEach(
13+
function (element) {
14+
element.addEventListener('click', function () {
15+
console.log(element);
16+
let variation = element.getAttribute('data-variation');
17+
element.parentNode.querySelectorAll('.twig-doc-variation-body, h5').forEach(
18+
function (e) {
19+
e.classList.remove('active');
20+
});
21+
element.classList.add('active');
22+
element.parentNode.querySelector(`.twig-doc-variation-body[data-variation="${variation}"]`).classList.add('active');
23+
})
24+
}
25+
)
26+
});
27+
</script>
28+
{% endblock %}
29+
{% endblock %}
30+
</html>
231

32+
<body>
333
{% block body %}
434
<div id="twig-doc-container">
535

@@ -16,9 +46,9 @@
1646
</div>
1747
<!-- find better solution -->
1848
<script>
19-
function widthResizer(){
49+
function widthResizer() {
2050
var width = window.innerWidth
21-
if(document.getElementById("screenwidth")) {
51+
if (document.getElementById("screenwidth")) {
2252
document.getElementById("screenwidth").innerHTML = "Width: " + width + "px"
2353
}
2454
console.log(width)
@@ -64,10 +94,11 @@
6494
{% for items in components %}
6595
<div>
6696
{% for item in items %}
67-
{% include '@TwigDoc/component/_item.html.twig' with { component: item }%}
97+
{% include '@TwigDoc/component/_item.html.twig' with { component: item } %}
6898
{% endfor %}
6999
</div>
70100
{% endfor %}
71101
</div>
72102
</div>
73103
{% endblock %}
104+
</body>

templates/pages/invalid_components.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends'@TwigDoc/base.html.twig' %}
1+
{% extends'@TwigDoc/documentation.html.twig' %}
22

33
{% block body %}
44
<h1>Invalid Components</h1>

0 commit comments

Comments
 (0)