Skip to content

Commit 9894183

Browse files
committed
feat(native): warn on iOS when vertical-align is set on Text
iOS Fabric has no `textAlignVertical` handler so React Native's JS-side verticalAlign→textAlignVertical translation lands on a no-op there. Dev builds now warnOnce with the flex-wrapper workaround. Adds a showcase widget exercising the same path so the warning is reproducible.
1 parent c24b363 commit 9894183

6 files changed

Lines changed: 153 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"styled-components": patch
3+
---
4+
5+
Development builds now warn when `vertical-align` is set on a native `<Text>` running on iOS.
6+
7+
React Native 0.85 has no platform API for vertically aligning text inside a fixed-height `<Text>` on iOS, so the declaration silently has no effect there. Android and rn-web continue to render the property as authored. The warning names the situation and points to the workaround: wrap the Text in a View and use `justify-content` for the vertical alignment.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import styled from 'styled-components/native';
3+
import { theme as t } from '@/theme/tokens';
4+
5+
const Stack = styled.View`
6+
gap: ${t.space.sm}px;
7+
`;
8+
9+
const Tag = styled.Text`
10+
font-family: ${t.fontFamily.monoStrong};
11+
font-size: ${t.fontSize.monoSm}px;
12+
letter-spacing: 0.6px;
13+
text-transform: uppercase;
14+
color: ${t.colors.fgFaint};
15+
`;
16+
17+
const Hello = styled.Text<{ $align: 'top' | 'middle' | 'bottom' }>`
18+
height: 120px;
19+
background-color: #eee;
20+
color: black;
21+
font-size: 24px;
22+
text-decoration: underline #ff00aa;
23+
vertical-align: ${p => p.$align};
24+
`;
25+
26+
const ROWS: ReadonlyArray<'top' | 'middle' | 'bottom'> = ['top', 'middle', 'bottom'];
27+
28+
export function TextDecorationAlignment() {
29+
return (
30+
<Stack>
31+
{ROWS.map(align => (
32+
<React.Fragment key={align}>
33+
<Tag>vertical-align · {align}</Tag>
34+
<Hello $align={align}>Hello</Hello>
35+
</React.Fragment>
36+
))}
37+
</Stack>
38+
);
39+
}

‎packages/native-showcase/src/widgets/registry.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ReducedMotionBeacon } from './ReducedMotionBeacon';
2020
import { SafeAreaInsetsBadge } from './SafeAreaInsetsBadge';
2121
import { ShadowComposer } from './ShadowComposer';
2222
import { StandaloneTransforms } from './StandaloneTransforms';
23+
import { TextDecorationAlignment } from './TextDecorationAlignment';
2324
import { TextWrapShelf } from './TextWrapShelf';
2425
import { ThemeOverrides } from './ThemeOverrides';
2526
import { TransformPlayground } from './TransformPlayground';
@@ -220,6 +221,15 @@ export const fidgets: ReadonlyArray<FidgetEntry> = [
220221
category: 'Typography',
221222
Widget: TextWrapShelf,
222223
},
224+
{
225+
slug: 'text-decoration-alignment',
226+
title: 'Decoration vs vertical alignment',
227+
summary:
228+
'Tall `<Text>` boxes with `textAlignVertical` `top` / `center` / `bottom`. The magenta underline must track the glyphs vertically — drift on Android exposes a missing gravity offset in the decoration draw.',
229+
feature: 'textDecoration + textAlignVertical',
230+
category: 'Typography',
231+
Widget: TextDecorationAlignment,
232+
},
223233
{
224234
slug: 'safe-area-insets-badge',
225235
title: 'Safe-area insets badge',

‎packages/styled-components/src/native/transform/dev.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,31 @@ export function warnIfAndroidSkew(value: unknown): void {
142142
`\`skewX\` / \`skewY\` are silently dropped on Android (seen in transform: "${value}"). Android's \`View\` has no \`setSkew*\` API; RN's BaseViewManager decomposes the transform matrix and only applies translation, rotation, and scale to the View. Translates and rotates in the same value still apply;only the skew components are lost. Tracked at https://github.com/facebook/react-native/issues/27649.`
143143
);
144144
}
145+
146+
/**
147+
* Warn once if `vertical-align` is set on iOS. RN's `Text.js` translates
148+
* `verticalAlign` to `textAlignVertical` at the JS layer, but iOS Fabric
149+
* has no `textAlignVertical` handler (the prop only registers on Android
150+
* via `ReactTextViewManager`). So on iOS the declaration looks like it
151+
* works but never repositions the glyphs. There is no Text-level
152+
* workaround in RN 0.85; wrap the Text in a View and use flex
153+
* justification to vertically align the content within a fixed-height
154+
* container.
155+
*
156+
* No-op outside iOS. Callers must wrap in `if (__DEV__)`.
157+
*/
158+
export function warnIfIosVerticalAlign(value: string): void {
159+
if (value === 'auto') return;
160+
let os: string | undefined;
161+
try {
162+
os = (require('react-native') as { Platform?: { OS?: string } }).Platform?.OS;
163+
} catch {
164+
return;
165+
}
166+
if (os !== 'ios') return;
167+
warnOnce(
168+
'native-vertical-align-ios',
169+
`\`vertical-align: ${value}\` has no effect on iOS \`<Text>\` in React Native 0.85 (no platform API; the prop only renders on Android and rn-web). To vertically align text within a fixed-height container on iOS, wrap the Text in a View with \`justify-content: <flex-start | center | flex-end>\`.`,
170+
value
171+
);
172+
}

