@reactuses/core 6.1.0 → 6.1.2

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/README.md CHANGED
@@ -18,7 +18,7 @@ npm i @reactuses/core
18
18
 
19
19
  ## MCP Support
20
20
 
21
- If you want to use the MCP (Meta Command Protocol) integration with reactuse, you can easily set it up with the following configuration. This allows you to run the `@reactuses/mcp` utility via `npx` for enhanced command-line support and automation.
21
+ If you want to use the MCP (Model Context Protocol) integration with reactuse, you can easily set it up with the following configuration. This allows you to run the `@reactuses/mcp` utility via `npx` for enhanced command-line support and automation.
22
22
 
23
23
  Add the following to your configuration:
24
24
 
package/dist/index.cjs CHANGED
@@ -1401,8 +1401,8 @@ function _async_to_generator$5(fn) {
1401
1401
  });
1402
1402
  };
1403
1403
  }
1404
- function _extends$3() {
1405
- _extends$3 = Object.assign || function(target) {
1404
+ function _extends$4() {
1405
+ _extends$4 = Object.assign || function(target) {
1406
1406
  for(var i = 1; i < arguments.length; i++){
1407
1407
  var source = arguments[i];
1408
1408
  for(var key in source){
@@ -1413,7 +1413,7 @@ function _extends$3() {
1413
1413
  }
1414
1414
  return target;
1415
1415
  };
1416
- return _extends$3.apply(this, arguments);
1416
+ return _extends$4.apply(this, arguments);
1417
1417
  }
1418
1418
  const DEFAULT_OPTIONS = {
1419
1419
  multiple: true,
@@ -1442,7 +1442,7 @@ const useFileDialog = (options = defaultOptions$1)=>{
1442
1442
  if (!inputRef.current) {
1443
1443
  return;
1444
1444
  }
1445
- const _options = _extends$3({}, DEFAULT_OPTIONS, options, localOptions);
1445
+ const _options = _extends$4({}, DEFAULT_OPTIONS, options, localOptions);
1446
1446
  inputRef.current.multiple = _options.multiple;
1447
1447
  inputRef.current.accept = _options.accept;
1448
1448
  // Only set capture attribute if it's explicitly provided
@@ -1880,8 +1880,8 @@ function _async_to_generator$4(fn) {
1880
1880
  });
1881
1881
  };
1882
1882
  }
1883
- function _extends$2() {
1884
- _extends$2 = Object.assign || function(target) {
1883
+ function _extends$3() {
1884
+ _extends$3 = Object.assign || function(target) {
1885
1885
  for(var i = 1; i < arguments.length; i++){
1886
1886
  var source = arguments[i];
1887
1887
  for(var key in source){
@@ -1892,15 +1892,15 @@ function _extends$2() {
1892
1892
  }
1893
1893
  return target;
1894
1894
  };
1895
- return _extends$2.apply(this, arguments);
1895
+ return _extends$3.apply(this, arguments);
1896
1896
  }
1897
1897
  const useInfiniteScroll = (target, onLoadMore, options = defaultOptions$1)=>{
1898
1898
  const savedLoadMore = useLatest(onLoadMore);
1899
1899
  var _options_direction;
1900
1900
  const direction = (_options_direction = options.direction) != null ? _options_direction : 'bottom';
1901
1901
  var _options_distance;
1902
- const state = useScroll(target, _extends$2({}, options, {
1903
- offset: _extends$2({
1902
+ const state = useScroll(target, _extends$3({}, options, {
1903
+ offset: _extends$3({
1904
1904
  [direction]: (_options_distance = options.distance) != null ? _options_distance : 0
1905
1905
  }, options.offset)
1906
1906
  }));
@@ -2627,6 +2627,131 @@ function getValue(position) {
2627
2627
  return getComputedStyle(document.documentElement).getPropertyValue(position);
2628
2628
  }
2629
2629
 
2630
+ function _extends$2() {
2631
+ _extends$2 = Object.assign || function(target) {
2632
+ for(var i = 1; i < arguments.length; i++){
2633
+ var source = arguments[i];
2634
+ for(var key in source){
2635
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
2636
+ target[key] = source[key];
2637
+ }
2638
+ }
2639
+ }
2640
+ return target;
2641
+ };
2642
+ return _extends$2.apply(this, arguments);
2643
+ }
2644
+ const initialState = {
2645
+ isScratching: false
2646
+ };
2647
+ const useScratch = (target, options = {})=>{
2648
+ const { disabled = false } = options;
2649
+ const [state, setState] = useRafState(initialState);
2650
+ const optionsRef = useLatest(options);
2651
+ const refState = React.useRef(state);
2652
+ const refScratching = React.useRef(false);
2653
+ const refAnimationFrame = React.useRef(null);
2654
+ const onMoveEvent = (docX, docY)=>{
2655
+ if (!refScratching.current) {
2656
+ return;
2657
+ }
2658
+ const el = getTargetElement(target);
2659
+ if (!el) {
2660
+ return;
2661
+ }
2662
+ if (refAnimationFrame.current !== null) {
2663
+ cancelAnimationFrame(refAnimationFrame.current);
2664
+ }
2665
+ refAnimationFrame.current = requestAnimationFrame(()=>{
2666
+ const { left, top } = el.getBoundingClientRect();
2667
+ const elX = left + window.scrollX;
2668
+ const elY = top + window.scrollY;
2669
+ const x = docX - elX;
2670
+ const y = docY - elY;
2671
+ setState((oldState)=>{
2672
+ const newState = _extends$2({}, oldState, {
2673
+ x,
2674
+ y,
2675
+ dx: x - (oldState.x || 0),
2676
+ dy: y - (oldState.y || 0),
2677
+ end: Date.now(),
2678
+ isScratching: true
2679
+ });
2680
+ refState.current = newState;
2681
+ (optionsRef.current.onScratch || noop)(newState);
2682
+ return newState;
2683
+ });
2684
+ });
2685
+ };
2686
+ const stopScratching = ()=>{
2687
+ if (!refScratching.current) {
2688
+ return;
2689
+ }
2690
+ refScratching.current = false;
2691
+ const endState = _extends$2({}, refState.current, {
2692
+ isScratching: false
2693
+ });
2694
+ refState.current = endState;
2695
+ (optionsRef.current.onScratchEnd || noop)(endState);
2696
+ setState(endState);
2697
+ };
2698
+ const startScratching = (docX, docY)=>{
2699
+ const el = getTargetElement(target);
2700
+ if (disabled || !el) {
2701
+ return;
2702
+ }
2703
+ refScratching.current = true;
2704
+ const { left, top } = el.getBoundingClientRect();
2705
+ const elX = left + window.scrollX;
2706
+ const elY = top + window.scrollY;
2707
+ const x = docX - elX;
2708
+ const y = docY - elY;
2709
+ const time = Date.now();
2710
+ const newState = {
2711
+ isScratching: true,
2712
+ start: time,
2713
+ end: time,
2714
+ docX,
2715
+ docY,
2716
+ x,
2717
+ y,
2718
+ dx: 0,
2719
+ dy: 0,
2720
+ elH: el.offsetHeight,
2721
+ elW: el.offsetWidth,
2722
+ elX,
2723
+ elY,
2724
+ posX: elX,
2725
+ posY: elY
2726
+ };
2727
+ refState.current = newState;
2728
+ (optionsRef.current.onScratchStart || noop)(newState);
2729
+ setState(newState);
2730
+ };
2731
+ useEventListener('mousedown', (event)=>{
2732
+ event.preventDefault();
2733
+ startScratching(event.pageX || event.clientX, event.pageY || event.clientY);
2734
+ }, target);
2735
+ useEventListener('touchstart', (event)=>{
2736
+ event.preventDefault();
2737
+ startScratching(event.changedTouches[0].pageX || event.changedTouches[0].clientX, event.changedTouches[0].pageY || event.changedTouches[0].clientY);
2738
+ }, target);
2739
+ useEventListener('mousemove', (event)=>{
2740
+ onMoveEvent(event.pageX || event.clientX, event.pageY || event.clientY);
2741
+ }, defaultWindow);
2742
+ useEventListener('touchmove', (event)=>{
2743
+ onMoveEvent(event.changedTouches[0].pageX || event.changedTouches[0].clientX, event.changedTouches[0].pageY || event.changedTouches[0].clientY);
2744
+ }, defaultWindow);
2745
+ useEventListener('mouseup', stopScratching, defaultWindow);
2746
+ useEventListener('touchend', stopScratching, defaultWindow);
2747
+ useUnmount(()=>{
2748
+ if (refAnimationFrame.current !== null) {
2749
+ cancelAnimationFrame(refAnimationFrame.current);
2750
+ }
2751
+ });
2752
+ return state;
2753
+ };
2754
+
2630
2755
  const useScriptTag = (src, onLoaded = noop, options = defaultOptions$1)=>{
2631
2756
  const { immediate = true, manual = false, type = 'text/javascript', async = true, crossOrigin, referrerPolicy, noModule, defer, attrs = {} } = options;
2632
2757
  const scriptTag = React.useRef(null);
@@ -4204,6 +4329,112 @@ const useMap = (initialValue)=>{
4204
4329
  };
4205
4330
  };
4206
4331
 
4332
+ const useSpeechRecognition = (options = {})=>{
4333
+ const { interimResults = true, continuous = true, maxAlternatives = 1, lang = 'en-US' } = options;
4334
+ const [isListening, setIsListening] = React.useState(false);
4335
+ const [isFinal, setIsFinal] = React.useState(false);
4336
+ const [result, setResult] = React.useState('');
4337
+ const [error, setError] = React.useState(undefined);
4338
+ const recognitionRef = React.useRef(undefined);
4339
+ const SpeechRecognitionClass = defaultWindow && (defaultWindow.SpeechRecognition || defaultWindow.webkitSpeechRecognition);
4340
+ const isSupported = useSupported(()=>SpeechRecognitionClass);
4341
+ const start = useEvent((startOptions)=>{
4342
+ if (!recognitionRef.current) {
4343
+ return;
4344
+ }
4345
+ // Apply options to recognition instance
4346
+ const { interimResults: newInterimResults = interimResults, continuous: newContinuous = continuous, maxAlternatives: newMaxAlternatives = maxAlternatives, lang: newLang = lang } = startOptions || {};
4347
+ recognitionRef.current.interimResults = newInterimResults;
4348
+ recognitionRef.current.continuous = newContinuous;
4349
+ recognitionRef.current.maxAlternatives = newMaxAlternatives;
4350
+ recognitionRef.current.lang = newLang;
4351
+ setIsListening(true);
4352
+ });
4353
+ const stop = useEvent(()=>{
4354
+ setIsListening(false);
4355
+ });
4356
+ const toggle = useEvent((value = !isListening, startOptions)=>{
4357
+ if (value) {
4358
+ start(startOptions);
4359
+ } else {
4360
+ stop();
4361
+ }
4362
+ });
4363
+ // Initialize recognition when supported
4364
+ React.useEffect(()=>{
4365
+ if (!isSupported || !SpeechRecognitionClass) {
4366
+ return;
4367
+ }
4368
+ const recognition = new SpeechRecognitionClass();
4369
+ recognitionRef.current = recognition;
4370
+ // Set up event listeners
4371
+ recognition.onstart = ()=>{
4372
+ setIsListening(true);
4373
+ setIsFinal(false);
4374
+ };
4375
+ recognition.onresult = (event)=>{
4376
+ const currentResult = event.results[event.resultIndex];
4377
+ const { transcript } = currentResult[0];
4378
+ setIsFinal(currentResult.isFinal);
4379
+ setResult(transcript);
4380
+ setError(undefined);
4381
+ };
4382
+ recognition.onerror = (event)=>{
4383
+ setError(event);
4384
+ };
4385
+ recognition.onend = ()=>{
4386
+ setIsListening(false);
4387
+ };
4388
+ return ()=>{
4389
+ if (recognition) {
4390
+ recognition.abort();
4391
+ }
4392
+ };
4393
+ }, [
4394
+ isSupported,
4395
+ SpeechRecognitionClass
4396
+ ]);
4397
+ // Handle listening state changes
4398
+ React.useEffect(()=>{
4399
+ if (!recognitionRef.current) {
4400
+ return;
4401
+ }
4402
+ if (isListening) {
4403
+ try {
4404
+ recognitionRef.current.start();
4405
+ } catch (err) {
4406
+ console.warn('Failed to start speech recognition:', err);
4407
+ setIsListening(false);
4408
+ }
4409
+ } else {
4410
+ try {
4411
+ recognitionRef.current.stop();
4412
+ } catch (err) {
4413
+ console.warn('Failed to stop speech recognition:', err);
4414
+ }
4415
+ }
4416
+ }, [
4417
+ isListening
4418
+ ]);
4419
+ // Cleanup on unmount
4420
+ useUnmount(()=>{
4421
+ if (recognitionRef.current) {
4422
+ recognitionRef.current.abort();
4423
+ }
4424
+ });
4425
+ return {
4426
+ isSupported,
4427
+ isListening,
4428
+ isFinal,
4429
+ recognition: recognitionRef.current,
4430
+ result,
4431
+ error,
4432
+ toggle,
4433
+ start,
4434
+ stop
4435
+ };
4436
+ };
4437
+
4207
4438
  exports.assignRef = assignRef;
4208
4439
  exports.defaultOptions = defaultOptions;
4209
4440
  exports.mergeRefs = mergeRefs;
@@ -4291,6 +4522,7 @@ exports.useRafFn = useRafFn;
4291
4522
  exports.useRafState = useRafState;
4292
4523
  exports.useReducedMotion = useReducedMotion;
4293
4524
  exports.useResizeObserver = useResizeObserver;
4525
+ exports.useScratch = useScratch;
4294
4526
  exports.useScreenSafeArea = useScreenSafeArea;
4295
4527
  exports.useScriptTag = useScriptTag;
4296
4528
  exports.useScroll = useScroll;
@@ -4298,6 +4530,7 @@ exports.useScrollIntoView = useScrollIntoView;
4298
4530
  exports.useScrollLock = useScrollLock;
4299
4531
  exports.useSessionStorage = useSessionStorage;
4300
4532
  exports.useSetState = useSetState;
4533
+ exports.useSpeechRecognition = useSpeechRecognition;
4301
4534
  exports.useSticky = useSticky;
4302
4535
  exports.useSupported = useSupported;
4303
4536
  exports.useTextDirection = useTextDirection;