Skip to content

Commit 1ad2ec9

Browse files
authored
Merge pull request #662 from AnthonyANI/fix-content-positioning
Fix content positioning for non-relative non-document body contentLocation elements
2 parents afbe06b + 598fa03 commit 1ad2ec9

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

src/slim-select/render.ts

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,48 +1555,62 @@ export default class Render {
15551555
return tempDiv.innerHTML
15561556
}
15571557

1558-
public moveContentAbove(): void {
1559-
// Get main and content height
1560-
const mainHeight = this.main.main.offsetHeight
1561-
const contentHeight = this.content.main.offsetHeight
1558+
private setContentDirection(direction: 'above' | 'below'): void {
1559+
const isAbove = direction === 'above'
1560+
const addClass = isAbove ? this.classes.dirAbove : this.classes.dirBelow
1561+
const removeClass = isAbove ? this.classes.dirBelow : this.classes.dirAbove
15621562

15631563
// Set direction classes on both main and content
1564-
this.main.main.classList.remove(this.classes.dirBelow)
1565-
this.main.main.classList.add(this.classes.dirAbove)
1566-
this.content.main.classList.remove(this.classes.dirBelow)
1567-
this.content.main.classList.add(this.classes.dirAbove)
1564+
this.main.main.classList.remove(removeClass)
1565+
this.main.main.classList.add(addClass)
1566+
this.content.main.classList.remove(removeClass)
1567+
this.content.main.classList.add(addClass)
1568+
1569+
// Set margin to position content
1570+
if (isAbove) {
1571+
const mainHeight = this.main.main.offsetHeight
1572+
const contentHeight = this.content.main.offsetHeight
1573+
this.content.main.style.margin = '-' + (mainHeight + contentHeight - 1) + 'px 0px 0px 0px'
1574+
} else {
1575+
this.content.main.style.margin = '-1px 0px 0px 0px'
1576+
}
1577+
}
1578+
1579+
private setContentPosition(): void {
1580+
if (this.settings.contentPosition === 'relative') {
1581+
return
1582+
}
15681583

1569-
// Set the content position
15701584
const containerRect = this.main.main.getBoundingClientRect()
1571-
this.content.main.style.margin = '-' + (mainHeight + contentHeight - 1) + 'px 0px 0px 0px'
1572-
this.content.main.style.top =
1573-
containerRect.top + containerRect.height + (this.settings.contentPosition === 'fixed' ? 0 : window.scrollY) + 'px'
1574-
this.content.main.style.left =
1575-
containerRect.left + (this.settings.contentPosition === 'fixed' ? 0 : window.scrollX) + 'px'
1585+
let top: number
1586+
let left: number
1587+
1588+
if (this.settings.contentPosition === 'fixed') {
1589+
// Fixed positioning - use viewport coordinates directly
1590+
top = containerRect.top + containerRect.height
1591+
left = containerRect.left
1592+
} else {
1593+
// Absolute positioning - calculate relative to offsetParent
1594+
const offsetParent = this.content.main.offsetParent as HTMLElement
1595+
const offsetParentRect = offsetParent ? offsetParent.getBoundingClientRect() : { top: 0, left: 0 }
1596+
1597+
top = containerRect.top - offsetParentRect.top + containerRect.height - (offsetParent?.clientTop || 0)
1598+
left = containerRect.left - offsetParentRect.left - (offsetParent?.clientLeft || 0)
1599+
}
1600+
1601+
this.content.main.style.top = top + 'px'
1602+
this.content.main.style.left = left + 'px'
15761603
this.content.main.style.width = containerRect.width + 'px'
15771604
}
15781605

1579-
public moveContentBelow(): void {
1580-
// Set direction classes on both main and content
1581-
this.main.main.classList.remove(this.classes.dirAbove)
1582-
this.main.main.classList.add(this.classes.dirBelow)
1583-
this.content.main.classList.remove(this.classes.dirAbove)
1584-
this.content.main.classList.add(this.classes.dirBelow)
1606+
public moveContentAbove(): void {
1607+
this.setContentDirection('above')
1608+
this.setContentPosition()
1609+
}
15851610

1586-
// Set the content position
1587-
const containerRect = this.main.main.getBoundingClientRect()
1588-
this.content.main.style.margin = '-1px 0px 0px 0px'
1589-
// Dont do anything if the content is relative
1590-
if (this.settings.contentPosition !== 'relative') {
1591-
this.content.main.style.top =
1592-
containerRect.top +
1593-
containerRect.height +
1594-
(this.settings.contentPosition === 'fixed' ? 0 : window.scrollY) +
1595-
'px'
1596-
this.content.main.style.left =
1597-
containerRect.left + (this.settings.contentPosition === 'fixed' ? 0 : window.scrollX) + 'px'
1598-
this.content.main.style.width = containerRect.width + 'px'
1599-
}
1611+
public moveContentBelow(): void {
1612+
this.setContentDirection('below')
1613+
this.setContentPosition()
16001614
}
16011615

16021616
public ensureElementInView(container: HTMLElement, element: HTMLElement): void {

src/slim-select/slimselect.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@
362362
}
363363
}
364364

365+
&.ss-dir-above .ss-search {
366+
border-bottom: none;
367+
border-top: 1px solid var(--ss-border-color);
368+
}
369+
365370
.ss-list {
366371
flex: 1 1 auto;
367372
height: auto;

0 commit comments

Comments
 (0)