‎packages/styled-components/src/native/transform/index.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Dict } from '../../types';
22
import * as $ from '../../utils/charCodes';
33
import hyphenateStyleName from '../../utils/hyphenateStyleName';
4-
import { warnIfAndroidSkew, warnOnce } from './dev';
4+
import { warnIfAndroidSkew, warnIfIosVerticalAlign, warnOnce } from './dev';
55
import { collapseIdenticalCommas, getPassthroughKeys, isLayeredCommaProp } from './passthrough';
66
import { staticColorFunctionToHex } from './polyfills/colorMath';
77
import { numericResultToRn, resolveStaticMathFunction } from './polyfills/mathFns';
@@ -78,8 +78,12 @@ export function transformDecl(prop: string, rawValue: string): Dict<any> {
7878

7979
const passthroughKeys = getPassthroughKeys(camel);
8080
if (passthroughKeys !== undefined) {
81-
if (__DEV__ && passthroughKeys[0] === 'transform') {
82-
warnIfAndroidSkew(rawValue);
81+
if (__DEV__) {
82+
if (passthroughKeys[0] === 'transform') {
83+
warnIfAndroidSkew(rawValue);
84+
} else if (camel === 'verticalAlign') {
85+
warnIfIosVerticalAlign(rawValue);
86+
}
8387
}
8488
const value = isLayeredCommaProp(camel) ? collapseIdenticalCommas(rawValue) : rawValue;
8589
if (passthroughKeys.length === 1) {

‎packages/styled-components/src/native/transform/test/devWarn.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getUserCallSite,
44
resetWarningsForTest,
55
warnIfAndroidSkew,
6+
warnIfIosVerticalAlign,
67
warnIfSentinelLeak,
78
} from '../dev';
89

@@ -167,6 +168,67 @@ describe('warnIfAndroidSkew', () => {
167168
});
168169
});
169170

171+
describe('warnIfIosVerticalAlign', () => {
172+
const realOS = Platform.OS;
173+
let warnSpy: jest.SpyInstance;
174+
175+
beforeEach(() => {
176+
resetWarningsForTest();
177+
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
178+
});
179+
180+
afterEach(() => {
181+
warnSpy.mockRestore();
182+
Object.defineProperty(Platform, 'OS', { value: realOS, configurable: true });
183+
});
184+
185+
function setPlatform(os: 'ios' | 'android' | 'web'): void {
186+
Object.defineProperty(Platform, 'OS', { value: os, configurable: true });
187+
}
188+
189+
it('warns when middle is set on iOS', () => {
190+
setPlatform('ios');
191+
warnIfIosVerticalAlign('middle');
192+
expect(warnSpy).toHaveBeenCalledTimes(1);
193+
expect(warnSpy.mock.calls[0][0]).toMatch(/`vertical-align: middle` has no effect on iOS/);
194+
expect(warnSpy.mock.calls[0][0]).toMatch(/wrap the Text in a View with `justify-content/);
195+
});
196+
197+
it('warns for top and bottom too', () => {
198+
setPlatform('ios');
199+
warnIfIosVerticalAlign('top');
200+
warnIfIosVerticalAlign('bottom');
201+
expect(warnSpy).toHaveBeenCalledTimes(2);
202+
});
203+
204+
it('does NOT warn for the default `auto` value', () => {
205+
setPlatform('ios');
206+
warnIfIosVerticalAlign('auto');
207+
expect(warnSpy).not.toHaveBeenCalled();
208+
});
209+
210+
it('does NOT warn on Android', () => {
211+
setPlatform('android');
212+
warnIfIosVerticalAlign('middle');
213+
expect(warnSpy).not.toHaveBeenCalled();
214+
});
215+
216+
it('does NOT warn on web', () => {
217+
setPlatform('web');
218+
warnIfIosVerticalAlign('middle');
219+
expect(warnSpy).not.toHaveBeenCalled();
220+
});
221+
222+
it('dedupes per value so each distinct value warns once', () => {
223+
setPlatform('ios');
224+
warnIfIosVerticalAlign('middle');
225+
warnIfIosVerticalAlign('middle');
226+
warnIfIosVerticalAlign('bottom');
227+
warnIfIosVerticalAlign('bottom');
228+
expect(warnSpy).toHaveBeenCalledTimes(2);
229+
});
230+
});
231+
170232
describe('warning call-site picker', () => {
171233
it('picks the first user-code frame from a typical stack', () => {
172234
const stack = [

0 commit comments

Comments
 (0)