Skip to content

Commit

Permalink
Feat(web-twig): Introduce Modal component
Browse files Browse the repository at this point in the history
  • Loading branch information
crishpeen committed Aug 12, 2022
1 parent 434affb commit 43f73ee
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/web-twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Fix: Make `TextField` `id` mandatory as it is required for linking the label to the input
- Feat: Enable glob function patterns in a paths
- Feat: Add new `Alert` variant - `informative`
- Feat: Introduce `Modal` component

## 1.8.0 - 2022-07-18

Expand Down
1 change: 1 addition & 0 deletions packages/web-twig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ or pure original implementation
- [Heading](./src/Resources/components/Heading/README.md)
- [CheckboxField](./src/Resources/components/CheckboxField/README.md)
- [Link](./src/Resources/components/Link/README.md)
- [Modal](./src/Resources/components/Modal/README.md)
- [Stack](./src/Resources/components/Stack/README.md)
- [Tabs](./src/Resources/components/Tabs/README.md)
- [Tag](./src/Resources/components/Tag/README.md)
Expand Down
47 changes: 47 additions & 0 deletions packages/web-twig/src/Resources/components/Modal/Modal.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{# API #}
{%- set _class = props.class | default(null) -%}
{%- set _closeLabel = props.closeLabel | default('Close') -%}
{%- set _id = props.id -%}

{# Class names #}
{%- set _bodyClassName = _spiritClassPrefix ~ 'Modal__body' -%}
{%- set _closeClassName = _spiritClassPrefix ~ 'Modal__close' -%}
{%- set _contentClassName = _spiritClassPrefix ~ 'Modal__content' -%}
{%- set _dialogClassName = _spiritClassPrefix ~ 'Modal__dialog' -%}
{%- set _headerClassName = _spiritClassPrefix ~ 'Modal__header' -%}
{%- set _rootClassName = _spiritClassPrefix ~ 'Modal' -%}

{# Attributes #}
{%- set _idAttr = _id ? 'id=' ~ _id ~ '' : null -%}

{# Miscellaneous #}
{%- set _classNames = [ _rootClassName, _class ] -%}
{%- set _mainPropsWithoutId = props | filter((value, prop) => prop != 'id') -%}

<dialog
{{ mainProps(_mainPropsWithoutId) }}
{{ classProp(_classNames) }}
{{ _idAttr }}
data-component="modal"
>
<div class="{{ _contentClassName }}">
<div class="{{ _dialogClassName }}">
<div class="{{ _headerClassName }}">
<Button
aria-controls="{{ _id }}"
aria-expanded="false"
color="tertiary"
data-dismiss="modal"
data-target="{{ '#' ~ _id }}"
isSquare
>
<span class="{{ _closeClassName }}" aria-hidden="true"></span>
<span class="accessibility-hidden">{{ _closeLabel }}</span>
</Button>
</div>
<div class="{{ _bodyClassName }}">
{% block content %}{% endblock %}
</div>
</div>
</div>
</dialog>
59 changes: 59 additions & 0 deletions packages/web-twig/src/Resources/components/Modal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Modal

This is Twig implementation of the [Modal] component.

Basic example usage:

```html
<button
type="button"
data-toggle="modal"
data-target="#modal-example"
aria-controls="modal-example"
aria-expanded="false"
>
Open Modal
</button>
<Modal id="modal-example" />
```

Advanced example usage:

```html
<button
type="button"
data-toggle="modal"
data-target="#modal-example"
aria-controls="modal-example"
aria-expanded="false"
>
Open Modal
</button>
<Modal id="modal-example" closeLabel="Dismiss" />
```

Without lexer:

```twig
{% embed "@spirit/modal.twig" with { props: {
id: 'modal-example',
}} %}
{% block content %}
Modal content
{% endblock %}
{% endembed %}
```

## API

| Prop name | Type | Default | Required | Description |
| ------------ | -------- | ------- | -------- | ------------------ |
| `class` | `string` | `null` | no | Custom CSS class |
| `closeLabel` | `string` | `Close` | no | Custom close label |
| `id` | `string` || yes | Modal ID |

On top of the API options, you can add `data-*` or `aria-*` attributes to
further extend component's descriptiveness and accessibility. These attributes
will be passed to the topmost HTML element of the component.

[modal]: https://github.com/lmc-eu/spirit-design-system/tree/main/packages/web/src/scss/components/Modal
1 change: 1 addition & 0 deletions packages/web-twig/src/Resources/twig-components/modal.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '@spirit/Modal/Modal.twig' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html><body>
<dialog class="Modal" id="modal-example" data-component="modal">
<div class="Modal__content">
<div class="Modal__dialog">
<div class="Modal__header">
<button aria-controls="modal-example" aria-expanded="false" data-dismiss="modal" data-target="#modal-example" class="Button Button--tertiary Button--square" type="button"> <span class="Modal__close" aria-hidden="true"></span>
<span class="accessibility-hidden">Close</span>
</button>
</div>
<div class="Modal__body">
Modal content
</div>
</div>
</div>
</dialog>
<dialog class="Modal" id="modal-example-dismiss" data-component="modal">
<div class="Modal__content">
<div class="Modal__dialog">
<div class="Modal__header">
<button aria-controls="modal-example-dismiss" aria-expanded="false" data-dismiss="modal" data-target="#modal-example-dismiss" class="Button Button--tertiary Button--square" type="button"> <span class="Modal__close" aria-hidden="true"></span>
<span class="accessibility-hidden">Dismiss</span>
</button>
</div>
<div class="Modal__body">
Modal content
</div>
</div>
</div>
</dialog>
</body></html>
6 changes: 6 additions & 0 deletions packages/web-twig/tests/components-fixtures/modalDefault.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Modal id="modal-example">
Modal content
</Modal>
<Modal id="modal-example-dismiss" closeLabel="Dismiss">
Modal content
</Modal>

0 comments on commit 43f73ee

Please sign in to comment.