@xsolla/xui-switch 0.149.1 → 0.151.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.
Files changed (2) hide show
  1. package/README.md +58 -211
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,257 +1,104 @@
1
1
  # Switch
2
2
 
3
- A cross-platform React switch component for toggling between on and off states. Provides a visual alternative to checkboxes for boolean settings.
3
+ A cross-platform toggle for binary on/off settings; a visual alternative to a checkbox.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
8
  npm install @xsolla/xui-switch
9
- # or
10
- yarn add @xsolla/xui-switch
11
9
  ```
12
10
 
13
- ## Demo
14
-
15
- ### Basic Switch
11
+ ## Imports
16
12
 
17
13
  ```tsx
18
- import * as React from 'react';
19
14
  import { Switch } from '@xsolla/xui-switch';
20
-
21
- export default function BasicSwitch() {
22
- const [enabled, setEnabled] = React.useState(false);
23
-
24
- return (
25
- <Switch
26
- checked={enabled}
27
- onValueChange={setEnabled}
28
- label="Enable notifications"
29
- />
30
- );
31
- }
15
+ import type { SwitchProps } from '@xsolla/xui-switch';
32
16
  ```
33
17
 
34
- ### Switch with Description
18
+ ## Quick start
35
19
 
36
20
  ```tsx
37
- import * as React from 'react';
38
- import { Switch } from '@xsolla/xui-switch';
39
-
40
- export default function SwitchWithDescription() {
41
- const [darkMode, setDarkMode] = React.useState(false);
21
+ const [enabled, setEnabled] = useState(false);
42
22
 
43
- return (
44
- <Switch
45
- checked={darkMode}
46
- onValueChange={setDarkMode}
47
- label="Dark Mode"
48
- description="Enable dark theme across the application"
49
- />
50
- );
51
- }
23
+ <Switch
24
+ checked={enabled}
25
+ onValueChange={setEnabled}
26
+ label="Enable notifications"
27
+ />;
52
28
  ```
53
29
 
54
- ### Label Position
30
+ ## API Reference
55
31
 
56
- ```tsx
57
- import * as React from 'react';
58
- import { Switch } from '@xsolla/xui-switch';
32
+ ### `<Switch>`
59
33
 
60
- export default function SwitchLabelPosition() {
61
- const [left, setLeft] = React.useState(false);
62
- const [right, setRight] = React.useState(false);
34
+ | Prop | Type | Default | Description |
35
+ | --- | --- | --- | --- |
36
+ | `checked` | `boolean` | `false` | Whether the switch is on. |
37
+ | `size` | `'sm' \| 'md' \| 'lg' \| 'xl'` | `'md'` | Size of the switch. |
38
+ | `state` | `'default' \| 'hover' \| 'disable' \| 'error'` | `'default'` | Visual state. |
39
+ | `disabled` | `boolean` | `false` | Disable the switch. |
40
+ | `label` | `string` | — | Label text shown next to the switch. |
41
+ | `labelPosition` | `'left' \| 'right'` | `'right'` | Position of the label. |
42
+ | `description` | `string` | — | Description text below the label. |
43
+ | `errorLabel` | `string` | — | Error message shown when `state === 'error'`. |
44
+ | `onValueChange` | `(value: boolean) => void` | — | Fired when toggled. |
45
+ | `ariaLabel` | `string` | — | Accessible label for screen readers. |
46
+ | `ariaLabelledBy` | `string` | — | ID of an element that labels the switch. |
47
+
48
+ Inherits `ThemeOverrideProps` (`themeMode`, `themeProductContext`).
63
49
 
64
- return (
65
- <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
66
- <Switch
67
- label="Label on the right (default)"
68
- labelPosition="right"
69
- checked={right}
70
- onValueChange={setRight}
71
- />
72
- <Switch
73
- label="Label on the left"
74
- labelPosition="left"
75
- checked={left}
76
- onValueChange={setLeft}
77
- />
78
- </div>
79
- );
80
- }
81
- ```
50
+ ## Examples
82
51
 
83
- ### Switch Sizes
52
+ ### With description
84
53
 
85
54
  ```tsx
