@warp-ds/core
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -13,2 +13,3 @@ type ClickEvent = TouchEvent | MouseEvent; | ||
labelledBy?: string; | ||
preventAcceleration?: boolean; | ||
} | ||
@@ -23,3 +24,3 @@ interface SliderState { | ||
} | ||
export declare function createHandlers({ props, sliderState, }: { | ||
export declare function createHandlers({ props, sliderState }: { | ||
props: SliderProps; | ||
@@ -29,4 +30,4 @@ sliderState: SliderState; | ||
handleKeyDown: (e: KeyboardEvent) => void; | ||
handleFocus: (e: FocusEvent | unknown) => void; | ||
handleBlur: (e: FocusEvent | unknown) => void; | ||
handleFocus: () => void; | ||
handleBlur: () => void; | ||
handleMouseDown: (e: KeyboardEvent) => void; | ||
@@ -33,0 +34,0 @@ handleClick: (e: ClickEvent | unknown) => void; |
@@ -1,7 +0,7 @@ | ||
import { validKeyCodes, validKeys, eventOptions, clamp, roundDecimals, } from "./helpers.js"; | ||
export function createHandlers({ props, sliderState, }) { | ||
import { validKeyCodes, validKeys, eventOptions, clamp, roundDecimals, } from './helpers.js'; | ||
export function createHandlers({ props, sliderState }) { | ||
const clampedChange = (n) => clamp(n, { max: props.max, min: props.min }); | ||
function getCoordinates(e) { | ||
const { left: offsetLeft, width: trackWidth } = sliderState.dimensions; | ||
const clientX = "touches" in e ? e.touches[0].clientX : e.clientX; | ||
const clientX = 'touches' in e ? e.touches[0].clientX : e.clientX; | ||
let left = Math.min(Math.max((clientX - offsetLeft - 16) / trackWidth, 0), 1) || 0; | ||
@@ -17,2 +17,6 @@ const value = props.min + left * (props.max - props.min); | ||
}; | ||
const keydownRepeat = { | ||
counter: 0, | ||
repeatsBeforeAcceleration: 3 | ||
}; | ||
function handleKeyDown(e) { | ||
@@ -23,33 +27,54 @@ const key = e.key; | ||
e.preventDefault(); | ||
if ([validKeys.left, validKeys.right, validKeys.up, validKeys.down].includes(key)) { | ||
const direction = [validKeys.right, validKeys.up].includes(key) ? 1 : -1; | ||
sliderState.position = clampedChange(sliderState.val + direction * sliderState.step); | ||
switch (key) { | ||
case validKeys.left: | ||
case validKeys.right: | ||
case validKeys.up: | ||
case validKeys.down: { | ||
const direction = (key === validKeys.right || key === validKeys.up) ? 1 : -1; | ||
let stepsToMove = sliderState.step; | ||
if (!props.preventAcceleration) { | ||
if (e.repeat) { | ||
keydownRepeat.counter++; | ||
const acceleratedStepPercentage = Math.min((keydownRepeat.counter - keydownRepeat.repeatsBeforeAcceleration) / 100, .2); | ||
stepsToMove = Math.max(sliderState.step, Math.ceil((direction > 0 ? props.max - sliderState.val : sliderState.val - props.min) * acceleratedStepPercentage)); | ||
} | ||
else { | ||
keydownRepeat.counter = 0; | ||
} | ||
} | ||
sliderState.position = clampedChange(sliderState.val + direction * stepsToMove); | ||
break; | ||
} | ||
case validKeys.home: | ||
sliderState.position = props.min; | ||
break; | ||
case validKeys.end: | ||
sliderState.position = props.max; | ||
break; | ||
case validKeys.pageup: | ||
case validKeys.pagedown: { | ||
const direction = key === validKeys.pageup ? 1 : -1; | ||
const minStepMultiplier = 2; | ||
const maxStepMultiplier = 50; | ||
sliderState.position = clampedChange(sliderState.val + | ||
direction * | ||
sliderState.step * | ||
Math.max(minStepMultiplier, Math.min(maxStepMultiplier, Math.ceil((props.max - props.min) / 10 / sliderState.step)))); | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
else if (key === validKeys.home) { | ||
sliderState.position = props.min; | ||
} | ||
else if (key === validKeys.end) { | ||
sliderState.position = props.max; | ||
} | ||
else { | ||
const direction = key === validKeys.pageup ? 1 : -1; | ||
const minStepMultiplier = 2; | ||
const maxStepMultiplier = 50; | ||
sliderState.position = clampedChange(sliderState.val + | ||
direction * | ||
sliderState.step * | ||
Math.max(minStepMultiplier, Math.min(maxStepMultiplier, Math.ceil((props.max - props.min) / 10 / sliderState.step)))); | ||
} | ||
} | ||
function handleFocus(e) { } | ||
function handleBlur(e) { } | ||
function handleFocus() { } | ||
function handleBlur() { } | ||
function handleMouseDown(e) { | ||
sliderState.sliderPressed = true; | ||
if ("touches" in e) { | ||
window.addEventListener("touchmove", handleMouseChange, eventOptions); | ||
window.addEventListener("touchend", handleMouseUp, { once: true }); | ||
if ('touches' in e) { | ||
window.addEventListener('touchmove', handleMouseChange, eventOptions); | ||
window.addEventListener('touchend', handleMouseUp, { once: true }); | ||
} | ||
else { | ||
window.addEventListener("mousemove", handleMouseChange, eventOptions); | ||
window.addEventListener("mouseup", handleMouseUp, { once: true }); | ||
window.addEventListener('mousemove', handleMouseChange, eventOptions); | ||
window.addEventListener('mouseup', handleMouseUp, { once: true }); | ||
} | ||
@@ -62,4 +87,4 @@ e.stopPropagation(); | ||
sliderState.sliderPressed = false; | ||
window.removeEventListener("touchmove", handleMouseChange); | ||
window.removeEventListener("mousemove", handleMouseChange); | ||
window.removeEventListener('touchmove', handleMouseChange); | ||
window.removeEventListener('mousemove', handleMouseChange); | ||
} | ||
@@ -73,5 +98,4 @@ function handleClick(e) { | ||
sliderState.thumbEl?.focus(); | ||
if (sliderState.position === n) | ||
return; | ||
sliderState.position = n; | ||
if (sliderState.position !== n) | ||
sliderState.position = n; | ||
} | ||
@@ -78,0 +102,0 @@ return { |
@@ -5,3 +5,3 @@ { | ||
"description": "Shared business logic for JS implementations of Warp Design System", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"type": "module", | ||
@@ -40,5 +40,5 @@ "exports": { | ||
"typescript": "^4.7.4", | ||
"@semantic-release/changelog": "github:semantic-release/changelog", | ||
"@semantic-release/git": "github:semantic-release/git", | ||
"semantic-release": "^20.1.1", | ||
"@semantic-release/changelog": "^6.0.3", | ||
"@semantic-release/git": "^10.0.1", | ||
"semantic-release": "^22.0.7", | ||
"cz-conventional-changelog": "^3.3.0" | ||
@@ -45,0 +45,0 @@ }, |
@@ -13,9 +13,8 @@ ## Warp JS Core | ||
[Semantic Release](https://github.com/semantic-release/semantic-release) to | ||
automate package publishing when making changes to the `main` or `alpha` branch. | ||
automate package publishing when making changes to the `main` branch. | ||
It is recommended to branch off the `alpha` branch. Make sure `alpha` branch is | ||
updated with the latest `main`. | ||
It is recommended to branch off the `main` branch. Make sure `main` branch is updated. | ||
Follow [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/#summary) | ||
when making changes. When your changes are ready for pull request, this should be | ||
opened against the `alpha` branch. | ||
opened against the `main` branch. | ||
@@ -22,0 +21,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25789
333
32