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

Commit 531e03d

Browse files
feat(native): say how a path is opened, and type a DatePicker's locale
Two things a Craft host now reads that a screen had no way to say. craft.navigation.open(path, { presentation: 'cover' }) opens the path over everything, in a stack of its own (craft-native/craft#526). The host reads that from the same message as the path, and the bridge sent the path alone, so the option went nowhere. A chrome button or a menu choice with an open path takes presentation too, and passes it on when pressed. DatePickerProps has locale (craft-native/craft#546): the language and region a picker writes its dates in. The prop already reached the host; a typed component could not name it.
1 parent a7d3273 commit 531e03d

6 files changed

Lines changed: 117 additions & 10 deletions

File tree

‎packages/stx/src/native/components/primitives.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ export interface DatePickerProps extends BaseProps {
431431
minuteInterval?: number
432432
/** `compact` (the default) shows a button that opens a calendar or clock; `inline` and `wheels` show them in place. */
433433
display?: 'compact' | 'inline' | 'wheels'
434+
/**
435+
* The language and region the picker writes its dates in, as a BCP 47 tag
436+
* (`de`, `en-GB`): "16. Nov. 2026" where the device alone would write
437+
* "Nov 16, 2026". Left out, the device's own. The value it reports does not
438+
* change with it.
439+
*/
440+
locale?: string
434441
/** A change, as `@change` hears it. */
435442
onValueChange?: (event: DatePickerChange) => void
436443
/** DOM-compatible change alias accepted by native hosts. */

‎packages/stx/src/native/runtime/craft-bridge.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ export function installCraftBridge(
11111111
const merged = mergeChromeButtons(ownButtons[side] as NativeNavigationButton[], effectiveChrome(), side, { name: craft.route?.name ?? '', path: craft.route?.path })
11121112
const presses = navButtonPresses[side]
11131113
presses.clear()
1114-
buttonPresses(merged, path => craft.navigation.open(path)).forEach((press, id) => presses.set(id, press))
1114+
buttonPresses(merged, (path, shown) => craft.navigation.open(path, shown)).forEach((press, id) => presses.set(id, press))
11151115
sentButtons[side] = buttonsKey(merged)
11161116
return merged.map(hostButton)
11171117
}
@@ -1122,9 +1122,11 @@ export function installCraftBridge(
11221122
back: () => protocol.goBack(),
11231123
// A finished task (a saved workout) closes to where its stack began.
11241124
popToRoot: () => protocol.popToRoot(),
1125-
open: (path: string) => {
1125+
open: (path: string, shown?: { presentation?: string }) => {
11261126
if (typeof path !== 'string' || !path.trim()) throw new TypeError('navigation.open needs a path, such as /m/workout/42')
1127-
return protocol.send('NAVIGATE_OPEN', { path })
1127+
// The host reads how to show it from the same message as the path.
1128+
const presentation = typeof shown?.presentation === 'string' && shown.presentation ? shown.presentation : undefined
1129+
return protocol.send('NAVIGATE_OPEN', presentation ? { path, presentation } : { path })
11281130
},
11291131
setOptions: (navigationOptions: AnyRecord) => {
11301132
if (!navigationOptions || typeof navigationOptions !== 'object') throw new TypeError('navigation.setOptions needs an object')

‎packages/stx/src/native/runtime/craft.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ import type { NativeLiveActivities } from '../live-activity'
1313
*/
1414

1515
/** One choice in a bar button's native menu. */
16+
/** How `navigation.open` shows the path it opens. */
17+
export interface NativeOpenOptions {
18+
/**
19+
* `'cover'`: over everything (the tab bar and the floating actions
20+
* included), sliding up, first in a navigation stack of its own with a close
21+
* button. Paths opened from it push inside it, and Back from its first
22+
* screen closes it. Left out, the path is pushed as before.
23+
*/
24+
presentation?: 'cover'
25+
}
26+
1627
export interface NativeNavigationMenuItem {
1728
id: string
1829
title: string
@@ -22,6 +33,8 @@ export interface NativeNavigationMenuItem {
2233
destructive?: boolean
2334
/** Where choosing it goes (`craft.navigation.open`). Left out on the screen it opens. */
2435
open?: string
36+
/** How `open` is shown: `'cover'` opens it over everything (see NativeOpenOptions). */
37+
presentation?: NativeOpenOptions['presentation']
2538
/** Kept in the screen; the host only sees the id. */
2639
onPress?: (event: { id: string }) => unknown
2740
}
@@ -83,6 +96,8 @@ export interface NativeAccessoryCompanion {
8396
title: string
8497
symbol: string
8598
accessibilityLabel?: string
99+
/** `'cover'`: a tap opens `path` over everything (see NativeOpenOptions). Left out, it is pushed. */
100+
presentation?: NativeOpenOptions['presentation']
86101
/**
87102
* On: drawn in the app's tint, filled where the symbol has a fill, and
88103
* announced as selected. For a mode the action switched (Maps' location
@@ -132,6 +147,8 @@ export interface NativeChromeButton extends NativeNavigationButton {
132147
* left off the screen already at that path.
133148
*/
134149
open?: string
150+
/** How `open` is shown: `'cover'` opens it over everything (see NativeOpenOptions). */
151+
presentation?: NativeOpenOptions['presentation']
135152
/** Screens (`craft.route.name`) it is left off as well. */
136153
hideOn?: string[]
137154
}
@@ -166,8 +183,9 @@ export interface NativeNavigation {
166183
/**
167184
* Open a path of the app (`/m/workout/42`). In a hybrid app the host shows
168185
* the native screen mapped to it, or pushes the web view at that path.
186+
* `{ presentation: 'cover' }` opens it over everything instead.
169187
*/
170-
open: (path: string) => string
188+
open: (path: string, options?: NativeOpenOptions) => string
171189
setOptions: (options: NativeNavigationOptions) => unknown
172190
/** A toolbar button was tapped; returns an unsubscribe function. */
173191
onButton: (callback: (event: { id: string }) => unknown) => () => void

‎packages/stx/src/native/runtime/navigation-chrome.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* what it returns.
1414
*/
1515

16-
import type { NativeChromeButton, NativeNavigationButton, NativeNavigationChrome, NativeTabItemOptions } from './craft'
16+
import type { NativeChromeButton, NativeNavigationButton, NativeNavigationChrome, NativeOpenOptions, NativeTabItemOptions } from './craft'
1717

1818
export type ButtonSide = 'leftButtons' | 'rightButtons'
1919

@@ -71,18 +71,20 @@ export function mergeChromeButtons(
7171

7272
/** What the host is shown for a button: no callbacks, and nothing chrome-only. */
7373
export function hostButton(button: NativeChromeButton): NativeNavigationButton {
74-
const { onPress, open, hideOn, menu, ...rest } = button
74+
const { onPress, open, presentation, hideOn, menu, ...rest } = button
7575
if (!menu) return rest
76-
return { ...rest, menu: menu.map(({ onPress: press, open: path, ...item }) => item) }
76+
return { ...rest, menu: menu.map(({ onPress: press, open: path, presentation: shown, ...item }) => item) }
7777
}
7878

7979
/** What pressing each button, or choosing from its menu, does in the screen. */
80-
export function buttonPresses(buttons: readonly NativeChromeButton[], open: (path: string) => unknown): Map<string, (event: { id: string }) => unknown> {
80+
export function buttonPresses(buttons: readonly NativeChromeButton[], open: (path: string, options?: NativeOpenOptions) => unknown): Map<string, (event: { id: string }) => unknown> {
8181
const presses = new Map<string, (event: { id: string }) => unknown>()
82-
const add = (target: { id: string, open?: string, onPress?: (event: { id: string }) => unknown }) => {
82+
const add = (target: { id: string, open?: string, presentation?: NativeOpenOptions['presentation'], onPress?: (event: { id: string }) => unknown }) => {
8383
const path = target.open
84+
// How it is shown goes with the path, as a call to navigation.open would give it.
85+
const shown = target.presentation ? { presentation: target.presentation } : undefined
8486
if (typeof target.onPress === 'function') presses.set(String(target.id), target.onPress)
85-
else if (path) presses.set(String(target.id), () => open(path))
87+
else if (path) presses.set(String(target.id), () => shown ? open(path, shown) : open(path))
8688
}
8789
for (const button of buttons) {
8890
add(button)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* A DatePicker is told which language to write its dates in
3+
* (craft-native/craft#546).
4+
*
5+
* The host writes dates the way the device is set, so an account whose
6+
* interface is German on an English phone read "Nov 16, 2026" in a German
7+
* screen. The host now takes `locale`; this is that prop reaching it, typed,
8+
* written or bound, and following its signal.
9+
*/
10+
import { afterEach, expect, it } from 'bun:test'
11+
import { compileNativeScreen, mountNativeScreen, unmountAllNativeScreens } from '../../src/native/testing'
12+
import type { DatePickerProps } from '../../src/native/components/primitives'
13+
14+
afterEach(unmountAllNativeScreens)
15+
16+
it('is a typed prop', () => {
17+
const props: DatePickerProps = { mode: 'date', value: '2026-11-16', locale: 'de-DE' }
18+
expect(props.locale).toBe('de-DE')
19+
})
20+
21+
it.each(['ios', 'android'] as const)('reaches the host written and bound, and follows its signal on %s', async (platform) => {
22+
const { code } = await compileNativeScreen(`<script client>
23+
const language = state('de')
24+
window.probe = { language }
25+
</script>
26+
<section>
27+
<DatePicker testID="written" mode="date" value="2026-11-16" locale="en-GB" />
28+
<DatePicker testID="bound" mode="date" value="2026-11-16" :locale="language()" />
29+
<DatePicker testID="device" mode="date" value="2026-11-16" />
30+
</section>`)
31+
const phone = mountNativeScreen(code, 'Screen', { platform })
32+
await phone.settle()
33+
expect(phone.byTestID('written')!.type).toBe('DatePicker')
34+
expect(phone.byTestID('written')!.props.locale).toBe('en-GB')
35+
expect(phone.byTestID('bound')!.props.locale).toBe('de')
36+
// Left out, the device's own: nothing is sent.
37+
expect(phone.byTestID('device')!.props.locale).toBeUndefined()
38+
39+
phone.context.probe.language.set('fr-CA')
40+
await phone.settle()
41+
expect(phone.byTestID('bound')!.props.locale).toBe('fr-CA')
42+
expect(phone.byTestID('bound')!.props.value).toBe('2026-11-16')
43+
}, 30_000)

‎packages/stx/test/native/navigation-chrome.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,41 @@ describe('craft.navigation.setChrome', () => {
8686
protocol.dispose()
8787
})
8888

89+
/*
90+
* craft-native/craft#526: a path can be opened over everything, in a stack
91+
* of its own, and the host reads that from the same message as the path.
92+
* The bridge sent the path alone, so `presentation: 'cover'` on a button or
93+
* in a call went nowhere and the screen was pushed like any other.
94+
*/
95+
it('says how a path is to be shown, from a call and from a button or a menu choice', () => {
96+
const { craft, sent, options, press, protocol } = host({ name: 'Feed', url: '/feed' })
97+
craft.navigation.open('/compose', { presentation: 'cover' })
98+
craft.navigation.open('/feed/7')
99+
craft.navigation.setChrome({ rightButtons: [
100+
{ id: 'new', symbol: 'plus', accessibilityLabel: 'New post', open: '/compose', presentation: 'cover' },
101+
{ id: 'more', symbol: 'ellipsis', accessibilityLabel: 'More', menu: [
102+
{ id: 'scan', title: 'Scan', open: '/scan', presentation: 'cover' },
103+
{ id: 'store', title: 'Store', open: '/store' },
104+
] },
105+
] })
106+
// It is the screen's to act on: the host is not sent it with the button.
107+
expect(options().at(-1)).toEqual({ rightButtons: [
108+
{ id: 'new', symbol: 'plus', accessibilityLabel: 'New post' },
109+
{ id: 'more', symbol: 'ellipsis', accessibilityLabel: 'More', menu: [{ id: 'scan', title: 'Scan' }, { id: 'store', title: 'Store' }] },
110+
] })
111+
press('new')
112+
press('scan')
113+
press('store')
114+
expect(sent.filter(message => message.type === 'NAVIGATE_OPEN').map(message => message.payload)).toEqual([
115+
{ path: '/compose', presentation: 'cover' },
116+
{ path: '/feed/7' },
117+
{ path: '/compose', presentation: 'cover' },
118+
{ path: '/scan', presentation: 'cover' },
119+
{ path: '/store' },
120+
])
121+
protocol.dispose()
122+
})
123+
89124
it('sends a button menu without callbacks or paths, and runs the choice that was picked', () => {
90125
const { craft, sent, options, press, protocol } = host({ name: 'Feed', url: '/feed' })
91126
const heard: string[] = []

0 commit comments

Comments
 (0)