86
- import * as React from 'react';
87
- import { Switch } from '@xsolla/xui-switch';
88
-
89
- export default function SwitchSizes() {
90
- return (
91
- <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
92
- <Switch size="sm" label="Small switch" checked />
93
- <Switch size="md" label="Medium switch (default)" checked />
94
- <Switch size="lg" label="Large switch" checked />
95
- <Switch size="xl" label="Extra large switch" checked />
96
- </div>
97
- );
98
- }
99
- ```
100
-
101
- ## Anatomy
102
-
103
- Import the component and use it directly:
104
-
105
- ```jsx
106
- import { Switch } from '@xsolla/xui-switch';
55
+ const [darkMode, setDarkMode] = useState(false);
107
56
 
108
57
  <Switch
109
- checked={isEnabled} // Whether switch is on
110
- onValueChange={handleToggle} // Toggle handler
111
- label="Setting label" // Label text
112
- labelPosition="right" // "left" | "right" (default: "right")
113
- description="Help text" // Description below label
114
- errorLabel="Error text" // Error message
115
- state="default" // Visual state
116
- size="md" // Size variant
117
- />
58
+ checked={darkMode}
59
+ onValueChange={setDarkMode}
60
+ label="Dark mode"
61
+ description="Enable dark theme across the application"
62
+ />;
118
63
  ```
119
64
 
120
- ## Examples
121
-
122
- ### Settings Panel
65
+ ### Label positions
123
66
 
124
67
  ```tsx
125
- import * as React from 'react';
126
- import { Switch } from '@xsolla/xui-switch';
127
-
128
- export default function SettingsPanel() {
129
- const [settings, setSettings] = React.useState({
130
- notifications: true,
131
- sounds: false,
132
- autoUpdate: true,
133
- analytics: false,
134
- });
135
-
136
- const toggle = (key: string) => (value: boolean) => {
137
- setSettings((prev) => ({ ...prev, [key]: value }));
138
- };
139
-
140
- return (
141
- <div style={{ display: 'flex', flexDirection: 'column', gap: 20 }}>
142
- <Switch
143
- checked={settings.notifications}
144
- onValueChange={toggle('notifications')}
145
- label="Push Notifications"
146
- description="Receive push notifications for important updates"
147
- />
148
- <Switch
149
- checked={settings.sounds}
150
- onValueChange={toggle('sounds')}
151
- label="Sound Effects"
152
- description="Play sounds for actions and alerts"
153
- />
154
- <Switch
155
- checked={settings.autoUpdate}
156
- onValueChange={toggle('autoUpdate')}
157
- label="Auto Update"
158
- description="Automatically download and install updates"
159
- />
160
- <Switch
161
- checked={settings.analytics}
162
- onValueChange={toggle('analytics')}
163
- label="Usage Analytics"
164
- description="Help improve the app by sharing anonymous usage data"
165
- />
166
- </div>
167
- );
168
- }
68
+ <Switch label="Label on the right" labelPosition="right" />
69
+ <Switch label="Label on the left" labelPosition="left" />
169
70
  ```
170
71
 
171
- ### Error State
72
+ ### Sizes
172
73
 
173
74
  ```tsx
174
- import * as React from 'react';
175
- import { Switch } from '@xsolla/xui-switch';
176
-
177
- export default function ErrorSwitch() {
178
- return (
179
- <Switch
180
- checked={false}
181
- state="error"
182
- label="Required Setting"
183
- errorLabel="This setting must be enabled to continue"
184
- />
185
- );
186
- }
75
+ <Switch size="sm" label="Small" checked />
76
+ <Switch size="md" label="Medium" checked />
77
+ <Switch size="lg" label="Large" checked />
78
+ <Switch size="xl" label="Extra large" checked />
187
79
  ```
188
80
 
189
- ### Disabled Switch
81
+ ### Error state
190
82
 
191
83
  ```tsx
