-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOC-324: Fix broken responsiveness (#1099)
- Loading branch information
Showing
4 changed files
with
176 additions
and
143 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
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 |
---|---|---|
@@ -1,33 +1,61 @@ | ||
--- | ||
type Props = Record<string | number | symbol, unknown>; | ||
interface Props { | ||
class?: string; | ||
headClass?: string; | ||
bodyClass?: string; | ||
rowClass?: string; | ||
cellClass?: string; | ||
} | ||
const { | ||
class: className = '', | ||
headClass = '', | ||
bodyClass = '', | ||
rowClass = '', | ||
cellClass = '', | ||
...rest | ||
} = Astro.props; | ||
--- | ||
|
||
<responsive-table class="contents"> | ||
<table {...Astro.props}> | ||
<slot /> | ||
</table> | ||
<responsive-table class="w-full overflow-x-auto"> | ||
<table class:list={["min-w-full table-auto", className]} {...rest}> | ||
<slot /> | ||
</table> | ||
</responsive-table> | ||
|
||
<script> | ||
class ResponsiveTable extends HTMLElement { | ||
connectedCallback() { | ||
const headRow = this.querySelector('thead tr'); | ||
const tbody = this.querySelector('tbody'); | ||
<style> | ||
@media (max-width: 640px) { | ||
responsive-table { | ||
display: block; | ||
overflow-x: auto; | ||
-webkit-overflow-scrolling: touch; | ||
-ms-overflow-style: -ms-autohiding-scrollbar; | ||
} | ||
} | ||
responsive-table table thead { | ||
display: none; | ||
} | ||
</style> | ||
|
||
if (headRow && tbody) { | ||
const headCells = Array.from(headRow.children); | ||
const bodyRows = Array.from(tbody.children); | ||
<script> | ||
class ResponsiveTable extends HTMLElement { | ||
connectedCallback() { | ||
const headRow = this.querySelector('thead tr'); | ||
const tbody = this.querySelector('tbody'); | ||
|
||
bodyRows.forEach(row => { | ||
const cells = Array.from(row.children); | ||
if (headRow && tbody) { | ||
const headCells = Array.from(headRow.children); | ||
const bodyRows = Array.from(tbody.children); | ||
|
||
cells.forEach((cell, index) => { | ||
cell.setAttribute('data-label', headCells[index].textContent ?? ''); | ||
}); | ||
}); | ||
} | ||
} | ||
bodyRows.forEach(row => { | ||
const cells = Array.from(row.children); | ||
cells.forEach((cell, index) => { | ||
cell.setAttribute('data-label', headCells[index].textContent ?? ''); | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
customElements.define('responsive-table', ResponsiveTable); | ||
</script> | ||
customElements.define('responsive-table', ResponsiveTable); | ||
</script> |
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
Oops, something went wrong.