@krapwoo/ds-viewer 0.6.6 → 0.6.7

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.
@@ -113,22 +113,21 @@ function Preview({ render, widths, surface, layout }: { render: () => React.Reac
113
113
  if (widths === 'full') return <SpecimenSurface surface={kind} style={styles.previewCard}>{render()}</SpecimenSurface>;
114
114
  if (layout === 'table') return <TablePreview render={render} widths={widths} surface={kind} />;
115
115
  return (
116
- <View style={styles.previewCard}>
116
+ <SpecimenSurface surface={kind} style={styles.previewCard}>
117
117
  <View style={styles.frames}>
118
118
  {widths.map((width, i) => (
119
- <SpecimenSurface
119
+ <View
120
120
  key={`${width}-${i}`}
121
- surface={kind}
122
121
  // Two live instances of the same component: name each so assistive tech can tell them apart.
123
122
  {...(widths.length > 1 ? ({ role: 'group', 'aria-label': frameLabel(width) } as Record<string, unknown>) : null)}
124
123
  style={[styles.frame, { width }]}
125
124
  >
126
125
  {widths.length > 1 && <Text style={styles.frameLabel}>{frameLabel(width)}</Text>}
127
126
  {render()}
128
- </SpecimenSurface>
127
+ </View>
129
128
  ))}
130
129
  </View>
131
- </View>
130
+ </SpecimenSurface>
132
131
  );
133
132
  }
134
133
 
@@ -116,25 +116,30 @@ function readTree(root: MeasurableNode, browser: Browser): LayoutBox {
116
116
  }
117
117
 
118
118
  export function SpecimenContent({ fill, align, capped = true, children }: { fill?: boolean; align?: SpecimenAlign; /** Phone-width cap for a filled example; off for page-width cells. */ capped?: boolean; children: React.ReactNode }) {
119
- // After layout (browser only): a `fill` example whose visible content is narrower than the cell
120
- // (a fixed-width skeleton, a tooltip around a small trigger) is centred instead of sitting
121
- // against the leading edge; an explicit `align` always wins. And in development, an example
119
+ // After layout (browser only): a genuinely narrow `fill` root (a fixed-width skeleton) is
120
+ // centred. Narrow text/icons inside a full-width layout do not collapse that layout's root.
121
+ // An explicit `align` always wins. And in development, an example
122
122
  // wider than its cell logs a warning naming a wider `specimenSize`.
123
123
  const address = useSpecimenAddress();
124
124
  const ref = useRef<View>(null);
125
125
  const [centerNarrow, setCenterNarrow] = useState(false);
126
126
  const warned = useRef(false);
127
+ useEffect(() => {
128
+ setCenterNarrow(false);
129
+ }, [fill, capped, align]);
127
130
  const measure = useCallback(() => {
128
131
  const browser = globalThis as unknown as Browser;
129
132
  const node = ref.current as unknown as MeasurableNode | null;
130
133
  if (!browser.Element || !browser.getComputedStyle || !node?.getBoundingClientRect) return;
131
134
  const wrapperWidth = node.getBoundingClientRect().width;
132
- const visible = paintedExtent(visibleBoxes(readTree(node, browser)));
135
+ const tree = readTree(node, browser);
136
+ const visible = paintedExtent(visibleBoxes(tree));
137
+ const rootWidth = Math.max(0, ...tree.children.map((child) => child.right - child.left));
133
138
  // Only for a phone-width cell. Centring exists for a small control adrift in a wide cell (a
134
139
  // tooltip, a fixed-width skeleton); a 'page' cell holds a layout, whose own content is
135
140
  // supposed to sit at the left of its own measure, and centring it collapses it to the width of
136
141
  // whatever it happens to have painted.
137
- if (capped && fill && align === undefined && !centerNarrow && shouldCenterFilledContent(visible, wrapperWidth)) setCenterNarrow(true);
142
+ if (capped && fill && align === undefined && !centerNarrow && shouldCenterFilledContent(visible, wrapperWidth, rootWidth)) setCenterNarrow(true);
138
143
  if ((globalThis as { __DEV__?: boolean }).__DEV__ && !warned.current && overflowsCell(visible, wrapperWidth)) {
139
144
  warned.current = true;
140
145
  console.warn(
@@ -143,7 +148,7 @@ export function SpecimenContent({ fill, align, capped = true, children }: { fill
143
148
  }
144
149
  }, [capped, fill, align, centerNarrow, address]);
145
150
  if (fill) {
146
- const alignItems = centerNarrow ? 'center' : specimenContentAlignItems(fill, align);
151
+ const alignItems = capped && align === undefined && centerNarrow ? 'center' : specimenContentAlignItems(fill, align);
147
152
  return <View ref={ref} onLayout={measure} style={[SPECIMEN_SURFACE_WIDTH_STYLE, filledContentStyle(capped), { alignItems }]}>{children}</View>;
148
153
  }
149
154
  return <View ref={ref} onLayout={measure} style={specimenContentBoundedStyle(align)}>{children}</View>;
@@ -89,12 +89,12 @@ export function specimenContentBoundedStyle(align: SpecimenAlign | undefined) {
89
89
  * `specimenFill.ts`). A page can force a fill with `specimenSurface`. */
90
90
  export const DEFAULT_SPECIMEN_SURFACE: SpecimenSurfaceKind = 'auto';
91
91
 
92
- /** A `fill` example stretches to its cell, but one with its own fixed width (a 160-point skeleton
93
- * bar) stays that width; it should then sit in the middle of the cell, not against its leading
94
- * edge. True when the measured example is narrower than its wrapper (by more than rounding). */
95
- export function shouldCenterFilledContent(contentWidth: number, wrapperWidth: number): boolean {
96
- if (!(contentWidth > 0) || !(wrapperWidth > 0)) return false;
97
- return contentWidth < wrapperWidth - 1;
92
+ /** Center genuinely narrow filled examples, not narrow paint inside a full-width fluid layout.
93
+ * The latter must retain stretch: centering its root would collapse it to its intrinsic width.
94
+ * Callers without a separate root measurement retain the established fixed-width behavior. */
95
+ export function shouldCenterFilledContent(contentWidth: number, wrapperWidth: number, rootWidth = contentWidth): boolean {
96
+ if (!(contentWidth > 0) || !(wrapperWidth > 0) || !(rootWidth > 0)) return false;
97
+ return contentWidth < wrapperWidth - 1 && rootWidth < wrapperWidth - 1;
98
98
  }
99
99
 
100
100
  /** The width of what's actually visible in an example: from the leftmost to the rightmost painted
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krapwoo/ds-viewer",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A browsable, always-current catalog for an Expo / React Native design system.",