@lynx-js/lynx-ui-switch 3.131.1 → 3.133.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @lynx-js/lynx-ui-switch
2
2
 
3
+ ## 3.133.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Improve Web and desktop touch compatibility by routing `onTouchStart`, ([#200](https://github.com/lynx-family/lynx-ui/pull/200))
8
+ `onTouchMove`, `onTouchEnd`, and `onTouchCancel` through shared touch
9
+ emulation.
10
+ - Updated dependencies [[`3932070`](https://github.com/lynx-family/lynx-ui/commit/393207073522e221a79e65c643df2afb329ec931)]:
11
+ - @lynx-js/lynx-ui-common@3.133.1
12
+
13
+ ## 3.133.0
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [[`85d2e9d`](https://github.com/lynx-family/lynx-ui/commit/85d2e9d12e9aeee3256856f1be0076524ddb7d8a)]:
18
+ - @lynx-js/lynx-ui-common@3.133.0
19
+
3
20
  ## 3.131.1
4
21
 
5
22
  ### Patch Changes
package/dist/switch.jsx CHANGED
@@ -16,7 +16,7 @@ function Switch({ style, className, children, onChange, checked: checkedProp, de
16
16
  if (!isControlled) setUncontrolledChecked(next);
17
17
  onChange?.(next);
18
18
  };
19
- const { pressed, bindtap, bindtouchcancel, bindtouchend, bindtouchstart } = usePressTap({
19
+ const { pressed, bindtap, ...touchHandlers } = usePressTap({
20
20
  disabled,
21
21
  onTap: handleChange
22
22
  });
@@ -34,7 +34,7 @@ function Switch({ style, className, children, onChange, checked: checkedProp, de
34
34
  disabled
35
35
  ]);
36
36
  return <SwitchContext.Provider value={switchContextValue}>
37
- <view bindtap={bindtap} bindtouchstart={bindtouchstart} bindtouchend={bindtouchend} bindtouchcancel={bindtouchcancel} event-through={false} style={style} className={clsx(className, {
37
+ <view bindtap={bindtap} {...touchHandlers} event-through={false} style={style} className={clsx(className, {
38
38
  'ui-active': active,
39
39
  'ui-checked': checked,
40
40
  'ui-disabled': disabled
@@ -1 +1 @@
1
- {"version":3,"file":"switch.jsx","sources":["webpack://@lynx-js/lynx-ui-switch/./src/switch.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { createContext, useContext, useMemo, useState } from '@lynx-js/react'\nimport type { ReactNode } from '@lynx-js/react'\n\nimport { clsx } from 'clsx'\n\nimport { render } from './render'\nimport type {\n SwitchProps,\n SwitchRenderProps,\n SwitchThumbProps,\n SwitchTrackProps,\n} from './types'\nimport { usePressTap } from './use-press-tap'\n\nconst SwitchContext = createContext<SwitchRenderProps>({\n checked: false,\n disabled: false,\n active: false,\n})\n\nfunction Switch({\n style,\n className,\n children,\n onChange,\n checked: checkedProp,\n defaultChecked = false,\n disabled = false,\n switchProps,\n}: SwitchProps): ReactNode {\n const isControlled = checkedProp !== undefined\n\n const [uncontrolledChecked, setUncontrolledChecked] = useState<boolean>(\n defaultChecked,\n )\n\n const checked = isControlled ? checkedProp : uncontrolledChecked\n\n const handleChange = () => {\n const next = !checked\n if (!isControlled) {\n setUncontrolledChecked(next)\n }\n onChange?.(next)\n }\n\n const { pressed, bindtap, bindtouchcancel, bindtouchend, bindtouchstart } =\n usePressTap({ disabled, onTap: handleChange })\n\n const active = useMemo(() => pressed && !disabled, [pressed, disabled])\n\n const switchContextValue: SwitchRenderProps = useMemo(() => ({\n active,\n checked,\n disabled,\n }), [active, checked, disabled])\n\n return (\n <SwitchContext.Provider value={switchContextValue}>\n <view\n bindtap={bindtap}\n bindtouchstart={bindtouchstart}\n bindtouchend={bindtouchend}\n bindtouchcancel={bindtouchcancel}\n event-through={false}\n style={style}\n className={clsx(className, {\n 'ui-active': active,\n 'ui-checked': checked,\n 'ui-disabled': disabled,\n })}\n {...switchProps}\n >\n {render<SwitchRenderProps>(switchContextValue, children)}\n </view>\n </SwitchContext.Provider>\n )\n}\n\nfunction SwitchThumb(props: SwitchThumbProps): ReactNode {\n const { children, className, style } = props\n const { active, checked, disabled } = useContext(SwitchContext)\n\n return (\n <view\n style={style}\n className={clsx(className, {\n 'ui-active': active,\n 'ui-checked': checked,\n 'ui-disabled': disabled,\n })}\n >\n {children}\n </view>\n )\n}\n\nfunction SwitchTrack(props: SwitchTrackProps): ReactNode {\n const { children, className, style } = props\n const { active, checked, disabled } = useContext(SwitchContext)\n\n return (\n <view\n style={style}\n className={clsx(className, {\n 'ui-active': active,\n 'ui-checked': checked,\n 'ui-disabled': disabled,\n })}\n >\n {children}\n </view>\n )\n}\n\nexport { Switch, SwitchThumb, SwitchTrack }\n"],"names":["SwitchContext","createContext","Switch","style","className","children","onChange","checkedProp","defaultChecked","disabled","switchProps","isControlled","undefined","uncontrolledChecked","setUncontrolledChecked","useState","checked","handleChange","next","pressed","bindtap","bindtouchcancel","bindtouchend","bindtouchstart","usePressTap","active","useMemo","switchContextValue","view","clsx","render","SwitchThumb","props","useContext","SwitchTrack"],"mappings":";;;;AAkBA,MAAMA,gBAAgBC,cAAiC;IACrD,SAAS;IACT,UAAU;IACV,QAAQ;AACV;AAEA,SAASC,OAAO,EACdC,KAAK,EACLC,SAAS,EACTC,QAAQ,EACRC,QAAQ,EACR,SAASC,WAAW,EACpBC,iBAAiB,KAAK,EACtBC,WAAW,KAAK,EAChBC,WAAW,EACC;IACZ,MAAMC,eAAeJ,AAAgBK,WAAhBL;IAErB,MAAM,CAACM,qBAAqBC,uBAAuB,GAAGC,SACpDP;IAGF,MAAMQ,UAAUL,eAAeJ,cAAcM;IAE7C,MAAMI,eAAe;QACnB,MAAMC,OAAO,CAACF;QACd,IAAI,CAACL,cACHG,uBAAuBI;QAEzBZ,WAAWY;IACb;IAEA,MAAM,EAAEC,OAAO,EAAEC,OAAO,EAAEC,eAAe,EAAEC,YAAY,EAAEC,cAAc,EAAE,GACvEC,YAAY;QAAEf;QAAU,OAAOQ;IAAa;IAE9C,MAAMQ,SAASC,QAAQ,IAAMP,WAAW,CAACV,UAAU;QAACU;QAASV;KAAS;IAEtE,MAAMkB,qBAAwCD,QAAQ,IAAO;YAC3DD;YACAT;YACAP;QACF,IAAI;QAACgB;QAAQT;QAASP;KAAS;IAE/B,QACGT,cAAc,SAAS,OAAO2B,oBAAoB;MACjD,CAACC,KACC,SAASR,SACT,gBAAgBG,gBAChB,cAAcD,cACd,iBAAiBD,iBACjB,eAAe,OACf,OAAOlB,OACP,WAAW0B,KAAKzB,WAAW;QACzB,aAAaqB;QACb,cAAcT;QACd,eAAeP;IACjB,KACC,GAAGC,WAAW,EAChB;QACC,CAACoB,OAA0BH,oBAAoBtB,UAAU;MAC3D,EAAEuB,KAAK;IACT,EAAE5B,cAAc;AAEpB;AAEA,SAAS+B,YAAYC,KAAuB;IAC1C,MAAM,EAAE3B,QAAQ,EAAED,SAAS,EAAED,KAAK,EAAE,GAAG6B;IACvC,MAAM,EAAEP,MAAM,EAAET,OAAO,EAAEP,QAAQ,EAAE,GAAGwB,WAAWjC;IAEjD,QACG4B,KACC,OAAOzB,OACP,WAAW0B,KAAKzB,WAAW;QACzB,aAAaqB;QACb,cAAcT;QACd,eAAeP;IACjB,IACD;MACC,CAACJ,SAAS;IACZ,EAAEuB;AAEN;AAEA,SAASM,YAAYF,KAAuB;IAC1C,MAAM,EAAE3B,QAAQ,EAAED,SAAS,EAAED,KAAK,EAAE,GAAG6B;IACvC,MAAM,EAAEP,MAAM,EAAET,OAAO,EAAEP,QAAQ,EAAE,GAAGwB,WAAWjC;IAEjD,QACG4B,KACC,OAAOzB,OACP,WAAW0B,KAAKzB,WAAW;QACzB,aAAaqB;QACb,cAAcT;QACd,eAAeP;IACjB,IACD;MACC,CAACJ,SAAS;IACZ,EAAEuB;AAEN"}
1
+ {"version":3,"file":"switch.jsx","sources":["webpack://@lynx-js/lynx-ui-switch/./src/switch.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { createContext, useContext, useMemo, useState } from '@lynx-js/react'\nimport type { ReactNode } from '@lynx-js/react'\n\nimport { clsx } from 'clsx'\n\nimport { render } from './render'\nimport type {\n SwitchProps,\n SwitchRenderProps,\n SwitchThumbProps,\n SwitchTrackProps,\n} from './types'\nimport { usePressTap } from './use-press-tap'\n\nconst SwitchContext = createContext<SwitchRenderProps>({\n checked: false,\n disabled: false,\n active: false,\n})\n\nfunction Switch({\n style,\n className,\n children,\n onChange,\n checked: checkedProp,\n defaultChecked = false,\n disabled = false,\n switchProps,\n}: SwitchProps): ReactNode {\n const isControlled = checkedProp !== undefined\n\n const [uncontrolledChecked, setUncontrolledChecked] = useState<boolean>(\n defaultChecked,\n )\n\n const checked = isControlled ? checkedProp : uncontrolledChecked\n\n const handleChange = () => {\n const next = !checked\n if (!isControlled) {\n setUncontrolledChecked(next)\n }\n onChange?.(next)\n }\n\n const { pressed, bindtap, ...touchHandlers } = usePressTap({\n disabled,\n onTap: handleChange,\n })\n\n const active = useMemo(() => pressed && !disabled, [pressed, disabled])\n\n const switchContextValue: SwitchRenderProps = useMemo(() => ({\n active,\n checked,\n disabled,\n }), [active, checked, disabled])\n\n return (\n <SwitchContext.Provider value={switchContextValue}>\n <view\n bindtap={bindtap}\n {...touchHandlers}\n event-through={false}\n style={style}\n className={clsx(className, {\n 'ui-active': active,\n 'ui-checked': checked,\n 'ui-disabled': disabled,\n })}\n {...switchProps}\n >\n {render<SwitchRenderProps>(switchContextValue, children)}\n </view>\n </SwitchContext.Provider>\n )\n}\n\nfunction SwitchThumb(props: SwitchThumbProps): ReactNode {\n const { children, className, style } = props\n const { active, checked, disabled } = useContext(SwitchContext)\n\n return (\n <view\n style={style}\n className={clsx(className, {\n 'ui-active': active,\n 'ui-checked': checked,\n 'ui-disabled': disabled,\n })}\n >\n {children}\n </view>\n )\n}\n\nfunction SwitchTrack(props: SwitchTrackProps): ReactNode {\n const { children, className, style } = props\n const { active, checked, disabled } = useContext(SwitchContext)\n\n return (\n <view\n style={style}\n className={clsx(className, {\n 'ui-active': active,\n 'ui-checked': checked,\n 'ui-disabled': disabled,\n })}\n >\n {children}\n </view>\n )\n}\n\nexport { Switch, SwitchThumb, SwitchTrack }\n"],"names":["SwitchContext","createContext","Switch","style","className","children","onChange","checkedProp","defaultChecked","disabled","switchProps","isControlled","undefined","uncontrolledChecked","setUncontrolledChecked","useState","checked","handleChange","next","pressed","bindtap","touchHandlers","usePressTap","active","useMemo","switchContextValue","view","clsx","render","SwitchThumb","props","useContext","SwitchTrack"],"mappings":";;;;AAkBA,MAAMA,gBAAgBC,cAAiC;IACrD,SAAS;IACT,UAAU;IACV,QAAQ;AACV;AAEA,SAASC,OAAO,EACdC,KAAK,EACLC,SAAS,EACTC,QAAQ,EACRC,QAAQ,EACR,SAASC,WAAW,EACpBC,iBAAiB,KAAK,EACtBC,WAAW,KAAK,EAChBC,WAAW,EACC;IACZ,MAAMC,eAAeJ,AAAgBK,WAAhBL;IAErB,MAAM,CAACM,qBAAqBC,uBAAuB,GAAGC,SACpDP;IAGF,MAAMQ,UAAUL,eAAeJ,cAAcM;IAE7C,MAAMI,eAAe;QACnB,MAAMC,OAAO,CAACF;QACd,IAAI,CAACL,cACHG,uBAAuBI;QAEzBZ,WAAWY;IACb;IAEA,MAAM,EAAEC,OAAO,EAAEC,OAAO,EAAE,GAAGC,eAAe,GAAGC,YAAY;QACzDb;QACA,OAAOQ;IACT;IAEA,MAAMM,SAASC,QAAQ,IAAML,WAAW,CAACV,UAAU;QAACU;QAASV;KAAS;IAEtE,MAAMgB,qBAAwCD,QAAQ,IAAO;YAC3DD;YACAP;YACAP;QACF,IAAI;QAACc;QAAQP;QAASP;KAAS;IAE/B,QACGT,cAAc,SAAS,OAAOyB,oBAAoB;MACjD,CAACC,KACC,SAASN,UACR,GAAGC,aAAa,EACjB,eAAe,OACf,OAAOlB,OACP,WAAWwB,KAAKvB,WAAW;QACzB,aAAamB;QACb,cAAcP;QACd,eAAeP;IACjB,KACC,GAAGC,WAAW,EAChB;QACC,CAACkB,OAA0BH,oBAAoBpB,UAAU;MAC3D,EAAEqB,KAAK;IACT,EAAE1B,cAAc;AAEpB;AAEA,SAAS6B,YAAYC,KAAuB;IAC1C,MAAM,EAAEzB,QAAQ,EAAED,SAAS,EAAED,KAAK,EAAE,GAAG2B;IACvC,MAAM,EAAEP,MAAM,EAAEP,OAAO,EAAEP,QAAQ,EAAE,GAAGsB,WAAW/B;IAEjD,QACG0B,KACC,OAAOvB,OACP,WAAWwB,KAAKvB,WAAW;QACzB,aAAamB;QACb,cAAcP;QACd,eAAeP;IACjB,IACD;MACC,CAACJ,SAAS;IACZ,EAAEqB;AAEN;AAEA,SAASM,YAAYF,KAAuB;IAC1C,MAAM,EAAEzB,QAAQ,EAAED,SAAS,EAAED,KAAK,EAAE,GAAG2B;IACvC,MAAM,EAAEP,MAAM,EAAEP,OAAO,EAAEP,QAAQ,EAAE,GAAGsB,WAAW/B;IAEjD,QACG0B,KACC,OAAOvB,OACP,WAAWwB,KAAKvB,WAAW;QACzB,aAAamB;QACb,cAAcP;QACd,eAAeP;IACjB,IACD;MACC,CAACJ,SAAS;IACZ,EAAEqB;AAEN"}
@@ -1,14 +1,8 @@
1
+ import { useTouchEmulation } from '@lynx-js/react-use';
1
2
  import type { TouchEvent } from '@lynx-js/types';
2
- interface UsePressTapReturnValue {
3
+ interface UsePressTapReturnValue extends ReturnType<typeof useTouchEmulation> {
3
4
  pressed: boolean;
4
5
  bindtap: (e: TouchEvent) => void;
5
- bindtouchstart: (e: TouchEvent) => void;
6
- bindtouchend: (e: TouchEvent) => void;
7
- bindtouchcancel: (e: TouchEvent) => void;
8
- }
9
- interface UsePressTapOptions {
10
- disabled?: boolean;
11
- onTap?: () => void;
12
6
  }
13
7
  /**
14
8
  * usePressTap
@@ -21,6 +15,9 @@ interface UsePressTapOptions {
21
15
  * - If the element becomes disabled during a press:
22
16
  * - The active state is cleared immediately.
23
17
  */
24
- export declare function usePressTap({ disabled, onTap }?: UsePressTapOptions): UsePressTapReturnValue;
18
+ export declare function usePressTap({ disabled, onTap }?: {
19
+ disabled?: boolean;
20
+ onTap?: () => void;
21
+ }): UsePressTapReturnValue;
25
22
  export {};
26
23
  //# sourceMappingURL=use-press-tap.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-press-tap.d.ts","sourceRoot":"","sources":["../src/use-press-tap.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAIhD,UAAU,sBAAsB;IAC9B,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,CAAC,CAAC,EAAE,UAAU,KAAK,IAAI,CAAA;IAChC,cAAc,EAAE,CAAC,CAAC,EAAE,UAAU,KAAK,IAAI,CAAA;IACvC,YAAY,EAAE,CAAC,CAAC,EAAE,UAAU,KAAK,IAAI,CAAA;IACrC,eAAe,EAAE,CAAC,CAAC,EAAE,UAAU,KAAK,IAAI,CAAA;CACzC;AAED,UAAU,kBAAkB;IAC1B,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,KAAK,CAAC,EAAE,MAAM,IAAI,CAAA;CACnB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CACzB,EAAE,QAAgB,EAAE,KAAK,EAAE,GAAE,kBAAuB,GACnD,sBAAsB,CAwBxB"}
1
+ {"version":3,"file":"use-press-tap.d.ts","sourceRoot":"","sources":["../src/use-press-tap.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAIhD,UAAU,sBAAuB,SAAQ,UAAU,CAAC,OAAO,iBAAiB,CAAC;IAC3E,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,CAAC,CAAC,EAAE,UAAU,KAAK,IAAI,CAAA;CACjC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CACzB,EAAE,QAAgB,EAAE,KAAK,EAAE,GAAE;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,IAAI,CAAA;CAAO,GAC3E,sBAAsB,CA4BxB"}
@@ -1,4 +1,5 @@
1
1
  import { useState } from "@lynx-js/react";
2
+ import { useTouchEmulation } from "@lynx-js/react-use";
2
3
  import { useEffectEvent } from "./use-effect-event.jsx";
3
4
  function usePressTap({ disabled = false, onTap } = {}) {
4
5
  const [pressed, setPressed] = useState(false);
@@ -13,12 +14,15 @@ function usePressTap({ disabled = false, onTap } = {}) {
13
14
  if (disabled) return;
14
15
  onTap?.();
15
16
  });
17
+ const touchHandlers = useTouchEmulation({
18
+ onTouchStart: press,
19
+ onTouchEnd: reset,
20
+ onTouchCancel: reset
21
+ });
16
22
  return {
17
23
  pressed,
18
24
  bindtap: handleTap,
19
- bindtouchstart: press,
20
- bindtouchend: reset,
21
- bindtouchcancel: reset
25
+ ...touchHandlers
22
26
  };
23
27
  }
24
28
  export { usePressTap };
@@ -1 +1 @@
1
- {"version":3,"file":"use-press-tap.jsx","sources":["webpack://@lynx-js/lynx-ui-switch/./src/use-press-tap.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useState } from '@lynx-js/react'\n\nimport type { TouchEvent } from '@lynx-js/types'\n\nimport { useEffectEvent } from './use-effect-event'\n\ninterface UsePressTapReturnValue {\n pressed: boolean\n bindtap: (e: TouchEvent) => void\n bindtouchstart: (e: TouchEvent) => void\n bindtouchend: (e: TouchEvent) => void\n bindtouchcancel: (e: TouchEvent) => void\n}\n\ninterface UsePressTapOptions {\n disabled?: boolean\n onTap?: () => void\n}\n\n/**\n * usePressTap\n *\n * Hook that provides press/tap interaction state similar to <button>.\n *\n * - When `disabled` is true:\n * - The element cannot become active.\n * - No tap will fire.\n * - If the element becomes disabled during a press:\n * - The active state is cleared immediately.\n */\nexport function usePressTap(\n { disabled = false, onTap }: UsePressTapOptions = {},\n): UsePressTapReturnValue {\n const [pressed, setPressed] = useState(false)\n\n const press = useEffectEvent(() => {\n if (disabled) return\n setPressed(true)\n })\n\n const reset = useEffectEvent(() => {\n setPressed(false)\n })\n\n const handleTap = useEffectEvent(() => {\n if (disabled) return\n onTap?.()\n })\n\n return {\n pressed,\n bindtap: handleTap,\n bindtouchstart: press,\n bindtouchend: reset,\n bindtouchcancel: reset,\n }\n}\n"],"names":["usePressTap","disabled","onTap","pressed","setPressed","useState","press","useEffectEvent","reset","handleTap"],"mappings":";;AAkCO,SAASA,YACd,EAAEC,WAAW,KAAK,EAAEC,KAAK,EAAsB,GAAG,CAAC,CAAC;IAEpD,MAAM,CAACC,SAASC,WAAW,GAAGC,SAAS;IAEvC,MAAMC,QAAQC,eAAe;QAC3B,IAAIN,UAAU;QACdG,WAAW;IACb;IAEA,MAAMI,QAAQD,eAAe;QAC3BH,WAAW;IACb;IAEA,MAAMK,YAAYF,eAAe;QAC/B,IAAIN,UAAU;QACdC;IACF;IAEA,OAAO;QACLC;QACA,SAASM;QACT,gBAAgBH;QAChB,cAAcE;QACd,iBAAiBA;IACnB;AACF"}
1
+ {"version":3,"file":"use-press-tap.jsx","sources":["webpack://@lynx-js/lynx-ui-switch/./src/use-press-tap.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useState } from '@lynx-js/react'\n\nimport { useTouchEmulation } from '@lynx-js/react-use'\nimport type { TouchEvent } from '@lynx-js/types'\n\nimport { useEffectEvent } from './use-effect-event'\n\ninterface UsePressTapReturnValue extends ReturnType<typeof useTouchEmulation> {\n pressed: boolean\n bindtap: (e: TouchEvent) => void\n}\n\n/**\n * usePressTap\n *\n * Hook that provides press/tap interaction state similar to <button>.\n *\n * - When `disabled` is true:\n * - The element cannot become active.\n * - No tap will fire.\n * - If the element becomes disabled during a press:\n * - The active state is cleared immediately.\n */\nexport function usePressTap(\n { disabled = false, onTap }: { disabled?: boolean, onTap?: () => void } = {},\n): UsePressTapReturnValue {\n const [pressed, setPressed] = useState(false)\n\n const press = useEffectEvent(() => {\n if (disabled) return\n setPressed(true)\n })\n\n const reset = useEffectEvent(() => {\n setPressed(false)\n })\n\n const handleTap = useEffectEvent(() => {\n if (disabled) return\n onTap?.()\n })\n\n const touchHandlers = useTouchEmulation({\n onTouchStart: press,\n onTouchEnd: reset,\n onTouchCancel: reset,\n })\n\n return {\n pressed,\n bindtap: handleTap,\n ...touchHandlers,\n }\n}\n"],"names":["usePressTap","disabled","onTap","pressed","setPressed","useState","press","useEffectEvent","reset","handleTap","touchHandlers","useTouchEmulation"],"mappings":";;;AA2BO,SAASA,YACd,EAAEC,WAAW,KAAK,EAAEC,KAAK,EAA8C,GAAG,CAAC,CAAC;IAE5E,MAAM,CAACC,SAASC,WAAW,GAAGC,SAAS;IAEvC,MAAMC,QAAQC,eAAe;QAC3B,IAAIN,UAAU;QACdG,WAAW;IACb;IAEA,MAAMI,QAAQD,eAAe;QAC3BH,WAAW;IACb;IAEA,MAAMK,YAAYF,eAAe;QAC/B,IAAIN,UAAU;QACdC;IACF;IAEA,MAAMQ,gBAAgBC,kBAAkB;QACtC,cAAcL;QACd,YAAYE;QACZ,eAAeA;IACjB;IAEA,OAAO;QACLL;QACA,SAASM;QACT,GAAGC,aAAa;IAClB;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/lynx-ui-switch",
3
- "version": "3.131.1",
3
+ "version": "3.133.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/lynx-family/lynx-ui.git",
@@ -25,11 +25,12 @@
25
25
  "dist/**"
26
26
  ],
27
27
  "dependencies": {
28
+ "@lynx-js/react-use": "0.1.3",
28
29
  "clsx": "^2.1.1",
29
- "@lynx-js/lynx-ui-common": "3.131.0"
30
+ "@lynx-js/lynx-ui-common": "3.133.1"
30
31
  },
31
32
  "devDependencies": {
32
- "@lynx-js/react": "0.113.0",
33
+ "@lynx-js/react": "0.114.0",
33
34
  "@lynx-js/types": "3.6.0",
34
35
  "@rslib/core": "^0.16.0",
35
36
  "@types/react": "^18.3.8",
package/src/switch.tsx CHANGED
@@ -48,8 +48,10 @@ function Switch({
48
48
  onChange?.(next)
49
49
  }
50
50
 
51
- const { pressed, bindtap, bindtouchcancel, bindtouchend, bindtouchstart } =
52
- usePressTap({ disabled, onTap: handleChange })
51
+ const { pressed, bindtap, ...touchHandlers } = usePressTap({
52
+ disabled,
53
+ onTap: handleChange,
54
+ })
53
55
 
54
56
  const active = useMemo(() => pressed && !disabled, [pressed, disabled])
55
57
 
@@ -63,9 +65,7 @@ function Switch({
63
65
  <SwitchContext.Provider value={switchContextValue}>
64
66
  <view
65
67
  bindtap={bindtap}
66
- bindtouchstart={bindtouchstart}
67
- bindtouchend={bindtouchend}
68
- bindtouchcancel={bindtouchcancel}
68
+ {...touchHandlers}
69
69
  event-through={false}
70
70
  style={style}
71
71
  className={clsx(className, {
@@ -4,21 +4,14 @@
4
4
 
5
5
  import { useState } from '@lynx-js/react'
6
6
 
7
+ import { useTouchEmulation } from '@lynx-js/react-use'
7
8
  import type { TouchEvent } from '@lynx-js/types'
8
9
 
9
10
  import { useEffectEvent } from './use-effect-event'
10
11
 
11
- interface UsePressTapReturnValue {
12
+ interface UsePressTapReturnValue extends ReturnType<typeof useTouchEmulation> {
12
13
  pressed: boolean
13
14
  bindtap: (e: TouchEvent) => void
14
- bindtouchstart: (e: TouchEvent) => void
15
- bindtouchend: (e: TouchEvent) => void
16
- bindtouchcancel: (e: TouchEvent) => void
17
- }
18
-
19
- interface UsePressTapOptions {
20
- disabled?: boolean
21
- onTap?: () => void
22
15
  }
23
16
 
24
17
  /**
@@ -33,7 +26,7 @@ interface UsePressTapOptions {
33
26
  * - The active state is cleared immediately.
34
27
  */
35
28
  export function usePressTap(
36
- { disabled = false, onTap }: UsePressTapOptions = {},
29
+ { disabled = false, onTap }: { disabled?: boolean, onTap?: () => void } = {},
37
30
  ): UsePressTapReturnValue {
38
31
  const [pressed, setPressed] = useState(false)
39
32
 
@@ -51,11 +44,15 @@ export function usePressTap(
51
44
  onTap?.()
52
45
  })
53
46
 
47
+ const touchHandlers = useTouchEmulation({
48
+ onTouchStart: press,
49
+ onTouchEnd: reset,
50
+ onTouchCancel: reset,
51
+ })
52
+
54
53
  return {
55
54
  pressed,
56
55
  bindtap: handleTap,
57
- bindtouchstart: press,
58
- bindtouchend: reset,
59
- bindtouchcancel: reset,
56
+ ...touchHandlers,
60
57
  }
61
58
  }