Skip to content

Commit d4bbfb0

Browse files
devplasegunadebayo
andauthored
feat(scroll-area): add dragging state to scroll area context and machine (#2652)
* feat(scroll-area): add dragging state to scroll area context and machine - Introduced a new `dragging` property in the ScrollAreaContext interface. - Updated the state machine to manage the `dragging` state during drag events. - Enhanced the connect function to expose the `dragging` state for UI binding. * feat(scroll-area): add hover and dragging data attributes to connect function * docs: changelog --------- Co-authored-by: Segun Adebayo <joseshegs@gmail.com>
1 parent 2fa0d95 commit d4bbfb0

4 files changed

Lines changed: 15 additions & 2 deletions

File tree

.changeset/tricky-meals-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/scroll-area": patch
3+
---
4+
5+
Add `data-dragging` attribute to scroll area parts for ease of styling.

packages/machines/scroll-area/src/scroll-area.connect.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function connect<T extends PropTypes>(
4040
const horizontal = props.orientation === "horizontal"
4141
return {
4242
hovering: context.get("hovering"),
43+
dragging: context.get("dragging"),
4344
scrolling: context.get(horizontal ? "scrollingX" : "scrollingY"),
4445
hidden: horizontal ? hiddenState.scrollbarXHidden : hiddenState.scrollbarYHidden,
4546
}
@@ -126,6 +127,7 @@ export function connect<T extends PropTypes>(
126127
"data-orientation": orientation,
127128
"data-scrolling": dataAttr(context.get(orientation === "horizontal" ? "scrollingX" : "scrollingY")),
128129
"data-hover": dataAttr(context.get("hovering")),
130+
"data-dragging": dataAttr(context.get("dragging")),
129131
"data-overflow-x": dataAttr(!hiddenState.scrollbarXHidden),
130132
"data-overflow-y": dataAttr(!hiddenState.scrollbarYHidden),
131133
onPointerUp() {
@@ -167,6 +169,8 @@ export function connect<T extends PropTypes>(
167169
...parts.thumb.attrs,
168170
"data-ownedby": dom.getRootId(scope),
169171
"data-orientation": orientation,
172+
"data-hover": dataAttr(context.get("hovering")),
173+
"data-dragging": dataAttr(context.get("dragging")),
170174
onPointerDown(event) {
171175
if (event.button !== 0) return
172176
const point = getEventPoint(event)

packages/machines/scroll-area/src/scroll-area.machine.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const machine = createMachine<ScrollAreaSchema>({
2424
scrollingX: bindable<boolean>(() => ({ defaultValue: false })),
2525
scrollingY: bindable<boolean>(() => ({ defaultValue: false })),
2626
hovering: bindable<boolean>(() => ({ defaultValue: false })),
27+
dragging: bindable<boolean>(() => ({ defaultValue: false })),
2728
touchModality: bindable<boolean>(() => ({ defaultValue: false })),
2829
atSides: bindable<ScrollRecord<boolean>>(() => ({
2930
defaultValue: { top: true, right: false, bottom: false, left: true },
@@ -359,7 +360,7 @@ export const machine = createMachine<ScrollAreaSchema>({
359360
}
360361
},
361362

362-
startDragging({ event, refs, scope }) {
363+
startDragging({ event, refs, scope, context }) {
363364
refs.set("startX", event.point.x)
364365
refs.set("startY", event.point.y)
365366
refs.set("orientation", event.orientation)
@@ -369,6 +370,7 @@ export const machine = createMachine<ScrollAreaSchema>({
369370

370371
refs.set("startScrollTop", viewportEl.scrollTop)
371372
refs.set("startScrollLeft", viewportEl.scrollLeft)
373+
context.set("dragging", true)
372374
},
373375

374376
setDraggingScroll({ event, refs, scope, context }) {
@@ -424,8 +426,9 @@ export const machine = createMachine<ScrollAreaSchema>({
424426
}
425427
},
426428

427-
stopDragging({ refs }) {
429+
stopDragging({ refs, context }) {
428430
refs.set("orientation", null)
431+
context.set("dragging", false)
429432
},
430433

431434
clearTimeouts({ refs }) {

packages/machines/scroll-area/src/scroll-area.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface ScrollAreaContext {
4242
scrollingY: boolean
4343
hiddenState: ScrollbarHiddenState
4444
hovering: boolean
45+
dragging: boolean
4546
touchModality: boolean
4647
atSides: ScrollRecord<boolean>
4748
}

0 commit comments

Comments
 (0)