192
- import * as React from 'react';
193
- import { Switch } from '@xsolla/xui-switch';
194
-
195
- export default function DisabledSwitch() {
196
- return (
197
- <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
198
- <Switch state="disable" label="Disabled off" />
199
- <Switch state="disable" checked label="Disabled on" />
200
- </div>
201
- );
202
- }
84
+ <Switch
85
+ checked={false}
86
+ state="error"
87
+ label="Required setting"
88
+ errorLabel="This setting must be enabled to continue"
89
+ />
203
90
  ```
204
91
 
205
- ## API Reference
92
+ ### Disabled
206
93
 
207
- ### Switch
208
-
209
- A toggle switch component.
210
-
211
- **Switch Props:**
212
-
213
- | Prop | Type | Default | Description |
214
- | :--- | :--- | :------ | :---------- |
215
- | checked | `boolean` | `false` | Whether the switch is on. |
216
- | size | `"sm" \| "md" \| "lg" \| "xl"` | `"md"` | Size of the switch. |
217
- | state | `"default" \| "hover" \| "disable" \| "error"` | `"default"` | Visual state. |
218
- | label | `string` | - | Label text displayed next to switch. |
219
- | labelPosition | `"left" \| "right"` | `"right"` | Position of the label relative to the switch. |
220
- | description | `string` | - | Description text below the label. |
221
- | errorLabel | `string` | - | Error message when state is "error". |
222
- | onValueChange | `(value: boolean) => void` | - | Callback when toggled. |
223
- | ariaLabel | `string` | - | Accessible label for screen readers. |
224
- | ariaLabelledBy | `string` | - | ID of element that labels this switch. |
225
-
226
- **Size Configuration:**
227
-
228
- | Size | Track Width | Track Height | Knob Size |
229
- | :--- | :---------- | :----------- | :-------- |
230
- | sm | 32px | 18px | 14px |
231
- | md | 40px | 22px | 18px |
232
- | lg | 48px | 26px | 22px |
233
- | xl | 56px | 30px | 26px |
234
-
235
- ## Theming
236
-
237
- Switch uses the design system theme for colors:
238
-
239
- ```typescript
240
- // Colors accessed via theme
241
- theme.colors.control.switch.bgOff // Track background when off
242
- theme.colors.control.switch.bgOn // Track background when on
243
- theme.colors.control.switch.knob // Knob color
244
- theme.colors.control.switch.border // Border color
245
- theme.colors.content.primary // Label text
246
- theme.colors.content.secondary // Description text
247
- theme.colors.content.error // Error text
94
+ ```tsx
95
+ <Switch state="disable" label="Disabled off" />
96
+ <Switch state="disable" checked label="Disabled on" />
248
97
  ```
249
98
 
250
99
  ## Accessibility
251
100
 
252
- - Uses `role="switch"` semantic role
253
- - `aria-checked` reflects on/off state
254
- - Keyboard support (Space/Enter to toggle)
255
- - Focus indicator follows WCAG guidelines
256
- - Label properly associated with control
257
- - Disabled state announced to assistive technology
101
+ - Uses `role="switch"` and `aria-checked` to reflect state.
102
+ - Space and Enter toggle the switch.
103
+ - `description` and `errorLabel` are linked via `aria-describedby`.
104
+ - Disabled state is announced to assistive technology.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xsolla/xui-switch",
3
- "version": "0.149.1",
3
+ "version": "0.151.0",
4
4
  "main": "./web/index.js",
5
5
  "module": "./web/index.mjs",
6
6
  "types": "./web/index.d.ts",
@@ -13,8 +13,8 @@
13
13
  "test:coverage": "vitest run --coverage"
14
14
  },
15
15
  "dependencies": {
16
- "@xsolla/xui-core": "0.149.1",
17
- "@xsolla/xui-primitives-core": "0.149.1"
16
+ "@xsolla/xui-core": "0.151.0",
17
+ "@xsolla/xui-primitives-core": "0.151.0"
18
18
  },
19
19
  "peerDependencies": {
20
20
  "react": ">=16.8.0",