Skip to content

Commit 0dc9a86

Browse files
committed
Package Commit
1 parent f33b0e3 commit 0dc9a86

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 musaib999
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

index.d.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {ViewStyle} from 'react-native';
2+
3+
export interface ToggleProps {
4+
5+
6+
/**
7+
* Color of the switch
8+
*/
9+
color?: string,
10+
11+
/**
12+
* Size of the switch
13+
*/
14+
size?: number,
15+
16+
/**
17+
* Pass a true value if you want to use a filled color switch
18+
*/
19+
filled?: boolean
20+
21+
22+
/**
23+
* Color of the circle in switch
24+
*/
25+
circleColor?: string,
26+
27+
28+
/**
29+
* toggle status of the switch
30+
*/
31+
toggle?: boolean
32+
33+
34+
/**
35+
* Function to set the status of toggle switch which will be stored in your local state
36+
*/
37+
setToggle: React.Dispatch<React.SetStateAction<undefined>>,
38+
39+
}

index.tsx

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import React,{JSXElementConstructor} from 'react';
2+
import {View,StyleSheet,Pressable} from 'react-native';
3+
4+
5+
interface ToggleProps {
6+
7+
/**
8+
* Color of the switch
9+
*/
10+
color?: string,
11+
12+
/**
13+
* Size of the switch
14+
*/
15+
size?: number,
16+
17+
/**
18+
* Pass a true value if you want to use a filled color switch
19+
*/
20+
filled?: boolean
21+
22+
23+
/**
24+
* Color of the circle in switch
25+
*/
26+
circleColor?: string,
27+
28+
29+
/**
30+
* toggle status of the switch
31+
*/
32+
toggle: boolean
33+
34+
35+
/**
36+
* Function to set the status of toggle switch which will be stored in your local state
37+
*/
38+
setToggle: React.Dispatch<React.SetStateAction<undefined>>,
39+
40+
}
41+
42+
43+
const Toggle: React.FC<ToggleProps> = ({color,size,filled,circleColor,toggle,setToggle}) => {
44+
const _color = color ?? "#4C956C";
45+
const _circle = circleColor ?? _color;
46+
const _filled = filled ?? false;
47+
const _size = size ?? 25;
48+
49+
50+
function toggleContainerStyles(){
51+
if(!_filled){
52+
return [styles(_color,_size,_circle).toggleContainer, (!toggle) ? {borderColor: "#AAAAAA"} : null]
53+
}else{
54+
return [styles(_color,_size,_circle).toggleContainer,styles(_color,_size,_circle).toggleContainerFilled, (!toggle) ? {backgroundColor: "#AAAAAA", borderColor: "#AAAAAA"} : null]
55+
}
56+
}
57+
58+
function toggleCircleStyles(){
59+
if(!_filled){
60+
return [styles(_color,_size,_circle).circle, (toggle) ? {alignSelf: "flex-end"} : { backgroundColor: "#AAAAAA"}];
61+
}else{
62+
return [styles(_color,_size,_circle).circle, styles(_color,_size,_circle).circleFilled, (toggle) ? {alignSelf: "flex-end"} : { backgroundColor: "#FEFEFE"}]
63+
}
64+
}
65+
66+
return(
67+
<View>
68+
<Pressable style={toggleContainerStyles()} onPress={() => setToggle(!toggle)}>
69+
<View style={toggleCircleStyles()}></View>
70+
</Pressable>
71+
</View>
72+
)
73+
74+
75+
}
76+
77+
export default Toggle;
78+
79+
80+
81+
const styles = (color:string, size:number, circleColor:string) => {
82+
return StyleSheet.create({
83+
toggleContainer: { padding: size/10, width: 2.3*size, borderRadius: size, borderColor: color, borderWidth: 2.5 },
84+
toggleContainerFilled: { backgroundColor: color, borderColor: color },
85+
circle: { width: size, height: size, borderRadius: size/2, backgroundColor: circleColor },
86+
circleFilled: { backgroundColor: circleColor },
87+
});
88+
}

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "react-native-toggle-input",
3+
"version": "1.0.0",
4+
"description": "Customized toggle button \u001b[D\u001b[Cinput for react native and expo\u001b[D\u001b[D\u001b[C\u001b[C",
5+
"main": "index.tsx",
6+
"types": "index.d.ts",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/musaib999/react-native-toggle-input.git"
13+
},
14+
"keywords": [
15+
"toggle",
16+
"toggleinput",
17+
"easyswitch",
18+
"switch",
19+
"button",
20+
"toggleswitch"
21+
],
22+
"author": "Muhammad Musaib",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/musaib999/react-native-toggle-input/issues"
26+
},
27+
"homepage": "https://github.com/musaib999/react-native-toggle-input#readme"
28+
}

0 commit comments

Comments
 (0)