Skip to content

Commit ee0fc8c

Browse files
committed
Fix accessibility for table block
1 parent e39d1d9 commit ee0fc8c

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/js/classes/TableBlock.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import AbstractDomElement from './AbstractDomElement'
2+
import each from '../utils/each'
3+
4+
// ----
5+
// class
6+
// ----
7+
class TableBlock extends AbstractDomElement {
8+
constructor(element, options) {
9+
const instance = super(element, options)
10+
11+
// avoid double init :
12+
if (!instance.isNewInstance()) {
13+
return instance
14+
}
15+
16+
const el = this._element
17+
const table = el.querySelector('table')
18+
const thead = table.querySelector('thead')
19+
const legend = el.querySelector('figcaption')
20+
21+
// Tableau de données
22+
if (thead) {
23+
const ths = thead.querySelectorAll('th')
24+
25+
each(ths, function (th) {
26+
th.setAttribute('scope', 'col')
27+
})
28+
29+
// Fix de la légende / titre du tableau. figcaption n'est pas supporté : https://accessibilite.numerique.gouv.fr/methode/glossaire/#tableau-de-donnees-ayant-un-titre
30+
if (legend) {
31+
const caption = document.createElement('caption')
32+
caption.className = legend.className
33+
caption.innerHTML = legend.innerHTML
34+
table.insertBefore(caption, table.firstChild)
35+
legend.remove()
36+
}
37+
}
38+
39+
// Tableau de mise en forme
40+
if (!thead) {
41+
table.setAttribute('role', 'presentation')
42+
}
43+
}
44+
}
45+
46+
// ----
47+
// init
48+
// ----
49+
TableBlock.init('.wp-block-table')
50+
51+
// ----
52+
// export
53+
// ----
54+
export default TableBlock

src/js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import './classes/ButtonSeoClick'
55
import './classes/Header'
66
import './classes/Animation'
77
import './classes/ImageBlock'
8+
import './classes/TableBlock'

src/scss/06-blocks/core/_table.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@
22
width: 100%;
33
min-width: 240px;
44
border-collapse: collapse;
5+
6+
th {
7+
font-weight: 700;
8+
}
9+
10+
figcaption,
11+
caption {
12+
margin-bottom: var(--spacing--block-1);
13+
font-size: var(--paragraph--font-size-default);
14+
font-weight: 700;
15+
line-height: var(--paragraph--line-height-default);
16+
text-align: left;
17+
}
518
}
619

720
.wp-block-table {
821
@extend %table;
22+
23+
display: flex;
24+
flex-direction: column-reverse;
25+
align-items: flex-start;
926
}
1027

1128
// Not apply style to ACF fields

0 commit comments

Comments
 (0)