Skip to content

withDrag ​

Adds a dragged(props) hook from the drag service.

Usage ​

js
import { 
Base
,
withDrag
} from '@studiometa/js-toolkit';
export default class
Draggable
extends
withDrag
(
Base
, {
target
: (
instance
) =>
instance
.
$el
,
dampFactor
: 0.5,
}) { static
config
= {
name
: 'Draggable',
}; /** * @param {import('@studiometa/js-toolkit').DragServiceProps} props */
dragged
(
props
) {
this.
$el
.
style
.
transform
= `translate(${
props
.
x
}px, ${
props
.
y
}px)`;
} }

Parameters ​

  • BaseClass (Base): The class to add draggable capabilities to
  • options? ({ target?: (instance:Base) => HTMLElement, dampFactor?: number }): Options to choose the draggable target element and the drag dampFactor.

Return value ​

  • Base: A child of the given class with draggable capabilities

API ​

Hooks ​

dragged ​

The dragged hook runs while the user drags the target element.

Arguments

Examples ​

Move the root element ​

This decorator adds drag capabilities to the root element of a component.

js
import { 
Base
,
withDrag
} from '@studiometa/js-toolkit';
export default class
Draggable
extends
withDrag
(
Base
) {
static
config
= {
name
: 'Draggable',
}; /** * @param {import('@studiometa/js-toolkit').DragServiceProps} props */
dragged
(
props
) {
this.
$el
.
style
.
transform
= `translate(${
props
.
x
}px, ${
props
.
y
}px)`;
} }

TIP

We recommend using the domScheduler utility to update the root element position, see the Draggable component source for more details.

Move a ref element ​

js
import { 
Base
,
withDrag
} from '@studiometa/js-toolkit';
export default class
Draggable
extends
withDrag
(
Base
, {
target
: (
instance
) =>
instance
.
$refs
.
draggbale
,
}) { static
config
= {
name
: 'Draggable',
refs
: ['draggable'],
}; /** * @param {import('@studiometa/js-toolkit').DragServiceProps} props */
dragged
(
props
) {
this.
$refs
.
draggable
.style.transform = `translate(${
props
.
x
}px, ${
props
.
y
}px)`;
} }

TIP

You can use the transform(element, transforms) function to apply transform to an element.

MIT Licensed