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

Commit 36b0ffc

Browse files
chrisbbreuerclaude
andcommitted
fix(native): draw the shared controls as AppKit's own on the Mac host
The shared Button resolved to the phone's filled pressable on every native host and read `title` once on the server. On Craft's AppKit host that drew a tall blue button with no words for `<Button :title="...">`, ignored `symbol`, a caller's font size and colour, and drew nothing for a ghost button whose only words were a bound title. - Button is the host's push button (an NSButton) on macOS, on every variant and for `appearance="mac"`: `title`, `label`, `text`, `symbol` and `disabled` are live, a written `primary`, `danger`, `ghost` or `toolbar` asks the host for that look and anything else is the plain push button. A phone keeps the look it had. - Which host a screen runs on is known only on the device, so a component may mark several host roots and keep one. What the caller wrote on the tag is forwarded to each at compile time, and a bound attribute is delivered to each at run time. - Select feeds the picker `options` as well as `items` (the name the AppKit NSPopUpButton reads) and follows a bound `disabled`. - `variant="mac"` on the AppKit host sizes TextInput, PasswordInput, Checkbox, Switch, Select and SegmentedControl as AppKit does. Checkbox no longer takes its stylesheet-only Mac markup on a native host. - `iconOnly` keeps its spelling on the way to the host. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 6b2f63b commit 36b0ffc

17 files changed

Lines changed: 613 additions & 41 deletions

