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