@lynx-js/lynx-ui-switch 0.0.0

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # @lynx-js/lynx-ui-switch
2
+
3
+ A headless Switch (toggle) component for ReactLynx. It provides unstyled primitives for toggle states.
4
+
5
+ ## Installation
6
+
7
+ We strongly recommend installing and using this component through the main `@lynx-js/lynx-ui` package:
8
+
9
+ ```bash
10
+ # pnpm (recommended)
11
+ pnpm add @lynx-js/lynx-ui
12
+
13
+ # npm
14
+ npm install @lynx-js/lynx-ui
15
+
16
+ # yarn
17
+ yarn add @lynx-js/lynx-ui
18
+ ```
19
+
20
+ _(If necessary, you can still install the standalone package via `pnpm add @lynx-js/lynx-ui-switch`)_
21
+
22
+ ## Usage
23
+
24
+ The `lynx-ui-switch` follows a headless composition pattern.
25
+
26
+ [View Full Examples](https://github.com/lynx-family/lynx-ui/tree/main/apps/examples/Switch)
27
+
28
+ ## Component Structure
29
+
30
+ The `Switch` component is composed of the following sub-components:
31
+
32
+ ```tsx
33
+ <Switch>
34
+ <SwitchTrack />
35
+ <SwitchThumb />
36
+ </Switch>
37
+ ```
38
+
39
+ - **`Switch`**: The root container that manages the toggle state.
40
+ - **`SwitchTrack`**: The track/rail of the switch.
41
+ - **`SwitchThumb`**: The sliding thumb indicator.
42
+
43
+ ## About @lynx-js/lynx-ui
44
+
45
+ This component is part of `@lynx-js/lynx-ui`, a headless UI library officially maintained by the Lynx team, provided as a reference for building flexible, universal, and high-performance ReactLynx components.
46
+
47
+ ## License
48
+
49
+ [**lynx-ui**](https://github.com/lynx-family/lynx-ui) is [**Apache License 2.0**](./LICENSE) licensed.
@@ -0,0 +1,3 @@
1
+ export { Switch, SwitchThumb, SwitchTrack } from './switch';
2
+ export type { SwitchProps, SwitchThumbProps, SwitchTrackProps, SwitchRenderProps, } from './types';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAC3D,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,SAAS,CAAA"}
package/dist/index.jsx ADDED
@@ -0,0 +1,2 @@
1
+ import { Switch, SwitchThumb, SwitchTrack } from "./switch.jsx";
2
+ export { Switch, SwitchThumb, SwitchTrack };
@@ -0,0 +1,4 @@
1
+ import type { ReactNode } from '@lynx-js/react';
2
+ declare function render<T>(api: T, children: ((api: T) => ReactNode) | ReactNode): ReactNode;
3
+ export { render };
4
+ //# sourceMappingURL=render.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C,iBAAS,MAAM,CAAC,CAAC,EACf,GAAG,EAAE,CAAC,EACN,QAAQ,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,KAAK,SAAS,CAAC,GAAG,SAAS,GAC5C,SAAS,CAIX;AAED,OAAO,EAAE,MAAM,EAAE,CAAA"}
@@ -0,0 +1,6 @@
1
+ function render(api, children) {
2
+ return 'function' == typeof children ? children(api) : children;
3
+ }
4
+ export { render };
5
+
6
+ //# sourceMappingURL=render.jsx.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render.jsx","sources":["webpack://@lynx-js/lynx-ui-switch/./src/render.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 type { ReactNode } from '@lynx-js/react'\n\nfunction render<T>(\n api: T,\n children: ((api: T) => ReactNode) | ReactNode,\n): ReactNode {\n return typeof children === 'function'\n ? (children as (api: T) => ReactNode)(api)\n : children\n}\n\nexport { render }\n"],"names":["render","api","children"],"mappings":"AAMA,SAASA,OACPC,GAAM,EACNC,QAA6C;IAE7C,OAAO,AAAoB,cAApB,OAAOA,WACTA,SAAmCD,OACpCC;AACN"}
@@ -0,0 +1,7 @@
1
+ import type { ReactNode } from '@lynx-js/react';
2
+ import type { SwitchProps, SwitchThumbProps, SwitchTrackProps } from './types';
3
+ declare function Switch({ style, className, children, onChange, checked: checkedProp, defaultChecked, disabled, switchProps, }: SwitchProps): ReactNode;
4
+ declare function SwitchThumb(props: SwitchThumbProps): ReactNode;
5
+ declare function SwitchTrack(props: SwitchTrackProps): ReactNode;
6
+ export { Switch, SwitchThumb, SwitchTrack };
7
+ //# sourceMappingURL=switch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../src/switch.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAK/C,OAAO,KAAK,EACV,WAAW,EAEX,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,SAAS,CAAA;AAShB,iBAAS,MAAM,CAAC,EACd,KAAK,EACL,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,OAAO,EAAE,WAAW,EACpB,cAAsB,EACtB,QAAgB,EAChB,WAAW,GACZ,EAAE,WAAW,GAAG,SAAS,CAgDzB;AAED,iBAAS,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,SAAS,CAgBvD;AAED,iBAAS,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,SAAS,CAgBvD;AAED,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,CAAA"}
@@ -0,0 +1,70 @@
1
+ import { createContext, useContext, useMemo, useState } from "@lynx-js/react";
2
+ import { clsx } from "clsx";
3
+ import { render } from "./render.jsx";
4
+ import { usePressTap } from "./use-press-tap.jsx";
5
+ const SwitchContext = createContext({
6
+ checked: false,
7
+ disabled: false,
8
+ active: false
9
+ });
10
+ function Switch({ style, className, children, onChange, checked: checkedProp, defaultChecked = false, disabled = false, switchProps }) {
11
+ const isControlled = void 0 !== checkedProp;
12
+ const [uncontrolledChecked, setUncontrolledChecked] = useState(defaultChecked);
13
+ const checked = isControlled ? checkedProp : uncontrolledChecked;
14
+ const handleChange = ()=>{
15
+ const next = !checked;
16
+ if (!isControlled) setUncontrolledChecked(next);
17
+ onChange?.(next);
18
+ };
19
+ const { pressed, bindtap, bindtouchcancel, bindtouchend, bindtouchstart } = usePressTap({
20
+ disabled,
21
+ onTap: handleChange
22
+ });
23
+ const active = useMemo(()=>pressed && !disabled, [
24
+ pressed,
25
+ disabled
26
+ ]);
27
+ const switchContextValue = useMemo(()=>({
28
+ active,
29
+ checked,
30
+ disabled
31
+ }), [
32
+ active,
33
+ checked,
34
+ disabled
35
+ ]);
36
+ return <SwitchContext.Provider value={switchContextValue}>
37
+ <view bindtap={bindtap} bindtouchstart={bindtouchstart} bindtouchend={bindtouchend} bindtouchcancel={bindtouchcancel} event-through={false} style={style} className={clsx(className, {
38
+ 'ui-active': active,
39
+ 'ui-checked': checked,
40
+ 'ui-disabled': disabled
41
+ })} {...switchProps}>
42
+ {render(switchContextValue, children)}
43
+ </view>
44
+ </SwitchContext.Provider>;
45
+ }
46
+ function SwitchThumb(props) {
47
+ const { children, className, style } = props;
48
+ const { active, checked, disabled } = useContext(SwitchContext);
49
+ return <view style={style} className={clsx(className, {
50
+ 'ui-active': active,
51
+ 'ui-checked': checked,
52
+ 'ui-disabled': disabled
53
+ })}>
54
+ {children}
55
+ </view>;
56
+ }
57
+ function SwitchTrack(props) {
58
+ const { children, className, style } = props;
59
+ const { active, checked, disabled } = useContext(SwitchContext);
60
+ return <view style={style} className={clsx(className, {
61
+ 'ui-active': active,
62
+ 'ui-checked': checked,
63
+ 'ui-disabled': disabled
64
+ })}>
65
+ {children}
66
+ </view>;
67
+ }
68
+ export { Switch, SwitchThumb, SwitchTrack };
69
+
70
+ //# sourceMappingURL=switch.jsx.map
@@ -0,0 +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"}
@@ -0,0 +1,2 @@
1
+ export type * from './index.docs';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAIA,mBAAmB,cAAc,CAAA"}
@@ -0,0 +1,131 @@
1
+ import type { ReactNode } from '@lynx-js/react';
2
+ import type { CSSProperties, ViewProps } from '@lynx-js/types';
3
+ interface ComponentBasicProps {
4
+ /**
5
+ * className
6
+ * @zh 类名
7
+ * @Android
8
+ * @iOS
9
+ */
10
+ className?: string;
11
+ /**
12
+ * style
13
+ * @zh 样式
14
+ * @Android
15
+ * @iOS
16
+ */
17
+ style?: CSSProperties;
18
+ }
19
+ /** Props for the Switch component.
20
+ * @zh Switch 组件的属性
21
+ */
22
+ export interface SwitchProps extends ComponentBasicProps {
23
+ /**
24
+ * Whether the Switch is checked.
25
+ * If `checked` is provided, the component is controlled and `defaultChecked` is ignored.
26
+ * @zh 是否选中。如果设置了 `checked`,则 Switch 将处于受控模式,此时 `defaultChecked` 将被忽略。
27
+ * @iOS
28
+ * @Android
29
+ */
30
+ checked?: boolean;
31
+ /**
32
+ * The initial checked state when used as an uncontrolled component.
33
+ * @defaultValue false
34
+ * @zh 默认选中状态,仅在非受控模式下生效。
35
+ * @iOS
36
+ * @Android
37
+ */
38
+ defaultChecked?: boolean;
39
+ /**
40
+ * Whether the user can interact with the Switch.
41
+ * When `true`, the Switch cannot be toggled.
42
+ * @defaultValue false
43
+ * @zh 是否禁用,当为 `true` 时用户无法操作。
44
+ * @Android
45
+ * @iOS
46
+ */
47
+ disabled?: boolean;
48
+ /**
49
+ * Callback fired when the checked state changes.
50
+ * @param checked - The new checked state.
51
+ * @zh 状态变化时触发的回调函数。参数 `checked` 表示最新的选中状态。
52
+ * @Android
53
+ * @iOS
54
+ */
55
+ onChange?: (checked: boolean) => void;
56
+ /**
57
+ * Switch supports original view props to be directly spread in this prop.
58
+ * @Android
59
+ * @iOS
60
+ * @zh Switch 支持将原始视图属性直接展开到这个属性中。
61
+ */
62
+ switchProps?: ViewProps;
63
+ /**
64
+ * Content displayed inside or next to the Switch.
65
+ * @zh Switch 的内容,可以是节点或根据状态动态渲染的函数。
66
+ * @docTypeFallback ReactNode | ((status: { checked: boolean; active: boolean; disabled: boolean }) => ReactNode)
67
+ * @iOS
68
+ * @Android
69
+ */
70
+ children?: ReactNode | ((status: SwitchRenderProps) => ReactNode);
71
+ }
72
+ /** Render state props passed to custom Switch content.
73
+ * @zh Switch 自定义渲染的状态属性
74
+ */
75
+ export interface SwitchRenderProps {
76
+ /**
77
+ * Whether the Switch is currently checked.
78
+ * @zh 是否选中。
79
+ * @iOS
80
+ * @Android
81
+ */
82
+ checked: boolean;
83
+ /**
84
+ * Whether the Switch is active (pressed or being interacted with).
85
+ * @zh 是否处于激活状态(按下或交互中)。
86
+ * @iOS
87
+ * @Android
88
+ */
89
+ active: boolean;
90
+ /**
91
+ * Whether the Switch is disabled.
92
+ * @zh 是否禁用。
93
+ * @iOS
94
+ * @Android
95
+ */
96
+ disabled: boolean;
97
+ }
98
+ /**
99
+ * The thumb of the Switch.
100
+ * The draggable/clickable handle that toggles the state.
101
+ * @zh Switch 的手柄部分。用于点击或拖动以切换状态。
102
+ */
103
+ export interface SwitchThumbProps extends ComponentBasicProps {
104
+ /**
105
+ * Content slot of the thumb.
106
+ * Allows custom elements such as icons.
107
+ * @zh 手柄的内容插槽,可放置自定义元素(如图标)。
108
+ * @iOS
109
+ * @Android
110
+ */
111
+ children?: ReactNode;
112
+ }
113
+ /**
114
+ * The track of the Switch.
115
+ * Represents the background area on which the thumb slides.
116
+ * Can also be used for pressed overlays or state decorations.
117
+ *
118
+ * @zh Switch 的轨道部分。拇指滑动的背景区域,也可用于绘制按下态覆盖层或状态装饰。
119
+ */
120
+ export interface SwitchTrackProps extends ComponentBasicProps {
121
+ /**
122
+ * Content slot of the track.
123
+ * Allows custom elements, can be used to add overlays or state decorations.
124
+ * @zh 轨道的内容插槽,可用于添加覆盖层或状态装饰。
125
+ * @iOS
126
+ * @Android
127
+ */
128
+ children?: ReactNode;
129
+ }
130
+ export {};
131
+ //# sourceMappingURL=index.docs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.docs.d.ts","sourceRoot":"","sources":["../../src/types/index.docs.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE9D,UAAU,mBAAmB;IAC3B;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,aAAa,CAAA;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,WAAY,SAAQ,mBAAmB;IACtD;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,OAAO,CAAA;IAExB;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;IAElB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAA;IAErC;;;;;OAKG;IACH,WAAW,CAAC,EAAE,SAAS,CAAA;IAEvB;;;;;;OAMG;IACH,QAAQ,CAAC,EACL,SAAS,GACT,CAAC,CACD,MAAM,EAAE,iBAAiB,KACtB,SAAS,CAAC,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;OAKG;IACH,OAAO,EAAE,OAAO,CAAA;IAEhB;;;;;OAKG;IACH,MAAM,EAAE,OAAO,CAAA;IAEf;;;;;OAKG;IACH,QAAQ,EAAE,OAAO,CAAA;CAClB;AAED;;;;GAIG;AACH,MAAM,WAAW,gBAAiB,SAAQ,mBAAmB;IAC3D;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAA;CACrB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,gBAAiB,SAAQ,mBAAmB;IAC3D;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAA;CACrB"}
File without changes
File without changes
@@ -0,0 +1,13 @@
1
+ /**
2
+ * useEffectEvent
3
+ *
4
+ * - Internally stores the latest `fn` in a ref, updated via useEffect.
5
+ * - Returns a memoized proxy function that delegates calls to `ref.current`.
6
+ * - Useful when extracting a non-reactive function dependency out of the reactive Effect around it.
7
+ *
8
+ * @param fn The callback function to be stabilized.
9
+ * @returns A stable function that always invokes the latest `fn`.
10
+ */
11
+ declare function useEffectEvent<TArgs extends unknown[]>(fn: ((...args: TArgs) => void) | undefined): (...args: TArgs) => void;
12
+ export { useEffectEvent };
13
+ //# sourceMappingURL=use-effect-event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-effect-event.d.ts","sourceRoot":"","sources":["../src/use-effect-event.ts"],"names":[],"mappings":"AAMA;;;;;;;;;GASG;AAEH,iBAAS,cAAc,CAAC,KAAK,SAAS,OAAO,EAAE,EAC7C,EAAE,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,IAAI,CAAC,GAAG,SAAS,GACzC,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,IAAI,CAiB1B;AAED,OAAO,EAAE,cAAc,EAAE,CAAA"}
@@ -0,0 +1,18 @@
1
+ import { useEffect, useMemo, useRef } from "@lynx-js/react";
2
+ function useEffectEvent(fn) {
3
+ const ref = useRef(fn);
4
+ useEffect(()=>{
5
+ ref.current = fn;
6
+ }, [
7
+ fn
8
+ ]);
9
+ return useMemo(()=>{
10
+ const proxy = (...args)=>{
11
+ ref.current?.(...args);
12
+ };
13
+ return proxy;
14
+ }, []);
15
+ }
16
+ export { useEffectEvent };
17
+
18
+ //# sourceMappingURL=use-effect-event.jsx.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-effect-event.jsx","sources":["webpack://@lynx-js/lynx-ui-switch/./src/use-effect-event.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 { useEffect, useMemo, useRef } from '@lynx-js/react'\n\n/**\n * useEffectEvent\n *\n * - Internally stores the latest `fn` in a ref, updated via useEffect.\n * - Returns a memoized proxy function that delegates calls to `ref.current`.\n * - Useful when extracting a non-reactive function dependency out of the reactive Effect around it.\n *\n * @param fn The callback function to be stabilized.\n * @returns A stable function that always invokes the latest `fn`.\n */\n\nfunction useEffectEvent<TArgs extends unknown[]>(\n fn: ((...args: TArgs) => void) | undefined,\n): (...args: TArgs) => void {\n const ref = useRef(fn)\n\n useEffect(() => {\n ref.current = fn\n }, [fn])\n\n // Stable proxy that delegates to the latest ref.\n // typed as F so calls are type-safe (no \"unsafe call\" rule).\n // useMemo is clearer here: to memoize a proxy function as a value,\n // not define an inline handler as with useCallback.\n return useMemo(() => {\n const proxy = (...args: TArgs) => {\n ref.current?.(...args)\n }\n return proxy\n }, [])\n}\n\nexport { useEffectEvent }\n"],"names":["useEffectEvent","fn","ref","useRef","useEffect","useMemo","proxy","args"],"mappings":";AAiBA,SAASA,eACPC,EAA0C;IAE1C,MAAMC,MAAMC,OAAOF;IAEnBG,UAAU;QACRF,IAAI,OAAO,GAAGD;IAChB,GAAG;QAACA;KAAG;IAMP,OAAOI,QAAQ;QACb,MAAMC,QAAQ,CAAC,GAAGC;YAChBL,IAAI,OAAO,MAAMK;QACnB;QACA,OAAOD;IACT,GAAG,EAAE;AACP"}
@@ -0,0 +1,26 @@
1
+ import type { TouchEvent } from '@lynx-js/types';
2
+ interface UsePressTapReturnValue {
3
+ pressed: boolean;
4
+ 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
+ }
13
+ /**
14
+ * usePressTap
15
+ *
16
+ * Hook that provides press/tap interaction state similar to <button>.
17
+ *
18
+ * - When `disabled` is true:
19
+ * - The element cannot become active.
20
+ * - No tap will fire.
21
+ * - If the element becomes disabled during a press:
22
+ * - The active state is cleared immediately.
23
+ */
24
+ export declare function usePressTap({ disabled, onTap }?: UsePressTapOptions): UsePressTapReturnValue;
25
+ export {};
26
+ //# sourceMappingURL=use-press-tap.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,26 @@
1
+ import { useState } from "@lynx-js/react";
2
+ import { useEffectEvent } from "./use-effect-event.jsx";
3
+ function usePressTap({ disabled = false, onTap } = {}) {
4
+ const [pressed, setPressed] = useState(false);
5
+ const press = useEffectEvent(()=>{
6
+ if (disabled) return;
7
+ setPressed(true);
8
+ });
9
+ const reset = useEffectEvent(()=>{
10
+ setPressed(false);
11
+ });
12
+ const handleTap = useEffectEvent(()=>{
13
+ if (disabled) return;
14
+ onTap?.();
15
+ });
16
+ return {
17
+ pressed,
18
+ bindtap: handleTap,
19
+ bindtouchstart: press,
20
+ bindtouchend: reset,
21
+ bindtouchcancel: reset
22
+ };
23
+ }
24
+ export { usePressTap };
25
+
26
+ //# sourceMappingURL=use-press-tap.jsx.map
@@ -0,0 +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"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@lynx-js/lynx-ui-switch",
3
+ "version": "0.0.0",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/lynx-family/lynx-ui.git",
7
+ "directory": "packages/lynx-ui-switch"
8
+ },
9
+ "license": "Apache-2.0",
10
+ "author": {
11
+ "name": "The Lynx Authors"
12
+ },
13
+ "sideEffects": false,
14
+ "type": "module",
15
+ "main": "./dist/index.jsx",
16
+ "module": "./dist/index.jsx",
17
+ "types": "./dist/index.d.ts",
18
+ "files": [
19
+ "src/**",
20
+ "README.md",
21
+ "CHANGELOG.md",
22
+ "LICENSE",
23
+ "tsconfig.docs.json",
24
+ "tsconfig.json",
25
+ "dist/**"
26
+ ],
27
+ "scripts": {
28
+ "build": "rslib build",
29
+ "build:watch": "rslib build --watch"
30
+ },
31
+ "dependencies": {
32
+ "clsx": "catalog:"
33
+ },
34
+ "devDependencies": {
35
+ "@lynx-js/react": "catalog:",
36
+ "@lynx-js/types": "catalog:",
37
+ "@rslib/core": "catalog:",
38
+ "@types/react": "catalog:",
39
+ "typescript": "catalog:"
40
+ },
41
+ "peerDependencies": {
42
+ "@lynx-js/react": ">=0.100.0",
43
+ "@lynx-js/types": "*",
44
+ "@types/react": "^18"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "jsnext:source": "./src/index.ts"
50
+ }
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ export { Switch, SwitchThumb, SwitchTrack } from './switch'
6
+ export type {
7
+ SwitchProps,
8
+ SwitchThumbProps,
9
+ SwitchTrackProps,
10
+ SwitchRenderProps,
11
+ } from './types'
package/src/render.ts ADDED
@@ -0,0 +1,16 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import type { ReactNode } from '@lynx-js/react'
6
+
7
+ function render<T>(
8
+ api: T,
9
+ children: ((api: T) => ReactNode) | ReactNode,
10
+ ): ReactNode {
11
+ return typeof children === 'function'
12
+ ? (children as (api: T) => ReactNode)(api)
13
+ : children
14
+ }
15
+
16
+ export { render }
package/src/switch.tsx ADDED
@@ -0,0 +1,120 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { createContext, useContext, useMemo, useState } from '@lynx-js/react'
6
+ import type { ReactNode } from '@lynx-js/react'
7
+
8
+ import { clsx } from 'clsx'
9
+
10
+ import { render } from './render'
11
+ import type {
12
+ SwitchProps,
13
+ SwitchRenderProps,
14
+ SwitchThumbProps,
15
+ SwitchTrackProps,
16
+ } from './types'
17
+ import { usePressTap } from './use-press-tap'
18
+
19
+ const SwitchContext = createContext<SwitchRenderProps>({
20
+ checked: false,
21
+ disabled: false,
22
+ active: false,
23
+ })
24
+
25
+ function Switch({
26
+ style,
27
+ className,
28
+ children,
29
+ onChange,
30
+ checked: checkedProp,
31
+ defaultChecked = false,
32
+ disabled = false,
33
+ switchProps,
34
+ }: SwitchProps): ReactNode {
35
+ const isControlled = checkedProp !== undefined
36
+
37
+ const [uncontrolledChecked, setUncontrolledChecked] = useState<boolean>(
38
+ defaultChecked,
39
+ )
40
+
41
+ const checked = isControlled ? checkedProp : uncontrolledChecked
42
+
43
+ const handleChange = () => {
44
+ const next = !checked
45
+ if (!isControlled) {
46
+ setUncontrolledChecked(next)
47
+ }
48
+ onChange?.(next)
49
+ }
50
+
51
+ const { pressed, bindtap, bindtouchcancel, bindtouchend, bindtouchstart } =
52
+ usePressTap({ disabled, onTap: handleChange })
53
+
54
+ const active = useMemo(() => pressed && !disabled, [pressed, disabled])
55
+
56
+ const switchContextValue: SwitchRenderProps = useMemo(() => ({
57
+ active,
58
+ checked,
59
+ disabled,
60
+ }), [active, checked, disabled])
61
+
62
+ return (
63
+ <SwitchContext.Provider value={switchContextValue}>
64
+ <view
65
+ bindtap={bindtap}
66
+ bindtouchstart={bindtouchstart}
67
+ bindtouchend={bindtouchend}
68
+ bindtouchcancel={bindtouchcancel}
69
+ event-through={false}
70
+ style={style}
71
+ className={clsx(className, {
72
+ 'ui-active': active,
73
+ 'ui-checked': checked,
74
+ 'ui-disabled': disabled,
75
+ })}
76
+ {...switchProps}
77
+ >
78
+ {render<SwitchRenderProps>(switchContextValue, children)}
79
+ </view>
80
+ </SwitchContext.Provider>
81
+ )
82
+ }
83
+
84
+ function SwitchThumb(props: SwitchThumbProps): ReactNode {
85
+ const { children, className, style } = props
86
+ const { active, checked, disabled } = useContext(SwitchContext)
87
+
88
+ return (
89
+ <view
90
+ style={style}
91
+ className={clsx(className, {
92
+ 'ui-active': active,
93
+ 'ui-checked': checked,
94
+ 'ui-disabled': disabled,
95
+ })}
96
+ >
97
+ {children}
98
+ </view>
99
+ )
100
+ }
101
+
102
+ function SwitchTrack(props: SwitchTrackProps): ReactNode {
103
+ const { children, className, style } = props
104
+ const { active, checked, disabled } = useContext(SwitchContext)
105
+
106
+ return (
107
+ <view
108
+ style={style}
109
+ className={clsx(className, {
110
+ 'ui-active': active,
111
+ 'ui-checked': checked,
112
+ 'ui-disabled': disabled,
113
+ })}
114
+ >
115
+ {children}
116
+ </view>
117
+ )
118
+ }
119
+
120
+ export { Switch, SwitchThumb, SwitchTrack }
@@ -0,0 +1,149 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import type { ReactNode } from '@lynx-js/react'
6
+
7
+ import type { CSSProperties, ViewProps } from '@lynx-js/types'
8
+
9
+ interface ComponentBasicProps {
10
+ /**
11
+ * className
12
+ * @zh 类名
13
+ * @Android
14
+ * @iOS
15
+ */
16
+ className?: string
17
+ /**
18
+ * style
19
+ * @zh 样式
20
+ * @Android
21
+ * @iOS
22
+ */
23
+ style?: CSSProperties
24
+ }
25
+
26
+ /** Props for the Switch component.
27
+ * @zh Switch 组件的属性
28
+ */
29
+ export interface SwitchProps extends ComponentBasicProps {
30
+ /**
31
+ * Whether the Switch is checked.
32
+ * If `checked` is provided, the component is controlled and `defaultChecked` is ignored.
33
+ * @zh 是否选中。如果设置了 `checked`,则 Switch 将处于受控模式,此时 `defaultChecked` 将被忽略。
34
+ * @iOS
35
+ * @Android
36
+ */
37
+ checked?: boolean
38
+ /**
39
+ * The initial checked state when used as an uncontrolled component.
40
+ * @defaultValue false
41
+ * @zh 默认选中状态,仅在非受控模式下生效。
42
+ * @iOS
43
+ * @Android
44
+ */
45
+ defaultChecked?: boolean
46
+
47
+ /**
48
+ * Whether the user can interact with the Switch.
49
+ * When `true`, the Switch cannot be toggled.
50
+ * @defaultValue false
51
+ * @zh 是否禁用,当为 `true` 时用户无法操作。
52
+ * @Android
53
+ * @iOS
54
+ */
55
+ disabled?: boolean
56
+
57
+ /**
58
+ * Callback fired when the checked state changes.
59
+ * @param checked - The new checked state.
60
+ * @zh 状态变化时触发的回调函数。参数 `checked` 表示最新的选中状态。
61
+ * @Android
62
+ * @iOS
63
+ */
64
+ onChange?: (checked: boolean) => void
65
+
66
+ /**
67
+ * Switch supports original view props to be directly spread in this prop.
68
+ * @Android
69
+ * @iOS
70
+ * @zh Switch 支持将原始视图属性直接展开到这个属性中。
71
+ */
72
+ switchProps?: ViewProps
73
+
74
+ /**
75
+ * Content displayed inside or next to the Switch.
76
+ * @zh Switch 的内容,可以是节点或根据状态动态渲染的函数。
77
+ * @docTypeFallback ReactNode | ((status: { checked: boolean; active: boolean; disabled: boolean }) => ReactNode)
78
+ * @iOS
79
+ * @Android
80
+ */
81
+ children?:
82
+ | ReactNode
83
+ | ((
84
+ status: SwitchRenderProps,
85
+ ) => ReactNode)
86
+ }
87
+
88
+ /** Render state props passed to custom Switch content.
89
+ * @zh Switch 自定义渲染的状态属性
90
+ */
91
+ export interface SwitchRenderProps {
92
+ /**
93
+ * Whether the Switch is currently checked.
94
+ * @zh 是否选中。
95
+ * @iOS
96
+ * @Android
97
+ */
98
+ checked: boolean
99
+
100
+ /**
101
+ * Whether the Switch is active (pressed or being interacted with).
102
+ * @zh 是否处于激活状态(按下或交互中)。
103
+ * @iOS
104
+ * @Android
105
+ */
106
+ active: boolean
107
+
108
+ /**
109
+ * Whether the Switch is disabled.
110
+ * @zh 是否禁用。
111
+ * @iOS
112
+ * @Android
113
+ */
114
+ disabled: boolean
115
+ }
116
+
117
+ /**
118
+ * The thumb of the Switch.
119
+ * The draggable/clickable handle that toggles the state.
120
+ * @zh Switch 的手柄部分。用于点击或拖动以切换状态。
121
+ */
122
+ export interface SwitchThumbProps extends ComponentBasicProps {
123
+ /**
124
+ * Content slot of the thumb.
125
+ * Allows custom elements such as icons.
126
+ * @zh 手柄的内容插槽,可放置自定义元素(如图标)。
127
+ * @iOS
128
+ * @Android
129
+ */
130
+ children?: ReactNode
131
+ }
132
+
133
+ /**
134
+ * The track of the Switch.
135
+ * Represents the background area on which the thumb slides.
136
+ * Can also be used for pressed overlays or state decorations.
137
+ *
138
+ * @zh Switch 的轨道部分。拇指滑动的背景区域,也可用于绘制按下态覆盖层或状态装饰。
139
+ */
140
+ export interface SwitchTrackProps extends ComponentBasicProps {
141
+ /**
142
+ * Content slot of the track.
143
+ * Allows custom elements, can be used to add overlays or state decorations.
144
+ * @zh 轨道的内容插槽,可用于添加覆盖层或状态装饰。
145
+ * @iOS
146
+ * @Android
147
+ */
148
+ children?: ReactNode
149
+ }
@@ -0,0 +1,5 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ export type * from './index.docs'
@@ -0,0 +1,39 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { useEffect, useMemo, useRef } from '@lynx-js/react'
6
+
7
+ /**
8
+ * useEffectEvent
9
+ *
10
+ * - Internally stores the latest `fn` in a ref, updated via useEffect.
11
+ * - Returns a memoized proxy function that delegates calls to `ref.current`.
12
+ * - Useful when extracting a non-reactive function dependency out of the reactive Effect around it.
13
+ *
14
+ * @param fn The callback function to be stabilized.
15
+ * @returns A stable function that always invokes the latest `fn`.
16
+ */
17
+
18
+ function useEffectEvent<TArgs extends unknown[]>(
19
+ fn: ((...args: TArgs) => void) | undefined,
20
+ ): (...args: TArgs) => void {
21
+ const ref = useRef(fn)
22
+
23
+ useEffect(() => {
24
+ ref.current = fn
25
+ }, [fn])
26
+
27
+ // Stable proxy that delegates to the latest ref.
28
+ // typed as F so calls are type-safe (no "unsafe call" rule).
29
+ // useMemo is clearer here: to memoize a proxy function as a value,
30
+ // not define an inline handler as with useCallback.
31
+ return useMemo(() => {
32
+ const proxy = (...args: TArgs) => {
33
+ ref.current?.(...args)
34
+ }
35
+ return proxy
36
+ }, [])
37
+ }
38
+
39
+ export { useEffectEvent }
@@ -0,0 +1,61 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { useState } from '@lynx-js/react'
6
+
7
+ import type { TouchEvent } from '@lynx-js/types'
8
+
9
+ import { useEffectEvent } from './use-effect-event'
10
+
11
+ interface UsePressTapReturnValue {
12
+ pressed: boolean
13
+ 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
+ }
23
+
24
+ /**
25
+ * usePressTap
26
+ *
27
+ * Hook that provides press/tap interaction state similar to <button>.
28
+ *
29
+ * - When `disabled` is true:
30
+ * - The element cannot become active.
31
+ * - No tap will fire.
32
+ * - If the element becomes disabled during a press:
33
+ * - The active state is cleared immediately.
34
+ */
35
+ export function usePressTap(
36
+ { disabled = false, onTap }: UsePressTapOptions = {},
37
+ ): UsePressTapReturnValue {
38
+ const [pressed, setPressed] = useState(false)
39
+
40
+ const press = useEffectEvent(() => {
41
+ if (disabled) return
42
+ setPressed(true)
43
+ })
44
+
45
+ const reset = useEffectEvent(() => {
46
+ setPressed(false)
47
+ })
48
+
49
+ const handleTap = useEffectEvent(() => {
50
+ if (disabled) return
51
+ onTap?.()
52
+ })
53
+
54
+ return {
55
+ pressed,
56
+ bindtap: handleTap,
57
+ bindtouchstart: press,
58
+ bindtouchend: reset,
59
+ bindtouchcancel: reset,
60
+ }
61
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tools/configs/tsconfig.docs.json",
3
+ "include": [
4
+ "src",
5
+ "dist/types/index.docs.d.ts"
6
+ ]
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tools/configs/tsconfig.lib.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ },
6
+ "include": [
7
+ "src",
8
+ ],
9
+ }