Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -96,6 +96,7 @@ const OnOffText: React.FunctionComponent = () => {
<View style={commonTestStyles.settingsPicker}>
<Switch defaultChecked={true} labelPosition={'before'} label={'Autosave'} onText={'On'} offText={'Off'} />
<Switch defaultChecked={true} labelPosition={'above'} label={'Autosave'} onText={'On'} offText={'Off'} />
<Switch defaultChecked={true} labelPosition={'after'} label={'Autosave'} onText={'On'} offText={'Off'} />
</View>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "On/off text renders with labelPosition=before",
"packageName": "@fluentui-react-native/switch",
"email": "winlarry@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add new example for Switch on/off text section",
"packageName": "@fluentui-react-native/tester",
"email": "winlarry@microsoft.com",
"dependentChangeType": "patch"
}
10 changes: 10 additions & 0 deletions packages/components/Switch/src/Switch.styling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ export const stylingSettings: UseStylingOptions<SwitchProps, SwitchSlotProps, Sw
}),
['color', ...fontStyles.keys],
),
onOffTextContainer: buildProps(
() => ({
style: {
flexDirection: 'column',
alignItems: 'flex-start',
justifyContent: 'center',
},
}),
[],
),
onOffText: buildProps(
(tokens: SwitchTokens, theme: Theme) => ({
style: {
Expand Down
24 changes: 21 additions & 3 deletions packages/components/Switch/src/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/** @jsx withSlots */
import type { StyleProp } from 'react-native';
import { View, AccessibilityInfo, Pressable, Animated, Platform } from 'react-native';

import type { UseSlots } from '@fluentui-react-native/framework';
import { compose, mergeProps, withSlots } from '@fluentui-react-native/framework';
import { compose, memoize, mergeProps, withSlots } from '@fluentui-react-native/framework';
import { Text } from '@fluentui-react-native/text';
import type { TextStyle } from '@office-iss/react-native-win32';

import { stylingSettings } from './Switch.styling';
import type { SwitchType, SwitchState, SwitchProps } from './Switch.types';
Expand Down Expand Up @@ -44,6 +46,7 @@ export const Switch = compose<SwitchType>({
track: Animated.View, // Conversion from View to Animated.View for Animated API to work
thumb: Animated.View,
toggleContainer: View,
onOffTextContainer: View,
onOffText: Text,
},
useRender: (userProps: SwitchProps, useSlots: UseSlots<SwitchType>) => {
Expand All @@ -68,10 +71,10 @@ export const Switch = compose<SwitchType>({
// now return the handler for finishing render
return (final: SwitchProps) => {
const { label, offText, onText, labelPosition, ...mergedProps } = mergeProps(switchInfo.props, final);
const onOffText = switchInfo.state.toggled ? onText : offText;
const displayOnOffText = !!offText || !!onText;
const isReduceMotionEnabled = AccessibilityInfo.isReduceMotionEnabled;
const thumbAnimation = isReduceMotionEnabled ? { animationClass: 'Ribbon_SwitchThumb' } : null;

return (
<Slots.root {...mergedProps}>
<Slots.label>{label}</Slots.label>
Expand All @@ -80,10 +83,25 @@ export const Switch = compose<SwitchType>({
<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>}
{displayOnOffText && (
/**
* If the onText and offText are different lengths, this can cause unwanted shifting of the track, if labelPosition = "after",
* or the label, if labelPosition = "above". We fix this by rendering both texts and setting the height of the text to hide to
* zero. This way, even when not visible, the hidden text still takes up its width to prevent shifting.
*/
<Slots.onOffTextContainer>
<Slots.onOffText style={getOnOffTextStyle('on', switchInfo.state.toggled)}>{onText}</Slots.onOffText>
<Slots.onOffText style={getOnOffTextStyle('off', switchInfo.state.toggled)}>{offText}</Slots.onOffText>
</Slots.onOffTextContainer>
)}
</Slots.toggleContainer>
</Slots.root>
);
};
},
});

const onOffTextStyleWorker = (text: 'on' | 'off', isOn: boolean): StyleProp<TextStyle> => ({
height: (text === 'on' && isOn) || (text === 'off' && !isOn) ? undefined : 0,
});
const getOnOffTextStyle = memoize(onOffTextStyleWorker);
1 change: 1 addition & 0 deletions packages/components/Switch/src/Switch.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export interface SwitchSlotProps {
track: IViewProps;
thumb: IViewProps;
toggleContainer: IViewProps;
onOffTextContainer: IViewProps;
onOffText: TextProps;
}

Expand Down
10 changes: 0 additions & 10 deletions packages/components/Switch/src/useSwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ export const useSwitch = (props: SwitchProps, animationConfig?: AnimationConfig)
console.warn("The props 'defaultChecked' and 'checked' are mutually exclusive. Use only one of the props, do not use both.");
}

if (labelPosition === 'after' || labelPosition === undefined) {
if (__DEV__ && (!!props.onText || !!props.offText)) {
console.warn(
"The prop labelPosition's value of \"after\" and the props 'onText' or 'offText' are mutually exclusive. Try setting 'labelPosition' value to \"before\" or \"above\" instead.",
);
}
props.onText = null;
props.offText = null;
}

const onClickWithFocus = useOnPressWithFocus(focusRef, toggleCallback);
const pressable = usePressableState({ ...rest, onPress: onClickWithFocus });
const onKeyUpProps = useKeyProps(toggleCallback, ' ', 'Enter');
Expand Down