Skip to content
Open
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
21 changes: 15 additions & 6 deletions src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
import Animated from 'react-native-reanimated';
import type { AnimatedStyle } from 'react-native-reanimated';

import type { TextInputMetrics } from './constants';
import { useTextInput } from './hooks';
import { styles } from './styles';
import TextInputErrorIcon from './TextInputErrorIcon';
Expand Down Expand Up @@ -88,6 +89,7 @@ export type TextInputVariant = 'filled' | 'outlined';
export type TextInputSharedApi = {
input: React.RefObject<React.ComponentRef<typeof NativeTextInput> | null>;
theme: InternalTheme;
metrics: TextInputMetrics;
isFocused: boolean;
isHovered?: boolean;
isRTL: boolean;
Expand Down Expand Up @@ -117,6 +119,7 @@ export type FilledTextInputHookData = SharedTextInputStyleData & {
hasError: boolean;
hasSuffix: boolean;
animatedLabelWrapperStyles: StyleProp<AnimatedStyle<StyleProp<ViewStyle>>>;
labelStyles: StyleProp<ViewStyle>;
containerStyles: StyleProp<ViewStyle>;
fieldStyles: StyleProp<ViewStyle>;
disabledBackgroundStyles: StyleProp<ViewStyle> | undefined;
Expand All @@ -131,6 +134,7 @@ export type OutlinedTextInputHookData = SharedTextInputStyleData & {
hasError: boolean;
hasSuffix: boolean;
animatedLabelWrapperStyles: StyleProp<AnimatedStyle<StyleProp<ViewStyle>>>;
labelStyles: StyleProp<ViewStyle>;
containerStyles: StyleProp<ViewStyle>;
fieldStyles: StyleProp<ViewStyle>;
disabledBackgroundStyles: undefined;
Expand Down Expand Up @@ -165,6 +169,7 @@ export type TextInputHookReturn = SharedTextInputStyleData & {
| StyleProp<AnimatedStyle<StyleProp<TextStyle>>>
| undefined;
animatedLabelWrapperStyles: StyleProp<AnimatedStyle<StyleProp<ViewStyle>>>;
labelStyles: StyleProp<ViewStyle>;
containerStyles: StyleProp<ViewStyle>;
fieldStyles: StyleProp<ViewStyle>;
disabledBackgroundStyles: StyleProp<ViewStyle> | undefined;
Expand Down Expand Up @@ -360,6 +365,7 @@ function TextInput({
outlineStyles,
animatedActiveOutlineStyles,
animatedLabelWrapperStyles,
labelStyles,
animatedLabelTextStyles,
animatedAffixStyle,
containerStyles,
Expand Down Expand Up @@ -446,12 +452,15 @@ function TextInput({

{!!label && (
<Animated.View aria-hidden style={animatedLabelWrapperStyles}>
<Animated.Text
{...accessibilityProps.label}
style={animatedLabelTextStyles}
>
{label}
</Animated.Text>
<View style={labelStyles}>
<Animated.Text
{...accessibilityProps.label}
numberOfLines={1}
style={animatedLabelTextStyles}
>
{label}
</Animated.Text>
</View>
</Animated.View>
)}

Expand Down
113 changes: 61 additions & 52 deletions src/components/TextInput/constants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { PixelRatio } from 'react-native';

import { Easing } from 'react-native-reanimated';

import { tokens } from '../../theme/tokens';
import { motionDuration, motionEasing } from '../../theme/tokens/sys/motion';
import { defaultShapes } from '../../theme/tokens/sys/shape';

export const fontScale = PixelRatio.getFontScale();

/**
* Common constants for the text input component.
*/
Expand All @@ -18,13 +14,6 @@ export const BASELINE_TEXT_INPUT_PADDING_VERTICAL = 8;
export const TEXT_INPUT_INPUT_WRAPPER_PADDING_HORIZONTAL = 16;
export const TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL = 12;

export const TEXT_INPUT_HEIGHT = Math.ceil(
BASELINE_TEXT_INPUT_HEIGHT * fontScale
);
export const TEXT_INPUT_PADDING_VERTICAL = Math.ceil(
BASELINE_TEXT_INPUT_PADDING_VERTICAL * fontScale
);

export const TEXT_INPUT_BORDER_RADIUS = defaultShapes.corner.extraSmall;

export const LABEL_START_OFFSET_WITHOUT_ACCESSORY =
Expand Down Expand Up @@ -70,25 +59,8 @@ export const FILLED_LABEL_START_OFFSET_WITH_ACCESSORY =
TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL +
TEXT_INPUT_INPUT_WRAPPER_PADDING_HORIZONTAL;

export const FILLED_ACTIVE_LABEL_TOP_POSITION = TEXT_INPUT_PADDING_VERTICAL;

export const FILLED_INACTIVE_LABEL_TOP_POSITION = Math.ceil(
((BASELINE_TEXT_INPUT_HEIGHT -
2 * BASELINE_TEXT_INPUT_PADDING_VERTICAL -
INPUT_FONT_SIZE) /
2 +
BASELINE_TEXT_INPUT_PADDING_VERTICAL) *
fontScale
);

export const FILLED_MULTILINE_PADDING_TOP =
Math.ceil(ACTIVE_LABEL_FONT_SIZE * fontScale) + TEXT_INPUT_PADDING_VERTICAL;

export const FILLED_DISABLED_CONTAINER_OPACITY = 0.04;

export const FILLED_PADDING_BOTTOM =
TEXT_INPUT_PADDING_VERTICAL + FILLED_LINE_HEIGHT_DELTA;

/**
* Constants for the outlined variant.
*/
Expand All @@ -97,15 +69,6 @@ const OUTLINED_LINE_HEIGHT_DELTA = 2;

export const OUTLINED_DISABLED_OUTLINE_OPACITY = 0.12;

export const OUTLINED_MULTILINE_PADDING_TOP = Math.ceil(
((BASELINE_TEXT_INPUT_HEIGHT -
2 * BASELINE_TEXT_INPUT_PADDING_VERTICAL -
INPUT_FONT_SIZE) /
2 -
OUTLINED_LINE_HEIGHT_DELTA) *
fontScale
);

export const OUTLINED_LABEL_PADDING_HORIZONTAL = 4;

export const OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY =
Expand All @@ -114,21 +77,6 @@ export const OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY =
TEXT_INPUT_INPUT_WRAPPER_PADDING_HORIZONTAL -
OUTLINED_LABEL_PADDING_HORIZONTAL;

export const OUTLINED_ACTIVE_LABEL_TOP_POSITION = Math.ceil(
(-BASELINE_TEXT_INPUT_PADDING_VERTICAL + OUTLINED_LINE_HEIGHT_DELTA) *
fontScale
);

export const OUTLINED_INACTIVE_LABEL_TOP_POSITION = Math.ceil(
((BASELINE_TEXT_INPUT_HEIGHT -
2 * BASELINE_TEXT_INPUT_PADDING_VERTICAL -
INPUT_FONT_SIZE) /
2 +
BASELINE_TEXT_INPUT_PADDING_VERTICAL -
OUTLINED_LINE_HEIGHT_DELTA) *
fontScale
);

/** Positive distance; apply sign in animation using `isRTL` from `useLocale`. */
export const OUTLINED_LABEL_TRANSLATE_DISTANCE_WITH_ACCESSORY =
ACCESSORY_SIZE +
Expand All @@ -138,3 +86,64 @@ export const OUTLINED_LABEL_TRANSLATE_DISTANCE_WITH_ACCESSORY =
/** Positive distance; apply sign in animation using `isRTL` from `useLocale`. */
export const OUTLINED_LABEL_TRANSLATE_DISTANCE_WITHOUT_ACCESSORY =
OUTLINED_LABEL_PADDING_HORIZONTAL;

/**
* Label end offsets, measured from the field's end edge to the end of the
* input text, so a long label is cut off where the text is.
*/
export const LABEL_END_OFFSET_WITHOUT_ACCESSORY =
TEXT_INPUT_INPUT_WRAPPER_PADDING_HORIZONTAL;

export const LABEL_END_OFFSET_WITH_ACCESSORY =
ACCESSORY_SIZE +
TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL +
TEXT_INPUT_INPUT_WRAPPER_PADDING_HORIZONTAL;

// Space between the input text and the resting label, scaled with the text.
const BASELINE_INACTIVE_LABEL_OFFSET =
(BASELINE_TEXT_INPUT_HEIGHT -
2 * BASELINE_TEXT_INPUT_PADDING_VERTICAL -
INPUT_FONT_SIZE) /
2 +
BASELINE_TEXT_INPUT_PADDING_VERTICAL;

/**
* Sizes that grow with the user's text size. Read the current `fontScale`
* from `useWindowDimensions` so a text size change while the app runs resizes
* the field.
*/
export const getTextInputMetrics = (fontScale: number) => {
const paddingVertical = Math.ceil(
BASELINE_TEXT_INPUT_PADDING_VERTICAL * fontScale
);

return {
height: Math.ceil(BASELINE_TEXT_INPUT_HEIGHT * fontScale),
paddingVertical,
filled: {
activeLabelTop: paddingVertical,
inactiveLabelTop: Math.ceil(BASELINE_INACTIVE_LABEL_OFFSET * fontScale),
multilinePaddingTop:
Math.ceil(ACTIVE_LABEL_FONT_SIZE * fontScale) + paddingVertical,
paddingBottom: paddingVertical + FILLED_LINE_HEIGHT_DELTA,
},
outlined: {
activeLabelTop: Math.ceil(
(-BASELINE_TEXT_INPUT_PADDING_VERTICAL + OUTLINED_LINE_HEIGHT_DELTA) *
fontScale
),
inactiveLabelTop: Math.ceil(
(BASELINE_INACTIVE_LABEL_OFFSET - OUTLINED_LINE_HEIGHT_DELTA) *
fontScale
),
multilinePaddingTop: Math.ceil(
(BASELINE_INACTIVE_LABEL_OFFSET -
BASELINE_TEXT_INPUT_PADDING_VERTICAL -
OUTLINED_LINE_HEIGHT_DELTA) *
fontScale
),
},
};
};

export type TextInputMetrics = ReturnType<typeof getTextInputMetrics>;
45 changes: 37 additions & 8 deletions src/components/TextInput/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
AccessibilityInfo,
Platform,
TextInput as NativeTextInput,
useWindowDimensions,
} from 'react-native';
import type { BlurEvent, FocusEvent } from 'react-native';

Expand All @@ -26,9 +27,11 @@ import { scheduleOnRN } from 'react-native-worklets';
import {
ACTIVE_LABEL_FONT_SIZE,
ERROR_ANNOUNCEMENT_DELAY_MS,
getTextInputMetrics,
INACTIVE_LABEL_FONT_SIZE,
TIMING_CONFIG,
} from './constants';
import type { TextInputMetrics } from './constants';
import type {
TextInputAnimationState,
TextInputFlags,
Expand All @@ -52,16 +55,20 @@ import type { InternalTheme } from '../../theme/types';

const useTextInputAnimation = ({
variant,
metrics,
isRTL,
isDisabled,
hasAccessory,
hasEndAccessory,
value,
defaultValue,
}: {
variant: TextInputVariant;
metrics: TextInputMetrics;
isRTL: boolean;
isDisabled: boolean;
hasAccessory: boolean;
hasEndAccessory: boolean;
value: string | undefined;
defaultValue: string | undefined;
}): TextInputAnimationState & TextInputAnimationHandlers => {
Expand All @@ -82,13 +89,20 @@ const useTextInputAnimation = ({
}
);

const { activeTop, inactiveTop, translateXEnd } = getTextInputAnimationLayout(
{
variant,
hasAccessory,
isRTL,
}
);
const {
activeTop,
inactiveTop,
translateXEnd,
activeEnd,
inactiveEnd,
endKey,
} = getTextInputAnimationLayout({
variant,
metrics,
hasAccessory,
hasEndAccessory,
isRTL,
});

const runFocusAnimation = (hasText: boolean) => {
focusSV.value = withTiming(1, TIMING_CONFIG);
Expand All @@ -110,13 +124,15 @@ const useTextInputAnimation = ({

const animatedLabelWrapperStyle = useAnimatedStyle(() => {
const top = interpolate(floatSV.value, [0, 1], [inactiveTop, activeTop]);
const end = interpolate(floatSV.value, [0, 1], [inactiveEnd, activeEnd]);

if (variant === 'filled') {
return { top };
return { top, [endKey]: end };
}

return {
top,
[endKey]: end,
transform: [
{ translateX: interpolate(floatSV.value, [0, 1], [0, translateXEnd]) },
],
Expand Down Expand Up @@ -280,6 +296,7 @@ const useTextInputLayout = ({
props,
input,
theme,
metrics,
flags,
isFocused,
isHovered,
Expand All @@ -289,6 +306,7 @@ const useTextInputLayout = ({
props: TextInputProps;
input: RefObject<ComponentRef<typeof NativeTextInput> | null>;
theme: InternalTheme;
metrics: TextInputMetrics;
flags: TextInputFlags;
isFocused: boolean;
isHovered: boolean;
Expand All @@ -313,6 +331,7 @@ const useTextInputLayout = ({
{
input,
theme,
metrics,
isFocused,
isHovered,
isRTL,
Expand All @@ -330,6 +349,7 @@ const useTextInputLayout = ({
{
input,
theme,
metrics,
isFocused,
isHovered,
isRTL,
Expand Down Expand Up @@ -364,6 +384,11 @@ export const useTextInput = (props: TextInputProps): TextInputHookReturn => {

const { direction } = useLocale();

// Read the text size on every render, so a change while the app runs
// resizes the field.
const { fontScale } = useWindowDimensions();
const metrics = getTextInputMetrics(fontScale);

const isControlled = props.value !== undefined;
const isRTL = direction === 'rtl';
const hasAccessory = !!props.startAccessory;
Expand All @@ -373,9 +398,12 @@ export const useTextInput = (props: TextInputProps): TextInputHookReturn => {

const animation = useTextInputAnimation({
variant,
metrics,
isRTL,
isDisabled: !!props.disabled,
hasAccessory,
// The error icon takes the end accessory's place when there is none.
hasEndAccessory: !!props.endAccessory || !!props.error,
value: props.value,
defaultValue: props.defaultValue,
});
Expand Down Expand Up @@ -450,6 +478,7 @@ export const useTextInput = (props: TextInputProps): TextInputHookReturn => {
props,
input,
theme,
metrics,
flags,
isFocused,
isHovered,
Expand Down
Loading
Loading