Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a0001a9
i dont know i did but anyways
rohanprasadofficial Dec 14, 2022
380005d
some progress
rohanprasadofficial Dec 19, 2022
9646c78
not sure if it work
rohanprasadofficial Dec 20, 2022
f83b31c
working animation
rohanprasadofficial Dec 21, 2022
48bd9c8
working animation
rohanprasadofficial Dec 21, 2022
bebfadc
works for anroid
rohanprasadofficial Dec 22, 2022
c1c3e36
finally working
rohanprasadofficial Dec 23, 2022
2d5023f
refactorinig useSwitch animation
rohanprasadofficial Dec 23, 2022
d5cf69b
revert customised checked file
rohanprasadofficial Dec 23, 2022
fec6a2c
remove used
rohanprasadofficial Dec 23, 2022
0a9d716
Change files
rohanprasadofficial Dec 23, 2022
f6f1b56
Merge branch 'master' into switch-animation
rohanprasadofficial Dec 23, 2022
6d629a7
refactor and add comments
rohanprasadofficial Dec 23, 2022
b718f47
refactoring
rohanprasadofficial Dec 23, 2022
81f72f8
Merge branch 'master' into switch-animation
rohanprasadofficial Dec 26, 2022
cf06b3c
fix test re-render fails
rohanprasadofficial Dec 26, 2022
3e298e5
lookup change
rohanprasadofficial Dec 26, 2022
70f8462
Merge branch 'main' into switch-animation
rohanprasadofficial Dec 27, 2022
9e42cb8
Merge branch 'master' into switch-animation
rohanprasadofficial Dec 28, 2022
4512ec4
snapshot update
rohanprasadofficial Dec 28, 2022
216169d
Merge branch 'master' into switch-animation
rohanprasadofficial Dec 29, 2022
7bfe6e5
revert to prev impl
rohanprasadofficial Jan 2, 2023
0e827e8
fix typo and error
rohanprasadofficial Jan 2, 2023
668203d
update snapshot
rohanprasadofficial Jan 2, 2023
d682237
Merge branch 'master' into switch-animation
rohanprasadofficial Jan 4, 2023
7077ae0
disabled state bg color fix , color slow transitions
rohanprasadofficial Jan 4, 2023
ee091c4
fix render fail and label text tokens
rohanprasadofficial Jan 4, 2023
650455e
rename IAnimationConfig to AnimationConfig , nit fix
rohanprasadofficial Jan 4, 2023
e384c58
nit changes
rohanprasadofficial Jan 4, 2023
cc49078
Merge branch 'master' into switch-animation
rohanprasadofficial Jan 5, 2023
fab7ba0
address feedbacks
rohanprasadofficial Jan 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "revert customised checked file",
"packageName": "@fluentui-react-native/switch",
"email": "rohanpd.work@gmail.com",
"dependentChangeType": "patch"
}
29 changes: 22 additions & 7 deletions packages/experimental/Switch/src/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @jsx withSlots */
import { View, AccessibilityInfo, Pressable } from 'react-native';
import { View, AccessibilityInfo, Pressable, Animated, Platform } from 'react-native';
import { Text } from '@fluentui-react-native/text';
import { switchName, SwitchType, SwitchState, SwitchProps } from './Switch.types';
import { stylingSettings } from './Switch.styling';
Expand Down Expand Up @@ -29,19 +29,34 @@ export const switchLookup = (layer: string, state: SwitchState, userProps: Switc
);
};

const isMobile = Platform.OS === 'android' || Platform.OS == 'ios';

