|
7 | 7 | * @position Storybook demonstrations for List consumers |
8 | 8 | */ |
9 | 9 |
|
10 | | -import {useState} from 'react'; |
| 10 | +import {useEffect, useRef, useState} from 'react'; |
11 | 11 | import * as stylex from '@stylexjs/stylex'; |
12 | 12 | import type {Meta, StoryObj} from '@storybook/react'; |
13 | 13 | import {List, ListItem} from '@astryxdesign/core/List'; |
@@ -120,6 +120,254 @@ export const Compact: Story = { |
120 | 120 | ), |
121 | 121 | }; |
122 | 122 |
|
| 123 | +/** How far a row's touch target reaches above and below its box, px. */ |
| 124 | +interface TouchReach { |
| 125 | + readonly label: string; |
| 126 | + readonly above: number; |
| 127 | + readonly below: number; |
| 128 | +} |
| 129 | + |
| 130 | +/** One striped band of a row's touch target, outside the row's own box. */ |
| 131 | +interface TouchBand { |
| 132 | + readonly key: string; |
| 133 | + readonly left: number; |
| 134 | + readonly top: number; |
| 135 | + readonly width: number; |
| 136 | + readonly height: number; |
| 137 | +} |
| 138 | + |
| 139 | +interface TouchTargets { |
| 140 | + readonly bands: ReadonlyArray<TouchBand>; |
| 141 | + readonly reach: ReadonlyArray<TouchReach>; |
| 142 | +} |
| 143 | + |
| 144 | +const TOUCH_TARGET_ROWS = ['Row 1', 'Row 2', 'Row 3', 'Row 4']; |
| 145 | + |
| 146 | +/** A length rounded to the nearest half pixel. */ |
| 147 | +const toHalfPx = (value: number) => Math.round(value * 2) / 2; |
| 148 | + |
| 149 | +/** |
| 150 | + * Reads each row's touch hit layer, the row's own `::after`, as the browser |
| 151 | + * lays it out, and the bands of it past the row's box. Under a fine pointer a |
| 152 | + * row has no layer, so nothing is read. |
| 153 | + */ |
| 154 | +function readTouchTargets(frame: HTMLElement): TouchTargets { |
| 155 | + const origin = frame.getBoundingClientRect(); |
| 156 | + const bands: TouchBand[] = []; |
| 157 | + const reach: TouchReach[] = []; |
| 158 | + frame.querySelectorAll<HTMLElement>('ul > li').forEach((row, index) => { |
| 159 | + const layer = getComputedStyle(row, '::after'); |
| 160 | + if (layer.position !== 'absolute') { |
| 161 | + return; |
| 162 | + } |
| 163 | + const style = getComputedStyle(row); |
| 164 | + const box = row.getBoundingClientRect(); |
| 165 | + // The layer's offsets start at the row's padding box, inside its border. |
| 166 | + const top = |
| 167 | + box.top + parseFloat(style.borderTopWidth) + parseFloat(layer.top); |
| 168 | + const bottom = |
| 169 | + box.bottom - |
| 170 | + parseFloat(style.borderBottomWidth) - |
| 171 | + parseFloat(layer.bottom); |
| 172 | + const left = |
| 173 | + box.left + parseFloat(style.borderLeftWidth) + parseFloat(layer.left); |
| 174 | + const right = |
| 175 | + box.right - parseFloat(style.borderRightWidth) - parseFloat(layer.right); |
| 176 | + if (![top, bottom, left, right].every(Number.isFinite)) { |
| 177 | + return; |
| 178 | + } |
| 179 | + const label = TOUCH_TARGET_ROWS[index] ?? `Row ${index + 1}`; |
| 180 | + reach.push({ |
| 181 | + label, |
| 182 | + above: toHalfPx(box.top - top), |
| 183 | + below: toHalfPx(bottom - box.bottom), |
| 184 | + }); |
| 185 | + const addBand = ( |
| 186 | + side: string, |
| 187 | + fromX: number, |
| 188 | + fromY: number, |
| 189 | + toX: number, |
| 190 | + toY: number, |
| 191 | + ) => { |
| 192 | + if (toX - fromX >= 0.5 && toY - fromY >= 0.5) { |
| 193 | + bands.push({ |
| 194 | + key: `${label} ${side}`, |
| 195 | + left: fromX - origin.left, |
| 196 | + top: fromY - origin.top, |
| 197 | + width: toX - fromX, |
| 198 | + height: toY - fromY, |
| 199 | + }); |
| 200 | + } |
| 201 | + }; |
| 202 | + addBand('above', left, top, right, box.top); |
| 203 | + addBand('below', left, box.bottom, right, bottom); |
| 204 | + addBand('left', left, box.top, box.left, box.bottom); |
| 205 | + addBand('right', box.right, box.top, right, box.bottom); |
| 206 | + }); |
| 207 | + return {bands, reach}; |
| 208 | +} |
| 209 | + |
| 210 | +const touchTargetStyles = stylex.create({ |
| 211 | + frame: { |
| 212 | + position: 'relative', |
| 213 | + padding: 24, |
| 214 | + }, |
| 215 | + bands: { |
| 216 | + position: 'absolute', |
| 217 | + inset: 0, |
| 218 | + pointerEvents: 'none', |
| 219 | + }, |
| 220 | + band: { |
| 221 | + position: 'absolute', |
| 222 | + backgroundImage: |
| 223 | + 'repeating-linear-gradient(-45deg, color-mix(in srgb, var(--color-accent) 60%, transparent) 0 2px, transparent 2px 5px)', |
| 224 | + outline: |
| 225 | + '1px solid color-mix(in srgb, var(--color-accent) 80%, transparent)', |
| 226 | + outlineOffset: -1, |
| 227 | + }, |
| 228 | +}); |
| 229 | + |
| 230 | +function CompactTouchTargetsDemo() { |
| 231 | + const frameRef = useRef<HTMLDivElement | null>(null); |
| 232 | + // Whether a row handled the click that the page is dispatching. |
| 233 | + const rowFiredRef = useRef(false); |
| 234 | + const [lastTapped, setLastTapped] = useState('nothing yet'); |
| 235 | + const [targets, setTargets] = useState<TouchTargets>({ |
| 236 | + bands: [], |
| 237 | + reach: [], |
| 238 | + }); |
| 239 | + |
| 240 | + // Syncs with the browser's layout and the page's clicks. Each row's hit insets |
| 241 | + // arrive after the first frame and change with the page, so the bands follow |
| 242 | + // them. A click that no row handles reads "none". |
| 243 | + useEffect(() => { |
| 244 | + const frame = frameRef.current; |
| 245 | + if (frame == null) { |
| 246 | + return undefined; |
| 247 | + } |
| 248 | + let pending = 0; |
| 249 | + const measure = () => { |
| 250 | + pending = 0; |
| 251 | + setTargets(readTouchTargets(frame)); |
| 252 | + }; |
| 253 | + const schedule = () => { |
| 254 | + if (pending === 0) { |
| 255 | + pending = requestAnimationFrame(measure); |
| 256 | + } |
| 257 | + }; |
| 258 | + const lists = frame.querySelectorAll('ul'); |
| 259 | + const insets = new MutationObserver(schedule); |
| 260 | + const sizes = new ResizeObserver(schedule); |
| 261 | + sizes.observe(frame); |
| 262 | + lists.forEach(list => { |
| 263 | + insets.observe(list, { |
| 264 | + attributes: true, |
| 265 | + attributeFilter: ['style'], |
| 266 | + subtree: true, |
| 267 | + }); |
| 268 | + sizes.observe(list); |
| 269 | + }); |
| 270 | + const doc = frame.ownerDocument; |
| 271 | + let verdict = 0; |
| 272 | + // The capture phase hears every click, including one the shared hit area |
| 273 | + // swallows. The check runs once the click's dispatch has ended. |
| 274 | + const onAnyClick = () => { |
| 275 | + rowFiredRef.current = false; |
| 276 | + clearTimeout(verdict); |
| 277 | + verdict = window.setTimeout(() => { |
| 278 | + if (!rowFiredRef.current) { |
| 279 | + setLastTapped('none'); |
| 280 | + } |
| 281 | + }); |
| 282 | + }; |
| 283 | + doc.addEventListener('click', onAnyClick, true); |
| 284 | + schedule(); |
| 285 | + return () => { |
| 286 | + cancelAnimationFrame(pending); |
| 287 | + clearTimeout(verdict); |
| 288 | + insets.disconnect(); |
| 289 | + sizes.disconnect(); |
| 290 | + doc.removeEventListener('click', onAnyClick, true); |
| 291 | + }; |
| 292 | + }, []); |
| 293 | + |
| 294 | + const onRowClick = (label: string) => { |
| 295 | + rowFiredRef.current = true; |
| 296 | + setLastTapped(label); |
| 297 | + }; |
| 298 | + const row = (label: string) => ( |
| 299 | + <ListItem key={label} label={label} onClick={() => onRowClick(label)} /> |
| 300 | + ); |
| 301 | + return ( |
| 302 | + <VStack gap={4}> |
| 303 | + <div aria-live="polite"> |
| 304 | + <Text>Last tapped: {lastTapped}</Text> |
| 305 | + </div> |
| 306 | + <div ref={frameRef} {...stylex.props(touchTargetStyles.frame)}> |
| 307 | + <VStack gap={2}> |
| 308 | + <List density="compact" hasDividers> |
| 309 | + {TOUCH_TARGET_ROWS.slice(0, 2).map(row)} |
| 310 | + </List> |
| 311 | + <List density="compact" hasDividers> |
| 312 | + {TOUCH_TARGET_ROWS.slice(2).map(row)} |
| 313 | + </List> |
| 314 | + </VStack> |
| 315 | + <div aria-hidden="true" {...stylex.props(touchTargetStyles.bands)}> |
| 316 | + {targets.bands.map(band => ( |
| 317 | + <div |
| 318 | + key={band.key} |
| 319 | + {...stylex.props(touchTargetStyles.band)} |
| 320 | + style={{ |
| 321 | + left: band.left, |
| 322 | + top: band.top, |
| 323 | + width: band.width, |
| 324 | + height: band.height, |
| 325 | + }} |
| 326 | + /> |
| 327 | + ))} |
| 328 | + </div> |
| 329 | + </div> |
| 330 | + <VStack gap={1}> |
| 331 | + {targets.reach.length === 0 ? ( |
| 332 | + <Text color="secondary"> |
| 333 | + No bands with a mouse: a row's target is its own box. Open this |
| 334 | + story on a touch screen. |
| 335 | + </Text> |
| 336 | + ) : ( |
| 337 | + targets.reach.map(({label, above, below}) => ( |
| 338 | + <Text key={label} color="secondary"> |
| 339 | + {label}: {above} px above, {below} px below |
| 340 | + </Text> |
| 341 | + )) |
| 342 | + )} |
| 343 | + </VStack> |
| 344 | + </VStack> |
| 345 | + ); |
| 346 | +} |
| 347 | + |
| 348 | +/** |
| 349 | + * Each compact row's touch target under a finger, drawn as a striped band |
| 350 | + * past the row's box. The bands are read from the rows' own hit layers, and |
| 351 | + * the readout names the row a tap fires, or "none". |
| 352 | + */ |
| 353 | +export const CompactTouchTargets: Story = { |
| 354 | + render: () => <CompactTouchTargetsDemo />, |
| 355 | + parameters: { |
| 356 | + docs: { |
| 357 | + description: { |
| 358 | + story: |
| 359 | + "Under a finger each compact row's touch target reaches past its " + |
| 360 | + 'box where nothing is near, up to 44 px tall. The striped bands ' + |
| 361 | + "show that reach, read from the rows' own hit layers. They sit " + |
| 362 | + 'above the first row, below the last one, and halfway across the ' + |
| 363 | + 'gap between the two lists. Touching rows have no band between ' + |
| 364 | + 'them. The ' + |
| 365 | + 'readout names the row a tap fires, or "none".', |
| 366 | + }, |
| 367 | + }, |
| 368 | + }, |
| 369 | +}; |
| 370 | + |
123 | 371 | export const Spacious: Story = { |
124 | 372 | render: args => ( |
125 | 373 | <List density="spacious" {...args}> |
|
0 commit comments