@alchemy.run/sigil 0.0.0-alpha.1 → 0.0.0-alpha.2
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.
- package/README.md +299 -299
- package/dist/ansi.d.ts +223 -0
- package/dist/ansi.js +2 -0
- package/dist/{devtools-QpCMm9JH.mjs → devtools-BhYGjb7h.js} +1 -1
- package/dist/index-DDVME65c.d.ts +919 -0
- package/dist/index.d.ts +1657 -0
- package/dist/index.js +4643 -0
- package/dist/sgr-CMfEpjSk.d.ts +91 -0
- package/dist/truncate-CBiyyZzw.js +2156 -0
- package/dist/yoga-5jKhYCJC.js +3465 -0
- package/dist/yoga.d.ts +2 -0
- package/dist/yoga.js +2 -0
- package/package.json +37 -17
- package/src/ansi/chalk.ts +179 -0
- package/src/ansi/cursor.ts +48 -0
- package/src/ansi/east-asian-width.ts +215 -0
- package/src/ansi/escapes.ts +128 -0
- package/src/ansi/index.ts +27 -0
- package/src/ansi/sgr.ts +237 -0
- package/src/ansi/slice.ts +43 -0
- package/src/ansi/string-width.ts +236 -0
- package/src/ansi/strip.ts +33 -0
- package/src/ansi/supports-color.ts +213 -0
- package/src/ansi/tokenize.ts +453 -0
- package/src/ansi/truncate.ts +194 -0
- package/src/ansi/widest-line.ts +12 -0
- package/src/ansi/wrap.ts +766 -0
- package/src/ansi-tokenizer.ts +510 -0
- package/src/auto-bind.ts +41 -0
- package/src/boxes.ts +100 -0
- package/src/code-excerpt.ts +39 -0
- package/src/colorize.ts +60 -0
- package/src/components/AccessibilityContext.ts +5 -0
- package/src/components/AnimationContext.ts +24 -0
- package/src/components/App.tsx +781 -0
- package/src/components/AppContext.ts +111 -0
- package/src/components/BackgroundContext.ts +8 -0
- package/src/components/Box.tsx +116 -0
- package/src/components/CursorContext.ts +19 -0
- package/src/components/ErrorBoundary.tsx +38 -0
- package/src/components/ErrorOverview.tsx +133 -0
- package/src/components/FocusContext.ts +30 -0
- package/src/components/Newline.tsx +15 -0
- package/src/components/Spacer.tsx +10 -0
- package/src/components/Static.tsx +59 -0
- package/src/components/StderrContext.ts +26 -0
- package/src/components/StdinContext.ts +49 -0
- package/src/components/StdoutContext.ts +28 -0
- package/src/components/Text.tsx +144 -0
- package/src/components/Transform.tsx +37 -0
- package/src/cursor-position.ts +103 -0
- package/src/devtools-window-polyfill.ts +73 -0
- package/src/devtools.ts +43 -0
- package/src/dom.ts +292 -0
- package/src/get-max-width.ts +11 -0
- package/src/global.d.ts +36 -0
- package/src/hooks/use-animation.ts +142 -0
- package/src/hooks/use-app.ts +8 -0
- package/src/hooks/use-box-metrics.ts +134 -0
- package/src/hooks/use-cursor.ts +33 -0
- package/src/hooks/use-focus-manager.ts +62 -0
- package/src/hooks/use-focus.ts +83 -0
- package/src/hooks/use-input.ts +267 -0
- package/src/hooks/use-is-screen-reader-enabled.ts +12 -0
- package/src/hooks/use-paste.ts +78 -0
- package/src/hooks/use-stderr.ts +8 -0
- package/src/hooks/use-stdin.ts +10 -0
- package/src/hooks/use-stdout.ts +8 -0
- package/src/hooks/use-window-size.ts +41 -0
- package/src/indent-string.ts +16 -0
- package/src/index.ts +44 -0
- package/src/ink.tsx +1506 -0
- package/src/input-parser.ts +283 -0
- package/src/instances.ts +9 -0
- package/src/is-in-ci.ts +7 -0
- package/src/kitty-keyboard.ts +57 -0
- package/src/log-update.ts +370 -0
- package/src/measure-element.ts +62 -0
- package/src/measure-text.ts +31 -0
- package/src/output.ts +308 -0
- package/src/parse-keypress.ts +516 -0
- package/src/parse-stack-line.ts +139 -0
- package/src/patch-console.ts +62 -0
- package/src/quick-lru.ts +85 -0
- package/src/reconciler.ts +451 -0
- package/src/render-background.ts +38 -0
- package/src/render-border.ts +134 -0
- package/src/render-node-to-output.ts +191 -0
- package/src/render-to-string.ts +131 -0
- package/src/render.ts +276 -0
- package/src/renderer.ts +73 -0
- package/src/sanitize-ansi.ts +33 -0
- package/src/signal-exit.ts +107 -0
- package/src/squash-text-nodes.ts +40 -0
- package/src/stream.ts +30 -0
- package/src/styles.ts +748 -0
- package/src/terminal-size.ts +57 -0
- package/src/throttle.ts +73 -0
- package/src/types.ts +15 -0
- package/src/utils.ts +40 -0
- package/src/wrap-text.ts +50 -0
- package/src/write-synchronized.ts +9 -0
- package/src/yoga/config.ts +57 -0
- package/src/yoga/core/absoluteLayout.ts +626 -0
- package/src/yoga/core/baseline.ts +66 -0
- package/src/yoga/core/cache.ts +136 -0
- package/src/yoga/core/calculateLayout.ts +2920 -0
- package/src/yoga/core/config.ts +104 -0
- package/src/yoga/core/flexLine.ts +177 -0
- package/src/yoga/core/helpers.ts +293 -0
- package/src/yoga/core/layoutResults.ts +167 -0
- package/src/yoga/core/node.ts +611 -0
- package/src/yoga/core/numeric.ts +44 -0
- package/src/yoga/core/pixelGrid.ts +151 -0
- package/src/yoga/core/style.ts +887 -0
- package/src/yoga/core/types.ts +224 -0
- package/src/yoga/generated/YGEnums.ts +263 -0
- package/src/yoga/index.ts +19 -0
- package/src/yoga/node.ts +1140 -0
- package/dist/index.d.mts +0 -2379
- package/dist/index.mjs +0 -10072
package/src/global.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ReactNode, type Key, type Ref } from "react";
|
|
2
|
+
|
|
3
|
+
import { type DOMElement } from "./dom.ts";
|
|
4
|
+
import { type Styles } from "./styles.ts";
|
|
5
|
+
import { type Except } from "./types.ts";
|
|
6
|
+
|
|
7
|
+
declare module "react" {
|
|
8
|
+
namespace JSX {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
10
|
+
interface IntrinsicElements {
|
|
11
|
+
"ink-box": Ink.Box;
|
|
12
|
+
"ink-text": Ink.Text;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare namespace Ink {
|
|
18
|
+
type Box = {
|
|
19
|
+
internal_static?: boolean;
|
|
20
|
+
children?: ReactNode;
|
|
21
|
+
key?: Key;
|
|
22
|
+
ref?: Ref<DOMElement>;
|
|
23
|
+
style?: Except<Styles, "textWrap">;
|
|
24
|
+
internal_accessibility?: DOMElement["internal_accessibility"];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type Text = {
|
|
28
|
+
children?: ReactNode;
|
|
29
|
+
key?: Key;
|
|
30
|
+
style?: Styles;
|
|
31
|
+
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
33
|
+
internal_transform?: (children: string, index: number) => string;
|
|
34
|
+
internal_accessibility?: DOMElement["internal_accessibility"];
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { useState, useLayoutEffect, useRef, useCallback, useContext } from "react";
|
|
2
|
+
|
|
3
|
+
import { animationContext as AnimationContext } from "../components/AnimationContext.ts";
|
|
4
|
+
|
|
5
|
+
const defaultAnimationInterval = 100;
|
|
6
|
+
const maximumTimerInterval = 2_147_483_647;
|
|
7
|
+
const zeroAnimState = { frame: 0, time: 0, delta: 0 };
|
|
8
|
+
|
|
9
|
+
type Options = {
|
|
10
|
+
/**
|
|
11
|
+
Time between ticks in milliseconds.
|
|
12
|
+
|
|
13
|
+
@default 100
|
|
14
|
+
*/
|
|
15
|
+
readonly interval?: number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
Whether the animation is running. When set to `false`, the animation stops. When toggled back to `true`, all values reset to `0`.
|
|
19
|
+
|
|
20
|
+
@default true
|
|
21
|
+
*/
|
|
22
|
+
readonly isActive?: boolean;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type AnimationResult = {
|
|
26
|
+
/**
|
|
27
|
+
Discrete counter that increments by 1 each interval. Useful for indexed sequences like spinner frames.
|
|
28
|
+
*/
|
|
29
|
+
readonly frame: number;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
Total elapsed time in milliseconds since the animation started or was last reset. Useful for continuous math-based animations like sine waves: `Math.sin(time / 1000 * Math.PI * 2)`.
|
|
33
|
+
*/
|
|
34
|
+
readonly time: number;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
Time in milliseconds since the previous rendered tick. Accounts for throttled renders. Useful for physics-based or velocity-driven motion: `position += speed * delta`.
|
|
38
|
+
*/
|
|
39
|
+
readonly delta: number;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
Resets `frame`, `time`, and `delta` to `0` and restarts timing from the current moment. Useful for one-shot animations triggered by events.
|
|
43
|
+
*/
|
|
44
|
+
readonly reset: () => void;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
A React hook that drives animations. Returns a frame counter, elapsed time, frame delta, and a reset function. All animations share a single timer internally, so multiple animated components consolidate into one render cycle.
|
|
49
|
+
|
|
50
|
+
@example
|
|
51
|
+
```
|
|
52
|
+
import {Text, useAnimation} from 'ink';
|
|
53
|
+
|
|
54
|
+
const Spinner = () => {
|
|
55
|
+
const {frame} = useAnimation({interval: 80});
|
|
56
|
+
const characters = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
|
57
|
+
|
|
58
|
+
return <Text>{characters[frame % characters.length]}</Text>;
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
*/
|
|
62
|
+
export function useAnimation(options?: Options): AnimationResult {
|
|
63
|
+
const { interval = defaultAnimationInterval, isActive = true } = options ?? {};
|
|
64
|
+
const safeInterval = normalizeAnimationInterval(interval);
|
|
65
|
+
const { subscribe, renderThrottleMs } = useContext(AnimationContext);
|
|
66
|
+
const [resetKey, setResetKey] = useState(0);
|
|
67
|
+
const [animState, setAnimState] = useState(zeroAnimState);
|
|
68
|
+
const nextRenderTimeRef = useRef(0);
|
|
69
|
+
const lastRenderTimeRef = useRef(0);
|
|
70
|
+
const previousOptionsRef = useRef({ isActive, safeInterval, resetKey });
|
|
71
|
+
const previousOptions = previousOptionsRef.current;
|
|
72
|
+
const shouldReset =
|
|
73
|
+
isActive &&
|
|
74
|
+
(safeInterval !== previousOptions.safeInterval ||
|
|
75
|
+
!previousOptions.isActive ||
|
|
76
|
+
resetKey !== previousOptions.resetKey);
|
|
77
|
+
|
|
78
|
+
const reset = useCallback(() => {
|
|
79
|
+
setResetKey((k) => k + 1);
|
|
80
|
+
}, []);
|
|
81
|
+
|
|
82
|
+
useLayoutEffect(() => {
|
|
83
|
+
if (!isActive) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Reset to zero immediately so any render that occurs between this
|
|
88
|
+
// effect commit and the first tick shows zeros, not stale values.
|
|
89
|
+
// On initial mount this is a no-op: Object.is bails out because the
|
|
90
|
+
// state was initialized with the same zeroAnimState reference.
|
|
91
|
+
setAnimState(zeroAnimState);
|
|
92
|
+
|
|
93
|
+
let startTime = 0;
|
|
94
|
+
|
|
95
|
+
const { startTime: subscriberStartTime, unsubscribe } = subscribe((currentTime) => {
|
|
96
|
+
const isThrottled = renderThrottleMs > 0 && currentTime < nextRenderTimeRef.current;
|
|
97
|
+
|
|
98
|
+
if (isThrottled) {
|
|
99
|
+
// Coalesce intermediate ticks while Ink is inside the current
|
|
100
|
+
// render-throttle window; the next allowed render will jump
|
|
101
|
+
// straight to the latest elapsed values.
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const elapsed = currentTime - startTime;
|
|
106
|
+
const nextDelta = currentTime - lastRenderTimeRef.current;
|
|
107
|
+
|
|
108
|
+
lastRenderTimeRef.current = currentTime;
|
|
109
|
+
nextRenderTimeRef.current = currentTime + renderThrottleMs;
|
|
110
|
+
setAnimState({
|
|
111
|
+
frame: Math.floor(elapsed / safeInterval),
|
|
112
|
+
time: elapsed,
|
|
113
|
+
delta: nextDelta,
|
|
114
|
+
});
|
|
115
|
+
}, safeInterval);
|
|
116
|
+
// Use the scheduler's start time instead of sampling our own clock so the
|
|
117
|
+
// first delivered tick cannot start one frame late.
|
|
118
|
+
startTime = subscriberStartTime;
|
|
119
|
+
lastRenderTimeRef.current = subscriberStartTime;
|
|
120
|
+
nextRenderTimeRef.current = startTime + renderThrottleMs;
|
|
121
|
+
|
|
122
|
+
return unsubscribe;
|
|
123
|
+
}, [safeInterval, isActive, subscribe, renderThrottleMs, resetKey]);
|
|
124
|
+
|
|
125
|
+
useLayoutEffect(() => {
|
|
126
|
+
previousOptionsRef.current = { isActive, safeInterval, resetKey };
|
|
127
|
+
}, [isActive, safeInterval, resetKey]);
|
|
128
|
+
|
|
129
|
+
if (shouldReset) {
|
|
130
|
+
return { ...zeroAnimState, reset };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return { ...animState, reset };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function normalizeAnimationInterval(interval: number): number {
|
|
137
|
+
if (!Number.isFinite(interval)) {
|
|
138
|
+
return defaultAnimationInterval;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return Math.min(maximumTimerInterval, Math.max(1, interval));
|
|
142
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { type RefObject, useState, useEffect, useCallback, useMemo } from "react";
|
|
2
|
+
|
|
3
|
+
import { type DOMElement, addLayoutListener } from "../dom.ts";
|
|
4
|
+
|
|
5
|
+
// Yoga's `right`/`bottom` are omitted: always `0` for flow layout and unintuitive for absolute positioning.
|
|
6
|
+
/**
|
|
7
|
+
Metrics of a box element.
|
|
8
|
+
|
|
9
|
+
All positions are relative to the element's parent.
|
|
10
|
+
*/
|
|
11
|
+
export type BoxMetrics = {
|
|
12
|
+
/**
|
|
13
|
+
Element width.
|
|
14
|
+
*/
|
|
15
|
+
readonly width: number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
Element height.
|
|
19
|
+
*/
|
|
20
|
+
readonly height: number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
Distance from the left edge of the parent.
|
|
24
|
+
*/
|
|
25
|
+
readonly left: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
Distance from the top edge of the parent.
|
|
29
|
+
*/
|
|
30
|
+
readonly top: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type UseBoxMetricsResult = BoxMetrics & {
|
|
34
|
+
/**
|
|
35
|
+
Whether the currently tracked element has been measured in the latest layout pass.
|
|
36
|
+
*/
|
|
37
|
+
readonly hasMeasured: boolean;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const emptyMetrics: BoxMetrics = {
|
|
41
|
+
width: 0,
|
|
42
|
+
height: 0,
|
|
43
|
+
left: 0,
|
|
44
|
+
top: 0,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-restricted-types
|
|
48
|
+
const findRootNode = (node: DOMElement | null): DOMElement | undefined => {
|
|
49
|
+
if (!node) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!node.parentNode) {
|
|
54
|
+
return node.nodeName === "ink-root" ? node : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return findRootNode(node.parentNode);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
A React hook that returns the current layout metrics for a tracked box element.
|
|
62
|
+
It updates when layout changes (for example terminal resize, sibling/content changes, or position changes).
|
|
63
|
+
|
|
64
|
+
The hook returns `{width: 0, height: 0, left: 0, top: 0}` until the first layout pass completes. It also returns zeros when the tracked ref is detached.
|
|
65
|
+
|
|
66
|
+
Use `hasMeasured` to detect when the currently tracked element has been measured.
|
|
67
|
+
|
|
68
|
+
@example
|
|
69
|
+
```tsx
|
|
70
|
+
import {useRef} from 'react';
|
|
71
|
+
import {Box, Text, useBoxMetrics} from 'ink';
|
|
72
|
+
|
|
73
|
+
const Example = () => {
|
|
74
|
+
const ref = useRef(null);
|
|
75
|
+
const {width, height, left, top, hasMeasured} = useBoxMetrics(ref);
|
|
76
|
+
return (
|
|
77
|
+
<Box ref={ref}>
|
|
78
|
+
<Text>
|
|
79
|
+
{hasMeasured ? `${width}x${height} at ${left},${top}` : 'Measuring...'}
|
|
80
|
+
</Text>
|
|
81
|
+
</Box>
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
*/
|
|
86
|
+
export const useBoxMetrics = (
|
|
87
|
+
/* eslint-disable-next-line @typescript-eslint/no-restricted-types --
|
|
88
|
+
Creating a ref object with an initial null, especially when the ref object
|
|
89
|
+
will be passed to a DOM node's ref attribute, is common in React. */
|
|
90
|
+
ref: RefObject<DOMElement | null>,
|
|
91
|
+
): UseBoxMetricsResult => {
|
|
92
|
+
const [metrics, setMetrics] = useState(emptyMetrics);
|
|
93
|
+
const [hasMeasured, setHasMeasured] = useState(false);
|
|
94
|
+
|
|
95
|
+
const updateMetrics = useCallback(() => {
|
|
96
|
+
const layout = ref.current?.yogaNode?.getComputedLayout() ?? emptyMetrics;
|
|
97
|
+
|
|
98
|
+
setMetrics((previousMetrics) => {
|
|
99
|
+
const hasChanged =
|
|
100
|
+
previousMetrics.width !== layout.width ||
|
|
101
|
+
previousMetrics.height !== layout.height ||
|
|
102
|
+
previousMetrics.left !== layout.left ||
|
|
103
|
+
previousMetrics.top !== layout.top;
|
|
104
|
+
|
|
105
|
+
return hasChanged ? layout : previousMetrics;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
setHasMeasured(Boolean(ref.current));
|
|
109
|
+
}, [ref]);
|
|
110
|
+
|
|
111
|
+
// Runs after every render of this component.
|
|
112
|
+
// This keeps metrics fresh when local state/props in this subtree change.
|
|
113
|
+
useEffect(updateMetrics);
|
|
114
|
+
|
|
115
|
+
// Subscribe to root layout commits so memoized components still receive
|
|
116
|
+
// sibling-driven position/size updates, even when they skip re-rendering.
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
const rootNode = findRootNode(ref.current);
|
|
119
|
+
|
|
120
|
+
if (!rootNode) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return addLayoutListener(rootNode, updateMetrics);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
return useMemo(
|
|
128
|
+
() => ({
|
|
129
|
+
...metrics,
|
|
130
|
+
hasMeasured,
|
|
131
|
+
}),
|
|
132
|
+
[metrics, hasMeasured],
|
|
133
|
+
);
|
|
134
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useContext, useRef, useCallback, useInsertionEffect } from "react";
|
|
2
|
+
|
|
3
|
+
import { CursorContext } from "../components/CursorContext.ts";
|
|
4
|
+
import { type CursorPosition } from "../log-update.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
A React hook that returns methods to control the terminal cursor position.
|
|
8
|
+
|
|
9
|
+
Setting a cursor position makes the cursor visible at the specified coordinates (relative to the Ink output origin). This is useful for IME (Input Method Editor) support, where the composing character is displayed at the cursor location.
|
|
10
|
+
|
|
11
|
+
Pass `undefined` to hide the cursor.
|
|
12
|
+
*/
|
|
13
|
+
export const useCursor = () => {
|
|
14
|
+
const context = useContext(CursorContext);
|
|
15
|
+
const positionRef = useRef<CursorPosition | undefined>(undefined);
|
|
16
|
+
|
|
17
|
+
const setCursorPosition = useCallback((position: CursorPosition | undefined) => {
|
|
18
|
+
positionRef.current = position;
|
|
19
|
+
}, []);
|
|
20
|
+
|
|
21
|
+
// Propagate cursor position to log-update only during commit phase.
|
|
22
|
+
// useInsertionEffect runs before resetAfterCommit (which triggers onRender),
|
|
23
|
+
// and does NOT run for abandoned concurrent renders (e.g. suspended components).
|
|
24
|
+
// This prevents cursor state from leaking across render boundaries.
|
|
25
|
+
useInsertionEffect(() => {
|
|
26
|
+
context.setCursorPosition(positionRef.current);
|
|
27
|
+
return () => {
|
|
28
|
+
context.setCursorPosition(undefined);
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return { setCursorPosition };
|
|
33
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
|
|
3
|
+
import { FocusContext, type Props } from "../components/FocusContext.ts";
|
|
4
|
+
|
|
5
|
+
type Output = {
|
|
6
|
+
/**
|
|
7
|
+
Enable focus management for all components.
|
|
8
|
+
*/
|
|
9
|
+
enableFocus: Props["enableFocus"];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
Disable focus management for all components. The currently active component (if there's one) will lose its focus.
|
|
13
|
+
*/
|
|
14
|
+
disableFocus: Props["disableFocus"];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
Switch focus to the next focusable component. If there's no active component right now, focus will be given to the first focusable component. If the active component is the last in the list of focusable components, focus will be switched to the first focusable component.
|
|
18
|
+
*/
|
|
19
|
+
focusNext: Props["focusNext"];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
Switch focus to the previous focusable component. If there's no active component right now, focus will be given to the first focusable component. If the active component is the first in the list of focusable components, focus will be switched to the last focusable component.
|
|
23
|
+
*/
|
|
24
|
+
focusPrevious: Props["focusPrevious"];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
Switch focus to the element with provided `id`. If there's no element with that `id`, focus is not changed.
|
|
28
|
+
*/
|
|
29
|
+
focus: Props["focus"];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
The ID of the currently focused component, or `undefined` if no component is focused.
|
|
33
|
+
|
|
34
|
+
@example
|
|
35
|
+
```tsx
|
|
36
|
+
import {Text, useFocusManager} from 'ink';
|
|
37
|
+
|
|
38
|
+
const Example = () => {
|
|
39
|
+
const {activeId} = useFocusManager();
|
|
40
|
+
|
|
41
|
+
return <Text>Focused: {activeId ?? 'none'}</Text>;
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
*/
|
|
45
|
+
activeId: Props["activeId"];
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
A React hook that returns methods to enable or disable focus management for all components or manually switch focus to the next or previous components.
|
|
50
|
+
*/
|
|
51
|
+
export const useFocusManager = (): Output => {
|
|
52
|
+
const focusContext = useContext(FocusContext);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
enableFocus: focusContext.enableFocus,
|
|
56
|
+
disableFocus: focusContext.disableFocus,
|
|
57
|
+
focusNext: focusContext.focusNext,
|
|
58
|
+
focusPrevious: focusContext.focusPrevious,
|
|
59
|
+
focus: focusContext.focus,
|
|
60
|
+
activeId: focusContext.activeId,
|
|
61
|
+
};
|
|
62
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { useEffect, useContext, useMemo } from "react";
|
|
2
|
+
|
|
3
|
+
import { FocusContext } from "../components/FocusContext.ts";
|
|
4
|
+
import { useStdin } from "./use-stdin.ts";
|
|
5
|
+
|
|
6
|
+
type Input = {
|
|
7
|
+
/**
|
|
8
|
+
Enable or disable this component's focus, while still maintaining its position in the list of focusable components.
|
|
9
|
+
*/
|
|
10
|
+
isActive?: boolean;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
Auto-focus this component if there's no active (focused) component right now.
|
|
14
|
+
*/
|
|
15
|
+
autoFocus?: boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
Assign an ID to this component, so it can be programmatically focused with `focus(id)`.
|
|
19
|
+
*/
|
|
20
|
+
id?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type Output = {
|
|
24
|
+
/**
|
|
25
|
+
Determines whether this component is focused.
|
|
26
|
+
*/
|
|
27
|
+
isFocused: boolean;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
Allows focusing a specific element with the provided `id`.
|
|
31
|
+
*/
|
|
32
|
+
focus: (id: string) => void;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
A React hook that returns focus state and focus controls for the current component.
|
|
37
|
+
A component that uses the `useFocus` hook becomes "focusable" to Ink, so when the user presses <kbd>Tab</kbd>, Ink will switch focus to this component. If there are multiple components that execute the `useFocus` hook, focus will be given to them in the order in which these components are rendered.
|
|
38
|
+
*/
|
|
39
|
+
export const useFocus = ({
|
|
40
|
+
isActive = true,
|
|
41
|
+
autoFocus = false,
|
|
42
|
+
id: customId,
|
|
43
|
+
}: Input = {}): Output => {
|
|
44
|
+
const { isRawModeSupported, setRawMode } = useStdin();
|
|
45
|
+
const { activeId, add, remove, activate, deactivate, focus } = useContext(FocusContext);
|
|
46
|
+
|
|
47
|
+
const id = useMemo(() => {
|
|
48
|
+
return customId ?? Math.random().toString().slice(2, 7);
|
|
49
|
+
}, [customId]);
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
add(id, { autoFocus });
|
|
53
|
+
|
|
54
|
+
return () => {
|
|
55
|
+
remove(id);
|
|
56
|
+
};
|
|
57
|
+
}, [id, autoFocus, add, remove]);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (isActive) {
|
|
61
|
+
activate(id);
|
|
62
|
+
} else {
|
|
63
|
+
deactivate(id);
|
|
64
|
+
}
|
|
65
|
+
}, [isActive, id, activate, deactivate]);
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (!isRawModeSupported || !isActive) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
setRawMode(true);
|
|
73
|
+
|
|
74
|
+
return () => {
|
|
75
|
+
setRawMode(false);
|
|
76
|
+
};
|
|
77
|
+
}, [isActive, isRawModeSupported, setRawMode]);
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
isFocused: Boolean(id) && activeId === id,
|
|
81
|
+
focus,
|
|
82
|
+
};
|
|
83
|
+
};
|