Skip to content

Commit

Permalink
fix(pagination): consider padding in container query and css
Browse files Browse the repository at this point in the history
- after adding padding in dark mode, responsive was broken
- use total width - padding as the container width to determine number
of pages to display
- css tweaks to make everything look nicer at various widths and paddings
  • Loading branch information
unleashit committed Jan 31, 2024
1 parent 7a2dff7 commit 2ccf613
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/pagination/src/numbers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const Numbers = ({
let maxPages;
if (containerWidth <= 1000) {
const [_, breakPointPages]: any = [
[450, 2],
[550, 3],
[340, 2],
[380, 3],
[650, 4],
[850, 8],
[1000, 10],
Expand Down
22 changes: 20 additions & 2 deletions packages/pagination/src/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,26 @@ const Pagination = ({

useEffect(() => {
const setWidth = throttle(() => {
const width = containerRef.current ? containerRef.current.offsetWidth : 0;
setContainerWidth(width);
let effectiveWidth = 0;
if (containerRef.current) {
const container = window.getComputedStyle(containerRef.current);
const totalWidth = Number(
container.getPropertyValue('width').replace(/px$/, ''),
);

// subtract padding plus a buffer to account for smaller overall space
// when padding is introduced
const leftPad =
Number(
container.getPropertyValue('padding-left').replace(/px$/, ''),
) * 2;
const rightPad =
Number(
container.getPropertyValue('padding-right').replace(/px$/, ''),
) * 2;
effectiveWidth = totalWidth - leftPad - rightPad;
}
setContainerWidth(effectiveWidth);
}, 500);

setWidth();
Expand Down
36 changes: 35 additions & 1 deletion packages/pagination/src/scss/pagination.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,41 @@ $darkmode-page-active: $ltgray;
}


@media screen and (max-width: 1000px) {
@media screen and (max-width: $break-xs) {
.number {
width: 2.1rem;
height: 2.1rem;
line-height: 2.1rem;
font-size: 0.8rem;
}
.number {
margin-right: 0.4rem;
}
.ellipsis {
margin-right: 0.2rem;
}
.prev, .chevronLeft {
margin-right: 0.3rem;
}
.chevronRight {
margin-left: 0.3rem;
}
}

@media screen and (max-width: 350px) {
.ellipsis {
display: none;
}
}

/*
hack assumes default padding is set in dark mode theme
consider container query if browser support is good enough
*/
@media screen and (max-width: 400px) {
[data-theme='dark'] {
.ellipsis {
display: none;
}
}
}

0 comments on commit 2ccf613

Please sign in to comment.