Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

create numericGestureControl

Sultan9a08533b1995
diff --git a/app/shared/design-system/components/primitives/numericGestureControl.tsx b/app/shared/design-system/components/primitives/numericGestureControl.tsx
new file mode 100644
index 0000000000..c492ef241d
--- /dev/null
+++ b/app/shared/design-system/components/primitives/numericGestureControl.tsx
@@ -0,0 +1,44 @@
+/*
+ @example
+ numericGestureControl(document.querySelector('input'), {
+ onValueChange: (event) => {
+ event.preventDefault();
+ event.target.value = event.value;
+ event.target.select();
+ }
+ })
+*/
+export const numericGestureControl = (targetNode, {
+ initialValue = 0,
+ direction = 'horizontal' || 'vertical',
+ onValueChange = ({value = 0, preventDefault = () => null}) => null,
+}) => {
+ const {ownerDocument} = targetNode;
+ const eventNames = [ 'pointerup', 'pointerdown', 'pointermove' ];
+ const state = { type: '', value: initialValue, cursor: direction === 'horizontal' ? 'ew-resize' : 'ns-resize' };
+ const handleEvent = ({ type, target, movementY, movementX, pointerId, pressure }) => {
+ const movement = direction === 'horizontal' ? movementX : movementY;
+ switch (type) {
+ case 'pointerup': {
+ targetNode.releasePointerCapture(pointerId);
+ break;
+ }
+ case 'pointerdown': {
+ targetNode.setPointerCapture(pointerId);
+ break;
+ }
+ case 'pointermove': {
+ if (pressure) onValueChange({target, value: state.value += movement, preventDefault: () => event.preventDefault()});
+ break;
+ }
+ }
+ };
+ targetNode.style.setProperty('cursor', state.cursor);
+ eventNames.forEach((eventName) => targetNode.addEventListener(eventName, handleEvent));
+ return {
+ disconnectedCallback: () => {
+ targetNode.style.removeProperty('cursor');
+ eventNames.forEach((eventName) => targetNode.removeEventListener(eventName, handleEvent))
+ },
+ };
+};