Giter VIP home page Giter VIP logo

toggle-switch-react-native's Introduction

Toggle Switch Component for react native, it works on Android, iOS and Web (react-native-web).

Buy Me A Coffee

Content

Installation

    1. Run npm i toggle-switch-react-native --save
    1. import ToggleSwitch from 'toggle-switch-react-native'

Demo

Screenshots

Getting started

To Get Started, Import toggle-switch-react-native to your js file.

import ToggleSwitch from 'toggle-switch-react-native'

Inside your component's render method, or any other method returning views, use ToggleSwitch:

<ToggleSwitch
  isOn={false}
  onColor="green"
  offColor="red"
  label="Example label"
  labelStyle={{ color: "black", fontWeight: "900" }}
  size="large"
  onToggle={isOn => console.log("changed to : ", isOn)}
/>

API

Props Type Optional Default Description
isOn Boolean true 'false' Default state, true for On, false for off
onColor String true '#634fc9' On Color
offColor String true '#ecf0f1' Off Color
label String true Custom Label Text on the Left of the toggle Button
labelStyle Object, StyleSheet true {marginHorizontal: 10} Custom Styling for the Label Text View
thumbOnStyle Object, StyleSheet true null Custom Styling for the On Thumb
thumbOffStyle Object, StyleSheet true null Custom Styling for the Off Thumb
trackOnStyle Object, StyleSheet true null Custom Styling for the On Track
trackOffStyle Object, StyleSheet true null Custom Styling for the Off Track
size String true 'medium' Size of the toggle switch button ( 'large', 'medium', 'small')
icon React Component true null Icon for the toggle. Can be any React Component considerate of size
onToggle Function Callback false none Callback when the toggle switch component changes the state, params: isOn
disabled Boolean true 'false' Disable toggling the component
animationSpeed Number true 300 Speed of the toggle animation

Contribution

If you encountered an Issue, please add a screenshot of the bug or a code snippet.

Quickest way to solve issue is to reproduce it on the example project

Pull requests are welcome. If you want to change the API or imporove something, feel free to create an issue and discuss it first.


MIT Licensed

toggle-switch-react-native's People

Contributors

aminebenkeroum avatar ashkan4ever avatar chungweileong94 avatar dependabot[bot] avatar g3z avatar hakimscode avatar maxs555 avatar og24715 avatar pedromdev avatar proserve avatar pruthvip avatar ts-ign0re avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

toggle-switch-react-native's Issues

Able to set lable and toggle in seperate style?

Is it possible to add "Label" style to left and "Toggle" to right in the same usage? The label will be on left of Toggle for now but it seems can't add more space style between it. May be I just don't know how to do it. Can't you guys (someone) guide me? Thanks

Disabled styles

It could be nice to have disabled style prop. It's better to change the color or opacity when the switch is disabled so it doesn't look like it's active. Thanks!

TypeScript definition should work

When installing this lib in a TypeScript project (e.g default Expo project) the included definition does not work.

Should probably look like this (which I'm using):

/// <reference types="node" />

declare module 'toggle-switch-react-native' {
  import React from 'react';
  import {StyleProp, TextStyle, ViewStyle} from 'react-native';

  export type ToggleSwitchProps = {
    isOn: boolean;
    label?: string;
    onColor?: string;
    offColor?: string;
    size?: string;
    labelStyle?: StyleProp<TextStyle>;
    thumbOnStyle?: StyleProp<ViewStyle>;
    thumbOffStyle?: StyleProp<ViewStyle>;
    trackOnStyle?: StyleProp<ViewStyle>;
    trackOffStyle?: StyleProp<ViewStyle>;
    onToggle?: (isOn: boolean) => any;
    icon?: React.ReactNode;
    disabled?: boolean;
    animationSpeed?: number;
    useNativeDriver?: boolean;
    circleColor?: string;
  };

  class ToggleSwitch extends React.Component<ToggleSwitchProps> {}

  export default ToggleSwitch;
}

Support hitSlop props

Sometimes the hit area is too small to tap on, it will be good if we can have a prop to control the hitSlop.

Opacity makes color weird

OS: Android

[Problem]
When I set opacity(style) at the parent of ToggleSwitch,
Circle's color is turned weird color.
I can't make circle white if the setting item is disabled.

up: opacity 0.3
down: opacity 1
스크린샷 2023-03-07 오전 9 50 59

is this Android OS Setting issue? auto-contrast??

Not working example

Please see this gif, you can know about this issue all.

kapture 2018-06-06 at 11 12 39

I started working to fix it, will make a PR soon.

rtl support

The library does not seems to be supporting rtl :
WhatsApp Image 2019-09-05 at 10 15 11

the red dot is not inside the component.

package don't support Typescript

Typing this:

npm i --save-dev @types/toggle-switch-react-native

Will result:

'@types/toggle-switch-react-native@*' is not in this registry.
You should bug the author to publish it (or use the name yourself!)

Can't wait to see it fix :)

Thank you,
Take care

Issue with ios app

Hi,
Can you help here.
Toggle-switch-react-native is working fine for android but in ios it is not working it,s not getting on please help...
Will it work for ios or only for android.

Failed parsing on Web

Module parse failed: Unexpected token (52:19)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|   }
| 
>   static propTypes = {
|     isOn: PropTypes.bool.isRequired,
|     label: PropTypes.string,

babel.config.js

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ["babel-preset-expo"],
    plugins: [
      "@babel/plugin-proposal-class-properties",

      [
        "babel-plugin-inline-import",
        {
          extensions: [".svg"],
        },
      ],
    ],
  };
};

Medium Size is wrong

This should be the setting for medium toggle button, not the current one

{ width: 60, padding: 14, circleWidth: 24, circleHeight: 24, translateX: 30 }

New Features

You can also add the following features
1.Label text need to change dynamically for on and off like "available" "not-available"
2.Label text position left or right options
3.Icon size with custom height and width instead of small,medium, and large

isOn state not updating dynamically

<ToggleSwitch isOn={this.state.toggle} />

When I change the toggle state to true once all the components are rendered it doesn't update the toggle button to true

My code for changong the toggle state :
this.setState({ toggle: true })

Adding in Accessibility props

Hi, I noticed there was no way to pass in any Accessibility props so users with their screen readers enabled cannot know what this plugin can do.

At minimum:

  • accessibilityLabel
  • accessibilityHint
  • accessibilityRole (could default this to either switch or checkbox but not required).

I was planning on making a PR to add in these props but wanted to submit this "issue/enhancement" first.

Not working in flatlist

Hey i'm using this in flatlist which render multiple switches, but it is not updating isOn value

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.