Skip to content

Commit 5cae24c

Browse files
committed
revert: use offset rect
1 parent ff5d002 commit 5cae24c

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

.changeset/fix-indicator-scroll.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@zag-js/radio-group": patch
3+
"@zag-js/tabs": patch
4+
---
5+
6+
Revert to using `offsetLeft`/`offsetTop` to fix indicator positioning in scrollable containers.

packages/machines/radio-group/src/radio-group.dom.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,12 @@ export const getRadioEl = (ctx: Scope, value: string | null) => {
3333
return ctx.getById(getItemId(ctx, value))
3434
}
3535

36-
export const getOffsetRect = (el: HTMLElement | undefined) => {
37-
if (!el) {
38-
return { left: 0, top: 0, width: 0, height: 0 }
39-
}
40-
const rect = el.getBoundingClientRect()
41-
const parent = el.offsetParent?.getBoundingClientRect()
42-
return {
43-
left: parent ? rect.left - parent.left : rect.left,
44-
top: parent ? rect.top - parent.top : rect.top,
45-
width: rect.width,
46-
height: rect.height,
47-
}
48-
}
36+
export const getOffsetRect = (el: HTMLElement | undefined) => ({
37+
left: el?.offsetLeft ?? 0,
38+
top: el?.offsetTop ?? 0,
39+
width: el?.offsetWidth ?? 0,
40+
height: el?.offsetHeight ?? 0,
41+
})
4942

5043
export const getRectById = (ctx: Scope, id: string) => {
5144
const radioEl = ctx.getById(getItemId(ctx, id))

packages/machines/tabs/src/tabs.dom.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,12 @@ export const getNextTriggerEl = (ctx: Scope, opts: { value: string; loopFocus?:
2626
export const getPrevTriggerEl = (ctx: Scope, opts: { value: string; loopFocus?: boolean | undefined }) =>
2727
prevById(getElements(ctx), getTriggerId(ctx, opts.value), opts.loopFocus)
2828

29-
export const getOffsetRect = (el: HTMLElement | undefined) => {
30-
if (!el) {
31-
return { left: 0, top: 0, width: 0, height: 0 }
32-
}
33-
const rect = el.getBoundingClientRect()
34-
const parent = el.offsetParent?.getBoundingClientRect()
35-
return {
36-
left: parent ? rect.left - parent.left : rect.left,
37-
top: parent ? rect.top - parent.top : rect.top,
38-
width: rect.width,
39-
height: rect.height,
40-
}
41-
}
29+
export const getOffsetRect = (el: HTMLElement | undefined) => ({
30+
left: el?.offsetLeft ?? 0,
31+
top: el?.offsetTop ?? 0,
32+
width: el?.offsetWidth ?? 0,
33+
height: el?.offsetHeight ?? 0,
34+
})
4235

4336
export const getRectById = (ctx: Scope, id: string) => {
4437
const tab = itemById(getElements(ctx), getTriggerId(ctx, id))

0 commit comments

Comments
 (0)