-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtable-saw.js
More file actions
181 lines (153 loc) · 4.22 KB
/
table-saw.js
File metadata and controls
181 lines (153 loc) · 4.22 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class Tablesaw extends HTMLElement {
static dupes = {};
constructor() {
super();
this.autoOffset = 50;
this._needsStylesheet = true;
this.attrs = {
breakpoint: "breakpoint",
breakpointBackwardsCompat: "media",
type: "type",
ratio: "ratio",
label: "data-tablesaw-label",
zeropad: "zero-padding",
forceTextAlign: "text-align"
};
this.defaults = {
breakpoint: '(max-width: 39.9375em)', // same as Filament Group’s Tablesaw
ratio: '1fr 2fr',
};
this.classes = {
wrap: "tablesaw-wrap"
}
this.props = {
ratio: "--table-saw-ratio",
bold: "--table-saw-header-bold",
};
}
generateCss(breakpoint, type) {
return `
table-saw.${this._id} {
display: block;
${type === "container" ? "container-type: inline-size;" : ""}
}
@${type} ${breakpoint} {
table-saw.${this._id} thead :is(th, td) {
position: absolute;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px, 1px, 1px, 1px);
}
table-saw.${this._id} :is(tbody, tfoot) tr {
display: block;
}
table-saw.${this._id} :is(tbody, tfoot) :is(th, td):before {
font-weight: var(${this.props.bold});
content: attr(${this.attrs.label});
}
table-saw.${this._id} :is(tbody, tfoot) :is(th, td) {
display: grid;
gap: 0 1em;
grid-template-columns: var(${this.props.ratio}, ${this.defaults.ratio});
}
table-saw.${this._id}[${this.attrs.forceTextAlign}] :is(tbody, tfoot) :is(th, td) {
text-align: ${this.getAttribute(this.attrs.forceTextAlign) || "left"};
}
table-saw.${this._id}[${this.attrs.zeropad}] :is(tbody, tfoot) :is(th, td) {
padding-left: 0;
padding-right: 0;
}
}`;
}
connectedCallback() {
// Cut-the-mustard
// https://caniuse.com/mdn-api_cssstylesheet_replacesync
if(!("replaceSync" in CSSStyleSheet.prototype)) {
return;
}
this.addHeaders();
this.setRatio();
if(!this._needsStylesheet) {
return;
}
let sheet = new CSSStyleSheet();
let breakpoint = this.getAttribute(this.attrs.breakpoint) || this.getAttribute(this.attrs.breakpointBackwardsCompat) || this.defaults.breakpoint;
let type = this.getAttribute(this.attrs.type) || "media";
this._id = `ts_${type.slice(0, 1)}${breakpoint.replace(/[^a-z0-9]/gi, "_")}`;
this.classList.add(this._id);
if(!Tablesaw.dupes[this._id]) {
let css = this.generateCss(breakpoint, type);
sheet.replaceSync(css);
let root = this.getRootNode();
root.adoptedStyleSheets.push(sheet);
// only add to global de-dupe if not a shadow root
if(root.host && root !== root.host.shadowRoot) {
Tablesaw.dupes[this._id] = true;
}
}
}
addHeaders() {
let headerCells = this.querySelectorAll("thead th");
let labels = Array.from(headerCells).map((cell, index) => {
// Set headers to be bold (if headers are bold)
if(index === 0) {
let styles = window.getComputedStyle(cell);
if(styles) {
// Copy other styles?
let bold = styles.getPropertyValue("font-weight");
this.setBold(bold);
}
}
let label = cell.innerText.trim();
if (label === "") {
label = cell.textContent.trim();
}
return label;
});
if(labels.length === 0) {
this._needsStylesheet = false;
console.error("No `<th>` elements found:", this);
return;
}
let cells = this.querySelectorAll("tbody :is(td, th)");
for(let cell of cells) {
if(!labels[cell.cellIndex]) {
continue;
}
cell.setAttribute(this.attrs.label, labels[cell.cellIndex]);
let nodeCount = 0;
for(let n of cell.childNodes) {
// text or element node
if(n.nodeType === 3 || n.nodeType === 1) {
nodeCount++;
}
}
// wrap if this cell has child nodes for correct grid alignment
if(nodeCount > 1) {
let wrapper = document.createElement("div");
wrapper.classList.add(this.classes.wrap);
while(cell.firstChild) {
wrapper.appendChild(cell.firstChild);
}
cell.appendChild(wrapper);
}
}
}
setBold(bold) {
if(bold || bold === "") {
this.style.setProperty(this.props.bold, bold);
}
}
setRatio() {
let ratio = this.getAttribute(this.attrs.ratio);
if(ratio) {
let ratioString = ratio.split("/").join("fr ") + "fr";
this.style.setProperty(this.props.ratio, ratioString);
}
}
}
if("customElements" in window) {
window.customElements.define("table-saw", Tablesaw);
}
export { Tablesaw };