@oxy.so/bloom 7.14.2 → 7.14.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { useSurfaceLayer } from '../surface/use-surface-layer';
2
2
  import type { LinkButtonProps } from './types';
3
- import React, { forwardRef, useMemo, useRef, useEffect, memo, type ComponentType } from 'react';
3
+ import React, { forwardRef, useMemo, useRef, useEffect, memo, type ComponentType, createContext, useContext } from 'react';
4
4
  import { resolveIconSlot } from '../icons/render-icon';
5
5
  import {
6
6
  ActivityIndicator,
@@ -154,7 +154,6 @@ type ButtonPressableProps = Pick<
154
154
  | 'accessibilityLabel'
155
155
  | 'accessibilityRole'
156
156
  | 'accessibilityState'
157
- | 'children'
158
157
  | 'className'
159
158
  | 'disabled'
160
159
  | 'hitSlop'
@@ -168,11 +167,64 @@ type ButtonPressableProps = Pick<
168
167
  | 'onPressIn'
169
168
  | 'onPressOut'
170
169
  | 'testID'
171
- > & { style?: StyleProp<ViewStyle>; 'aria-hidden'?: boolean; 'aria-expanded'?: boolean; 'aria-pressed'?: boolean };
170
+ > & { children?: React.ReactNode; style?: StyleProp<ViewStyle>; baseStyle?: StyleProp<ViewStyle>; 'aria-hidden'?: boolean; 'aria-expanded'?: boolean; 'aria-pressed'?: boolean };
172
171
 
173
- const ButtonPressable: ComponentType<ButtonPressableProps> = Pressable;
172
+ const CONTENT_TEXT_KEYS = ['color', 'fontFamily', 'fontSize', 'fontWeight', 'fontStyle',
173
+ 'lineHeight', 'letterSpacing', 'textAlign', 'textDecorationLine'] as const;
174
174
 
175
- const StyledPressable: ComponentType<ButtonPressableProps & React.RefAttributes<View>> = styled(ButtonPressable, {
175
+ const ContentStyleContext = createContext<{ text?: TextStyle; layout?: ViewStyle }>({});
176
+
177
+ // Resolve utilities without mixing Bloom's defaults into their input. The
178
+ // defaults are merged only after interop, so caller classes have final say.
179
+ const ButtonPressable = forwardRef<View, ButtonPressableProps>(function ButtonPressable({
180
+ baseStyle, style, children, ...props
181
+ }, ref) {
182
+ const resolved = StyleSheet.flatten(style) as TextStyle | undefined;
183
+ const layout: TextStyle = { ...resolved };
184
+ const text: TextStyle = {};
185
+ // Read a copy: the CSS resolver can share its cached style objects with
186
+ // other controls. Moving properties out of that cache changes their styles.
187
+ for (const key of CONTENT_TEXT_KEYS) {
188
+ if (resolved?.[key] !== undefined) Object.assign(text, { [key]: resolved[key] });
189
+ delete layout[key];
190
+ }
191
+ return <Pressable {...props} ref={ref} style={[baseStyle, layout]}>
192
+ <ContentStyleContext.Provider value={{ text, layout }}>
193
+ {children}
194
+ </ContentStyleContext.Provider>
195
+ </Pressable>;
196
+ });
197
+
198
+ function ButtonContent({ loading, gap, children }: {
199
+ loading: boolean; gap: number; children: (style: TextStyle) => React.ReactNode;
200
+ }) {
201
+ const { text, layout } = useContext(ContentStyleContext);
202
+ const contentLayout = layout && {
203
+ ...(layout.flexDirection !== undefined && { flexDirection: layout.flexDirection }),
204
+ ...(layout.flexWrap !== undefined && { flexWrap: layout.flexWrap }),
205
+ ...(layout.alignItems !== undefined && { alignItems: layout.alignItems }),
206
+ ...(layout.justifyContent !== undefined && { justifyContent: layout.justifyContent }),
207
+ ...(layout.gap !== undefined && { gap: layout.gap }),
208
+ ...(layout.rowGap !== undefined && { rowGap: layout.rowGap }),
209
+ ...(layout.columnGap !== undefined && { columnGap: layout.columnGap }),
210
+ };
211
+ return <View pointerEvents={loading ? 'none' : undefined}
212
+ style={[styles.content, { gap }, contentLayout, loading && { opacity: 0 }]}
213
+ importantForAccessibility={loading ? 'no-hide-descendants' : undefined}
214
+ accessibilityElementsHidden={loading || undefined}>
215
+ {children(text ?? {})}
216
+ </View>;
217
+ }
218
+
219
+ function ButtonLoading({ color, fallback }: { color?: string; fallback: string }) {
220
+ const { text } = useContext(ContentStyleContext);
221
+ return <View pointerEvents="none" style={styles.loadingOverlay}>
222
+ <ActivityIndicator size="small" color={color ?? text?.color ?? fallback} />
223
+ </View>;
224
+ }
225
+
226
+ const ButtonInterop: ComponentType<ButtonPressableProps> = ButtonPressable;
227
+ const StyledPressable: ComponentType<ButtonPressableProps & React.RefAttributes<View>> = styled(ButtonInterop, {
176
228
  className: 'style',
177
229
  });
178
230
 
@@ -339,55 +391,45 @@ const ButtonComponent = forwardRef<View, ButtonProps>(function ButtonComponent({
339
391
  : (android ? ANDROID_SIZE_HIT_SLOP : SIZE_HIT_SLOP)[size];
340
392
 
341
393
  const IconFromProp = icon != null && isIconComponent(icon) ? icon : null;
342
- const iconNode = IconFromProp ? (
343
- <IconFromProp width={iconSize} height={iconSize} fill={paint.foreground} />
344
- ) : (
345
- (icon as React.ReactNode) ?? null
346
- );
347
-
348
- const content = (
349
- <>
350
- {resolveIconSlot(renderLeadingIcon, iconSize, paint.foreground, () =>
351
- LeadingIcon ? (
352
- <LeadingIcon width={iconSize} height={iconSize} fill={paint.foreground} />
353
- ) : null,
354
- )}
355
- {leading}
356
- {iconNode}
357
- {children != null && (!isSquare || (!renderLeadingIcon && !LeadingIcon && !iconNode))
358
- ? renderTextContent(children, text => (
359
- <Text variant={textVariant ?? geometry.type} numberOfLines={numberOfLines} style={[computedTextStyle, textStyle]}>
360
- {text}
361
- </Text>
362
- )) : null}
363
- {trailing}
364
- {!isSquare
365
- ? resolveIconSlot(renderTrailingIcon, iconSize, paint.foreground, () =>
366
- TrailingIcon ? (
367
- <TrailingIcon width={iconSize} height={iconSize} fill={paint.foreground} />
368
- ) : null,
369
- )
370
- : null}
371
- </>
372
- );
394
+ const content = (classText: TextStyle) => {
395
+ const foreground = typeof classText.color === 'string' ? classText.color : paint.foreground;
396
+ const iconNode = IconFromProp ? (
397
+ <IconFromProp width={iconSize} height={iconSize} fill={foreground} />
398
+ ) : (
399
+ (icon as React.ReactNode) ?? null
400
+ );
401
+
402
+ return (
403
+ <>
404
+ {resolveIconSlot(renderLeadingIcon, iconSize, foreground, () =>
405
+ LeadingIcon ? (
406
+ <LeadingIcon width={iconSize} height={iconSize} fill={foreground} />
407
+ ) : null,
408
+ )}
409
+ {leading}
410
+ {iconNode}
411
+ {children != null && (!isSquare || (!renderLeadingIcon && !LeadingIcon && !iconNode))
412
+ ? renderTextContent(children, text => (
413
+ <Text variant={textVariant ?? geometry.type} numberOfLines={numberOfLines} style={[computedTextStyle, classText, textStyle]}>
414
+ {text}
415
+ </Text>
416
+ )) : null}
417
+ {trailing}
418
+ {!isSquare
419
+ ? resolveIconSlot(renderTrailingIcon, iconSize, foreground, () =>
420
+ TrailingIcon ? (
421
+ <TrailingIcon width={iconSize} height={iconSize} fill={foreground} />
422
+ ) : null,
423
+ )
424
+ : null}
425
+ </>
426
+ );
427
+ };
373
428
 
374
429
  const handlePress = isInteractionBlocked
375
430
  ? undefined
376
431
  : onPress ?? (href != null ? () => void Linking.openURL(href) : undefined);
377
432
 
378
- // Retain one content host for its entire lifetime. Match caller layout
379
- // overrides inside it, while padding and the outer button box stay put.
380
- const callerStyle = StyleSheet.flatten(style);
381
- const contentLayout = callerStyle && {
382
- ...(callerStyle.flexDirection !== undefined && { flexDirection: callerStyle.flexDirection }),
383
- ...(callerStyle.flexWrap !== undefined && { flexWrap: callerStyle.flexWrap }),
384
- ...(callerStyle.alignItems !== undefined && { alignItems: callerStyle.alignItems }),
385
- ...(callerStyle.justifyContent !== undefined && { justifyContent: callerStyle.justifyContent }),
386
- ...(callerStyle.gap !== undefined && { gap: callerStyle.gap }),
387
- ...(callerStyle.rowGap !== undefined && { rowGap: callerStyle.rowGap }),
388
- ...(callerStyle.columnGap !== undefined && { columnGap: callerStyle.columnGap }),
389
- };
390
-
391
433
  return (
392
434
  <StyledPressable
393
435
  ref={ref}
@@ -396,10 +438,8 @@ const ButtonComponent = forwardRef<View, ButtonProps>(function ButtonComponent({
396
438
  accessibilityElementsHidden={accessibilityElementsHidden}
397
439
  importantForAccessibility={importantForAccessibility}
398
440
  className={className}
399
- style={[
400
- baseStyles,
401
- style,
402
- ]}
441
+ baseStyle={baseStyles}
442
+ style={style}
403
443
  onLongPress={isInteractionBlocked ? undefined : onLongPress}
404
444
  onPress={handlePress ? event => {
405
445
  if (stopPropagation) event.stopPropagation();
@@ -428,24 +468,8 @@ const ButtonComponent = forwardRef<View, ButtonProps>(function ButtonComponent({
428
468
  {paint.surface ? (
429
469
  <SurfacePaint fill={paint.background} radius={StyleSheet.flatten(style)?.borderRadius ?? BUTTON_RADIUS} />
430
470
  ) : null}
431
- <View
432
- pointerEvents={loading ? 'none' : undefined}
433
- style={[
434
- styles.content,
435
- { gap: geometry.gap },
436
- contentLayout,
437
- loading && { opacity: 0 },
438
- ]}
439
- importantForAccessibility={loading ? 'no-hide-descendants' : undefined}
440
- accessibilityElementsHidden={loading || undefined}
441
- >
442
- {content}
443
- </View>
444
- {loading ? (
445
- <View pointerEvents="none" style={styles.loadingOverlay}>
446
- <ActivityIndicator size="small" color={loadingColor ?? paint.foreground} />
447
- </View>
448
- ) : null}
471
+ <ButtonContent loading={loading} gap={geometry.gap}>{content}</ButtonContent>
472
+ {loading ? <ButtonLoading color={loadingColor} fallback={paint.foreground} /> : null}
449
473
  </StyledPressable>
450
474
  );
451
475
  });
@@ -62,10 +62,18 @@ const DISABLED = '.bloom-btn:disabled:not([aria-busy="true"]),\n.bloom-btn[aria-
62
62
  * applies none of them, so a modifier class alone proves nothing about whether
63
63
  * an underline is drawn.
64
64
  */
65
- export const BLOOM_BUTTON_CSS = interactiveWebCss({
65
+ export const BLOOM_BUTTON_CSS = `@layer base {\n${interactiveWebCss({
66
66
  selector: '.bloom-btn',
67
67
  varPrefix: 'bloom-btn',
68
68
  base: `
69
+ height: var(--bloom-btn-height);
70
+ width: var(--bloom-btn-width, auto);
71
+ padding-inline: var(--bloom-btn-padding);
72
+ border-radius: var(--bloom-btn-radius);
73
+ font-size: var(--bloom-btn-font-size);
74
+ line-height: var(--bloom-btn-line-height);
75
+ font-weight: var(--bloom-btn-font-weight);
76
+ letter-spacing: var(--bloom-btn-letter-spacing, normal);
69
77
  flex-direction: row;
70
78
  gap: var(--bloom-btn-gap, 2px);
71
79
  position: relative;
@@ -134,7 +142,7 @@ ${surfaceMaterialCss('.bloom-btn--surface', 'var(--bloom-btn-bg)', `background-c
134
142
  transition: none;
135
143
  }
136
144
  }`,
137
- });
145
+ })}\n}`;
138
146
 
139
147
  // ---------------------------------------------------------------------------
140
148
  // Component
@@ -223,14 +231,13 @@ const ButtonWebComponent = forwardRef<View, ButtonProps>(function ButtonWebCompo
223
231
  const containerStyle = useMemo((): CSSProperties => {
224
232
  const shadow = palette.shadow ? BUTTON_SHADOW[theme.isDark ? 'dark' : 'light'] : 'none';
225
233
  const base: CSSProperties = {
226
- height: geometry.height,
227
- paddingLeft: geometry.paddingHorizontal,
228
- paddingRight: geometry.paddingHorizontal,
229
- borderRadius: BUTTON_RADIUS,
230
- fontSize: geometry.fontSize,
231
- lineHeight: `${geometry.lineHeight}px`,
232
- fontWeight: Number(geometry.fontWeight),
233
- letterSpacing: geometry.letterSpacing || undefined,
234
+ ['--bloom-btn-height' as string]: `${geometry.height}px`,
235
+ ['--bloom-btn-padding' as string]: `${geometry.paddingHorizontal}px`,
236
+ ['--bloom-btn-radius' as string]: `${BUTTON_RADIUS}px`,
237
+ ['--bloom-btn-font-size' as string]: `${geometry.fontSize}px`,
238
+ ['--bloom-btn-line-height' as string]: `${geometry.lineHeight}px`,
239
+ ['--bloom-btn-font-weight' as string]: geometry.fontWeight,
240
+ ['--bloom-btn-letter-spacing' as string]: geometry.letterSpacing ? `${geometry.letterSpacing}px` : 'normal',
234
241
  // CSS custom props consumed by the static stylesheet — see its header.
235
242
  ['--bloom-surface-rim' as string]: resolveSurfaceOptics(theme.isDark).rim,
236
243
  ['--bloom-surface-sheen' as string]: resolveSurfaceOptics(theme.isDark).sheenCss,
@@ -254,22 +261,30 @@ const ButtonWebComponent = forwardRef<View, ButtonProps>(function ButtonWebCompo
254
261
  ['--bloom-btn-border-active' as string]: palette.active.border,
255
262
  ['--bloom-btn-border-disabled' as string]: palette.disabled.border,
256
263
  };
264
+ // Keep the existing inline defaults for callers without utilities. When
265
+ // classes are present, the same defaults come from the base layer.
266
+ if (!className) Object.assign(base, {
267
+ height: geometry.height, paddingLeft: geometry.paddingHorizontal,
268
+ paddingRight: geometry.paddingHorizontal, borderRadius: BUTTON_RADIUS,
269
+ fontSize: geometry.fontSize, lineHeight: `${geometry.lineHeight}px`,
270
+ fontWeight: Number(geometry.fontWeight), letterSpacing: geometry.letterSpacing || undefined,
271
+ });
257
272
  if (isSquare) {
258
- base.width = geometry.height;
259
- base.paddingLeft = 0;
260
- base.paddingRight = 0;
273
+ if (!className) Object.assign(base, { width: geometry.height, paddingLeft: 0, paddingRight: 0 });
274
+ (base as Record<string, unknown>)['--bloom-btn-width'] = `${geometry.height}px`;
275
+ (base as Record<string, unknown>)['--bloom-btn-padding'] = '0px';
261
276
  }
262
277
  if (isLink && !isSquare) {
263
278
  // LinkButton: no container at all — the label's own line box,
264
279
  // a 4px gap, and a 4px corner that only the focus ring shows.
265
- base.height = undefined;
266
- base.paddingLeft = 0;
267
- base.paddingRight = 0;
268
- base.borderRadius = 4;
280
+ if (!className) Object.assign(base, { height: undefined, paddingLeft: 0, paddingRight: 0, borderRadius: 4 });
281
+ (base as Record<string, unknown>)['--bloom-btn-height'] = 'auto';
282
+ (base as Record<string, unknown>)['--bloom-btn-padding'] = '0px';
283
+ (base as Record<string, unknown>)['--bloom-btn-radius'] = '4px';
269
284
  (base as Record<string, unknown>)['--bloom-btn-gap'] = `${LINK_BUTTON_GAP}px`;
270
285
  }
271
286
  return base;
272
- }, [geometry, palette, theme.isDark, isSquare, isIconVariant, isLink, togglePressed, loading]);
287
+ }, [geometry, palette, theme.isDark, isSquare, isIconVariant, isLink, togglePressed, loading, className]);
273
288
 
274
289
  const handleClick = useCallback(
275
290
  (event: MouseEvent<HTMLElement>) => {
@@ -293,7 +308,7 @@ const ButtonWebComponent = forwardRef<View, ButtonProps>(function ButtonWebCompo
293
308
  .join(' ');
294
309
 
295
310
  const spinnerColor =
296
- loadingColor ?? (disabled ? palette.disabled.foreground : palette.rest.foreground);
311
+ loadingColor ?? 'currentColor';
297
312
 
298
313
  // Normalize the caller's `style` (single object, StyleProp array, or falsy)
299
314
  // into ONE flat plain object here, once, so neither raw-DOM merge site below