mirror of
https://github.com/hanzoai/gui.git
synced 2026-10-25 02:31:41 +00:00
Both old names are owned by a different npm account (artemis-prime), so publishing them returns 403 — which is how this surfaced: the 8.0.0 run shipped @hanzogui/logo and @hanzogui/demos depending on [email protected], a version that cannot be published and therefore does not exist. Those two were uninstallable. @hanzo/gui and @hanzogui/* are ours, so the framework now carries the name it is imported by everywhere downstream, and the loader moves under the scope we control. The rewrite is limited to package references. `DEBUG?.startsWith('hanzogui')` is a log-namespace filter and join(dir, 'hanzogui') is a path — rewriting either would change behaviour, so both are untouched. Co-authored-by: Hanzo Dev <[email protected]>
172 lines
3.5 KiB
Plaintext
172 lines
3.5 KiB
Plaintext
---
|
|
title: Switch
|
|
description: A simple switch component
|
|
name: switch
|
|
component: Switch
|
|
package: switch
|
|
demoName: Switch
|
|
---
|
|
|
|
# Switch
|
|
|
|
<Description>Use in forms to toggle between two states.</Description>
|
|
|
|
<HeroContainer showAnimationDriverControl>
|
|
<SwitchDemo />
|
|
</HeroContainer>
|
|
|
|
```tsx hero template=Switch
|
|
|
|
```
|
|
|
|
<Highlights
|
|
features={[
|
|
`Accessible, easy to compose and customize.`,
|
|
`Style and animate both frame and thumb.`,
|
|
`Sizable & works controlled or uncontrolled.`,
|
|
`Native prop that renders native Switch on mobile`,
|
|
]}
|
|
/>
|
|
|
|
## Installation
|
|
|
|
Switch is already installed in `hanzogui`, or you can install it independently:
|
|
|
|
```bash
|
|
npm install @hanzogui/switch
|
|
```
|
|
|
|
## Usage
|
|
|
|
```tsx
|
|
import { Switch } from '@hanzo/gui' // or '@hanzogui/switch'
|
|
|
|
export default () => (
|
|
<Switch size="$4">
|
|
<Switch.Thumb animation="bouncy" />
|
|
</Switch>
|
|
)
|
|
```
|
|
|
|
## Headless with `createSwitch`
|
|
|
|
Using the `createSwitch` export, you can create a fully custom switch without using any of the default styles. This is similar to `unstyled`, but it doesn't assume the props `size` or `unstyled` exist, and it won't automatically apply the `active` theme.
|
|
|
|
You must pass `SwitchContext` as the `context` option to your Frame and Thumb styled components.
|
|
|
|
If you define a `checked` variant, it will apply those styles.
|
|
|
|
Here's an example:
|
|
|
|
```tsx
|
|
import { Stack, styled } from '@hanzogui/core'
|
|
import { createSwitch } from '@hanzogui/switch'
|
|
|
|
const Frame = styled(Stack, {
|
|
variants: {
|
|
checked: {
|
|
true: {
|
|
backgroundColor: 'yellow',
|
|
},
|
|
false: {
|
|
backgroundColor: 'green',
|
|
},
|
|
},
|
|
} as const,
|
|
})
|
|
|
|
const Thumb = styled(Stack, {
|
|
variants: {
|
|
checked: {
|
|
true: {
|
|
opacity: 1,
|
|
},
|
|
false: {
|
|
opacity: 0.5,
|
|
},
|
|
},
|
|
} as const,
|
|
})
|
|
|
|
export const Switch = createSwitch({
|
|
Frame,
|
|
Thumb,
|
|
})
|
|
```
|
|
|
|
## API Reference
|
|
|
|
### Switch
|
|
|
|
`Switch` extends Stack views inheriting all the [Gui standard props](/docs/intro/props), plus:
|
|
|
|
<PropsTable
|
|
data={[
|
|
{
|
|
name: 'labeledBy',
|
|
type: 'string',
|
|
description: `Set aria-labeled-by.`,
|
|
},
|
|
{
|
|
name: 'name',
|
|
type: 'string',
|
|
description: `Equivalent to input name.`,
|
|
},
|
|
{
|
|
name: 'value',
|
|
type: 'string',
|
|
description: `Give it a value (for use in HTML forms).`,
|
|
},
|
|
{
|
|
name: 'checked',
|
|
type: 'boolean',
|
|
description: `Control the input.`,
|
|
},
|
|
{
|
|
name: 'defaultChecked',
|
|
type: 'boolean',
|
|
description: `Uncontrolled default value.`,
|
|
},
|
|
{
|
|
name: 'required',
|
|
type: 'boolean',
|
|
description: `Sets aria-required.`,
|
|
},
|
|
{
|
|
name: 'onCheckedChange',
|
|
type: '(checked: boolean) => void',
|
|
},
|
|
{
|
|
name: 'unstyled',
|
|
type: 'boolean',
|
|
default: 'false',
|
|
description: `When true, remove all default hanzogui styling.`,
|
|
},
|
|
{
|
|
name: 'native',
|
|
type: 'NativeValue<"mobile" | "ios" | "android">',
|
|
description: `Render to a native switch. (Not supported on web)`,
|
|
},
|
|
{
|
|
name: 'nativeProps',
|
|
type: 'SwitchProps (from `react-native`)',
|
|
description: `Props to pass to the native Switch;`,
|
|
},
|
|
]}
|
|
/>
|
|
|
|
### Switch.Thumb
|
|
|
|
`Switch.Thumb` extends Stack views inheriting all the [Gui standard props](/docs/intro/props), plus:
|
|
|
|
<PropsTable
|
|
data={[
|
|
{
|
|
name: 'unstyled',
|
|
type: 'boolean',
|
|
default: 'false',
|
|
description: `When true, remove all default hanzogui styling.`,
|
|
},
|
|
]}
|
|
/>
|