Skip to content

Commit

Permalink
Refactor(web-twig): Refactor directory structure of components
Browse files Browse the repository at this point in the history
  * hold context of each component in the same directory
  • Loading branch information
literat committed Jul 30, 2022
1 parent 20cb419 commit dfb68d1
Show file tree
Hide file tree
Showing 78 changed files with 87 additions and 58 deletions.
1 change: 1 addition & 0 deletions packages/web-twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Introduce `Pill` component
- Introduce `Tabs` component
- Refactor components and update their readme
- Refactor components directory tree

## 1.7.0 - 2022-05-09
- Add Svg twig extension for optimal loading of svg files as inline
Expand Down
47 changes: 47 additions & 0 deletions packages/web-twig/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,52 @@

> Guide for contributors.
## Components definition
In order to maintain the uniformity of writing and functioning of components in HTML-like syntax, it is necessary to accept the following rules in the implementation.

## Rules in components

1. Name of components must be camelCase with first letter small.
2. New components must contain a property class so that they can be extended according to the [instructions](./docs/extendComponents.md)
```twig
{% set _class = (props.class is defined) ? ' ' ~ props.class : '' -%}
```

2. Components must have block content when is not self-closing. This block is in HTML-like syntax used to define children.
3. Components must contain attribute props. This is so that it resembles the React components as much as possible.
4. All internal twig variables should start by underscore (this represents private variables).

```twig
{% set _privateAttr = (props.attr is defined) ? pros.attr : '' %}
```

### Example of component definition

```twig
{# This represent main component class and prepend class prefix #}
{% set _className = _spiritClassPrefix ~ 'MainComponentClass' %}
{# This represent extendable component #}
{% set _class = (props.class is defined) ? ' ' ~ props.class : '' -%}
<span class="{{ _className }}{{ _class }}">
{# This represent children #}
{%- block content %}{% endblock %}
</span>
```

### Example of component usage

```twig
{% embed "@spirit/button.twig" with { props: {
attr: 'value'
}} %}
{% block content %}
Some children content
{% endblock %}
{% endembed %}
```

## Release new version
* run script `. ./newVersion.sh <version>`
47 changes: 0 additions & 47 deletions packages/web-twig/docs/contribution.md

This file was deleted.

2 changes: 1 addition & 1 deletion packages/web-twig/docs/extendComponents.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ After the previous change, we can now call the Button component with additional
<Button color="primary" size="small">Primary buttom</Button>
```

[Button]: https://github.com/lmc-eu/spirit-design-system/tree/main/packages/web/src/components/Button
[Button]: https://github.com/lmc-eu/spirit-design-system/tree/main/packages/web/src/components/Button
2 changes: 1 addition & 1 deletion packages/web-twig/docs/inlineSVG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ then it is possible to call in the component
{{ inlineSvg('@icons-assets/iconName.svg', { class: ..., title: ... }) }}
```

if the icon does not exist, an empty string is returned and messages are sent to the symfony error logger.
if the icon does not exist, an empty string is returned and messages are sent to the symfony error logger.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class SpiritWebTwigExtension extends Extension

public const PARAMETER_ICONS_PATH_ALIAS = 'spirit_web_twig.icons.alias';

public const DEFAULT_TWIG_COMPONENTS_PATH = __DIR__ . '/../Resources/twig-components';

public const DEFAULT_COMPONENTS_PATH = __DIR__ . '/../Resources/components';

public const DEFAULT_PATH_ALIAS = 'spirit';
Expand All @@ -46,7 +48,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');

