镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit a7d3273

Browse files
fix(native): hand a node the props its classes imply on the first draw too (#2195)
object-cover is not a style on a phone: it is the Image's resizeMode prop, as truncate is a Text's numberOfLines. The host was handed those only for a node created after the screen's first draw. One that was on screen from the start got its style and nothing else, and so did every node on the redraw that follows a refused batch. So a cover photo whose address was already known at the first draw reached the host with no resizeMode and was drawn fitted between two bars, while the thumbnails in a loop under it, created a moment later with the same classes, filled their boxes. A truncated title on screen from the start was not truncated either. The first draw and the redraw now carry them, as creation already did.
1 parent 02935c2 commit a7d3273

2 files changed

Lines changed: 126 additions & 12 deletions

File tree

‎packages/stx/src/native/runtime/native-host.ts‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,21 +1071,38 @@ export function createNativeHost(options: NativeHostOptions): NativeHost {
10711071
}
10721072
record.reportedClasses = unsupported
10731073
}
1074+
const derived = classProps(id, record, classes)
1075+
if (derived) {
1076+
Object.assign(record.props, derived)
1077+
if (record.materialized) emit({ op: 'updateNode', id, patch: { props: derived } })
1078+
}
1079+
}
1080+
1081+
/**
1082+
* What a node's classes say about its props rather than its style: an
1083+
* icon's symbol, a line limit (`truncate`, `line-clamp-2`), how an image
1084+
* fills its box (`object-cover`). Undefined when they say nothing.
1085+
*
1086+
* A host reads these as props, so every way a node comes to exist has to
1087+
* hand them over: created later (materialize), drawn with the screen
1088+
* (adopt) and redrawn after a refused batch (snapshot). The last two used
1089+
* to resolve the style alone. An `object-cover` image that was on screen at
1090+
* the first draw reached the host with no resizeMode and was drawn fitted
1091+
* between two bars, while the same image inside a loop, created a moment
1092+
* later, filled its box (stacksjs/stx#2195).
1093+
*/
1094+
function classProps(id: string, record: NodeRecord, classes: ReturnType<typeof classStylesOf>): Record<string, unknown> | undefined {
1095+
let derived: Record<string, unknown> | undefined
10741096
if (record.type === 'Icon' && classes.icon) {
10751097
const icon = resolveNodeIcon(id, record, classes.icon)
1076-
record.props.symbol = icon.symbol
1077-
record.props.iconify = icon.iconify
1078-
if (record.materialized) emit({ op: 'updateNode', id, patch: { props: { symbol: record.props.symbol, iconify: icon.iconify } } })
1079-
}
1080-
if (classes.numberOfLines !== undefined) {
1081-
record.props.numberOfLines = classes.numberOfLines
1082-
if (record.materialized) emit({ op: 'updateNode', id, patch: { props: { numberOfLines: classes.numberOfLines } } })
1098+
derived = { symbol: icon.symbol, iconify: icon.iconify }
10831099
}
1100+
if (classes.numberOfLines !== undefined)
1101+
(derived ??= {}).numberOfLines = classes.numberOfLines
10841102
// `object-cover` on an <img> or <video>: how it fills its box.
1085-
if (classes.resizeMode && (record.type === 'Image' || record.type === 'ImageBackground' || record.type === 'Video') && record.props.resizeMode !== classes.resizeMode) {
1086-
record.props.resizeMode = classes.resizeMode
1087-
if (record.materialized) emit({ op: 'updateNode', id, patch: { props: { resizeMode: classes.resizeMode } } })
1088-
}
1103+
if (classes.resizeMode && (record.type === 'Image' || record.type === 'ImageBackground' || record.type === 'Video') && record.props.resizeMode !== classes.resizeMode)
1104+
(derived ??= {}).resizeMode = classes.resizeMode
1105+
return derived
10891106
}
10901107

