react-native-paper icon indicating copy to clipboard operation
react-native-paper copied to clipboard

M3 styled switch

Open yuameshi opened this issue 3 years ago • 42 comments

Is your feature request related to a problem? Please describe.

get a m3 styled switch component

Describe the solution you'd like

just edit stylesheet

Additional context

Official document

Official document

Diff between M2 & M3

Expected style(M3)

Current style(M2)

yuameshi avatar Apr 02 '23 06:04 yuameshi

any updates?

yuameshi avatar Apr 04 '23 02:04 yuameshi

Hey @yuameshi, nothing has changed from my last comment related to that topic

lukewalczak avatar Apr 04 '23 07:04 lukewalczak

----------------------Edit------------------------------------------------------------ I have commented updated component to match better with Material you in comments bellow this. This one had some bugs

If in case anyone is looking, I have created a "Material You"-like Swtich Component, behaves smoothly, it's touch behaves like Material you Switch. I have followed Material 3 guidelines for making it. Here's a video of tapping and draggin actions :

https://github.com/callstack/react-native-paper/assets/150055488/ee6796ae-b14e-45e1-9079-53111390f4d0

If anyone likes it, here's the code for component file:

-------Removing Code as there is updated one at snack here.

Also install libraries which are imported before using

npm install react-native-gesture-handler react-native-paper react-native-reanimated

If you don't have reanimated installed, see guide to install reanimated because it needs a couple of extra steps to setup properly.

It works completely fine with( and only with) React Native paper and changes color and theme too according to paperprovider, tested on Android on Web, though on web, if you are on Desktop, it works fine on tap, but if you try to drag it, it dosent change value. If you wanna run only on mobile, I think this would work great. Also there is attribute for icon : <MaterialSwitch icon={"icon_name"}/>, just pass one of MaterialCommunityIcons name as string and it will render a icon when switch is on.

Hope this heps !

crystal7-io avatar Dec 17 '23 10:12 crystal7-io

@lukewalczak I worked a bit off and created a Material you styled switch with reanimated and gesture handler and it can be used with react native paper, here I am using it in my app :

https://github.com/callstack/react-native-paper/assets/150055488/842c8f91-9ab5-4d18-b1f2-55349e9c1bd0

and here is Files by Google app switch :

https://github.com/callstack/react-native-paper/assets/150055488/c8008b19-9d33-4053-86fc-ea1139131449

What do you think, pretty much same ? I mean the material you styled switch just hits different, it changes whole look of the ui, Should implement this keeping the current switch, users can use anyone which they want.

crystal7-io avatar Jan 18 '24 17:01 crystal7-io

@crystalarcus Can you share the updated component file code? Thanks!

rollingmoai avatar Jan 19 '24 01:01 rollingmoai

@crystalarcus Can you send the updated component file code? Thanks!

@rollingmoai sure!, actually there are 2 components I have made for personal use : MaterialSwitch and MaterialSwitchList as you see in above video, the switch components is many times used with listitem component, like you click listitem and it switches the switch. The list item i used above is actualy react-native-paper's List.Item component. Sadly it's not quite possible to use List item and gesture handler togther, it causes bugs, so in MaterialSwitchList component, you can't control the switch by holding it and moving your fingers, but even the Switch from Files by Google app, as shown in video above can't do that. You can't control it by holding and moving ur finger to change switch, you just simply need to press it, and whole List.Item is pressed. So it behaves like single component. So I had to make 2 separate components to integrate it with List.Item. The other component MaterialSwitch, behaves normally like a swtich, you can hold it with fingers, or simply tap it. I will share the code for both components in few hours, just need some finishing touch to make it useful for others, cause I just designed it for my personal use.

crystal7-io avatar Jan 19 '24 03:01 crystal7-io

@rollingmoai @lukewalczak So here's it finally completed and read to use, here's a video:

Android ( Expo Go ) :

https://github.com/callstack/react-native-paper/assets/150055488/f7147146-375e-4f75-9370-ebdfd60fe8c0

Web (Chrome, Windows , the mouse appears laggy because I was capturing with xbox gamebar) :

https://github.com/callstack/react-native-paper/assets/150055488/573f7327-0bb9-411c-a7a1-f823d5929628

here's full code :-

-------Removing Code as there is updated one at snack here.

