Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
feat: Add expandable component (#46)
Browse files Browse the repository at this point in the history
# PR description

I've tried to make the composition of the button structure easy to follow inside the template, by following a path of which variable is defined and later use how.

Although it's tempting to expose the `collapsed` property to the public, in order to allow for `knobs`, I haven't, following the story of the React component.

All buttons have been updated to include `data-ecl-label` attribute, probably something not [migrated](https://github.com/ec-europa/europa-component-library/blob/v2-dev/src/systems/ec/implementations/react/packages/button/src/Button.jsx#L38) yet because of a lack of use case. Which happens in this pull request, as the vanilla JS implementation requires the presence of this attribute.

The `@ecl/ec-utility-typography` dependency is added here, but not in the React, because React can render children in a more flexible way, here we need it because of `ecl-u-type-paragraph-m`.

## QA Checklist

In order to ensure a safe and quick review, please check that your PR follow those guidelines:

- [x] I have put the vanilla component as `devDependencies`
- [x] I have put the specs package as `devDependencies`
- [x] I have added the components directly used in the twig file (with `include` or `embed`) as `dependencies`
- [x] My component is listed in `@ecl-twig/ec-components`'s `dependencies`
- [x] My variables naming follow the guidelines (snake case for twig)
- [x] I have provided tests
- [x] I have provided documentation (for the "notes" tab)
- [x] If my local `yarn.lock` contains changes, I have committed it
- [x] I have given my PR the proper label (`pr: review needed` to indicate that I'm done and now waiting for a review ,`pr: wip` to indicate that I'm actively working on it ...)
  • Loading branch information
kalinchernev authored and emeryro committed Mar 6, 2019
1 parent 0b167d2 commit dc17683
Show file tree
Hide file tree
Showing 19 changed files with 424 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`EC - Composition renders correctly 1`] = `
"<button class=\\"ecl-button ecl-button--primary\\" type=\\"submit\\">
<span class=\\"ecl-button__container\\"
><span class=\\"ecl-button__label\\">Example button</span></span
><span class=\\"ecl-button__label\\" data-ecl-label>Example button</span></span
>
</button>
<blockquote class=\\"ecl-blockquote\\">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exports[`EC - Accordion renders correctly 1`] = `
</svg>
<span
class="ecl-button__label"
data-ecl-label=""
>
Delivery of last pending proposals, a common Destiny of unity, the hour of European Democracy
</span>
Expand Down Expand Up @@ -72,6 +73,7 @@ exports[`EC - Accordion renders correctly 1`] = `
</svg>
<span
class="ecl-button__label"
data-ecl-label=""
>
Energy union and climate
</span>
Expand Down Expand Up @@ -112,6 +114,7 @@ exports[`EC - Accordion renders correctly 1`] = `
</svg>
<span
class="ecl-button__label"
data-ecl-label=""
>
Delivery of last pending proposals, a common Destiny of unity, the hour of European Democracy
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`EC - Button CTA button - icon after renders correctly 1`] = `
>
<span
class="ecl-button__label"
data-ecl-label=""
>
CTA Button with icon
</span>
Expand Down Expand Up @@ -45,6 +46,7 @@ exports[`EC - Button CTA button - icon before renders correctly 1`] = `
</svg>
<span
class="ecl-button__label"
data-ecl-label=""
>
Call to action button
</span>
Expand All @@ -62,6 +64,7 @@ exports[`EC - Button CTA renders correctly 1`] = `
>
<span
class="ecl-button__label"
data-ecl-label=""
>
Call to action button
</span>
Expand All @@ -88,6 +91,7 @@ exports[`EC - Button Ghost renders correctly 1`] = `
>
<span
class="ecl-button__label"
data-ecl-label=""
>
Ghost button
</span>
Expand All @@ -105,6 +109,7 @@ exports[`EC - Button Primary renders correctly 1`] = `
>
<span
class="ecl-button__label"
data-ecl-label=""
>
Primary button
</span>
Expand All @@ -122,6 +127,7 @@ exports[`EC - Button Search renders correctly 1`] = `
>
<span
class="ecl-button__label"
data-ecl-label=""
>
Search button
</span>
Expand All @@ -139,6 +145,7 @@ exports[`EC - Button Secondary renders correctly 1`] = `
>
<span
class="ecl-button__label"
data-ecl-label=""
>
Secondary button
</span>
Expand Down
2 changes: 1 addition & 1 deletion src/ec/packages/ec-component-button/button.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
{%- if _icon.name is not empty and _icon_position == 'before' -%}
{%- include '../ec-component-icon/icon.html.twig' with { icon: _icon, extra_classes: 'ecl-button__icon ecl-button__icon--before' } -%}
{%- endif -%}
<span class="ecl-button__label">{% block label _label %}</span>
<span class="ecl-button__label" data-ecl-label> {% block label _label %} </span>
{%- if _icon.name is not empty and _icon_position == 'after' -%}
{%- include '../ec-component-icon/icon.html.twig' with { icon: _icon, extra_classes: 'ecl-button__icon ecl-button__icon--after' } -%}
{%- endif -%}
Expand Down
4 changes: 4 additions & 0 deletions src/ec/packages/ec-component-expandable/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__snapshots__
*.story.js
*.test.js
**/*.md
7 changes: 7 additions & 0 deletions src/ec/packages/ec-component-expandable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ECL Twig - EC Expandable component

npm package: `@ecl-twig/ec-component-expandable`

```shell
npm install --save @ecl-twig/ec-component-expandable
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EC - Expandable renders correctly 1`] = `
<div
class="ecl-expandable"
data-ecl-expandable="true"
>
<button
aria-controls="expandable-example-content"
class="ecl-button ecl-button--secondary ecl-expandable__toggle"
data-ecl-expandable-toggle="true"
data-ecl-label-collapsed="Collapsed button"
data-ecl-label-expanded="Expanded button"
type="button"
>
<span
class="ecl-button__container"
>
<span
class="ecl-button__label"
data-ecl-label=""
>
Collapsed button
</span>
<svg
aria-controls="expandable-example-content"
aria-hidden="true"
class="ecl-icon ecl-icon--fluid ecl-icon--rotate-180 ecl-button__icon ecl-button__icon--after"
data-ecl-expandable-toggle="true"
data-ecl-label-collapsed="Collapsed button"
data-ecl-label-expanded="Expanded button"
focusable="false"
>
<use
xlink:href="example#ui--corner-arrow"
/>
</svg>
</span>
</button>
<div
class="ecl-expandable__content"
hidden=""
id="expandable-example-content"
>
<p
class="ecl-u-type-paragraph-m"
>
The EU is building an energy union that ensures Europe’s energy supply is safe, viable and accessible to all. In doing so, it can boost the economy and attract investments that can create new jobs opportunities.
</p>
</div>
</div>
`;

exports[`EC - Expandable renders correctly when expanded 1`] = `
<div
class="ecl-expandable"
data-ecl-expandable="true"
>
<button
aria-controls="expandable-example-content"
aria-expanded="true"
class="ecl-button ecl-button--secondary ecl-expandable__toggle"
data-ecl-expandable-toggle="true"
data-ecl-label-collapsed="Collapsed button"
data-ecl-label-expanded="Expanded button"
type="button"
>
<span
class="ecl-button__container"
>
<span
class="ecl-button__label"
data-ecl-label=""
>
Collapsed button
</span>
<svg
aria-controls="expandable-example-content"
aria-expanded="true"
aria-hidden="true"
class="ecl-icon ecl-icon--fluid ecl-icon--rotate-180 ecl-button__icon ecl-button__icon--after"
data-ecl-expandable-toggle="true"
data-ecl-label-collapsed="Collapsed button"
data-ecl-label-expanded="Expanded button"
focusable="false"
>
<use
xlink:href="example#ui--corner-arrow"
/>
</svg>
</span>
</button>
<div
class="ecl-expandable__content"
id="expandable-example-content"
>
<p
class="ecl-u-type-paragraph-m"
>
The EU is building an energy union that ensures Europe’s energy supply is safe, viable and accessible to all. In doing so, it can boost the economy and attract investments that can create new jobs opportunities.
</p>
</div>
</div>
`;
22 changes: 22 additions & 0 deletions src/ec/packages/ec-component-expandable/demo/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable import/no-extraneous-dependencies, no-param-reassign */
import specData from '@ecl/ec-specs-expandable/demo/data';

const adapter = initialData => {
// Copy reference specification demo data.
const adaptedData = JSON.parse(JSON.stringify(initialData));

adaptedData.label_expanded = adaptedData.labelExpanded;
adaptedData.label_collapsed = adaptedData.labelCollapsed;

const [type, name] = adaptedData.button.icon.shape.split('--');

adaptedData.button.icon = {
type,
name,
...adaptedData.button.icon,
};

return adaptedData;
};

export default adapter(specData);
47 changes: 47 additions & 0 deletions src/ec/packages/ec-component-expandable/docs/expandable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ECL Twig - EC Expandable component

npm package: `@ecl-twig/ec-component-expandable`

```shell
npm install --save @ecl-twig/ec-component-expandable
```

### Parameters:

- "id" (string) (default: '')
- "label_expanded" (string) (default: '')
- "label_collapsed" (string) (default: '')
- "label_collapsed" (string) (default: '')
- "expanded" (boolean) (default: false) Rendered in collapsed state by default. Expanded state by default is also possible.
- "button" (predefined structure) : Button component structure
- "extra_classes" (optional) (string) (default: '') Extra classes (space separated)
- "extra_attributes" (optional) (array) (default: []) Extra attributes
- "name" (string) Attribute name, eg. 'data-test'
- "value" (string) Attribute value, eg: 'data-test-1'

### Blocks:

- "content"

### Example:

<!-- prettier-ignore -->
```twig
{% include 'path/to/expandable.html.twig' with {
id: 'expandable-example',
button: {
label: 'Collapsed button',
variant: 'secondary',
icon: {
type: 'ui',
name: 'corner-arrow',
transform: 'rotate-180',
size: 'fluid',
},
},
label_expanded: 'Expanded button',
label_collapsed: 'Collapsed button',
content:
'The EU is building an energy union that ensures Europe’s energy supply is safe, viable and accessible to all. In doing so, it can boost the economy and attract investments that can create new jobs opportunities.',
} %}
```
94 changes: 94 additions & 0 deletions src/ec/packages/ec-component-expandable/expandable.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{% spaceless %}

{#
Parameters:
- "id" (string) (default: '')
- "label_expanded" (string) (default: '')
- "label_collapsed" (string) (default: '')
- "label_collapsed" (string) (default: '')
- "expanded" (boolean) (default: false)
- "button" (predefined structure) : Button component structure
- "extra_classes" (string) (default: '')
- "extra_attributes" (array) (default: []): format: [
{
"name" (string) (default: ''),
"value" (string) (default: '')
},
...
]
Blocks:
- "content"
#}

{# Internal properties #}

{% set _id = id|default('') %}
{% set _css_class = 'ecl-expandable' %}
{% set _extra_attributes = "data-ecl-expandable='true'" %}

{% set _label_expanded = label_expanded|default('') %}
{% set _label_collapsed = label_collapsed|default('') %}
{% set _aria_controls = _id ~ '-content' %}
{% set _content = content|default('') %}

{% set _expanded = expanded|default(false) %}
{% set _content_hidden_attribute = '' %}

{# Internal logic - Process properties #}

{% if extra_classes is defined and extra_classes is not empty %}
{% set _css_class = _css_class ~ ' ' ~ extra_classes %}
{% endif %}

{% if extra_attributes is defined and extra_attributes is not empty and extra_attributes is iterable %}
{% for attr in extra_attributes %}
{% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name ~ '="' ~ attr.value ~ '"' %}
{% endfor %}
{% endif %}

{# Print the result #}

<div class="{{ _css_class }}"{{ _extra_attributes|raw }}>
{% set _button_attributes = [
{ name: 'aria-controls', value: _aria_controls },
{ name: 'data-ecl-expandable-toggle', value: 'true' },
{ name: 'data-ecl-label-expanded', value: _label_expanded },
{ name: 'data-ecl-label-collapsed', value: _label_collapsed },
]
%}

{% if _expanded %}
{% set _button_attributes = _button_attributes|merge([
{ name: 'aria-expanded', value: _expanded },
])
%}

{% set _label = _label_expanded %}
{% set _content_hidden_attribute = '' %}

{% else %}
{% set _label = _label_collapsed %}
{% set _content_hidden_attribute = 'hidden' %}
{% endif %}

{% include '../ec-component-button/button.html.twig' with {
label: _label,
variant: 'secondary',
type: 'button',
icon_position: "after",
extra_classes: 'ecl-expandable__toggle',
extra_attributes: _button_attributes,
}|merge(button) %}
<div
id="{{ _id ~ '-content' }}"
class="ecl-expandable__content"
{{ _content_hidden_attribute }}
>
<p class="ecl-u-type-paragraph-m">
{%- block content _content -%}
</p>
</div>
</div>

{% endspaceless %}
Loading

0 comments on commit dc17683

Please sign in to comment.