$container->setParameter(self::PARAMETER_PATHS, array_merge($config['paths'], [self::DEFAULT_COMPONENTS_PATH]));
$container->setParameter(self::PARAMETER_PATHS, array_merge($config['paths'], [self::DEFAULT_TWIG_COMPONENTS_PATH, self::DEFAULT_COMPONENTS_PATH]));
$container->setParameter(self::PARAMETER_SPIRIT_CSS_CLASS_PREFIX, isset($config['spirit_css_class_prefix']) ? $config['spirit_css_class_prefix'] . '-' : null);
$container->setParameter(self::PARAMETER_PATH_ALIAS, $config['paths_alias']);
$container->setParameter(self::PARAMETER_HTML_SYNTAX_LEXER, (bool) $config['html_syntax_lexer']);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/alert.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Alert/Alert.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Button/Button.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/ButtonLink/ButtonLink.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/CheckboxField/CheckboxField.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Container/Container.twig' %}
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/grid.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Grid/Grid.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Header/Header.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Heading/Heading.twig' %}
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/link.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Link/Link.twig' %}
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/nav.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Header/Nav.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Header/NavItem.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Header/NavLink.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Header/Navbar.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Header/NavbarActions.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Header/NavbarClose.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Header/NavbarToggle.twig' %}
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/pill.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Pill/Pill.twig' %}
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/stack.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Stack/Stack.twig' %}
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/tabs.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Tabs/index.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Tabs/TabsItem.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Tabs/TabsLink.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Tabs/TabsPane.twig' %}
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/tag.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Tag/Tag.twig' %}
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/text.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Text/Text.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/TextField/TextField.twig' %}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testShouldRegisterTwigPaths(array $paths, int $expectedCalls): v
*/
public function registerTwigPathsDataProvider(): array
{
$defaultPath = realpath(__DIR__ . '/../../../src/DependencyInjection') . '/../Resources/components';
$defaultPath = realpath(__DIR__ . '/../../../src/DependencyInjection') . '/../Resources/twig-components';

return [
'test should register random one path' => [
Expand All @@ -70,7 +70,7 @@ public function registerTwigPathsDataProvider(): array
['dir1/', 'dir2'], 2,
],
'test should register paths with default' => [
['dir1/', 'dir2', $defaultPath], 4,
['dir1/', 'dir2', $defaultPath], 3,
],
];
}
Expand Down
3 changes: 2 additions & 1 deletion packages/web-twig/tests/Helper/TwigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ public static function setup(
array $extendedComponentsPath = []
): Environment {
$loader = new FilesystemLoader($defaultTemplatePath);
$paths = array_merge($extendedComponentsPath, [SpiritWebTwigExtension::DEFAULT_COMPONENTS_PATH]);
$paths = array_merge($extendedComponentsPath, [SpiritWebTwigExtension::DEFAULT_TWIG_COMPONENTS_PATH, SpiritWebTwigExtension::DEFAULT_COMPONENTS_PATH]);

foreach ($paths as $path) {
$loader->addPath($path, $defaultAlias);
}

$loader->addPath(SpiritWebTwigExtension::DEFAULT_PARTIALS_PATH, SpiritWebTwigExtension::DEFAULT_PARTIALS_ALIAS);
$loader->addPath(SpiritWebTwigExtension::DEFAULT_TWIG_COMPONENTS_PATH, SpiritWebTwigExtension::DEFAULT_PATH_ALIAS);
$loader->addPath(SpiritWebTwigExtension::DEFAULT_COMPONENTS_PATH, SpiritWebTwigExtension::DEFAULT_PATH_ALIAS);

$twig = new Environment($loader, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
isRequired: "true",
validationState: "error",
message: "validation failed",
}} %}
}} %}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
<span>col 5</span>
<span>col 6</span>
{% endblock %}
{% endembed %}
{% endembed %}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
<div>List item 1</div>
</li>
{% endblock %}
{% endembed %}
{% endembed %}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
isRequired: "true",
validationState: "error",
message: "validation failed",
}} %}
}} %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% set _sizeClass = props.size is defined ? _spiritClassPrefix ~ 'Button--' ~ props.size %}
{% set props = props|merge({ 'class': _sizeClass }) %}

{% extends "@spirit/button.twig" %}
{% extends "@spirit/button.twig" %}

0 comments on commit dfb68d1

Please sign in to comment.