Looking for documentation on gluestack-ui v2 with NativeWind? Refer to this link.
The current component is functioning as expected, but we have decided to rebuild it entirely to align with our vision and enhance its performance. It is primarily based on React Native, and while we have made minimal modifications so far, our upcoming rebuild will involve significant changes to ensure it closely matches our desired structure and meets our objectives.
This is an illustration of a Themed Switch component with default configuration.
size
isDisabled
<Switchsize="md"isDisabled={false}/>
API Reference
Import
To use this component in your project, include the following import statement in your file.
import{ Switch } from '@gluestack-ui/themed';
Anatomy
The structure provided below can help you identify and understand a Switch component's various parts.
exportdefault()=><Switch/>
Component Props
This section provides a comprehensive reference list for the component props, detailing descriptions, properties, types, and default behavior for easy project integration.
Switch
Contains all switch related layout style props and actions.
It inherits all the properties of React Native's Switch component.
Prop
Type
Default
Description
isDisabled
boolean
false
When true, the switch is disabled and cannot be toggled
isInvalid
boolean
false
When true, the switch displays an error state.
isRequired
boolean
false
When true, sets aria-required="true" on the switch.
isHovered
boolean
false
When true, the switch displays a hover state.
value
boolean
false
The value of the switch. If true the switch will be turned on.
defaultValue
boolean
false
The defaultValue of the switch. If true the switch will be turned on initially.
onToggle
() => any
-
Callback to be invoked when switch value is changed.
Accessibility
We have outlined the various features that ensure the Button component is accessible to all users, including those with disabilities. These features help ensure that your application is inclusive and meets accessibility standards.Adheres to the WAI-ARIA design pattern.
Keyboard
Tab: Moves focus to the next focusable element.
Space: Triggers the switch's action.
Screen Reader
VoiceOver: When the switch is focused, the screen reader will announce the switch's action and its current state.
Themed
The themed version of the component is a pre-styled version of the component, which allows you to quickly integrate the component into your project. The component's design and functionality are fully defined, allowing you to focus on the more important aspects of your project. To know more about Themed Library please visit this link.
Props
Switch component is created using Switch component from react-native. It extends all the props supported by React Native Switch, utility props and the props mentioned below.
Switch
Name
Value
Default
size
sm | md | lg
md
Note: These props are exclusively applicable when utilizing the default configuration of gluestack-ui/config. If you are using a custom theme, these props may not be available.
Examples
The Examples section provides visual representations of the different variants of the component, allowing you to quickly and easily determine which one best fits your needs. Simply copy the code and integrate it into your project.
Switch With Label
An example of a switch component with a label, which includes another switch component for toggling additional options.
<HStackspace="md">
<Switch/>
<Textsize="sm">Allow notifications</Text>
</HStack>
Checked State
An example of a switch component used within a checked state component to represent a pre-selected or activated option.
<HStackspace="md">
<SwitchdefaultValue={true}/>
<Textsize="sm">Public profile</Text>
</HStack>
Color scheme
An example of a switch component being used with a different color scheme.
<VStackspace="md">
<Switch
sx={{
_light:{
props:{
trackColor:{
false:"$backgroundLight300",
true:"$indigo600",
},
},
},
_dark:{
props:{
trackColor:{
false:"$backgroundDark700",
true:"$indigo600",
},
},
},
}}
/>
<Switch
sx={{
_light:{
props:{
trackColor:{
false:"$backgroundLight300",
true:"$emerald600",
},
},
},
_dark:{
props:{
trackColor:{
false:"$backgroundDark700",
true:"$emerald600",
},
},
},
}}
/>
</VStack>
Unstyled
We provide in-depth information on how to customize and extend the component's functionality to meet your needs. Below, we describe how to modify the component to suit your requirements.
Spec Doc
Explore the comprehensive details of the Switch in this document, including its implementation details, checklist, and potential future additions. Dive into the thought process behind the component and gain insights into its development journey.