-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add attributes macro copied from GOV.UK
- Loading branch information
1 parent
7118439
commit 8543e1a
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
{# Adapted from https://github.com/alphagov/govuk-frontend/blob/main/packages/govuk-frontend/src/govuk/macros/attributes.njk #} | ||
|
||
{# | ||
Renders component attributes as string | ||
* By default or using `optional: false`, attributes render as ` name="value"` | ||
* Using `optional: true`, attributes with empty (`null`, `undefined` or `false`) values are omitted | ||
* Using `optional: true`, attributes with `true` (boolean) values render `name` only without value | ||
{@link https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HTML} | ||
@example | ||
Output attribute ` aria-hidden="true"` when `true` (boolean) or `"true"` (string) | ||
```njk | ||
nhsukAttributes({ | ||
"aria-hidden": true | ||
}) | ||
``` | ||
@example | ||
Output attribute ` aria-hidden="false"` when `false` (boolean) or `"false"` (string) | ||
```njk | ||
nhsukAttributes({ | ||
"aria-hidden": false | ||
}) | ||
``` | ||
@example | ||
Output attribute ` hidden=""` when `null`, `undefined` or empty `""` (string) | ||
```njk | ||
nhsukAttributes({ | ||
"hidden": undefined | ||
}) | ||
``` | ||
@example | ||
Output attribute ` hidden` as boolean attribute when optional and `true` | ||
```njk | ||
nhsukAttributes({ | ||
hidden: { | ||
value: true, | ||
optional: true | ||
}, | ||
}) | ||
``` | ||
@example | ||
Output empty string when optional and `null`, `undefined` or `false` | ||
```njk | ||
nhsukAttributes({ | ||
hidden: { | ||
value: undefined, | ||
optional: true | ||
}, | ||
}) | ||
``` | ||
@private | ||
@param {{ [attribute: string]: string | { value: string, optional?: boolean } } | string} attributes - Component attributes param | ||
#} | ||
{% macro nhsukAttributes(attributes) -%} | ||
{#- Default attributes output -#} | ||
{% set attributesHtml = attributes if attributes is string else "" %} | ||
|
||
{#- Append attribute name/value pairs -#} | ||
{%- if attributes is mapping %} | ||
{% for name, value in attributes %} | ||
{#- Detect if this is a `safe` filtered value. Just pass the value through if so. -#} | ||
{#- https://github.com/alphagov/govuk-frontend/issues/4937 -#} | ||
{% if value is mapping and not [undefined, null].includes(value.val) and value.length %} | ||
{% set value = value.val %} | ||
{% endif %} | ||
|
||
{#- Set default attribute options -#} | ||
{% set options = value if value is mapping else { | ||
value: value, | ||
optional: false | ||
} %} | ||
|
||
{#- Output ` name` only (no value) for boolean attributes -#} | ||
{% if options.optional === true and options.value === true %} | ||
{% set attributesHtml = attributesHtml + " " + name | escape %} | ||
{#- Skip optional empty attributes or output ` name="value"` pair by default -#} | ||
{% elif (options.optional === true and not [undefined, null, false].includes(options.value)) or options.optional !== true %} | ||
{% set attributesHtml = attributesHtml + " " + name | escape + '="' + options.value | escape + '"' %} | ||
{% endif %} | ||
{% endfor %} | ||
{% endif -%} | ||
|
||
{{- attributesHtml | safe -}} | ||
{%- endmacro %} |