Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/htl 113692 limit dragging to header only #1545

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "pcln-design-system",
"comment": "Drawer is now draggable when user only drags the header, not the content",
"type": "patch"
}
],
"packageName": "pcln-design-system"
}
1 change: 0 additions & 1 deletion packages/core/src/Drawer/Drawer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ function DrawerScrollable(props) {
height='100vh'
style={{ backgroundColor: 'grey', height: '100vh', position: 'relative' }}
>
<Text>Note: Mobile only feature - Please test on browser iOS/android device emulator</Text>
<Text>Previous position: {snapState.prevSnapPosition}</Text>
<Text>Current position: {snapState.currSnapPosition}</Text>
<Drawer heading='Header' isOpen={true} {...props} onSnapPositionChange={handleSnapStateChange}>
Expand Down
19 changes: 17 additions & 2 deletions packages/core/src/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Text } from '../Text/Text'
import { DrawerRoot, DrawerWrapper, HeaderButton } from './Drawer.styled'
import { ChevronDown, Close } from 'pcln-icons'
import { AnimatePresence, motion } from 'framer-motion'
import { AnimatePresence, motion, useDragControls } from 'framer-motion'
import { SpaceProps, LayoutProps } from 'styled-system'
import { useScrollWithShadow } from '../useScrollWithShadows/useScrollWithShadow'
import { theme } from '../theme'
Expand Down Expand Up @@ -72,6 +72,8 @@
const { boxShadow, onScrollHandler } = useScrollWithShadow()
const { snapPosition, handleSnap } = useSnap(snapHeights, snapDimensions, onSnapPositionChange)
const SnapContainer = snapHeights ? motion.div : 'div'
const headerRef = React.useRef(null)
const dragControls = useDragControls()

return (
<SnapContainer
Expand All @@ -92,6 +94,8 @@
dragConstraints={{ top: 0, bottom: 0 }}
{...(snapHeights ? { onDragEnd: handleSnap } : {})}
data-testid='snap-container'
dragControls={dragControls}
dragListener={false}
>
<AnimatePresence>
{isOpen && (
Expand Down Expand Up @@ -124,7 +128,18 @@
>
<Flex flexDirection='column'>
{(heading || onClose || onCollapse) && (
<Flex flexDirection='column'>
<Flex
ref={headerRef}
/* c8 ignore next */
onPointerDown={(e) => {
if (snapHeights) {
coolestKev marked this conversation as resolved.
Show resolved Hide resolved
dragControls.start(e)
}

Check warning on line 137 in packages/core/src/Drawer/Drawer.tsx

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Drawer/Drawer.tsx#L135-L137

Added lines #L135 - L137 were not covered by tests
}}
flexDirection='column'
style={{ touchAction: 'none' }}
data-testid='drawer-header-container'
>
<Flex flexDirection='row' p={3}>
{typeof heading === 'string' ? (
<Text width='100%' textStyle='heading4'>
Expand Down
42 changes: 19 additions & 23 deletions packages/core/src/Drawer/hooks/useSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export function useSnap(snapHeights, snapDimensions, onSnapPositionChange) {
const [snapPosition, setSnapPosition] = useState(MIDDLE)

const handleSnap = (...args) => {
const pointerType = args?.[0]?.pointerType // mouse, touch, etc.
const type = args?.[0]?.type // click, pointerup (drag), pointercancel (scroll)
const info = args?.[1]
const scrollOffset = info.offset.y

Expand All @@ -45,29 +43,27 @@ export function useSnap(snapHeights, snapDimensions, onSnapPositionChange) {
* container movement.
*/

if (pointerType === 'touch' && type === 'pointerup') {
coolestKev marked this conversation as resolved.
Show resolved Hide resolved
// Scroll down logic
if (SCROLL_DOWN) {
if (snapPosition === TOP) {
setSnapPosition(MIDDLE)
onSnapPositionChange({ prevSnapPosition: 'TOP', currSnapPosition: 'MIDDLE' })
}
if (snapPosition === MIDDLE) {
setSnapPosition(BOTTOM)
onSnapPositionChange({ prevSnapPosition: 'MIDDLE', currSnapPosition: 'BOTTOM' })
}
// Scroll down logic
if (SCROLL_DOWN) {
if (snapPosition === TOP) {
setSnapPosition(MIDDLE)
onSnapPositionChange({ prevSnapPosition: 'TOP', currSnapPosition: 'MIDDLE' })
}
if (snapPosition === MIDDLE) {
setSnapPosition(BOTTOM)
onSnapPositionChange({ prevSnapPosition: 'MIDDLE', currSnapPosition: 'BOTTOM' })
}
}

// Scroll up logic
else if (SCROLL_UP) {
if (snapPosition === BOTTOM) {
setSnapPosition(MIDDLE)
onSnapPositionChange({ prevSnapPosition: 'BOTTOM', currSnapPosition: 'MIDDLE' })
}
if (snapPosition === MIDDLE) {
setSnapPosition(TOP)
onSnapPositionChange({ prevSnapPosition: 'MIDDLE', currSnapPosition: 'TOP' })
}
// Scroll up logic
else if (SCROLL_UP) {
if (snapPosition === BOTTOM) {
setSnapPosition(MIDDLE)
onSnapPositionChange({ prevSnapPosition: 'BOTTOM', currSnapPosition: 'MIDDLE' })
}
if (snapPosition === MIDDLE) {
setSnapPosition(TOP)
onSnapPositionChange({ prevSnapPosition: 'MIDDLE', currSnapPosition: 'TOP' })
}
}
}
Expand Down
Loading