bento-layout.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Layout for the bento grid, in grid cells.
  3. *
  4. * The first layout packs cards in order exactly like CSS
  5. * `grid-auto-flow: row dense` (first fit from the top-left), so the server can
  6. * render it. While a card is dragged, it claims the cell under it and every
  7. * other card re-packs tightly around it in the order they had when the drag
  8. * began, like icons and widgets on a phone's home screen. The result depends
  9. * only on where the card is held, so the grid never oscillates, and there are
  10. * no holes left behind.
  11. */
  12. export type Size = readonly [w: number, h: number];
  13. export type Cell = { id: string; x: number; y: number; w: number; h: number };
  14. /**
  15. * Layout tiers: columns, and the viewport width where each starts. Cells stay
  16. * between ~230px and ~350px from small tablets up, the range the cards' larger
  17. * type is drawn for; phones get their own sizes (wide phones a square hero).
  18. */
  19. export const TIERS = [
  20. { cols: 2, min: 0 },
  21. { cols: 2, min: 480 },
  22. { cols: 2, min: 640 },
  23. { cols: 3, min: 768 },
  24. { cols: 4, min: 1024 },
  25. ] as const;
  26. export const tierFor = (width: number) => {
  27. let tier = 0;
  28. TIERS.forEach((t, i) => {
  29. if (width >= t.min) tier = i;
  30. });
  31. return tier;
  32. };
  33. /** First fit from the top-left, in order (what CSS `row dense` does). */
  34. export function pack(
  35. ids: readonly string[],
  36. sizeOf: (id: string) => Size,
  37. cols: number,
  38. ): Cell[] {
  39. const taken: boolean[][] = [];
  40. const fits = (x: number, y: number, w: number, h: number) => {
  41. for (let r = y; r < y + h; r++) {
  42. for (let c = x; c < x + w; c++) if (taken[r]?.[c]) return false;
  43. }
  44. return true;
  45. };
  46. return ids.map((id) => {
  47. const [sw, h] = sizeOf(id);
  48. const w = Math.min(sw, cols);
  49. for (let y = 0; ; y++) {
  50. for (let x = 0; x + w <= cols; x++) {
  51. if (!fits(x, y, w, h)) continue;
  52. for (let r = y; r < y + h; r++) {
  53. taken[r] ??= [];
  54. for (let c = x; c < x + w; c++) taken[r][c] = true;
  55. }
  56. return { id, x, y, w, h };
  57. }
  58. }
  59. });
  60. }
  61. /**
  62. * Hold card `id` at (x, y) and pack every other card around it, first fit in
  63. * `order`. If nothing is above the held card, it floats up to meet the rest.
  64. */
  65. export function placeAt(
  66. order: readonly string[],
  67. sizeOf: (id: string) => Size,
  68. cols: number,
  69. id: string,
  70. x: number,
  71. y: number,
  72. ): Cell[] {
  73. const [sw, h] = sizeOf(id);
  74. const w = Math.min(sw, cols);
  75. const taken: boolean[][] = [];
  76. const mark = (c: Cell, value: boolean) => {
  77. for (let r = c.y; r < c.y + c.h; r++) {
  78. taken[r] ??= [];
  79. for (let col = c.x; col < c.x + c.w; col++) taken[r][col] = value;
  80. }
  81. };
  82. const fits = (cx: number, cy: number, cw: number, ch: number) => {
  83. for (let r = cy; r < cy + ch; r++) {
  84. for (let col = cx; col < cx + cw; col++)
  85. if (taken[r]?.[col]) return false;
  86. }
  87. return true;
  88. };
  89. const held: Cell = { id, x: Math.min(Math.max(x, 0), cols - w), y, w, h };
  90. mark(held, true);
  91. const others = order
  92. .filter((o) => o !== id)
  93. .map((o) => {
  94. const [ow0, oh] = sizeOf(o);
  95. const ow = Math.min(ow0, cols);
  96. for (let cy = 0; ; cy++) {
  97. for (let cx = 0; cx + ow <= cols; cx++) {
  98. if (!fits(cx, cy, ow, oh)) continue;
  99. const cell = { id: o, x: cx, y: cy, w: ow, h: oh };
  100. mark(cell, true);
  101. return cell;
  102. }
  103. }
  104. });
  105. // Don't leave the held card hanging below empty space.
  106. mark(held, false);
  107. while (held.y > 0 && fits(held.x, held.y - 1, w, 1)) held.y--;
  108. return [held, ...others];
  109. }
  110. export const sameLayout = (a: Cell[], b: Cell[]) =>
  111. a.length === b.length &&
  112. a.every((c) => {
  113. const d = b.find((x) => x.id === c.id);
  114. return d && d.x === c.x && d.y === c.y;
  115. });
  116. /** Reading order of a layout: top to bottom, left to right. */
  117. export const readingOrder = (cells: Cell[]) =>
  118. [...cells].sort((a, b) => a.y - b.y || a.x - b.x).map((c) => c.id);