Skip to content

Commit eda642a

Browse files
committed
fix(table): initial responsive table work
1 parent 58e1135 commit eda642a

File tree

4 files changed

+211
-4
lines changed

4 files changed

+211
-4
lines changed

docs/app/views/examples/components/table/_preview.html.erb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,78 @@ sample_table = {
112112
}
113113
%>
114114

115+
<%= sage_component SageTable, {
116+
caption: %(
117+
Block cells can be used with anchor tags to link the entire cell, or <code>&lt;div&gt;</code> and <code>&lt;span&gt;</code> elements for content
118+
),
119+
headers: [
120+
"Name",
121+
"Email",
122+
"Revenue",
123+
"Status"
124+
],
125+
rows: [
126+
[
127+
%(
128+
#{link_to "Frank Dux", "#", class: "sage-table-cell__link"}
129+
),
130+
"fd@email.com",
131+
"$275.43",
132+
%(
133+
#{sage_component(SageButton, {
134+
value: "Icon only",
135+
small: true,
136+
style: "secondary",
137+
subtle: true,
138+
icon: {
139+
style: "only",
140+
name: "dot-menu-horizontal"
141+
},
142+
small: true
143+
})})
144+
],
145+
[
146+
%(
147+
#{link_to "Stinkmeaner", "#", class: "sage-table-cell__link"}
148+
),
149+
"st@email.com",
150+
"$775.43",
151+
%(
152+
#{sage_component(SageButton, {
153+
value: "Icon only",
154+
small: true,
155+
style: "secondary",
156+
subtle: true,
157+
icon: {
158+
style: "only",
159+
name: "dot-menu-horizontal"
160+
},
161+
small: true
162+
})})
163+
],
164+
[
165+
%(
166+
#{link_to "Huey Freeman", "#", class: "sage-table-cell__link"}
167+
),
168+
"hf@email.com",
169+
"$1275.43",
170+
%(
171+
#{sage_component(SageButton, {
172+
value: "Icon only",
173+
small: true,
174+
style: "secondary",
175+
subtle: true,
176+
icon: {
177+
style: "only",
178+
name: "dot-menu-horizontal"
179+
},
180+
small: true
181+
})})
182+
],
183+
]
184+
} %>
185+
186+
115187
<h3 class="t-sage-heading-6">Responsive table with borders</h3>
116188
<%= sage_component SageTable, {
117189
has_borders: true,

docs/lib/sage_rails/app/views/sage_components/_sage_table.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ is_responsive = component.responsive.present? ? component.responsive : true
1717
<%= "sage-table--selectable" if component.selectable %>
1818
<%= "sage-table--condensed" if component.condensed %>
1919
"
20+
data-js-table
2021
<%= component.generated_html_attributes.html_safe %>
2122
>
2223
<% if component.caption.present? %>

packages/sage-assets/lib/stylesheets/components/_table.scss

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ $-table-caption-font-size: "%t-sage-body-small";
1818
$-table-caption-alignment: center;
1919
$-table-cell-font-color: sage-color(charcoal, 200);
2020
$-table-cell-font-color-strong: sage-color(charcoal, 400);
21-
$-table-cell-type-spec: "%t-sage-body-small-med";
22-
$-table-cell-type-spec-strong: "%t-sage-body-small-semi";
21+
$-table-cell-type-spec: "%t-sage-body-small";
22+
$-table-cell-type-spec-strong: "%t-sage-body-small-med";
2323
$-table-heading-font-color: sage-color(charcoal, 500);
24-
$-table-heading-type-spec: "%t-sage-body-small-semi";
24+
$-table-heading-type-spec: "%t-sage-body-small-med";
2525

2626
// Overflow gradient
2727
$-table-overflow-indicator-width: sage-spacing(sm);
@@ -311,7 +311,9 @@ $-table-avatar-width: rem(32px);
311311
}
312312

