Skip to content

Commit c35166c

Browse files
committed
Use a different approach to update .dhcp-hosts height
- replace DOMContentLoaded listener with a function and call it when needed - use javascript to update the CSS variable `--num-lines` - use CSS to calculate the elements height Signed-off-by: RD WebDesign <[email protected]>
1 parent 16f74e9 commit c35166c

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

scripts/js/settings-dhcp.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ function renderStaticDHCPTable() {
438438

439439
tbody.find(".delete-static-row, .add-static-row").prop("disabled", false);
440440
tbody.find(".edit-hint-row").remove();
441+
showLineNumbers();
441442
}
442443

443444
// Copy button for each lease row copies the lease as a new static lease line
@@ -452,7 +453,7 @@ $(document).on("click", ".copy-to-static", function () {
452453
});
453454

454455
// Add line numbers to the textarea for static DHCP hosts
455-
document.addEventListener("DOMContentLoaded", function () {
456+
function showLineNumbers() {
456457
const textarea = document.getElementById("dhcp-hosts");
457458
const linesElem = document.getElementById("dhcp-hosts-lines");
458459
let lastLineCount = 0;
@@ -473,14 +474,13 @@ document.addEventListener("DOMContentLoaded", function () {
473474
"letterSpacing",
474475
"lineHeight",
475476
"padding",
476-
"height",
477477
]) {
478478
linesElem.style[property] = globalThis.getComputedStyle(textarea)[property];
479479
}
480480

481-
// Match height and scroll
482-
linesElem.style.height = textarea.offsetHeight > 0 ? textarea.offsetHeight + "px" : "auto";
483-
}
481+
// Update "--num-lines" variable and let CSS handle the height
482+
$(".dhcp-hosts-wrapper").css("--num-lines", lines);
483+
}
484484

485485
function syncScroll() {
486486
linesElem.scrollTop = textarea.scrollTop;
@@ -497,7 +497,7 @@ document.addEventListener("DOMContentLoaded", function () {
497497
updateLineNumbers(true);
498498
syncScroll();
499499
}
500-
});
500+
}
501501

502502
$(document).on("input blur paste", "#StaticDHCPTable td.static-hwaddr", function () {
503503
const val = $(this).text().trim();

style/pi-hole.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,30 @@ div.dhcp-hosts-wrapper {
16511651
color: #fff;
16521652
}
16531653

1654+
/*
1655+
Calculate the total height using the number of lines and line-height. Also account for borders and padding.
1656+
Use the same values used by ".form-control" class:
1657+
line-height=1.42857; font-size=14px;
1658+
border-top=1px + padding-top=6px + padding-bottom=6px + border-bottom=1px
1659+
1660+
(num-lines * line-height * font-size) + 14px
1661+
*/
1662+
.dhcp-hosts-wrapper {
1663+
--num-lines: 1;
1664+
--max-lines: 12;
1665+
--total-height: calc((min(var(--num-lines), var(--max-lines)) * 1.42857 * 14px) + 14px);
1666+
}
1667+
1668+
#dhcp-hosts {
1669+
height: var(--total-height);
1670+
}
1671+
1672+
#dhcp-hosts-lines {
1673+
height: var(--total-height);
1674+
border: 1px solid transparent;
1675+
border-right-color: #ddd;
1676+
}
1677+
16541678
.save-static-row.disabled {
16551679
opacity: 0.15;
16561680
}

0 commit comments

Comments
 (0)