dssoca 0.15.0 → 0.16.0

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.
@@ -14,19 +14,38 @@
14
14
  * WAI-ARIA tooltip pattern.
15
15
  */
16
16
  text: string | Snippet
17
- /** Where the tooltip attaches relative to the trigger. Default `top`. */
17
+ /**
18
+ * Preferred side the tooltip attaches to relative to the trigger. Default `top`.
19
+ * With `avoidCollisions` (the default) this is only a preference — see below.
20
+ */
18
21
  placement?: Placement
22
+ /**
23
+ * Keep the tip visible when the preferred side has no room (DS-0146): flip to the
24
+ * opposite side, then to the perpendicular side with the most room, then shift along
25
+ * the cross axis so the tip never overhangs the viewport or an `overflow`-clipping
26
+ * ancestor. `false` pins the tip to `placement`. Default `true`.
27
+ */
28
+ avoidCollisions?: boolean
19
29
  /** Token size (sm|md|lg); inherits the global size when unset. */
20
30
  size?: Size
21
31
  /** Trigger content (wrapped in an inline-block container). */
22
32
  children: Snippet
23
33
  }
24
34
 
25
- let { text, placement = 'top', size, children }: Props = $props()
35
+ let { text, placement = 'top', avoidCollisions = true, size, children }: Props = $props()
26
36
 
27
37
  const tipId = $props.id()
28
38
 
29
39
  let open = $state(false)
40
+ let triggerEl = $state<HTMLElement | undefined>()
41
+ let tipEl = $state<HTMLElement | undefined>()
42
+
43
+ /** Side chosen by collision avoidance while open; `null` = use the preferred side. */
44
+ let flipped = $state<Placement | null>(null)
45
+ /** Side actually in use (the preferred side unless collision avoidance moved it). */
46
+ const resolved = $derived(flipped ?? placement)
47
+ /** Cross-axis nudge in px (x for top/bottom, y for left/right). */
48
+ let shift = $state(0)
30
49
 