Alright, now one thing, I am just a high school student, don't know about all components and attributes, I just used the knowledge I had. Just making this much was a tough job for me, so if you can optimize it, it would be great. Also it works well in my apps that I made for practicing, still if you find any bug, you can try fixing it. I know one bug and its, sometimes rarely if you touch on exact border of switch item, the handle circle will get bigger as if you are holding it and it will only get normal if you tap again on switch. It just happens rarely. I gave you a basic foundation that works pretty well, see if you can make it better, if not you can still directly use it ! Hope it helps someone !

( also can someone tell me how to make a snack of a project I currently have in my pc ? I want to put this on snack, would be better to try it yourself before implementing in your app )

crystal7-io avatar Jan 19 '24 18:01 crystal7-io

And here's how to use it : Material Switch example:

<MaterialSwitch
                selected={value}
                onPress={() => setValue(!value)} 
                switchOnIcon={'check'}
                switchOffIcon={'close'}
                fluid={false}
                />

Material Switch have 5 attributes, ( you can edit code and add more like colors, etc):

selected : boolean // value weather switch will be on or off, true : On, false : Off
onPress : function // function triggered after switch is pressed. ( NOTE: onPress is triggered after the switch changing animation is completed, that is, its passed as a callback to reanimated animation)
switchOnIcon : string or function which returns a icon component // Icon when switch is on, default is null
switchOffIcon : string or function which returns a icon component // Icon when switch is off, default is null
fluid : boolean // fluid like animation when switching, default is false

Material Switch List Item example:

  <MaterialSwitchListItem
              title={"Fluid animation "}
              listStyle={styles.listItem}
              fluid={true}
              selected={value}
              onPress={() => setValue(!value)} />

This one have 11 props, namely : title, description, titleStyle, listStyle, selected, onPress, leftIcon, switchOnIcon, switchOffIcon, rippleColor, fluid Out of these, five props : selected, onPress, fluid, switchOnIcon, switchOffIcon, are same from material switch mentioned above. the rest six :

title : "string" for List.Item title (from react-native-paper)
description : "string" for List.Item description (from react-native-paper)
listStyle: style for List.Item (from react-native-paper)
titleStyle: style for Title of List ( you can edit code to change description style, I didnt add it cause it looked fine.)
leftIcon :  string or function which returns a icon component // Icon to left side of List.Item, default is null
rippleColor: ripple color of List.Item (from react-native-paper) // default is the one used by List.Item 

crystal7-io avatar Jan 19 '24 19:01 crystal7-io

Thanks! Will test this out on iOS when I have time

rollingmoai avatar Jan 20 '24 01:01 rollingmoai

@rollingmoai would like to add one more thing, the state layer on mouse hover: unnamed I was focused to use it with android, so I didn't gave attention to desktop features. The thing is, the state layer opacity is 0.1 when you click on it, but desktop have mouse, which can be hovered. The opacity on mouse hover is 0.08. Will need to add that for better Desktop experience.

crystal7-io avatar Jan 20 '24 03:01 crystal7-io

It might be good to have a prop to set the duration of the animation as well.

P.S., Can you also add type annotations for typescript? If not, I can otherwise add them myself. Thanks!

rollingmoai avatar Jan 20 '24 03:01 rollingmoai

It might be good to have a prop to set the duration of the animation as well.

P.S., Can you also add type annotations for typescript? If not, I can otherwise add them myself. Thanks!

@rollingmoai Ah typescript, don't know much about it, React Native is actually my first time working with JavaScript. Though I can try it out, it can be a good opportunity to learn. And about animation, ya it's easy to add duration as props, just need to add {duration : duration) to withTiming and add it to prop, maybe put a shorthand if statement if prop is not provided than use default values, I will do it soon and give update. Also I made a snack. You can try it out on iOS.

crystal7-io avatar Jan 20 '24 04:01 crystal7-io

I will do it soon and give update. Also I made a snack. You can try it out on iOS.

Thanks, it works fine on iOS.

@rollingmoai would like to add one more thing, the state layer on mouse hover: unnamed I was focused to use it with android, so I didn't gave attention to desktop features. The thing is, the state layer opacity is 0.1 when you click on it, but desktop have mouse, which can be hovered. The opacity on mouse hover is 0.08. Will need to add that for better Desktop experience.

And the disabled prop too, I forgot to mention that was missing as well.

rollingmoai avatar Jan 20 '24 05:01 rollingmoai

@rollingmoai just to let you know, I have done finished setting project into typescript ( spent whole day today on w3school learning TS ) and also added type annotations to props, currently something like this :

