Skip to content

Commit

Permalink
feat(movable): variant 'screen' for limit.parent - limit to screen
Browse files Browse the repository at this point in the history
  • Loading branch information
vnphanquang committed Sep 23, 2022
1 parent c6f2a9a commit 0b69be1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-oranges-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@svelte-put/movable": minor
---

Add 'screen' variant for limit parent
16 changes: 13 additions & 3 deletions packages/actions/movable/src/movable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import { input } from './utils';
*
* Be aware of side effects:
*
* - element.style.position is set to `relative` (if not already 'absolute' / 'relative') the first time mousedown is triggered
* - element.style.position is set to `relative` (if not already 'absolute', 'relative', or 'fixed) the first time mousedown is triggered
*
* - document.body.userSelect is set to `none` after `mousedown` and restored on `mouseup`
*
Expand Down Expand Up @@ -143,7 +143,17 @@ export function movable(node: HTMLElement, parameters: MovableParameters = { ena
}

if (parent) {
const insideBoundingRect = parent.getBoundingClientRect();
let insideBoundingRect: Record<'top' | 'bottom' | 'left' | 'right', number>;
if (parent === 'screen') {
insideBoundingRect = {
top: 0,
bottom: window.innerHeight,
left: 0,
right: window.innerWidth,
};
} else {
insideBoundingRect = parent.getBoundingClientRect();
}

const newAbsTop = nodeBoundingRect.top + Δy + boundY;
if (newAbsTop < insideBoundingRect.top) {
Expand Down Expand Up @@ -216,7 +226,7 @@ export function movable(node: HTMLElement, parameters: MovableParameters = { ena

// init position style
const position = computedStyles.getPropertyValue('position');
if (position !== 'relative' && position !== 'absolute') {
if (position !== 'relative' && position !== 'absolute' && position !== 'fixed') {
node.style.position = 'relative';
}

Expand Down
4 changes: 2 additions & 2 deletions packages/actions/movable/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export type MovableLimitDelta = `${number}px` | `${number}%`;
*/
export interface MovableLimit {
/**
* Move node within this parent node
* Move node within this parent node or within screen
*/
parent?: HTMLElement;
parent?: HTMLElement | 'screen';
/**
* Bounding box limit in both axes
*/
Expand Down

0 comments on commit 0b69be1

Please sign in to comment.