Skip to content

Commit 83ce8b1

Browse files
committed
Add dir to language items
1 parent dd79f2a commit 83ce8b1

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

packages/govuk-frontend/src/govuk/components/language-switcher/language-switcher.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ params:
1616
type: string
1717
required: true
1818
description: The language tag for the language (for example, `cy` for Welsh). Added as `lang` and `hreflang` attributes so that assistive technologies pronounce the language name correctly.
19+
- name: dir
20+
type: string
21+
required: false
22+
description: The text direction for the language item element. Use this on every item when the switcher includes languages with different directions.
1923
- name: href
2024
type: string
2125
required: false
@@ -84,6 +88,17 @@ examples:
8488
- text: Cymraeg
8589
lang: cy
8690
current: true
91+
- name: with mixed text directions
92+
options:
93+
items:
94+
- text: English
95+
lang: en
96+
dir: ltr
97+
current: true
98+
- text: العربية
99+
lang: ar
100+
dir: rtl
101+
href: '#/ar'
87102

88103
# Hidden examples are not shown in the review app, but are used for tests and HTML fixtures
89104
- name: classes

packages/govuk-frontend/src/govuk/components/language-switcher/template.jsdom.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,22 @@ describe('Language switcher', () => {
159159

160160
expect($link.innerHTML).toContain('<span>Cymraeg</span>')
161161
})
162+
163+
it('sets dir attributes on language items', () => {
164+
document.body.innerHTML = render(
165+
'language-switcher',
166+
examples['with mixed text directions']
167+
)
168+
169+
const $currentLanguage = document.querySelector(
170+
'.govuk-language-switcher__text'
171+
)
172+
const $otherLanguage = document.querySelector(
173+
'.govuk-language-switcher__link'
174+
)
175+
176+
expect($currentLanguage).toHaveAttribute('dir', 'ltr')
177+
expect($otherLanguage).toHaveAttribute('dir', 'rtl')
178+
})
162179
})
163180
})

packages/govuk-frontend/src/govuk/components/language-switcher/template.njk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
{% if item.current %}
77
<li class="govuk-language-switcher__list-item">
88
<span class="govuk-language-switcher__text"
9-
{%- if item.lang %} lang="{{ item.lang }}"{% endif %} aria-current="true">
9+
{%- if item.lang %} lang="{{ item.lang }}"{% endif %}
10+
{%- if item.dir %} dir="{{ item.dir }}"{% endif %} aria-current="true">
1011
{{- item.html | safe if item.html else item.text -}}
1112
</span>
1213
</li>
1314
{% else %}
1415
<li class="govuk-language-switcher__list-item">
1516
<a class="govuk-language-switcher__link" href="{{ item.href }}"
16-
{%- if item.lang %} lang="{{ item.lang }}" hreflang="{{ item.lang }}"{% endif %} rel="alternate"
17+
{%- if item.lang %} lang="{{ item.lang }}" hreflang="{{ item.lang }}"{% endif %}
18+
{%- if item.dir %} dir="{{ item.dir }}"{% endif %} rel="alternate"
1719
{{- govukAttributes(item.attributes) }}>
1820
{{- item.html | safe if item.html else item.text -}}
1921
</a>

0 commit comments

Comments
 (0)