type SwitchProps = {
    selected: boolean,
    onPress: () => void,
    fluid?: boolean,
    switchOnIcon?: IconSource, // IconSource from 'react-native-paper/lib/typescript/components/Icon'
    switchOffIcon?: IconSource, // IconSource from 'react-native-paper/lib/typescript/components/Icon'
    disabled?: boolean,
}

and for Switch List Item

type SwitchListItemProps = {
    selected: boolean,
    onPress: () => void,
    fluid?: boolean,
    switchOnIcon?: IconSource,
    switchOffIcon?: IconSource,
    title: string,
    description?: string,
    titleStyle?: StyleProp<TextStyle>,
    descriptionStyle?: StyleProp<TextStyle>,
    listStyle?: StyleProp<ViewStyle>,
    leftIcon?: IconSource,
    rippleColor?: ColorValue,
    disabled?: boolean
}

I have added the disabled props too, just settings up few things, than I will update it on snack. (In love with typescript, I should have used it before ❤️)

ALSO, about duration, well I need to think over it a lil, cuz its not a single duration being used everywhere, but different components have different duration ( handle size, state layer size, track background color, etc). So we can't just change it with single prop, although, we can give a duration multiplier prop. It will work this way:

  1. All durations in component will be multiplied by animationSpeed variable, which can be passed as prop.
  2. Default value will be 1. Means all animatiosn will run on normal speed.
  3. if value is animationSpeed = 1.5 all animations will increase there speed, resulting in reduced overall duration, if set to animationSpeed = 0.5 all will decrease speed resulting in increased overall duration. Will this do ?

crystal7-io avatar Jan 20 '24 18:01 crystal7-io

@lukewalczak And here we go snack

  1. Added disabled prop
  2. Added TypeScript and type annotations
  3. Made state layer appear below switch

The 3rd point was changed due to this :

https://github.com/callstack/react-native-paper/assets/150055488/0ed7f2d9-e84e-41d6-9952-51bb8ec22cd9

See, it's Files by Google app, when switch is pressed the press indicator ( called state, the big circle with 0.1 opacity ) is only visible in area outside switch, like it's below the switch component. I looks like half moon on end sides. According to Material You Guidelines, the state appears on top of switch only on hovering ( eg with mouse ) but onPress it appears below the switch. Screenshot_20240121_020946_Samsung Internet

Left side switch are hovering and right side is pressed. See the State layer appears on top on left side but on right side it's only half visible. ( Who would have thought there would be this much of small detailings in a simple component like switch.... )

crystal7-io avatar Jan 20 '24 20:01 crystal7-io

Nice work!

I noticed a bug where the colors do not get restored when disabling and re-enabling the switch again:

https://github.com/callstack/react-native-paper/assets/75471378/ed660943-da3b-4c6c-a092-9dbc0eaf0134

And animations do not work too when disabled?

rollingmoai avatar Jan 21 '24 02:01 rollingmoai

Nice work!

I noticed a bug where the colors do not get restored when disabling and re-enabling the switch again:

Recording.2024-01-21.102422.mp4 And animations do not work too when disabled?

Oh that's quite weird, but I can tell root of problem is not color here but the opacity, actually disabled switch have opacity of 1.2 on background and 3.8 on handle ( the circle ). If you see clearly, when switch is renabled , the primary color is back but it's quite fade, means it's opacity bug. Looks like states are not getting updated, will try to fix it within an hour.

crystal7-io avatar Jan 21 '24 02:01 crystal7-io

@rollingmoai I have fixed the bug. Also now I have added a Switch Disable all Switch at bottom of in Snack Project. It will disable all switch except the default ones. image

So the bug was due to this : I used conditional style in switches for disabled and enabled, like

const style = useAnimatedStyle(()=>(
disabled? 
{
      //disabled style....
}:
{
     //enabled style.....
}
));

but instead of changing the style, it was overriding the style everytime disabled changed. So at first opacity was 1. But when disabled, opacity was 0.1. Than when enabled again, the enabled style is overridden over disable style. And enabled style didnt had any opacity attribute. So it used opacity from disabled. Now I added opacity 1 to enabled style. Its fixed 👍

crystal7-io avatar Jan 21 '24 03:01 crystal7-io

You forgot to set it for handleStyle too 😅 image

rollingmoai avatar Jan 21 '24 04:01 rollingmoai

@rollingmoai wait how?

https://github.com/callstack/react-native-paper/assets/150055488/eaf6a8b0-1a42-4d29-b162-06eb4bfa3f2d

It works fine in this one, but I will still add it to handle style as precaution.

crystal7-io avatar Jan 21 '24 04:01 crystal7-io