@rolster/react-components 19.8.19 → 19.8.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +166 -34
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/es/index.mjs +167 -35
- package/dist/es/index.mjs.map +1 -1
- package/dist/esm/components/molecules/Slider/Slider.d.ts +2 -1
- package/dist/esm/components/molecules/Slider/Slider.js +125 -35
- package/dist/esm/components/molecules/Slider/Slider.js.map +1 -1
- package/dist/esm/components/organisms/ImageEditor/ImageEditor.js +8 -2
- package/dist/esm/components/organisms/ImageEditor/ImageEditor.js.map +1 -1
- package/dist/esm/helpers/slider.d.ts +4 -0
- package/dist/esm/helpers/slider.js +37 -0
- package/dist/esm/helpers/slider.js.map +1 -0
- package/dist/esm/helpers/slider.spec.d.ts +1 -0
- package/dist/esm/helpers/slider.spec.js +90 -0
- package/dist/esm/helpers/slider.spec.js.map +1 -0
- package/package.json +6 -7
package/dist/cjs/index.cjs
CHANGED
|
@@ -2376,57 +2376,183 @@ function RlsPickerYearComponent({ date, disabled, formControl, maxDate, minDate,
|
|
|
2376
2376
|
}
|
|
2377
2377
|
const RlsPickerYear = require$$0.memo(RlsPickerYearComponent);
|
|
2378
2378
|
|
|
2379
|
-
|
|
2380
|
-
|
|
2379
|
+
const RATE_MIN = 0;
|
|
2380
|
+
const RATE_MAX = 100;
|
|
2381
|
+
const PRECISION = 12;
|
|
2382
|
+
function clampNumber(value, minValue, maxValue) {
|
|
2383
|
+
if (minValue > maxValue) {
|
|
2384
|
+
return minValue;
|
|
2385
|
+
}
|
|
2386
|
+
return Math.min(Math.max(value, minValue), maxValue);
|
|
2387
|
+
}
|
|
2388
|
+
function normalizeSliderValue(value, minValue, maxValue, step) {
|
|
2389
|
+
const valueClamped = clampNumber(value, minValue, maxValue);
|
|
2390
|
+
if (!step || step <= 0) {
|
|
2391
|
+
return valueClamped;
|
|
2392
|
+
}
|
|
2393
|
+
if (valueClamped <= minValue) {
|
|
2394
|
+
return minValue;
|
|
2395
|
+
}
|
|
2396
|
+
if (valueClamped >= maxValue) {
|
|
2397
|
+
return maxValue;
|
|
2398
|
+
}
|
|
2399
|
+
const steps = Math.round((valueClamped - minValue) / step);
|
|
2400
|
+
const valueStepped = parseFloat((minValue + steps * step).toPrecision(PRECISION));
|
|
2401
|
+
return clampNumber(valueStepped, minValue, maxValue);
|
|
2402
|
+
}
|
|
2403
|
+
function sliderValueToRate(value, minValue, maxValue) {
|
|
2404
|
+
const range = maxValue - minValue;
|
|
2405
|
+
if (range <= 0) {
|
|
2406
|
+
return RATE_MIN;
|
|
2407
|
+
}
|
|
2408
|
+
return clampNumber(((value - minValue) / range) * RATE_MAX, RATE_MIN, RATE_MAX);
|
|
2381
2409
|
}
|
|
2382
|
-
function
|
|
2383
|
-
const
|
|
2384
|
-
const
|
|
2385
|
-
return
|
|
2410
|
+
function sliderRateToValue(rate, minValue, maxValue, step) {
|
|
2411
|
+
const range = maxValue - minValue;
|
|
2412
|
+
const rateClamped = clampNumber(rate, RATE_MIN, RATE_MAX);
|
|
2413
|
+
return normalizeSliderValue(minValue + (range * rateClamped) / RATE_MAX, minValue, maxValue, step);
|
|
2386
2414
|
}
|
|
2387
|
-
|
|
2415
|
+
|
|
2416
|
+
const DEFAULT_STEP = 1;
|
|
2417
|
+
const PAGE_STEP_DIVISOR = 10;
|
|
2418
|
+
function RlsSliderComponent({ children, className, disabled, formControl, identifier, maxValue, minValue, onValue, prefixIcon, rlsTheme, step, value }) {
|
|
2388
2419
|
const minValueSlider = require$$0.useMemo(() => {
|
|
2389
2420
|
return minValue ?? 0;
|
|
2390
2421
|
}, [minValue]);
|
|
2391
2422
|
const maxValueSlider = require$$0.useMemo(() => {
|
|
2392
2423
|
return maxValue ?? 100;
|
|
2393
2424
|
}, [maxValue]);
|
|
2394
|
-
const
|
|
2395
|
-
|
|
2396
|
-
|
|
2425
|
+
const stepSlider = require$$0.useMemo(() => {
|
|
2426
|
+
return step && step > 0 ? step : DEFAULT_STEP;
|
|
2427
|
+
}, [step]);
|
|
2428
|
+
const [valueSlider, setValueSlider] = require$$0.useState(() => {
|
|
2429
|
+
return normalizeSliderValue(formControl?.value ?? value ?? 0, minValueSlider, maxValueSlider, stepSlider);
|
|
2430
|
+
});
|
|
2431
|
+
const [dragging, setDragging] = require$$0.useState(false);
|
|
2397
2432
|
const refTrack = require$$0.useRef(null);
|
|
2398
|
-
const refTrackOn = require$$0.useRef(null);
|
|
2399
2433
|
const refThumb = require$$0.useRef(null);
|
|
2434
|
+
const refValue = require$$0.useRef(valueSlider);
|
|
2435
|
+
const refValueExternal = require$$0.useRef(formControl?.value ?? value);
|
|
2436
|
+
const labelId = require$$0.useId();
|
|
2437
|
+
const rate = require$$0.useMemo(() => {
|
|
2438
|
+
return sliderValueToRate(valueSlider, minValueSlider, maxValueSlider);
|
|
2439
|
+
}, [valueSlider, minValueSlider, maxValueSlider]);
|
|
2400
2440
|
const classNameSlider = renderClassStatus('rls-slider', {
|
|
2401
2441
|
complet: valueSlider === maxValueSlider,
|
|
2402
2442
|
disabled: disabled,
|
|
2443
|
+
dragging: dragging,
|
|
2403
2444
|
empty: valueSlider === minValueSlider
|
|
2404
2445
|
}, className);
|
|
2446
|
+
const commitValue = require$$0.useCallback((valueNext) => {
|
|
2447
|
+
if (refValue.current === valueNext) {
|
|
2448
|
+
return;
|
|
2449
|
+
}
|
|
2450
|
+
refValue.current = valueNext;
|
|
2451
|
+
setValueSlider(valueNext);
|
|
2452
|
+
formControl?.setValue(valueNext);
|
|
2453
|
+
onValue?.(valueNext);
|
|
2454
|
+
}, [formControl, onValue]);
|
|
2455
|
+
const commitRate = require$$0.useCallback((rateNext) => {
|
|
2456
|
+
commitValue(sliderRateToValue(rateNext, minValueSlider, maxValueSlider, stepSlider));
|
|
2457
|
+
}, [commitValue, minValueSlider, maxValueSlider, stepSlider]);
|
|
2458
|
+
const calculateRateFromClientX = require$$0.useCallback((clientX) => {
|
|
2459
|
+
const { left, width } = refTrack.current.getBoundingClientRect();
|
|
2460
|
+
return width > 0 ? ((clientX - left) / width) * 100 : 0;
|
|
2461
|
+
}, []);
|
|
2462
|
+
const onPointerDown = require$$0.useCallback((event) => {
|
|
2463
|
+
if (disabled) {
|
|
2464
|
+
return;
|
|
2465
|
+
}
|
|
2466
|
+
event.preventDefault();
|
|
2467
|
+
refThumb.current.focus();
|
|
2468
|
+
setDragging(true);
|
|
2469
|
+
commitRate(calculateRateFromClientX(event.clientX));
|
|
2470
|
+
}, [disabled, commitRate, calculateRateFromClientX]);
|
|
2471
|
+
const calculateValueFromKey = require$$0.useCallback((key) => {
|
|
2472
|
+
const pageStep = Math.max(stepSlider, (maxValueSlider - minValueSlider) / PAGE_STEP_DIVISOR);
|
|
2473
|
+
switch (key) {
|
|
2474
|
+
case 'ArrowRight':
|
|
2475
|
+
case 'ArrowUp':
|
|
2476
|
+
return refValue.current + stepSlider;
|
|
2477
|
+
case 'ArrowLeft':
|
|
2478
|
+
case 'ArrowDown':
|
|
2479
|
+
return refValue.current - stepSlider;
|
|
2480
|
+
case 'PageUp':
|
|
2481
|
+
return refValue.current + pageStep;
|
|
2482
|
+
case 'PageDown':
|
|
2483
|
+
return refValue.current - pageStep;
|
|
2484
|
+
case 'Home':
|
|
2485
|
+
return minValueSlider;
|
|
2486
|
+
case 'End':
|
|
2487
|
+
return maxValueSlider;
|
|
2488
|
+
default:
|
|
2489
|
+
return undefined;
|
|
2490
|
+
}
|
|
2491
|
+
}, [stepSlider, minValueSlider, maxValueSlider]);
|
|
2492
|
+
const onKeyDown = require$$0.useCallback((event) => {
|
|
2493
|
+
if (disabled) {
|
|
2494
|
+
return;
|
|
2495
|
+
}
|
|
2496
|
+
const valueNext = calculateValueFromKey(event.key);
|
|
2497
|
+
if (valueNext === undefined) {
|
|
2498
|
+
return;
|
|
2499
|
+
}
|
|
2500
|
+
event.preventDefault();
|
|
2501
|
+
commitValue(normalizeSliderValue(valueNext, minValueSlider, maxValueSlider, stepSlider));
|
|
2502
|
+
}, [
|
|
2503
|
+
disabled,
|
|
2504
|
+
calculateValueFromKey,
|
|
2505
|
+
commitValue,
|
|
2506
|
+
minValueSlider,
|
|
2507
|
+
maxValueSlider,
|
|
2508
|
+
stepSlider
|
|
2509
|
+
]);
|
|
2510
|
+
require$$0.useEffect(() => {
|
|
2511
|
+
if (!dragging) {
|
|
2512
|
+
return;
|
|
2513
|
+
}
|
|
2514
|
+
const onPointerMove = (event) => {
|
|
2515
|
+
commitRate(calculateRateFromClientX(event.clientX));
|
|
2516
|
+
};
|
|
2517
|
+
const onPointerRelease = () => {
|
|
2518
|
+
setDragging(false);
|
|
2519
|
+
};
|
|
2520
|
+
window.addEventListener('pointermove', onPointerMove);
|
|
2521
|
+
window.addEventListener('pointerup', onPointerRelease);
|
|
2522
|
+
window.addEventListener('pointercancel', onPointerRelease);
|
|
2523
|
+
return () => {
|
|
2524
|
+
window.removeEventListener('pointermove', onPointerMove);
|
|
2525
|
+
window.removeEventListener('pointerup', onPointerRelease);
|
|
2526
|
+
window.removeEventListener('pointercancel', onPointerRelease);
|
|
2527
|
+
};
|
|
2528
|
+
}, [dragging, commitRate, calculateRateFromClientX]);
|
|
2405
2529
|
require$$0.useEffect(() => {
|
|
2406
2530
|
const valueInitial = formControl?.value ?? value ?? 0;
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
formControl?.setValue(valueSlider);
|
|
2411
|
-
onValue?.(valueSlider);
|
|
2531
|
+
if (valueInitial !== refValue.current) {
|
|
2532
|
+
formControl?.setValue(refValue.current);
|
|
2533
|
+
onValue?.(refValue.current);
|
|
2412
2534
|
}
|
|
2413
2535
|
}, []);
|
|
2414
|
-
|
|
2415
|
-
const
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2536
|
+
require$$0.useEffect(() => {
|
|
2537
|
+
const valueExternal = formControl?.value ?? value;
|
|
2538
|
+
if (valueExternal === undefined ||
|
|
2539
|
+
valueExternal === refValueExternal.current) {
|
|
2540
|
+
return;
|
|
2541
|
+
}
|
|
2542
|
+
refValueExternal.current = valueExternal;
|
|
2543
|
+
commitValue(normalizeSliderValue(valueExternal, minValueSlider, maxValueSlider, stepSlider));
|
|
2544
|
+
}, [
|
|
2545
|
+
value,
|
|
2546
|
+
formControl?.value,
|
|
2547
|
+
commitValue,
|
|
2548
|
+
minValueSlider,
|
|
2549
|
+
maxValueSlider,
|
|
2550
|
+
stepSlider
|
|
2551
|
+
]);
|
|
2552
|
+
require$$0.useEffect(() => {
|
|
2553
|
+
commitValue(normalizeSliderValue(refValue.current, minValueSlider, maxValueSlider, stepSlider));
|
|
2554
|
+
}, [commitValue, minValueSlider, maxValueSlider, stepSlider]);
|
|
2555
|
+
return (jsxRuntimeExports.jsxs("div", { id: identifier, className: classNameSlider, "rls-theme": rlsTheme, children: [children && (jsxRuntimeExports.jsx("span", { id: labelId, className: "rls-slider__label", children: children })), jsxRuntimeExports.jsxs("div", { className: "rls-slider__body", children: [prefixIcon && jsxRuntimeExports.jsx(RlsIcon, { value: prefixIcon }), jsxRuntimeExports.jsxs("div", { className: "rls-slider__component", onPointerDown: onPointerDown, children: [jsxRuntimeExports.jsx("div", { ref: refTrack, className: "rls-slider__track", children: jsxRuntimeExports.jsx("div", { className: "rls-slider__track__on", style: { width: `${rate}%` } }) }), jsxRuntimeExports.jsx("div", { ref: refThumb, className: "rls-slider__thumb", style: { left: `${rate}%` }, role: "slider", tabIndex: disabled ? -1 : 0, "aria-orientation": "horizontal", "aria-valuemin": minValueSlider, "aria-valuemax": maxValueSlider, "aria-valuenow": valueSlider, "aria-disabled": disabled || undefined, "aria-labelledby": children ? labelId : undefined, onKeyDown: onKeyDown, children: valueSlider })] })] })] }));
|
|
2430
2556
|
}
|
|
2431
2557
|
const RlsSlider = require$$0.memo(RlsSliderComponent);
|
|
2432
2558
|
|
|
@@ -4643,11 +4769,17 @@ function RlsImageEditorComponent(props) {
|
|
|
4643
4769
|
useResize({ refElement: refImage, onResize: onResizeElement });
|
|
4644
4770
|
const onCropImage = require$$0.useCallback(() => {
|
|
4645
4771
|
const cropProps = getCropProperties();
|
|
4646
|
-
const width = props.maxWidth
|
|
4647
|
-
|
|
4772
|
+
const width = Math.round(props.maxWidth
|
|
4773
|
+
? Math.min(props.maxWidth, cropProps.width)
|
|
4774
|
+
: cropProps.width);
|
|
4775
|
+
const height = Math.round(width * getRatioFactor(ratio));
|
|
4648
4776
|
refPicture.current.width = width;
|
|
4649
4777
|
refPicture.current.height = height;
|
|
4650
4778
|
const context = refPicture.current.getContext('2d');
|
|
4779
|
+
if (context) {
|
|
4780
|
+
context.imageSmoothingEnabled = true;
|
|
4781
|
+
context.imageSmoothingQuality = 'high';
|
|
4782
|
+
}
|
|
4651
4783
|
context?.drawImage(refCanvas.current, cropProps.left, cropProps.top, cropProps.width, cropProps.height, 0, 0, width, height);
|
|
4652
4784
|
refPicture.current.toBlob((blob) => {
|
|
4653
4785
|
if (blob) {
|