313313
.sage-table-cell__link {
314-
color: $-table-cell-font-color;
314+
@extend #{$-table-cell-type-spec-strong};
315+
316+
color: $-table-cell-font-color-strong;
315317
text-decoration: none;
316318

317319
&:focus,
@@ -344,3 +346,100 @@ $-table-avatar-width: rem(32px);
344346
margin-left: 0.5em;
345347
}
346348
}
349+
350+
@media screen and (max-width: sage-breakpoint(sm-max)) {
351+
.sage-table {
352+
thead {
353+
tr,
354+
&::after {
355+
display: none;
356+
}
357+
}
358+
359+
tbody tr {
360+
position: relative;
361+
362+
td:last-of-type {
363+
position: absolute;
364+
top: 1rem;
365+
right: 1rem;
366+
}
367+
}
368+
369+
td {
370+
padding: rem(6px) 0;
371+
}
372+
}
373+
374+
table,
375+
thead,
376+
tbody,
377+
th,
378+
td,
379+
tr,
380+
caption {
381+
display: block;
382+
}
383+
384+
tr {
385+
padding: rem(16px) rem(18px);
386+
border: sage-border(default);
387+
border-radius: sage-border(radius);
388+
389+
& + & {
390+
margin-top: sage-spacing(sm);
391+
}
392+
}
393+
394+
td {
395+
display: grid;
396+
grid-template-columns: auto auto;
397+
grid-gap: 1em 0.5em;
398+
justify-content: space-between;
399+
}
400+
401+
td:nth-of-type(4)::before,
402+
td:nth-of-type(5)::before {
403+
text-align: left;
404+
}
405+
406+
// TODO UXD - QUINTON - find scalable solution
407+
td:nth-child(1)::before {
408+
content: "Name: ";
409+
display: none;
410+
}
411+
412+
td:nth-child(2)::before {
413+
content: "Email: ";
414+
}
415+
416+
td:nth-child(3)::before {
417+
content: "Labels: ";
418+
}
419+
420+
td:nth-child(4)::before {
421+
content: "Status: ";
422+
}
423+
424+
td:nth-child(5)::before {
425+
content: "Status: ";
426+
}
427+
428+
td:nth-child(6)::before {
429+
content: "Status: ";
430+
}
431+
432+
td:nth-child(7)::before {
433+
content: "Status: ";
434+
}
435+
436+
td:last-child::before {
437+
display: none;
438+
}
439+
}
440+
441+
@media screen and (min-width: sage-breakpoint(md-min)) {
442+
.sage-table td::before {
443+
display: none;
444+
}
445+
}

packages/sage-system/lib/table.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
Sage.table = (function() {
2+
// ==================================================
3+
// Variables
4+
// ==================================================
5+
6+
// const SELECTOR_TABLE = "[data-js-table]";
7+
const SELECTOR_TABLE = ".sage-table";
28

39
// ==================================================
410
// Functions
@@ -24,6 +30,32 @@ Sage.table = (function() {
2430
});
2531
}
2632

33+
function ResponsiveCellHeaders(elem) {
34+
var THarray = [];
35+
var table = document.querySelector(SELECTOR_TABLE);
36+
var ths = table.getElementsByTagName("th");
37+
for (var i = 0; i < ths.length; i++) {
38+
var headingText = ths[i].innerHTML;
39+
THarray.push(headingText);
40+
}
41+
var styleElm = document.createElement("style"),
42+
styleSheet;
43+
document.head.appendChild(styleElm);
44+
styleSheet = styleElm.sheet;
45+
for (var i = 0; i < THarray.length; i++) {
46+
styleSheet.insertRule(
47+
"" +
48+
elem +
49+
" td:nth-child(" +
50+
(i + 1) +
51+
')::before {content:"' +
52+
THarray[i] +
53+
': ";}',
54+
styleSheet.cssRules.length
55+
);
56+
}
57+
}
58+
2759

2860
// reset classes on elements
2961
function removeActiveStyle(arr, className) {
@@ -37,6 +69,9 @@ Sage.table = (function() {
3769
if (document.querySelector('.sage-table--sortable') !== null) {
3870
sortEvents();
3971
}
72+
if (document.querySelector('.sage-table--sortable') !== null) {
73+
// ResponsiveCellHeaders(SELECTOR_TABLE);
74+
}
4075
}
4176

4277

0 commit comments

Comments
 (0)