‎packages/components/src/ui/button/Button.stx‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ export const nativeButtonClasses = `flex-row items-center justify-center ${nativ
181181
export const nativeTextClasses = `font-semibold ${nativeSize.text} ${nativeVariant.text}`
182182
export const legacyNativeTint = variant === 'primary' || variant === 'danger' ? '#ffffff' : '#007aff'
183183
export const nativeTintClass = nativeVariant.text
184+
/*
185+
* On a Mac (Craft's AppKit host) a button is the system's push button, an
186+
* NSButton the host draws, not the phone's filled one: 13pt words on the
187+
* standard bezel. The host knows four looks by name and only a written
188+
* `variant` asks for one: `primary` is the default (accent) button, `danger`
189+
* the destructive one, `ghost` a borderless one, `toolbar` the toolbar's.
190+
* Anything else, and no variant at all, is the plain push button, as
191+
* `appearance="mac"` has it on the web, because on a Mac most buttons are.
192+
* Which host this is is only known on the device, so both are in the screen
193+
* and the client script keeps one.
194+
*/
195+
const desktopVariants = ['primary', 'danger', 'ghost', 'toolbar']
196+
export const desktopVariant = desktopVariants.includes($props.variant) ? $props.variant : ''
197+
/** `title` names the button only when nothing in the slot does: beside content it is the tooltip. */
198+
export const titleNames = !slotWords
184199
</script>
185200

186201
<script client>
@@ -213,7 +228,18 @@ const labelProp = useReactiveProp('label', {{ label }}, { parse: value => value
213228
// Preserve native Button's text prop when the same shared file is selected.
214229
// Read it as a prop, not a root text binding that would erase icons/spinners.
215230
const textProp = useReactiveProp('text', '', { parse: value => value == null ? '' : String(value) })
216-
const labelText = derived(() => labelProp() || textProp())
231+
/*
232+
* `title` is the desktop spelling of the words (stacksjs/stx#2091), and it is
233+
* live as `label` is: `:title="busy() ? 'One moment' : 'Sign In'"` is a
234+
* binding the server never sees, so read once there it was no words at all
235+
* and the button drew empty. Beside slot content it stays the tooltip.
236+
*/
237+
const titleNames = {{ titleNames }}
238+
const titleProp = useReactiveProp('title', {{ titleWords }}, { parse: value => value == null ? '' : String(value) })
239+
const labelText = derived(() => labelProp() || textProp() || (titleNames ? titleProp() : ''))
240+
const symbolProp = useReactiveProp('symbol', {{ symbol }}, { parse: value => value == null ? '' : String(value) })
241+
// Craft's AppKit host: the button is the system's own (see the server script).
242+
const desktop = typeof craft !== 'undefined' && craft.platform === 'macos'
217243
const isLoading = useReactiveProp('loading', {{ loading }})
218244
// The accessible name is live too. Read once on the server, a bound one
219245
// (:ariaLabel="action.label" in a row of icon buttons) stayed on the boxless
@@ -237,11 +263,38 @@ function onClick(event) {
237263
</script>
238264

239265
@if($native)
266+
{{-- A Mac: the host's push button. Its words are `title` (the label, or the
267+
slot's text when there is no label), its glyph `symbol`. A caller's
268+
`class` and `style` land on it, so a font size and a colour are the
269+
button's own. --}}
270+
<button
271+
:if="desktop"
272+
data-native="Button"
273+
data-stx-native-host-root
274+
type="{{ type }}"
275+
class="text-accent {{ fullWidth ? 'w-full' : '' }} {{ className }}"
276+
@if(desktopVariant)variant="{{ desktopVariant }}"@endif
277+
@if(symbol)symbol="{{ symbol }}"@endif
278+
:symbol="symbolProp() || null"
279+
@if(iconOnly)iconOnly="true"@endif
280+
@if(label)title="{{ label }}"@endif
281+
:title="labelText() || null"
282+
@if(isDisabled)disabled@endif
283+
:disabled="isDisabled()"
284+
aria-busy="{{ loading ? 'true' : 'false' }}"
285+
:aria-busy="isLoading() ? 'true' : 'false'"
286+
@if(ariaLabel)aria-label="{{ ariaLabel }}"@endif
287+
:aria-label="String(ariaLabelProp() ?? '') || null"
288+
@click="onClick($event)"
289+
><slot /></button>
240290
<button
291+
:if="!desktop"
292+
data-stx-native-host-root
241293
type="{{ type }}"
242294
class="{{ nativeButtonClasses }}"
243295
activeOpacity="0.6"
244296
@if(symbol)symbol="{{ symbol }}"@endif
297+
:symbol="symbolProp() || null"
245298
@if(iconOnly)iconOnly="true"@endif
246299
@if(isDisabled)disabled@endif
247300
:disabled="isDisabled()"

‎packages/components/src/ui/checkbox/Checkbox.stx‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ export const switchAppearance = $props.appearance === 'switch'
2020
* are the browser's own; only the classes change, to `stx-mac-checkbox`,
2121
* which this file's <style> block draws from the `--stx-mac-*` tokens.
2222
*/
23-
export const mac = $props.variant === 'mac'
23+
export const mac = $props.variant === 'mac' && !$native
24+
/*
25+
* The Mac look is a stylesheet, and a native host has none: there the box is
26+
* the platform's own control whatever `variant` says. On a Mac (Craft's
27+
* AppKit host, known only on the device) `variant="mac"` also sets the words
28+
* at AppKit's sizes, 13pt over 11pt; a phone keeps its own.
29+
*/
30+
export const macNative = $props.variant === 'mac' && $native
2431

2532
const sizeClasses = { sm: 'h-4 w-4', md: 'h-5 w-5', lg: 'h-6 w-6' }
2633
const baseClasses = 'rounded border-line-strong text-accent focus:ring-accent focus:ring-2 focus:ring-offset-2 focus:ring-offset-panel transition-colors'
@@ -89,6 +96,9 @@ const isIndeterminate = useReactiveProp('indeterminate', {{ indeterminate }})
8996
const isChecked = useReactiveProp('checked', {{ checked }})
9097
const currentLabel = useReactiveProp('label', {{ label }}, { parse: value => value ?? '' })
9198

99+
// `variant="mac"` on Craft's AppKit host: the words at AppKit's sizes.
100+
const macDesktop = {{ macNative }} && typeof craft !== 'undefined' && craft.platform === 'macos'
101+
92102
function onInputChange(event) {
93103
if (isDisabled()) {
94104
event.target.checked = isChecked()
@@ -182,13 +192,13 @@ defineExpose({ isChecked })
182192

183193
<div :if="currentLabel() || {{ !!description }}" class="ml-3 text-sm leading-6">
184194
<label :if="currentLabel()" for="{{ inputId }}" class="{{ labelClasses }} inline-flex flex-row items-center" :class="{ 'opacity-50 cursor-not-allowed': isDisabled(), 'cursor-pointer': !isDisabled() }">
185-
<span :text="currentLabel()">{{ label }}</span>
195+
<span :text="currentLabel()"@if($native) :class="{ 'text-[13px]': macDesktop }"@endif>{{ label }}</span>
186196
@if(required)
187197
<span class="text-danger ml-1">*</span>
188198
@endif
189199
</label>
190200
@if(description)
191-
<p id="{{ descriptionId }}" class="{{ descriptionClasses }}">
201+
<p id="{{ descriptionId }}" class="{{ descriptionClasses }}"@if($native) :class="{ 'text-[11px]': macDesktop }"@endif>
192202
{{ description }}
193203
</p>
194204
@endif

‎packages/components/src/ui/input/PasswordInput.stx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ export const enterKeyHint = $props.enterKeyHint || $props.enterkeyhint || ''
101101
*/
102102
export const nativeAutocomplete = autocomplete || 'current-password'
103103
export const nativeFieldClasses = `min-h-12 pl-4 pr-11 text-[17px] text-[#000000] dark:text-[#ffffff] bg-[#767680]/12 dark:bg-[#767680]/24 rounded-[10px] border-0 ${error ? 'border border-[#ff3b30] dark:border-[#ff453a]' : ''} ${disabled ? 'opacity-50' : ''} ${inputClassName}`.replace(/\s+/g, ' ').trim()
104+
/*
105+
* `variant="mac"` on Craft's AppKit host is AppKit's secure field (an
106+
* NSSecureTextField), sized as TextInput's Mac field is. It has no reveal
107+
* button there: AppKit's secure field has none, and the host does not turn
108+
* one back into a plain field. Which host this is is known only on the
109+
* device, so the client script decides.
110+
*/
111+
export const nativeMac = mac && $native
112+
export const nativeMacFieldClasses = 'min-h-0 h-[22px] pl-0 pr-0 text-[13px] bg-[#00000000] dark:bg-[#00000000] rounded-none'
113+
export const nativeMacWrapClasses = 'w-[170px]'
104114
export const nativeLabelClasses = 'mb-1.5 px-4 text-[13px] uppercase text-[#6d6d72] dark:text-[#8d8d93]'
105115
export const nativeHelperClasses = `mt-1.5 px-4 text-[13px] ${error ? 'text-[#ff3b30] dark:text-[#ff453a]' : 'text-[#6d6d72] dark:text-[#8d8d93]'}`
106116
</script>
@@ -158,12 +168,14 @@ const inputType = derived(() => showPassword() ? 'text' : 'password')
158168
const toggleAriaLabel = derived(() => showPassword() ? 'Hide password' : 'Show password')
159169
// The Mac look colours its strength bar from this word rather than a class.
160170
const strengthLevel = derived(() => strengthText().toLowerCase())
171+
// `variant="mac"` on Craft's AppKit host: AppKit's own secure field (see the server script).
172+
const macDesktop = {{ nativeMac }} && typeof craft !== 'undefined' && craft.platform === 'macos'
161173

162174
defineExpose({ value: inputValue })
163175
</script>
164176

165177
@if($native)
166-
<div class="w-full {{ className }}">
178+
<div class="{{ className }}" :class="macDesktop ? '{{ nativeMacWrapClasses }}' : 'w-full'">
167179
@if(label)
168180
<label for="{{ inputId }}" class="{{ nativeLabelClasses }}">{{ label }}</label>
169181
@endif
@@ -189,8 +201,10 @@ defineExpose({ value: inputValue })
189201
@input="onInput($event)"
190202
@change="onChange($event)"
191203
aria-invalid="{{ error ? 'true' : 'false' }}"
204+
:class="{ '{{ nativeMacFieldClasses }}': macDesktop }"
192205
/>
193206
<button
207+
:if="!macDesktop"
194208
type="button"
195209
@click="toggleVisibility()"
196210
class="absolute top-0 bottom-0 right-0 w-11 items-center justify-center"

‎packages/components/src/ui/input/TextInput.stx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ export const autofocus = $bool($props.autofocus)
166166
* system red outline, a disabled one dims as UIKit's do.
167167
*/
168168
export const nativeFieldClasses = `min-h-12 px-4 text-[17px] text-[#000000] dark:text-[#ffffff] bg-[#767680]/12 dark:bg-[#767680]/24 rounded-[10px] border-0 ${inputClassName}`.trim()
169+
/*
170+
* `variant="mac"` on Craft's AppKit host is AppKit's text field (the host
171+
* draws an NSTextField): 22pt tall at 13pt, with the bezel and the fill the
172+
* system gives it, so the phone's fill, corners and padding come off. It is a
173+
* field at the end of a settings row, 170pt wide; `className="w-full"` (or
174+
* any width) makes it something else. Which host this is is known only on
175+
* the device, so these are classes the client script adds there.
176+
*/
177+
export const nativeMac = mac && $native
178+
export const nativeMacFieldClasses = 'min-h-0 h-[22px] px-0 text-[13px] bg-[#00000000] dark:bg-[#00000000] rounded-none'
179+
export const nativeMacWrapClasses = 'w-[170px]'
169180
export const nativeLabelClasses = 'mb-1.5 px-4 text-[13px] uppercase text-[#6d6d72] dark:text-[#8d8d93]'
170181
export const nativeHelperClasses = `mt-1.5 px-4 text-[13px] ${error ? 'text-[#ff3b30] dark:text-[#ff453a]' : 'text-[#6d6d72] dark:text-[#8d8d93]'}`
171182
</script>
@@ -177,6 +188,8 @@ const emit = defineEmits(['input', 'clear', 'focus', 'blur', 'submit', 'submit-e
177188
// Validity is live, for the same reason `disabled` is: a form posted with
178189
// fetch learns it after the submit, and nothing re-renders (#2047).
179190
const nativeClear = typeof craft !== 'undefined' && craft.capabilities.nativePlatformControls === true
191+
// `variant="mac"` on Craft's AppKit host: AppKit's own field (see the server script).
192+
const macDesktop = {{ nativeMac }} && typeof craft !== 'undefined' && craft.platform === 'macos'
180193
const isError = useReactiveProp('error', {{ error }})
181194
// Keep the physical control and its affordances live on both web and native.
182195
const isDisabled = useReactiveProp('disabled', {{ disabled }})
@@ -280,7 +293,7 @@ defineExpose({ value: inputValue, clear: onClear })
280293
</script>
281294

282295
@if($native)
283-
<div class="w-full {{ className }}">
296+
<div class="{{ className }}" :class="macDesktop ? '{{ nativeMacWrapClasses }}' : 'w-full'">
284297
@if(label)
285298
<label for="{{ inputId }}" class="{{ nativeLabelClasses }}">{{ label }}</label>
286299
@endif
@@ -315,7 +328,7 @@ defineExpose({ value: inputValue, clear: onClear })
315328
@end-editing="onNativeEndEditing($event)"
316329
aria-invalid="{{ error ? 'true' : 'false' }}"
317330
:aria-invalid="isError() ? 'true' : 'false'"
318-
:class="{ 'border border-[#ff3b30] dark:border-[#ff453a]': isError(), 'opacity-50': isDisabled() }"
331+
:class="{ 'border border-[#ff3b30] dark:border-[#ff453a]': isError(), 'opacity-50': isDisabled(), '{{ nativeMacFieldClasses }}': macDesktop }"
319332
@if(clearable):clearButtonMode="nativeClear ? 'while-editing' : 'never'" :style="nativeClear ? null : { paddingRight: 40 }"@endif
320333
/>
321334

‎packages/components/src/ui/segmented-control/SegmentedControl.stx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ const sizeClasses = {
6060
// of a phone broke in two ("Breakfa / st").
6161
const stretchedPadding = { sm: 'px-1', md: 'px-1.5', lg: 'px-1.5' }
6262
export const sizing = sizeClasses[size]
63+
/*
64+
* `variant="mac"` on Craft's AppKit host (known only on the device) is
65+
* AppKit's segmented control: 24pt tall whatever `size` says, as the web's
66+
* Mac look has one size. The host has no segmented control of its own, so
67+
* the segments are drawn there as they are where a phone has none.
68+
*/
69+
export const macNative = $props.variant === 'mac' && $native
6370

6471
// A block flex box already fills its column, so `stretch` needs no `w-full` -
6572
// which beside a margin (`mx-4`) ran the track off the screen's edge.
@@ -91,7 +98,8 @@ const groupLabel = useReactiveProp('label', {{ label }})
9198
const selected = useModel('value', { default: {{ value }}, parse: v => v == null ? '' : String(v) })
9299
const choices = useReactiveProp('options', {{ options }})
93100
const isDisabled = useReactiveProp('disabled', {{ disabled }})
94-
const segmentBase = {{ segmentBase }}
101+
const macDesktop = {{ macNative }} && typeof craft !== 'undefined' && craft.platform === 'macos'
102+
const segmentBase = macDesktop ? {{ segmentBase }}.replace(/\bmin-h-\S+/, 'min-h-[24px]').replace(/\bpy-\S+/, 'py-0.5') : {{ segmentBase }}
95103
const textSize = {{ sizing.text }}
96104
const groupID = {{ testID }}
97105
const nativeSegments = typeof craft !== 'undefined' && craft.capabilities.nativePlatformControls === true

‎packages/components/src/ui/select/Select.stx‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ const emit = defineEmits()
124124
// Validity is live, for the same reason `disabled` is: a form posted with
125125
// fetch learns it after the submit, and nothing re-renders (#2047).
126126
const isError = useReactiveProp('error', {{ error }})
127+
const macDesktop = {{ $native && mac }} && typeof craft !== 'undefined' && craft.platform === 'macos'
128+
// Live on a native host, where the picker is told so rather than styled so.
129+
const isDisabled = useReactiveProp('disabled', {{ disabled }})
127130
/*
128131
* Two-way, like TextInput (stacksjs/stx#2059): `v-model:value="group"` keeps
129132
* the page's signal in step, so a call site no longer pairs `:value` with an
@@ -180,18 +183,26 @@ defineExpose({ value: selectedValue, options: optionList })
180183
{{-- On a native host the control is the platform's picker (UIPickerView,
181184
Android's Spinner) fed with its items. Children are not something either
182185
picker holds, so slotted <option>s are a web-only spelling. --}}
183-
<div class="w-full {{ className }}">
186+
{{-- A phone's picker fills its row. `variant="mac"` on Craft's AppKit host
187+
is the pop-up button, as wide as its longest choice at the end of a
188+
settings row; which host this is is known only on the device. --}}
189+
<div class="{{ className }}" :class="{ 'w-full': !macDesktop }">
184190
@if(label)
185191
<label for="{{ selectId }}" class="mb-1.5 px-4 text-[13px] uppercase text-[#6d6d72] dark:text-[#8d8d93]">{{ label }}</label>
186192
@endif
193+
{{-- The choices under both names a host reads them by: the phone hosts'
194+
`items`, and `options` for Craft's AppKit host (an NSPopUpButton). --}}
187195
<div
188196
data-native="Picker"
189197
data-stx-native-host-root
190198
id="{{ selectId }}"
191199
:items="nativeItems()"
200+
:options="nativeItems()"
192201
:value="selectedValue()"
193202
@if(disabled)disabled@endif
194-
class="w-full disabled:opacity-50"
203+
:disabled="isDisabled()"
204+
class="disabled:opacity-50"
205+
:class="{ 'w-full': !macDesktop }"
195206
@change="onChange($event)"
196207
aria-invalid="{{ error ? 'true' : 'false' }}"
197208
:aria-invalid="isError() ? 'true' : 'false'"

‎packages/components/src/ui/switch/Switch.stx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,17 @@ function thumbClasses() {
8989
return `${thumbBase} ${pos} ${config.thumb}`.trim()
9090
}
9191

92+
// `variant="mac"` on Craft's AppKit host: the label at AppKit's 13pt beside the NSSwitch.
93+
const macDesktop = {{ $native && mac }} && typeof craft !== 'undefined' && craft.platform === 'macos'
94+
9295
defineExpose({ isChecked, toggle })
9396
</script>
9497

9598
@if($native)
9699
{{-- On a native host the control is the platform's own switch (UISwitch,
97100
Android's Switch), at the end of its row as a settings row puts it. --}}
98101
<label class="flex-row items-center justify-between gap-3 {{ className }}">
99-
<span :if="currentLabel()" :text="currentLabel()" class="flex-1 text-[17px] text-[#000000] dark:text-[#ffffff]">{{ label }}</span>
102+
<span :if="currentLabel()" :text="currentLabel()" class="flex-1 text-[17px] text-[#000000] dark:text-[#ffffff]" :class="{ 'text-[13px]': macDesktop }">{{ label }}</span>
100103
@if(name)
101104
<input
102105
type="hidden"

0 commit comments

Comments
 (0)