export const Switch = compose<SwitchType>({
displayName: switchName,
...stylingSettings,
slots: {
root: Pressable,
label: Text,
track: View,
thumb: View,
track: Animated.View, // Conversion from View to Animated.View for Animated API to work
thumb: Animated.View,
toggleContainer: View,
onOffText: Text,
},
useRender: (userProps: SwitchProps, useSlots: UseSlots<SwitchType>) => {
const switchInfo = useSwitch(userProps);
const switchOnSlot = useSlots(userProps, (layer) => switchLookup(layer, { toggled: true, disabled: userProps.disabled }, {}));
const switchOffSlot = useSlots(userProps, (layer) => switchLookup(layer, { toggled: false, disabled: userProps.disabled }, {}));

// For Mobile platform we are passing extra data to useSwitch for Animated API
const switchInfo = useSwitch(
userProps,
isMobile && {
toggleOnBgColor: switchOnSlot.track({}).props.style.backgroundColor,
toggleOffBgColor: switchOffSlot.track({}).props.style.backgroundColor,
trackWidth: switchOnSlot.track({}).props.style.width,
thumbWidth: switchOnSlot.thumb({}).props.style.width,
thumbMargin: switchOnSlot.thumb({}).props.style.margin,
},
);

// grab the styled slots
const Slots = useSlots(userProps, (layer) => switchLookup(layer, switchInfo.state, switchInfo.props));
Expand All @@ -53,13 +68,13 @@ export const Switch = compose<SwitchType>({
const displayOnOffText = !!offText || !!onText;
const isReduceMotionEnabled = AccessibilityInfo.isReduceMotionEnabled;
const thumbAnimation = isReduceMotionEnabled ? { animationClass: 'Ribbon_SwitchThumb' } : null;

return (
<Slots.root {...mergedProps}>
<Slots.label>{label}</Slots.label>
<Slots.toggleContainer>
<Slots.track>
<Slots.thumb {...thumbAnimation} />
{/* For the Mobile platform the animated styles are applied */}
<Slots.track {...(isMobile && { style: switchInfo.props.switchAnimationStyles.trackBackgroundStyle })}>
<Slots.thumb {...thumbAnimation} {...(isMobile && { style: switchInfo.props.switchAnimationStyles.thumbAnimatedStyle })} />
</Slots.track>
{displayOnOffText && <Slots.onOffText>{onOffText}</Slots.onOffText>}
</Slots.toggleContainer>
Expand Down
15 changes: 14 additions & 1 deletion packages/experimental/Switch/src/Switch.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ export interface SwitchTokens extends LayoutTokens, FontTokens, IBorderTokens, I
above?: SwitchTokens;
}

export interface AnimationConfig {
toggleOnBgColor: string;
toggleOffBgColor: string;
trackWidth: number;
thumbWidth: number;
thumbMargin: number;
}

export interface SwitchProps extends Omit<PressablePropsExtended, 'onPress'> {
/**
* Reference to the Switch
Expand Down Expand Up @@ -173,9 +181,14 @@ export interface SwitchProps extends Omit<PressablePropsExtended, 'onPress'> {
* Note :'before', 'above' are not supported on Android
*/
labelPosition?: 'before' | 'above' | 'after';

/**
* Disabled state of the switch.
*/
disabled?: boolean;
}

export type SwitchState = PressableState & { toggled?: boolean };
export type SwitchState = PressableState & { toggled?: boolean; disabled?: boolean };

export interface SwitchInfo {
props: SwitchProps & React.ComponentPropsWithRef<any>;
Expand Down
6 changes: 1 addition & 5 deletions packages/experimental/Switch/src/SwitchTokens.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export const defaultSwitchTokens: TokenSettings<SwitchTokens, Theme> = (t: Theme
trackHeight: 32,
trackWidth: 52,
thumbMargin: 3,
fontSize: t.typography.variants.body1.size,
fontWeight: t.typography.variants.body1.weight,
fontLineHeight: t.typography.variants.body1.lineHeight,
variant: 'body1',
color: t.colors.neutralForeground1,
elevation: t.shadows.shadow8.key.blur,

Expand All @@ -36,7 +34,6 @@ export const defaultSwitchTokens: TokenSettings<SwitchTokens, Theme> = (t: Theme
toggleOn: {
trackColor: t.colors.brandBackground,
thumbColor: t.colors.neutralBackgroundLightStatic,
justifyContent: 'flex-end',
pressed: {
thumbSize: 24,
thumbMargin: 4,
Expand All @@ -51,7 +48,6 @@ export const defaultSwitchTokens: TokenSettings<SwitchTokens, Theme> = (t: Theme
toggleOff: {
trackColor: t.colors.neutralBackground5,
thumbColor: t.colors.neutralBackgroundLightStatic,
justifyContent: 'flex-start',
pressed: {
thumbSize: 24,
thumbMargin: 4,
Expand Down
9 changes: 4 additions & 5 deletions packages/experimental/Switch/src/__tests__/Switch.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as React from 'react';
import * as renderer from 'react-test-renderer';
import { Switch } from '../Switch';
import { checkRenderConsistency, checkReRender } from '@fluentui-react-native/test-tools';
import { checkReRender } from '@fluentui-react-native/test-tools';

// mocks out setTimeout and other timer functions with mock functions , test will fail without this as we're using Animated API
jest.useFakeTimers();

it('Switch Default', () => {
const tree = renderer.create(<Switch label="Default Switch" />).toJSON();
Expand All @@ -13,10 +16,6 @@ it('Switch Disabled', () => {
expect(tree).toMatchSnapshot();
});

it('Simple switch rendering does not invalidate styling', () => {
checkRenderConsistency(() => <Switch label="Default Switch" />, 2);
});

it('Switch re-renders correctly', () => {
checkReRender(() => <Switch label="Render Twice" />, 2);
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,26 @@ exports[`Switch Default 1`] = `
"minWidth": undefined,
}
}
switchAnimationStyles={
Object {
"thumbAnimatedStyle": Object {
"transform": Array [
Object {
"translateX": 0,
},
],
},
"trackBackgroundStyle": Object {
"backgroundColor": "rgba(15, 108, 189, 1)",
},
}
}
>
<Text
style={
Object {
"color": "#242424",
"fontFamily": "Segoe UI",
"fontSize": 13,
"fontWeight": "400",
"margin": 0,
Expand All @@ -64,31 +79,34 @@ exports[`Switch Default 1`] = `
}
>
<View
collapsable={false}
style={
Object {
"alignItems": "center",
"backgroundColor": "#f0f0f0",
"backgroundColor": "rgba(15, 108, 189, 1)",
"borderRadius": 100,
"flexDirection": "row",
"height": 32,
"justifyContent": "flex-start",
"marginBottom": undefined,
"marginLeft": undefined,
"marginRight": 8,
"marginTop": undefined,
"width": 52,
}
}
>
<View
animationClass="Ribbon_SwitchThumb"
collapsable={false}
style={
Object {
"backgroundColor": "#ffffff",
"borderRadius": 100,
"elevation": 8,
"height": 26,
"margin": 3,
"transform": Array [
Object {
"translateX": 0,
},
],
"width": 26,
}
}
Expand Down Expand Up @@ -140,11 +158,26 @@ exports[`Switch Disabled 1`] = `
"minWidth": undefined,
}
}
switchAnimationStyles={
Object {
"thumbAnimatedStyle": Object {
"transform": Array [
Object {
"translateX": 0,
},
],
},
"trackBackgroundStyle": Object {
"backgroundColor": "rgba(180, 214, 250, 1)",
},
}
}
>
<Text
style={
Object {
"color": "#e0e0e0",
"fontFamily": "Segoe UI",
"fontSize": 13,
"fontWeight": "400",
"margin": 0,
Expand All @@ -162,31 +195,34 @@ exports[`Switch Disabled 1`] = `
}
>
<View
collapsable={false}
style={
Object {
"alignItems": "center",
"backgroundColor": "#f0f0f0",
"backgroundColor": "rgba(180, 214, 250, 1)",
"borderRadius": 100,
"flexDirection": "row",
"height": 32,
"justifyContent": "flex-start",
"marginBottom": undefined,
"marginLeft": undefined,
"marginRight": 8,
"marginTop": undefined,
"width": 52,
}
}
>
<View
animationClass="Ribbon_SwitchThumb"
collapsable={false}
style={
Object {
"backgroundColor": "#ffffff",
"borderRadius": 100,
"elevation": 0,
"height": 26,
"margin": 3,
"transform": Array [
Object {
"translateX": 0,
},
],
"width": 26,
}
}
Expand Down
Loading