10911108
function resolveNodeIcon(id: string, record: NodeRecord, name: string) {
@@ -1269,6 +1286,15 @@ export function createNativeHost(options: NativeHostOptions): NativeHost {
12691286
}
12701287
record.style = resolveStyle(record, node.__stxId)
12711288
if (descriptor) descriptor.style = { ...record.style } as STXNode['style']
1289+
// A node drawn with the screen is never created again, so what its
1290+
// classes say about its props goes out with that first draw (#2195).
1291+
if (classes) {
1292+
const derived = classProps(node.__stxId, record, classStylesOf(record, false))
1293+
if (derived) {
1294+
Object.assign(record.props, derived)
1295+
if (descriptor) Object.assign(descriptor.props, derived)
1296+
}
1297+
}
12721298
records.set(node.__stxId, record)
12731299
decorateControl(node, type)
12741300
decoratePosition(node)
@@ -1485,8 +1511,11 @@ export function createNativeHost(options: NativeHostOptions): NativeHost {
14851511
queue = []
14861512
const describe = (node: NativeNode): STXNode => {
14871513
const record = recordOf(node)
1488-
if (!record.materialized)
1514+
if (!record.materialized) {
14891515
record.style = resolveStyle(record, node.__stxId)
1516+
const derived = classProps(node.__stxId, record, classStylesOf(record, false))
1517+
if (derived) Object.assign(record.props, derived)
1518+
}
14901519
record.materialized = true
14911520
const children = record.children
14921521
.filter(child => child.__stxAnchor === undefined)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* What a node's classes say about its props reaches the host however the
3+
* node comes to exist (stacksjs/stx#2195).
4+
*
5+
* `object-cover` is not a style on a phone. It is the Image's `resizeMode`
6+
* prop, as `truncate` is a Text's `numberOfLines`. The host was handed them
7+
* only for a node created after the screen's first draw. One that was on
8+
* screen from the start, and every node on a redraw, got its style and
9+
* nothing else, so a product's cover photo that had loaded before the first
10+
* draw was fitted between two bars while the thumbnails in the loop under it,
11+
* created a moment later with the very same classes, filled their boxes.
12+
*/
13+
import { afterEach, describe, expect, it } from 'bun:test'
14+
import { compileNativeScreen, mountNativeScreen, unmountAllNativeScreens } from '../../src/native/testing'
15+
16+
afterEach(unmountAllNativeScreens)
17+
18+
const screen = (initial: string): string => `<script client>
19+
const product = state(${initial})
20+
const others = state([])
21+
window.probe = { product, others }
22+
</script>
23+
<section>
24+
<div class="flex overflow-hidden relative items-center justify-center h-[200px] rounded-[12px]">
25+
<img testID="cover" :if="product().cover" :src="product().cover" alt="" class="absolute inset-0 object-cover h-full w-full" />
26+
</div>
27+
<img testID="fixed" src="https://example.com/fixed.jpg" alt="" class="object-contain h-10 w-10" />
28+
<span testID="title" class="truncate">A long product title</span>
29+
<span testID="blurb" class="line-clamp-2">Two lines of description at the most</span>
30+
<div :for="other in others()" :key="other.id">
31+
<img testID="thumb" :src="other.cover" alt="" class="object-cover h-12 w-12" />
32+
</div>
33+
</section>`
34+
35+
/** The props a node was created with, or first drawn with, before any update. */
36+
function patched(phone: ReturnType<typeof mountNativeScreen>, id: string, prop: string): boolean {
37+
return phone.operations().some((op: any) => op.op === 'updateNode' && op.id === id && op.patch?.props && prop in op.patch.props)
38+
}
39+
40+
describe.each(['ios', 'android'] as const)('class-derived props on %s', (platform) => {
41+
it('are part of the first draw for what is on screen from the start', async () => {
42+
const { code } = await compileNativeScreen(screen(`{ cover: 'https://example.com/cover.jpg' }`))
43+
const phone = mountNativeScreen(code, 'Screen', { platform })
44+
await phone.settle()
45+
46+
const cover = phone.byTestID('cover')!
47+
expect(cover.props.resizeMode).toBe('cover')
48+
expect(phone.byTestID('fixed')!.props.resizeMode).toBe('contain')
49+
expect(phone.byTestID('title')!.props.numberOfLines).toBe(1)
50+
expect(phone.byTestID('blurb')!.props.numberOfLines).toBe(2)
51+
// Drawn that way, not corrected afterwards: a photo that is fitted and
52+
// then filled is seen to jump.
53+
expect(patched(phone, cover.id, 'resizeMode')).toBe(false)
54+
}, 30_000)
55+
56+
it('come with a node revealed later and with a row of a loop', async () => {
57+
const { code } = await compileNativeScreen(screen('{}'))
58+
const phone = mountNativeScreen(code, 'Screen', { platform })
59+
await phone.settle()
60+
expect(phone.byTestID('cover')).toBeUndefined()
61+
62+
phone.context.probe.product.set({ cover: 'https://example.com/cover.jpg' })
63+
phone.context.probe.others.set([{ id: 1, cover: 'https://example.com/a.jpg' }, { id: 2, cover: 'https://example.com/b.jpg' }])
64+
await phone.settle()
65+
expect(phone.byTestID('cover')!.props.resizeMode).toBe('cover')
66+
expect(phone.all().filter(node => node.props.testID === 'thumb').map(node => node.props.resizeMode)).toEqual(['cover', 'cover'])
67+
}, 30_000)
68+
69+
it('survive the redraw that follows a refused batch', async () => {
70+
const { code } = await compileNativeScreen(screen(`{ cover: 'https://example.com/cover.jpg' }`))
71+
const phone = mountNativeScreen(code, 'Screen', { platform })
72+
await phone.settle()
73+
phone.context.probe.others.set([{ id: 1, cover: 'https://example.com/a.jpg' }])
74+
await phone.settle()
75+
76+
// The host refuses a batch, and the screen is drawn again from what the
77+
// runtime holds.
78+
phone.send('MUTATION_ERROR', { code: 'UNKNOWN_NODE', message: 'refused for the test' })
79+
await phone.settle()
80+
expect(phone.byTestID('cover')!.props.resizeMode).toBe('cover')
81+
expect(phone.byTestID('fixed')!.props.resizeMode).toBe('contain')
82+
expect(phone.byTestID('thumb')!.props.resizeMode).toBe('cover')
83+
expect(phone.byTestID('title')!.props.numberOfLines).toBe(1)
84+
}, 30_000)
85+
})

0 commit comments

Comments
 (0)