@cosxai/ui 0.8.0 → 0.8.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosxai/ui",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "COSX design system — React 19 component primitives shared across product-meta and other consumers",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
@@ -13,6 +13,13 @@ export interface CheckboxProps {
13
13
  suffix?: React.ReactNode;
14
14
  disabled?: boolean;
15
15
  className?: string;
16
+ /**
17
+ * Test hook forwarded to the visual button. Consumers with
18
+ * getByTestId('…') queries can keep them working across the swap
19
+ * from native <input type="checkbox"> to this primitive without
20
+ * touching each test file.
21
+ */
22
+ "data-testid"?: string;
16
23
  }
17
24
 
18
25
  export function Checkbox({
@@ -22,6 +29,7 @@ export function Checkbox({
22
29
  suffix,
23
30
  disabled,
24
31
  className,
32
+ "data-testid": testId,
25
33
  }: CheckboxProps) {
26
34
  return (
27
35
  <label
@@ -43,6 +51,7 @@ export function Checkbox({
43
51
  aria-checked={checked}
44
52
  disabled={disabled}
45
53
  onClick={() => !disabled && onChange(!checked)}
54
+ data-testid={testId}
46
55
  style={{
47
56
  width: 16,
48
57
  height: 16,
@@ -34,6 +34,8 @@ export interface RadioProps {
34
34
  */
35
35
  value?: string;
36
36
  className?: string;
37
+ /** Test hook forwarded to the visual button — see Checkbox. */
38
+ "data-testid"?: string;
37
39
  }
38
40
 
39
41
  export function Radio({
@@ -45,6 +47,7 @@ export function Radio({
45
47
  name,
46
48
  value,
47
49
  className,
50
+ "data-testid": testId,
48
51
  }: RadioProps) {
49
52
  return (
50
53
  <label
@@ -66,6 +69,7 @@ export function Radio({
66
69
  aria-checked={checked}
67
70
  disabled={disabled}
68
71
  onClick={() => !disabled && !checked && onChange(true)}
72
+ data-testid={testId}
69
73
  style={{
70
74
  width: 16,
71
75
  height: 16,
@@ -29,23 +29,35 @@ export interface TooltipProps {
29
29
  }>;
30
30
  // ms before showing. Default 120.
31
31
  delay?: number;
32
- // "top" hovers above the trigger, "bottom" below. Default "top".
33
- placement?: "top" | "bottom";
32
+ // "top" hovers above the trigger, "bottom" below, "auto" prefers
33
+ // top but flips to bottom when the trigger sits near the viewport
34
+ // top edge (would otherwise clip against the browser chrome).
35
+ // Default "auto".
36
+ placement?: "top" | "bottom" | "auto";
34
37
  // Render even if `content` is falsy. Default false — callers can
35
38
  // wrap unconditionally without branching JSX.
36
39
  alwaysRender?: boolean;
37
40
  }
38
41
 
42
+ // AUTO_FLIP_THRESHOLD_PX = the vertical space "top" placement needs
43
+ // above the trigger (tooltip height + gap + a bit of breathing room).
44
+ // Trigger positioned closer than this to the viewport top gets a
45
+ // bottom-flip.
46
+ const AUTO_FLIP_THRESHOLD_PX = 48;
47
+
39
48
  export function Tooltip({
40
49
  content,
41
50
  children,
42
51
  delay = 120,
43
- placement = "top",
52
+ placement = "auto",
44
53
  alwaysRender = false,
45
54
  }: TooltipProps) {
46
55
  const triggerRef = useRef<HTMLElement | null>(null);
47
56
  const [open, setOpen] = useState(false);
48
57
  const [pos, setPos] = useState<{ x: number; y: number } | null>(null);
58
+ const [resolvedPlacement, setResolvedPlacement] = useState<"top" | "bottom">(
59
+ placement === "bottom" ? "bottom" : "top",
60
+ );
49
61
  const timerRef = useRef<number | null>(null);
50
62
 
51
63
  const cancelTimer = useCallback(() => {
@@ -59,9 +71,18 @@ export function Tooltip({
59
71
  const el = triggerRef.current;
60
72
  if (!el) return;
61
73
  const r = el.getBoundingClientRect();
74
+ // "auto" resolves to top when there's enough room above, else
75
+ // bottom. Explicit "top" / "bottom" are honoured verbatim.
76
+ let effective: "top" | "bottom";
77
+ if (placement === "auto") {
78
+ effective = r.top < AUTO_FLIP_THRESHOLD_PX ? "bottom" : "top";
79
+ } else {
80
+ effective = placement;
81
+ }
82
+ setResolvedPlacement(effective);
62
83
  setPos({
63
84
  x: r.left + r.width / 2,
64
- y: placement === "top" ? r.top : r.bottom,
85
+ y: effective === "top" ? r.top : r.bottom,
65
86
  });
66
87
  }, [placement]);
67
88
 
@@ -137,7 +158,7 @@ export function Tooltip({
137
158
  left: pos?.x ?? -9999,
138
159
  top: pos?.y ?? -9999,
139
160
  transform:
140
- placement === "top"
161
+ resolvedPlacement === "top"
141
162
  ? "translate(-50%, calc(-100% - 8px))"
142
163
  : "translate(-50%, 8px)",
143
164
  background: "var(--ck-bg-surface)",