Replace the `* 9999999px` multiplier trick in NodeGap with CSS container style queries

Open
#240 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Quiet
Tech stack
css, typescript
Domain
frontend

Research direction

Start with NodeGap.svelte and NodeCaret.svelte, locating the positioning rules that use the 9999999px and 99999px sentinels. Replace only the row/column branch selection with style-query branches for --row: 0 and --row: 1, leaving anchor fallback values out of scope. Done means the consumer-facing --row API remains unchanged and both axes retain their positioning behavior, including changes under media or container queries.

Written by the indexing model from the issue text.

Description

Problem

NodeGap.svelte and NodeCaret.svelte switch between row and column positioning by reading --row (0 or 1) and using a multiplier-inside-min() trick:

top: min(
  calc(var(--_s-b) + var(--_R) * 9999999px),
  calc(var(--_mid) + var(--_R) * 9999999px),
  calc(var(--_s-t) + var(--_C) * 9999999px)
);

Adding 9999999px to a branch makes it lose min() — effectively disabling the wrong-axis branch. The same trick repeats throughout positioning rules.

This works but has some downsides:

  • Hard ceiling at ~10 million px. Documents (or scroll containers) taller than the sentinel produce wrong positioning. We cannot go beyond this because of css limitations in e.g. blink browser (but would need to be tested).
  • Hard to read. Every coordinate carries + var(--_R) * 9999999px or + var(--_C) * 9999999px. Intent is buried in arithmetic.
  • Fragile. Changing the sentinel requires updating every site (currently 9999999px is paired with a 99999px sentinel elsewhere — easy to drift. Had lots of headaches because of this already).
Proposal

Use CSS container style queries to route positioning rules by --row value. Each branch contains only the calculations relevant to its axis — no sentinels, no disable trick.

.positioned.gap-after:not(.last) .svedit-selectable {
  /* shared declarations */
}

@container style(--row: 0) {
  .positioned.gap-after:not(.last) .svedit-selectable {
    top: min(calc(var(--_s-b)), var(--_mid));
    bottom: min(calc(var(--_n-t)), var(--_mid));
    /* … */
  }
}

@container style(--row: 1) {
  .positioned.gap-after:not(.last) .svedit-selectable {
    left: min(calc(var(--_s-r)), var(--_mid));
    right: min(calc(var(--_n-l)), var(--_mid));
    /* … */
  }
}

The consumer-facing API is unchanged: --row: 0 for vertical flow, --row: 1 for horizontal, declared on the node array or any ancestor. The multiplier trick disappears.

Why this works for cases the multiplier trick was protecting against

--row can change under media queries, container queries, or theme switches. Style queries re-evaluate just like any other matcher: when the consumer flips --row in a media query, the active branch swaps automatically. There is no static-axis assumption baked into the library.

Benefits
  • No document-height ceiling.
  • Positioning rules are readable in isolation — each branch reads as plain row or plain column code.
  • Removes paired sentinels (9999999px, 99999px) and the implicit contract between them.
  • Slightly less work for the engine: fewer anchor() resolutions per gap (only the active branch's anchors are evaluated).
Costs
  • Browser support. Container style queries: Chrome/Edge 111+ (Mar 2023), Safari 18 (Sep 2024), Firefox not yet supported. Affects orientation correctness on Firefox until it ships; layout still renders, gaps and carets just position as if for the default axis.
Out of scope
  • Anchor fallback values (anchor(..., 9999999px)) used for orphan-anchor handling — those are a separate concern from the row/column branch-disable trick.
Dominant language
TypeScript
Stars
691
Forks
20
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from michael/svedit

All issues in michael/svedit

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.