Use our Tailwind CSS Switch component to let users adjust settings on/off. The option that the Switch controls, as well as the state it's in, should be made clear from the corresponding inline label.
See below our example that will help you create simple and easy-to-use Switch component for your Tailwind CSS and React project.
You first need to import the DUISwitch Module into your application module
Code
import { DUISwitch } from "david-ui-angular"; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, DUISwitch], providers: [], bootstrap: [AppComponent], }) export class AppModule {}
Preview
Code
<!-- Use DUI Switch in html file --> <dui-switch color="gray"></dui-switch>
You can use different colors of the Switch component by manipulating the color attribute of the component
Preview
Code
<!-- Use DUI Switch in html file --> <dui-switch color="gray" [isChecked]="true"></dui-switch> <dui-switch color="red" [isChecked]="true"></dui-switch> <dui-switch color="purple" [isChecked]="true"></dui-switch>
You can add a label for the Switch
component by passing the label
prop to the switch
component.
Preview
Code
<!-- Use DUI switch in html file --> <dui-switch color="gray" label="Auto Recharge" [isChecked]="true"></dui-switch>
You can turn on/off the ripple effect for the switch
Component using ripple
attribute.
Preview
Code
<!-- Use DUI switch in html file --> <dui-switch color="gray" label="Ripple Effect On" [isChecked]="true" [ripple]="true" > </dui-switch> <dui-switch color="gray" label="Ripple Effect Off" [isChecked]="true" [ripple]="false"> </dui-switch>
You can disable the Switch component by passing the disabled prop to the Checkbox component.
Preview
Code
<!-- Use DUI switch in html file --> <dui-switch color="gray" label="Remember Me" [isChecked]="true" [disabled]="true" > </dui-switch> <dui-switch color="gray" label="Remember Me" [disabled]="true"></dui-switch>
Use the example below for a more complex usage of switch with label that contains some description.
Preview
You'll be able to login without password for 24 hours.
Code
<!-- Use DUI Switch in html file --> <dui-switch color="gray" label="Remember me"> <div> <dui-typography color="gray" variant="small" className="font-normal"> You'll be able to login without password for 24 hours. </dui-typography> </div> </dui-switch>
The following properties are available for dui-switch
Attribute | Type | Description | Default |
---|---|---|---|
label | string | switch label | '' |
color | Color | change switch color | blue |
className | string | Add custom className for switch | '' |
size | boolean | Change switch size | md |
disabled | boolean | Disable switch | false |
containerProps | containerProps | switch container class property | '' |
circleProps | circleProps | switch circle class property | '' |
ripple | boolean | Toggle ripple effect in switch | true |