-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path_macro.njk
133 lines (129 loc) · 5.83 KB
/
_macro.njk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{% from "components/icon/_macro.njk" import onsIcon %}
{% macro onsButton(params) %}
{# Set button icon #}
{% if params.iconType %}
{% set iconType = params.iconType %}
{% if params.iconPosition %}
{% set iconPosition = params.iconPosition %}
{% else %}
{# Default icon position before label #}
{% set iconPosition = "before" %}
{% endif %}
{% elif params.iconType is not defined and params.noIcon != true %}
{% if params.url and params.newWindow %}
{# CTA link opening in new tab #}
{% set iconType = "external-link" %}
{% set iconPosition = "after" %}
{% elif params.url %}
{# CTA link #}
{% set iconType = "arrow-next" %}
{% set iconPosition = "after" %}
{% endif %}
{% endif %}
{# Set button type #}
{% if params.type %}
{% set buttonType = params.type %}
{% else %}
{% set buttonType = 'submit' %}
{% endif %}
{# Set button variant attributes #}
{% if params.variants %}
{% if 'print' in params.variants %}
{# Print #}
{% if params.type is not defined %}
{% set buttonType = "button" %}
{% endif %}
{% set iconType = "print" %}
{% set iconPosition = "before" %}
{% set variantClasses = " ons-u-d-no ons-js-print-btn" %}
{% elif 'download' in params.variants %}
{# Download #}
{% set iconType = "download" %}
{% set iconPosition = "before" %}
{% if not params.removeDownloadAttribute %}
{% set variantAttributes = "download" %}
{% endif %}
{% elif 'mobile' in params.variants %}
{# CTA or mobile menu toggle #}
{% set iconType = "chevron" %}
{% set iconPosition = "after" %}
{% elif 'disabled' in params.variants %}
{% set variantAttributes = "disabled" %}
{% elif 'timer' in params.variants %}
{# Timer #}
{% set variantClasses = " ons-js-timer ons-js-submit-btn" %}
{% elif 'loader' in params.variants %}
{# Loader #}
{% set iconType = "loader" %}
{% set iconPosition = "after" %}
{% set variantClasses = " ons-btn--loader ons-js-loader ons-js-submit-btn" %}
{% elif 'search' in params.variants %}
{% set iconType = "search" %}
{% elif 'close' in params.variants %}
{% set iconType = "close" %}
{% endif %}
{% endif %}
{% set tag = "a" if params.url else "button" %}
{% set openingTag = "<" + tag %}
{% set closingTag = "</" + tag + ">" %}
{{ openingTag | safe }}
{% if params.url %}
href="{{ params.url }}" role="button"
{% else %}
type="{{ buttonType }}"
{% endif %}
class="ons-btn{{ ' ' + params.classes if params.classes else '' }}{% if params.variants and params.variants is not string %}{% for variant in params.variants %}{{ ' ' }}ons-btn--{{ variant }}{% endfor %}{% elif params.variants %}{{ ' ' }}ons-btn--{{ params.variants }}{% endif %}{{ ' ons-btn--link ons-js-submit-btn' if params.url }}{{ variantClasses }}"
{% if params.id %}id="{{ params.id }}"{% endif %}
{% if params.value and tag != "a" %}value="{{ params.value }}"{% endif %}
{% if params.name and tag != "a" %}name="{{ params.name }}"{% endif %}
{% if params.url and params.newWindow %}target="_blank" rel="noopener"{% endif %}
{{ variantAttributes }}
{% if params.attributes %}{% for attribute, value in (params.attributes.items() if params.attributes is mapping and params.attributes.items else params.attributes) %}{{ ' ' }}{{ attribute }}="{{ value }}"{% endfor %}{% endif %}
>
<span class="ons-btn__inner{{ ' ' + params.innerClasses if params.innerClasses else '' }}">
{%- if iconPosition == "before" or iconPosition == "after" -%}
{%- if iconPosition == "before" -%}
{{-
onsIcon({
"iconType": iconType,
"classes": 'ons-u-mr-2xs'
})
-}}
{%- endif -%}
<span class="ons-btn__text">{{- params.html | safe if params.html else params.text -}}</span>
{%- if iconPosition == "after" -%}
{{-
onsIcon({
"iconType": iconType,
"classes": 'ons-u-ml-2xs'
})
-}}
{%- endif -%}
{%- elif iconPosition == "only" -%}
{{-
onsIcon({
"iconType": iconType
})
-}}
<span class="ons-btn__text ons-u-vh@2xs@s">{{- params.html | safe if params.html else params.text -}}</span>
{%- else -%}
<span class="ons-btn__text">{{- params.html | safe if params.html else params.text -}}</span>
{%- endif -%}
</span>
{% if params.url and params.newWindow %}
<span class="ons-btn__new-window-description ons-u-vh">({{ params.newWindowDescription | default("opens in a new tab") }})</span>
{% endif %}
{% if params.buttonContext %}
<span class="ons-btn__context ons-u-vh">{{ params.buttonContext }}</span>
{% endif %}
{% if params.listeners %}
<!-- prettier-ignore-start -->
<script{% if csp_nonce %} nonce="{{ csp_nonce() }}"{% endif %}>
{% for listener, value in (params.listeners.items() if params.listeners is mapping and params.listeners.items else params.listeners) %}
document.getElementById("{{ params.id }}").addEventListener('{{ listener }}', function(){ {{ value }} });
{% endfor %}
</script>
<!-- prettier-ignore-end -->
{% endif %}
{{ closingTag | safe }}
{% endmacro %}