31
50
  function show() {
32
51
  open = true
@@ -45,6 +64,110 @@
45
64
 
46
65
  // Snippets are functions, so a runtime typeof check narrows the union.
47
66
  const tipSnippet = $derived(typeof text === 'function' ? text : undefined)
67
+
68
+ // --- collision avoidance ---------------------------------------------------
69
+ // Manual measurement (what Floating UI's `flip` + `shift` do internally) rather
70
+ // than a dependency or CSS anchor positioning: zero deps, every browser, and
71
+ // exercisable in jsdom. The closed tip already has layout (its `display: block`
72
+ // beats the UA `[hidden]` rule; hiding is visibility/opacity), so it can be
73
+ // measured before it is revealed.
74
+
75
+ type Rect = { top: number; right: number; bottom: number; left: number }
76
+
77
+ const OPPOSITE: Record<Placement, Placement> = {
78
+ top: 'bottom',
79
+ bottom: 'top',
80
+ left: 'right',
81
+ right: 'left',
82
+ }
83
+
84
+ /** Viewport ∩ every ancestor that clips overflow (a scrolling Panel, Modal body…). */
85
+ function clippingBoundary(from: HTMLElement): Rect {
86
+ const r: Rect = { top: 0, left: 0, right: window.innerWidth, bottom: window.innerHeight }
87
+ let el: HTMLElement | null = from.parentElement
88
+ while (el && el !== document.body) {
89
+ const cs = getComputedStyle(el)
90
+ if (cs.overflowX !== 'visible' || cs.overflowY !== 'visible') {
91
+ const b = el.getBoundingClientRect()
92
+ r.top = Math.max(r.top, b.top)
93
+ r.left = Math.max(r.left, b.left)
94
+ r.right = Math.min(r.right, b.right)
95
+ r.bottom = Math.min(r.bottom, b.bottom)
96
+ }
97
+ el = el.parentElement
98
+ }
99
+ return r
100
+ }
101
+
102
+ function position() {
103
+ if (!avoidCollisions || !triggerEl || !tipEl) {
104
+ flipped = null
105
+ shift = 0
106
+ return
107
+ }
108
+ const boundary = clippingBoundary(triggerEl)
109
+ const t = triggerEl.getBoundingClientRect()
110
+ const w = tipEl.offsetWidth
111
+ const h = tipEl.offsetHeight
112
+ const gap = parseFloat(getComputedStyle(tipEl).getPropertyValue('--ss-tooltip-offset')) || 6
113
+
114
+ const space: Record<Placement, number> = {
115
+ top: t.top - boundary.top,
116
+ bottom: boundary.bottom - t.bottom,
117
+ left: t.left - boundary.left,
118
+ right: boundary.right - t.right,
119
+ }
120
+ const need = (p: Placement) => (p === 'top' || p === 'bottom' ? h : w) + gap
121
+
122
+ // Preferred → opposite → perpendicular sides (roomier first) → most room overall.
123
+ const perpendicular: Placement[] =
124
+ placement === 'top' || placement === 'bottom' ? ['left', 'right'] : ['top', 'bottom']
125
+ perpendicular.sort((a, b) => space[b] - space[a])
126
+ const order: Placement[] = [placement, OPPOSITE[placement], ...perpendicular]
127
+ const side =
128
+ order.find((p) => space[p] >= need(p)) ??
129
+ order.reduce((best, p) => (space[p] > space[best] ? p : best))
130
+ flipped = side
131
+
132
+ // Cross-axis shift: the tip is centred on the trigger; clamp its edges inside the
133
+ // boundary (padded by the same gap it keeps from the trigger).
134
+ if (side === 'top' || side === 'bottom') {
135
+ const center = t.left + t.width / 2
136
+ const left = center - w / 2
137
+ const right = center + w / 2
138
+ const min = boundary.left + gap
139
+ const max = boundary.right - gap
140
+ shift = left < min ? min - left : right > max ? max - right : 0
141
+ } else {
142
+ const center = t.top + t.height / 2
143
+ const top = center - h / 2
144
+ const bottom = center + h / 2
145
+ const min = boundary.top + gap
146
+ const max = boundary.bottom - gap
147
+ shift = top < min ? min - top : bottom > max ? max - bottom : 0
148
+ }
149
+ }
150
+
151
+ // Client-only: while open, (re)position on show / prop change and on any scroll
152
+ // (capture catches nested scroll containers) or resize; on close, reset.
153
+ $effect(() => {
154
+ if (!open) {
155
+ flipped = null
156
+ shift = 0
157
+ return
158
+ }
159
+ // Read the props here so a change while open re-runs the effect.
160
+ void placement
161
+ void avoidCollisions
162
+ position()
163
+ const onMove = () => position()
164
+ window.addEventListener('scroll', onMove, { capture: true, passive: true })
165
+ window.addEventListener('resize', onMove, { passive: true })
166
+ return () => {
167
+ window.removeEventListener('scroll', onMove, { capture: true })
168
+ window.removeEventListener('resize', onMove)
169
+ }
170
+ })
48
171
  </script>
49
172
 
50
173
  <!--
@@ -56,6 +179,10 @@
56
179
 
57
180
  `text` is either a string or a snippet (DS-0144); a snippet renders inside the
58
181
  same tip element, so the accessible description stays whatever the tip contains.
182
+
183
+ `data-placement` is the *resolved* side (DS-0146): the preferred `placement`
184
+ unless collision avoidance flipped it. `--shift` nudges the tip along the cross
185
+ axis so it stays inside the viewport / clipping ancestor.
59
186
  -->
60
187
  <!-- svelte-ignore a11y_no_static_element_interactions -- the wrapper is a passive
61
188
  container; the real interactive element is the trigger snippet inside it, and the
@@ -63,7 +190,7 @@
63
190
  <span
64
191
  class="ss-tooltip"
65
192
  data-size-variant={sizeAttr}
66
- data-placement={placement}
193
+ data-placement={resolved}
67
194
  aria-describedby={open ? tipId : undefined}
68
195
  onmouseenter={show}
69
196
  onmouseleave={hide}
@@ -71,8 +198,16 @@
71
198
  onfocusout={hide}
72
199
  onkeydown={onKeydown}
73
200
  >
74
- <span class="trigger">{@render children()}</span>
75
- <span id={tipId} class="tip" class:open role="tooltip" hidden={!open}>
201
+ <span class="trigger" bind:this={triggerEl}>{@render children()}</span>
202
+ <span
203
+ id={tipId}
204
+ class="tip"
205
+ class:open
206
+ role="tooltip"
207
+ hidden={!open}
208
+ style:--shift="{shift}px"
209
+ bind:this={tipEl}
210
+ >
76
211
  {#if tipSnippet}{@render tipSnippet()}{:else}{text}{/if}
77
212
  </span>
78
213
  </span>
@@ -87,6 +222,7 @@
87
222
  }
88
223
 
89
224
  .tip {
225
+ --shift: 0px;
90
226
  position: absolute;
91
227
  z-index: 60;
92
228
  display: block;
@@ -115,45 +251,45 @@
115
251
  .ss-tooltip[data-placement=top] .tip {
116
252
  bottom: calc(100% + var(--ss-tooltip-offset, 6px));
117
253
  left: 50%;
118
- transform: translateX(-50%) scale(0.98);
254
+ transform: translateX(calc(-50% + var(--shift))) scale(0.98);
119
255
  transform-origin: bottom center;
120
256
  }
121
257
 
122
258
  .ss-tooltip[data-placement=top] .tip.open {
123
- transform: translateX(-50%) scale(1);
259
+ transform: translateX(calc(-50% + var(--shift))) scale(1);
124
260
  }
125
261
 
126
262
  .ss-tooltip[data-placement=bottom] .tip {
127
263
  top: calc(100% + var(--ss-tooltip-offset, 6px));
128
264
  left: 50%;
129
- transform: translateX(-50%) scale(0.98);
265
+ transform: translateX(calc(-50% + var(--shift))) scale(0.98);
130
266
  transform-origin: top center;
131
267
  }
132
268
 
133
269
  .ss-tooltip[data-placement=bottom] .tip.open {
134
- transform: translateX(-50%) scale(1);
270
+ transform: translateX(calc(-50% + var(--shift))) scale(1);
135
271
  }
136
272
 
137
273
  .ss-tooltip[data-placement=left] .tip {
138
274
  right: calc(100% + var(--ss-tooltip-offset, 6px));
139
275
  top: 50%;
140
- transform: translateY(-50%) scale(0.98);
276
+ transform: translateY(calc(-50% + var(--shift))) scale(0.98);
141
277
  transform-origin: right center;
142
278
  }
143
279
 
144
280
  .ss-tooltip[data-placement=left] .tip.open {
145
- transform: translateY(-50%) scale(1);
281
+ transform: translateY(calc(-50% + var(--shift))) scale(1);
146
282
  }
147
283
 
148
284
  .ss-tooltip[data-placement=right] .tip {
149
285
  left: calc(100% + var(--ss-tooltip-offset, 6px));
150
286
  top: 50%;
151
- transform: translateY(-50%) scale(0.98);
287
+ transform: translateY(calc(-50% + var(--shift))) scale(0.98);
152
288
  transform-origin: left center;
153
289
  }
154
290
 
155
291
  .ss-tooltip[data-placement=right] .tip.open {
156
- transform: translateY(-50%) scale(1);
292
+ transform: translateY(calc(-50% + var(--shift))) scale(1);
157
293
  }
158
294
 
159
295
  @media (prefers-reduced-motion: reduce) {
@@ -166,12 +302,12 @@
166
302
  .ss-tooltip[data-placement=top] .tip.open,
167
303
  .ss-tooltip[data-placement=bottom] .tip,
168
304
  .ss-tooltip[data-placement=bottom] .tip.open {
169
- transform: translateX(-50%);
305
+ transform: translateX(calc(-50% + var(--shift)));
170
306
  }
171
307
  .ss-tooltip[data-placement=left] .tip,
172
308
  .ss-tooltip[data-placement=left] .tip.open,
173
309
  .ss-tooltip[data-placement=right] .tip,
174
310
  .ss-tooltip[data-placement=right] .tip.open {
175
- transform: translateY(-50%);
311
+ transform: translateY(calc(-50% + var(--shift)));
176
312
  }
177
313
  }</style>
@@ -11,8 +11,18 @@ interface Props {
11
11
  * WAI-ARIA tooltip pattern.
12
12
  */
13
13
  text: string | Snippet;
14
- /** Where the tooltip attaches relative to the trigger. Default `top`. */
14
+ /**
15
+ * Preferred side the tooltip attaches to relative to the trigger. Default `top`.
16
+ * With `avoidCollisions` (the default) this is only a preference — see below.
17
+ */
15
18
  placement?: Placement;
19
+ /**
20
+ * Keep the tip visible when the preferred side has no room (DS-0146): flip to the
21
+ * opposite side, then to the perpendicular side with the most room, then shift along
22
+ * the cross axis so the tip never overhangs the viewport or an `overflow`-clipping
23
+ * ancestor. `false` pins the tip to `placement`. Default `true`.
24
+ */
25
+ avoidCollisions?: boolean;
16
26
  /** Token size (sm|md|lg); inherits the global size when unset. */
17
27
  size?: Size;
18
28
  /** Trigger content (wrapped in an inline-block container). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dssoca",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Svelte 5 design system — signal green on near-black, monospace-forward, zero border-radius; configured along two axes (color: dark/light, density: comfy/compact).",
5
5
  "license": "MIT",
6
6
  "author": "Rafael Passoca",