@justai/core 0.9.2 → 0.9.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +111 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justai/core",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "The framework-free heart of the persona shell — the load-bearing behaviour (auth capture, reveal, ready) as plain strings, so Astro and Next inject the SAME logic instead of each re-writing it.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
package/src/index.js CHANGED
@@ -514,7 +514,7 @@ export function personaScroll(options) {
514
514
  // promise is void forever: nobody is ever yanked back to a section they scrolled away from.
515
515
  let taken = false;
516
516
  let landed = false;
517
- const takeOver = () => { taken = true; };
517
+ const takeOver = () => { taken = true; navTo = null; };
518
518
  // What "reaching for it" IS: a wheel, a finger, a key, a drag on the bar. A CLICK is deliberately
519
519
  // not on this list — pressing a link to a cut is asking for a landing, not overriding one.
520
520
  for (const ev of ["wheel", "touchmove", "keydown"]) window.addEventListener(ev, takeOver, { passive: true });
@@ -565,8 +565,90 @@ export function personaScroll(options) {
565
565
  o.titleFor ||
566
566
  ((id) => (id === "kernel" ? text(".pname") : id === "posts" ? text(".handle") : ""));
567
567
 
568
+ // queries: the title strip jumps · the page jolts after tapping a cut · content shifts while scrolling
569
+ // · toolbar title animation causes a layout shift · landing lands 26px off
570
+ //
571
+ // ONE WRITER FOR THE STRIP, BECAUSE IT CHANGES THE PAGE'S HEIGHT.
572
+ //
573
+ // The strip is in flow, above every section, so opening it pushes the whole page down by its own
574
+ // 26px. That makes it a LAYOUT input to `landingY`, not decoration — and a landing computed before it
575
+ // opens is a landing that is wrong by exactly that much. Which is what a person felt: the smooth
576
+ // scroll ran, the strip opened mid-flight and dragged the content under it, and then `land()` snapped
577
+ // the page straight to the corrected number. Three motions for one tap.
578
+ //
579
+ // So the write is factored out here and can be applied WITHOUT its transition, which lets a
580
+ // navigation settle the height first and then scroll once, to a target that is already true.
581
+ // queries: strip height before it opens · landing computed against the wrong toolbar height
582
+ //
583
+ // THE STRIP'S OPEN HEIGHT, NEEDED BEFORE IT OPENS. A landing for anything below the strip has to be
584
+ // computed against the height the strip is GOING to have, not the one it has. Probed once and
585
+ // synchronously — added, measured and removed inside a single task — so no paint ever shows it.
586
+ let stripH = 0;
587
+ const stripHeight = () => {
588
+ if (stripH) return stripH;
589
+ if (!tbTitle) return 0;
590
+ if (tbTitle.classList.contains("show")) { stripH = tbTitle.offsetHeight; return stripH; }
591
+ const keep = tbTitle.style.transition;
592
+ tbTitle.style.transition = "none";
593
+ tbTitle.classList.add("show");
594
+ stripH = tbTitle.offsetHeight;
595
+ tbTitle.classList.remove("show");
596
+ void tbTitle.offsetHeight;
597
+ tbTitle.style.transition = keep;
598
+ return stripH;
599
+ };
600
+
601
+ // WHERE THE PAGE WILL REST ONCE THE STRIP HAS FINISHED MOVING — so the scroll can be aimed there
602
+ // while the strip is still on its way, and the two motions arrive together.
603
+ //
604
+ // Only what FOLLOWS the strip is affected. The kernel is anchored to `#below`'s top, which the strip
605
+ // cannot move because it lives inside it; the profile sits above `#below` entirely. Asked of the
606
+ // document rather than assumed, so this stays true if the shell is ever rearranged.
607
+ const landingAfterStrip = (sec, id) => {
608
+ const y = landingY(sec);
609
+ if (!tbTitle || sec === kernel) return y;
610
+ const willShow = !!titleFor(id);
611
+ if (willShow === tbTitle.classList.contains("show")) return y;
612
+ const stripIsAbove = sec.compareDocumentPosition(tbTitle) & Node.DOCUMENT_POSITION_PRECEDING;
613
+ if (!stripIsAbove) return y;
614
+ return y + (willShow ? stripHeight() : -stripHeight());
615
+ };
616
+
617
+ const setStrip = (id, animate) => {
618
+ if (!tbTitle) return;
619
+ const t = titleFor(id);
620
+ if (animate) {
621
+ tbTitle.classList.toggle("show", !!t);
622
+ if (t && tbTitleText && tbTitleText.textContent !== t) {
623
+ tbTitleText.classList.add("fade");
624
+ setTimeout(() => { tbTitleText.textContent = t; tbTitleText.classList.remove("fade"); }, 150);
625
+ }
626
+ return;
627
+ }
628
+ // Instant: the height must be final before this function returns, so the caller can measure. The
629
+ // text goes with it — a settled height showing the previous words would be its own small lie.
630
+ tbTitle.style.transition = "none";
631
+ tbTitle.classList.toggle("show", !!t);
632
+ if (t && tbTitleText) { tbTitleText.classList.remove("fade"); tbTitleText.textContent = t; }
633
+ void tbTitle.offsetHeight;
634
+ tbTitle.style.transition = "";
635
+ };
636
+
568
637
  let lastCut = null;
569
638
  let first = true;
639
+ // queries: the toolbar icon changes late · nav icon lags the tap · active state waits for the scroll
640
+ //
641
+ // THE DESTINATION, HELD FOR THE LENGTH OF THE TRIP.
642
+ //
643
+ // The spy is truthful: it reports the section the page is actually over. During a smooth scroll that
644
+ // is the section being LEFT, so the toolbar answered a tap only once the scroll finished — the icon
645
+ // changed a beat late for no reason a person could see. A tap is a request to arrive, the same
646
+ // reading the landing promise already takes, so the toolbar may show where it is going.
647
+ //
648
+ // Held rather than merely set, because setting it optimistically would be undone by the very next
649
+ // spy frame and flicker. Released the moment the page arrives, or the moment somebody takes over —
650
+ // nobody is shown a destination they have already abandoned.
651
+ let navTo = null;
570
652
  // The cut you are on is the last one you have reached — asked in the one vocabulary `landingY`
571
653
  // defines, so this can never answer something a click could not produce. A pixel of tolerance,
572
654
  // because a browser is free to land a smooth scroll a hair short of the number it was given.
@@ -575,26 +657,20 @@ export function personaScroll(options) {
575
657
  const y = window.scrollY + 1;
576
658
  let current = cuts[0];
577
659
  for (const c of cuts) if (landingY(c[0]) <= y) current = c;
660
+ if (navTo) {
661
+ if (current === navTo || taken) navTo = null;
662
+ else current = navTo;
663
+ }
578
664
  for (const c of cuts) c[1].classList.toggle("active", c === current);
579
665
  if (current === lastCut) return;
580
666
  lastCut = current;
581
667
  if (current[0].id && "#" + current[0].id !== location.hash) setHash(current[0].id);
582
668
 
583
- const t = titleFor(current[0].id);
584
- if (first) {
585
- // First paint is not a transition: the page opens at the kernel, so the title is already true
586
- // when it arrives. Animating it in tells a person something changed when nothing did.
587
- first = false;
588
- if (tbTitle) { tbTitle.style.transition = "none"; tbTitle.classList.toggle("show", !!t); }
589
- if (t && tbTitleText) tbTitleText.textContent = t;
590
- if (tbTitle) { void tbTitle.offsetHeight; tbTitle.style.transition = ""; }
591
- } else {
592
- if (tbTitle) tbTitle.classList.toggle("show", !!t);
593
- if (t && tbTitleText && tbTitleText.textContent !== t) {
594
- tbTitleText.classList.add("fade");
595
- setTimeout(() => { tbTitleText.textContent = t; tbTitleText.classList.remove("fade"); }, 150);
596
- }
597
- }
669
+ // First paint is not a transition: the page opens at the kernel, so the title is already true when
670
+ // it arrives. Animating it in tells a person something changed when nothing did.
671
+ const wasFirst = first;
672
+ first = false;
673
+ setStrip(current[0].id, !wasFirst);
598
674
  };
599
675
  // queries: scroll janks while the toolbar updates · scroll handler runs too often · the spy reads a
600
676
  // stale position · the section is right but the toolbar is one behind · scrollend
@@ -659,8 +735,19 @@ export function personaScroll(options) {
659
735
  // becomes current, which drags the scroll ~18px past the mark and tucks the kernel's first line
660
736
  // under the sticky bar. Without this the toolbar's own icon landed flush and the avatar did not,
661
737
  // for no reason a person could see.
738
+ // THE STRIP AND THE SCROLL RUN TOGETHER. The strip keeps its own transition — it is meant to be
739
+ // seen growing, and snapping it open instead just moves the jolt to the moment of the tap. What
740
+ // changes is the AIM: the scroll is sent to where the page will rest once the strip has finished,
741
+ // so the growing band and the moving page arrive at the same place at the same time and nothing
742
+ // has to be corrected afterwards.
743
+ // The icon answers the tap NOW, not when the scroll lands. `navTo` keeps it answered for the trip.
744
+ navTo = t;
745
+ for (const c of cuts) c[1].classList.toggle("active", c === t);
746
+ setStrip(t[0].id, true);
747
+ // `landed` stays re-armed as a net. If the aim above is ever wrong the old instant repair still
748
+ // catches it — a rare snap is a better failure than a landing left in the wrong place.
662
749
  landed = false;
663
- goTo(t[0], "smooth");
750
+ window.scrollTo({ top: landingAfterStrip(t[0], t[0].id), behavior: "smooth" });
664
751
  setHash(t[0].id);
665
752
  });
666
753
 
@@ -668,7 +755,13 @@ export function personaScroll(options) {
668
755
  // `setHash` uses replaceState and fires nothing, so this can only be somebody else's navigation.
669
756
  window.addEventListener("hashchange", () => {
670
757
  const t = cuts.find((c) => "#" + c[0].id === location.hash);
671
- if (t) { landed = false; goTo(t[0], "smooth"); }
758
+ if (t) {
759
+ navTo = t;
760
+ for (const c of cuts) c[1].classList.toggle("active", c === t);
761
+ setStrip(t[0].id, true);
762
+ landed = false;
763
+ window.scrollTo({ top: landingAfterStrip(t[0], t[0].id), behavior: "smooth" });
764
+ }
672
765
  });
673
766
  }
674
767