@kud/ink-ui 0.20.2 → 0.21.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.
Files changed (2) hide show
  1. package/dist/index.js +15 -14
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -665,26 +665,21 @@ var between = (from, to, t) => {
665
665
  width: Math.max(1, lerp(from.width, to.width))
666
666
  };
667
667
  };
668
- var nearestTo = (rules, rule, fallback) => {
669
- const centre = (r) => r.start + r.width / 2;
670
- if (rules.length === 0) return fallback;
671
- return rules.reduce(
672
- (best, r, i) => Math.abs(centre(r) - centre(rule)) < Math.abs(centre(rules[best]) - centre(rule)) ? i : best,
673
- Math.min(fallback, rules.length - 1)
674
- );
675
- };
676
668
  var useSlide = (rules, active) => {
677
669
  const target = rules[active] ?? null;
678
670
  const [step, setStep] = useState(SLIDE_STEPS);
679
671
  const from = useRef(target);
680
672
  const last = useRef(active);
673
+ const litIndex = useRef(active);
681
674
  useEffect(() => {
682
675
  if (last.current === active) return;
683
- from.current = step >= SLIDE_STEPS ? rules[last.current] ?? target : between(
676
+ const settled = step >= SLIDE_STEPS;
677
+ from.current = settled ? rules[last.current] ?? target : between(
684
678
  from.current ?? target,
685
679
  rules[last.current] ?? target,
686
680
  step / SLIDE_STEPS
687
681
  );
682
+ if (settled) litIndex.current = last.current;
688
683
  last.current = active;
689
684
  setStep(0);
690
685
  }, [active]);
@@ -693,9 +688,14 @@ var useSlide = (rules, active) => {
693
688
  const id = setTimeout(() => setStep((s) => s + 1), SLIDE_MS);
694
689
  return () => clearTimeout(id);
695
690
  }, [step]);
696
- if (!target) return null;
697
- if (step >= SLIDE_STEPS || !from.current) return target;
698
- return between(from.current, target, step / SLIDE_STEPS);
691
+ const arrived = step >= SLIDE_STEPS;
692
+ if (arrived) litIndex.current = active;
693
+ if (!target) return { rule: null, lit: litIndex.current };
694
+ if (arrived || !from.current) return { rule: target, lit: active };
695
+ return {
696
+ rule: between(from.current, target, step / SLIDE_STEPS),
697
+ lit: litIndex.current
698
+ };
699
699
  };
700
700
  var Tabs = ({ active, items }) => {
701
701
  const cells = items.map((item) => ({
@@ -714,8 +714,9 @@ var Tabs = ({ active, items }) => {
714
714
  x += gutterOf(cell) + labelOf(cell) + GAP;
715
715
  }
716
716
  const activeIndex = cells.findIndex((c) => c.isActive);
717
- const rule = useSlide(rules, activeIndex) ?? { start: 0, width: 0 };
718
- const litIndex = nearestTo(rules, rule, Math.max(0, activeIndex));
717
+ const slide = useSlide(rules, activeIndex);
718
+ const rule = slide.rule ?? { start: 0, width: 0 };
719
+ const litIndex = slide.lit;
719
720
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
720
721
  /* @__PURE__ */ jsx(Box, { gap: GAP, children: cells.map((cell, i) => /* @__PURE__ */ jsxs(Box, { children: [
721
722
  cell.marker ? /* @__PURE__ */ jsx(Text, { color: cell.markerColor, children: cell.marker }) : null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kud/ink-ui",
3
- "version": "0.20.2",
3
+ "version": "0.21.0",
4
4
  "description": "React component library for Ink CLIs",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",