@algolia/satellite 2.3.0-rc.1 → 2.3.0-rc.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/dist/cjs/Helpers/ShimmerText/ShimmerText.tailwind.js +1 -1
- package/dist/cjs/Layout/Chat/ChatContextAccordion/ChatContextAccordion.styles.d.ts +3 -3
- package/dist/cjs/Layout/Chat/ChatMessage/ChatMessage.styles.js +7 -1
- package/dist/cjs/Layout/Chat/ChatMessageLoader/ChatMessageLoader.styles.js +1 -1
- package/dist/cjs/Layout/Chat/ChatMessages/ChatMessages.d.ts +6 -4
- package/dist/cjs/Layout/Chat/ChatMessages/ChatMessages.js +44 -16
- package/dist/cjs/Layout/Chat/ChatMessages/useLastMessageHeight.d.ts +2 -4
- package/dist/cjs/Layout/Chat/ChatMessages/useLastMessageHeight.js +15 -14
- package/dist/cjs/Layout/Chat/ChatPrompt/ChatPrompt.d.ts +19 -11
- package/dist/cjs/Layout/Chat/ChatPrompt/ChatPrompt.js +7 -1
- package/dist/cjs/Layout/Chat/ChatPrompt/ChatPrompt.styles.js +1 -1
- package/dist/cjs/Layout/Chat/ChatPrompt/useFocusTyping.d.ts +12 -0
- package/dist/cjs/Layout/Chat/ChatPrompt/useFocusTyping.js +60 -0
- package/dist/esm/Helpers/ShimmerText/ShimmerText.tailwind.js +1 -1
- package/dist/esm/Layout/Chat/ChatContextAccordion/ChatContextAccordion.styles.d.ts +3 -3
- package/dist/esm/Layout/Chat/ChatMessage/ChatMessage.styles.js +7 -1
- package/dist/esm/Layout/Chat/ChatMessageLoader/ChatMessageLoader.styles.js +1 -1
- package/dist/esm/Layout/Chat/ChatMessages/ChatMessages.d.ts +6 -4
- package/dist/esm/Layout/Chat/ChatMessages/ChatMessages.js +45 -19
- package/dist/esm/Layout/Chat/ChatMessages/useLastMessageHeight.d.ts +2 -4
- package/dist/esm/Layout/Chat/ChatMessages/useLastMessageHeight.js +16 -15
- package/dist/esm/Layout/Chat/ChatPrompt/ChatPrompt.d.ts +19 -11
- package/dist/esm/Layout/Chat/ChatPrompt/ChatPrompt.js +7 -1
- package/dist/esm/Layout/Chat/ChatPrompt/ChatPrompt.styles.js +1 -1
- package/dist/esm/Layout/Chat/ChatPrompt/useFocusTyping.d.ts +12 -0
- package/dist/esm/Layout/Chat/ChatPrompt/useFocusTyping.js +54 -0
- package/dist/satellite.min.css +1 -1
- package/package.json +1 -1
@@ -6,9 +6,7 @@ var _excluded = ["messages", "userMessageProps", "assistantMessageProps", "loade
|
|
6
6
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
7
7
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
8
8
|
import { ChevronDownIcon } from "lucide-react";
|
9
|
-
import { useEffect } from "react";
|
10
|
-
import { useRef, useState } from "react";
|
11
|
-
import { forwardRef } from "react";
|
9
|
+
import { forwardRef, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
12
10
|
import { useStickToBottom } from "use-stick-to-bottom";
|
13
11
|
import { chatMessagesStyles } from "./ChatMessages.styles";
|
14
12
|
import { useLastMessageHeight } from "./useLastMessageHeight";
|
@@ -21,6 +19,12 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
21
19
|
var DEFAULT_CHAT_MESSAGE_LOCALE = {
|
22
20
|
scrollToBottomText: "Scroll to bottom"
|
23
21
|
};
|
22
|
+
function resolveProps(propsOrFn, message) {
|
23
|
+
if (typeof propsOrFn === "function") {
|
24
|
+
return propsOrFn(message);
|
25
|
+
}
|
26
|
+
return propsOrFn;
|
27
|
+
}
|
24
28
|
var ChatMessagesBase = function ChatMessagesBase(_ref, forwardedRef) {
|
25
29
|
var messages = _ref.messages,
|
26
30
|
userMessageProps = _ref.userMessageProps,
|
@@ -42,15 +46,16 @@ var ChatMessagesBase = function ChatMessagesBase(_ref, forwardedRef) {
|
|
42
46
|
var contextLocale = useLocale("chatMessage");
|
43
47
|
var locale = _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_CHAT_MESSAGE_LOCALE), contextLocale), propsLocale);
|
44
48
|
var _useStickToBottom = useStickToBottom({
|
45
|
-
|
46
|
-
initial: "
|
49
|
+
// @ts-expect-error - Types are wrong in use-stick-to-bottom
|
50
|
+
initial: "instant",
|
51
|
+
resize: "smooth"
|
47
52
|
}),
|
48
53
|
scrollRef = _useStickToBottom.scrollRef,
|
49
54
|
contentRef = _useStickToBottom.contentRef,
|
50
55
|
isAtBottom = _useStickToBottom.isAtBottom,
|
51
56
|
scrollToBottom = _useStickToBottom.scrollToBottom;
|
52
57
|
var styles = chatMessagesStyles();
|
53
|
-
var
|
58
|
+
var lastUserMessageRef = useRef(null);
|
54
59
|
var _useState = useState(false),
|
55
60
|
_useState2 = _slicedToArray(_useState, 2),
|
56
61
|
lastMessageSubmitted = _useState2[0],
|
@@ -58,19 +63,41 @@ var ChatMessagesBase = function ChatMessagesBase(_ref, forwardedRef) {
|
|
58
63
|
var lastUserMessage = _toConsumableArray(messages).reverse().find(function (m) {
|
59
64
|
return m.role === "user";
|
60
65
|
});
|
61
|
-
var
|
62
|
-
var
|
66
|
+
var lastMessageHeight = useLastMessageHeight(scrollRef, lastUserMessageRef, messages, lastMessageSubmitted, 24);
|
67
|
+
var lastMessageId = useMemo(function () {
|
68
|
+
var _messages;
|
69
|
+
return (_messages = messages[messages.length - 1]) === null || _messages === void 0 ? void 0 : _messages.id;
|
70
|
+
}, [messages]);
|
71
|
+
var getMessageProps = useCallback(function (message) {
|
72
|
+
var propsResult;
|
73
|
+
if (message.role === "assistant") {
|
74
|
+
propsResult = resolveProps(assistantMessageProps, message);
|
75
|
+
|
76
|
+
// If the last assistant message is streaming, we don't want to render actions or footer
|
77
|
+
if (status === "streaming" && lastMessageId === message.id) {
|
78
|
+
propsResult = _objectSpread(_objectSpread({}, propsResult), {}, {
|
79
|
+
renderFooter: undefined,
|
80
|
+
renderActions: undefined,
|
81
|
+
actions: undefined
|
82
|
+
});
|
83
|
+
}
|
84
|
+
} else {
|
85
|
+
propsResult = resolveProps(userMessageProps, message);
|
86
|
+
}
|
87
|
+
return propsResult;
|
88
|
+
}, [assistantMessageProps, userMessageProps, status, lastMessageId]);
|
63
89
|
useEffect(function () {
|
64
|
-
if (status
|
65
|
-
|
90
|
+
if (status === "submitted" && lastUserMessage) {
|
91
|
+
setLastMessageSubmitted(true);
|
66
92
|
}
|
67
|
-
|
68
|
-
}, [status, messages, lastUserMessage]);
|
93
|
+
}, [status, lastUserMessage]);
|
69
94
|
return /*#__PURE__*/_jsxs("div", _objectSpread(_objectSpread({}, props), {}, {
|
70
95
|
ref: forwardedRef,
|
71
96
|
className: styles.base({
|
72
97
|
className: className
|
73
98
|
}),
|
99
|
+
role: "log",
|
100
|
+
"aria-live": "polite",
|
74
101
|
children: [/*#__PURE__*/_jsx("div", {
|
75
102
|
ref: scrollRef,
|
76
103
|
className: styles.scroll({
|
@@ -86,16 +113,15 @@ var ChatMessagesBase = function ChatMessagesBase(_ref, forwardedRef) {
|
|
86
113
|
},
|
87
114
|
children: [messages.map(function (message) {
|
88
115
|
var isAssistant = message.role === "assistant";
|
116
|
+
var isLastUserMessage = (lastUserMessage === null || lastUserMessage === void 0 ? void 0 : lastUserMessage.id) === message.id;
|
89
117
|
return /*#__PURE__*/_jsx(ChatMessage, _objectSpread(_objectSpread({
|
90
|
-
ref:
|
91
|
-
if (el) messageRefs.current.set(message.id, el);else messageRefs.current["delete"](message.id);
|
92
|
-
},
|
118
|
+
ref: isLastUserMessage ? lastUserMessageRef : undefined,
|
93
119
|
side: isAssistant ? "left" : "right",
|
94
|
-
variant: isAssistant ? "subtle" : "neutral"
|
95
|
-
|
96
|
-
|
97
|
-
"data-role": message.role,
|
120
|
+
variant: isAssistant ? "subtle" : "neutral"
|
121
|
+
}, getMessageProps(message)), {}, {
|
122
|
+
content: renderMessage ? renderMessage(message) : message.content,
|
98
123
|
actionsExtraData: message,
|
124
|
+
"data-role": message.role,
|
99
125
|
"data-key": message.id
|
100
126
|
}), message.id);
|
101
127
|
}), status === "submitted" && (renderLoader !== null && renderLoader !== void 0 ? renderLoader : /*#__PURE__*/_jsx(ChatMessageLoader, _objectSpread({}, loaderProps))), status === "error" && (renderError !== null && renderError !== void 0 ? renderError : /*#__PURE__*/_jsx(ChatMessageError, _objectSpread(_objectSpread({}, errorProps), {}, {
|
@@ -1,10 +1,8 @@
|
|
1
1
|
import type { RefObject } from "react";
|
2
|
+
import type { ChatMessageBase } from "../../../Layout/Chat/types";
|
2
3
|
/**
|
3
4
|
* React hook to compute the “fill” height below the last user message,
|
4
5
|
* based on the scroll container’s height, the last user‐message element’s height,
|
5
6
|
* and any CSS gap/padding offsets.
|
6
7
|
*/
|
7
|
-
export declare function useLastMessageHeight(scrollRef: RefObject<HTMLElement>, messages:
|
8
|
-
id: string;
|
9
|
-
role: string;
|
10
|
-
}>, lastUserMessageEl: HTMLElement | undefined, lastMessageSubmitted: boolean, spacingOffset?: number): number;
|
8
|
+
export declare function useLastMessageHeight(scrollRef: RefObject<HTMLElement>, lastUserMessageRef: RefObject<HTMLDivElement | null>, messages: ChatMessageBase[], lastMessageSubmitted: boolean, spacingOffset?: number): number;
|
@@ -1,34 +1,35 @@
|
|
1
1
|
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
2
|
+
import { useRef } from "react";
|
2
3
|
import { useLayoutEffect, useState } from "react";
|
3
|
-
|
4
4
|
/**
|
5
5
|
* React hook to compute the “fill” height below the last user message,
|
6
6
|
* based on the scroll container’s height, the last user‐message element’s height,
|
7
7
|
* and any CSS gap/padding offsets.
|
8
8
|
*/
|
9
|
-
export function useLastMessageHeight(scrollRef,
|
9
|
+
export function useLastMessageHeight(scrollRef, lastUserMessageRef, messages, lastMessageSubmitted) {
|
10
10
|
var spacingOffset = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
|
11
11
|
var _useState = useState(0),
|
12
12
|
_useState2 = _slicedToArray(_useState, 2),
|
13
13
|
lastMessageHeight = _useState2[0],
|
14
14
|
setLastMessageHeight = _useState2[1];
|
15
|
-
var
|
16
|
-
|
17
|
-
if (
|
18
|
-
|
19
|
-
// Measure parent/container height
|
20
|
-
var parentHeight = scrollEl.clientHeight;
|
15
|
+
var offsetsRef = useRef(0);
|
16
|
+
function getStaticOffsets() {
|
17
|
+
if (offsetsRef.current || !scrollRef.current) return offsetsRef.current;
|
21
18
|
|
22
19
|
// Compute any CSS gap/rowGap/padding offsets by reading computed style
|
23
|
-
var computed = window.getComputedStyle(
|
20
|
+
var computed = window.getComputedStyle(scrollRef.current);
|
24
21
|
var rowGapValue = parseFloat(computed.rowGap) || parseFloat(computed.gap) || 0;
|
25
22
|
var paddingTop = parseFloat(computed.paddingTop) || 0;
|
26
23
|
var paddingBottom = parseFloat(computed.paddingBottom) || 0;
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
offsetsRef.current = spacingOffset + rowGapValue + paddingTop + paddingBottom;
|
25
|
+
return offsetsRef.current;
|
26
|
+
}
|
27
|
+
var updateLastMessageHeight = function updateLastMessageHeight() {
|
28
|
+
var scrollEl = scrollRef.current;
|
29
|
+
var lastUserMessageEl = lastUserMessageRef.current;
|
30
|
+
if (!scrollEl || messages.length === 0 || !lastMessageSubmitted || !lastUserMessageEl) return;
|
31
|
+
var height = Math.max(scrollEl.clientHeight - lastUserMessageEl.offsetHeight - getStaticOffsets(), 0);
|
32
|
+
setLastMessageHeight(height);
|
32
33
|
};
|
33
34
|
|
34
35
|
// Recompute whenever messages change and a new user message has just been submitted
|
@@ -36,6 +37,6 @@ export function useLastMessageHeight(scrollRef, messages, lastUserMessageEl, las
|
|
36
37
|
if (!lastMessageSubmitted) return;
|
37
38
|
updateLastMessageHeight();
|
38
39
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
39
|
-
}, [messages, lastMessageSubmitted]);
|
40
|
+
}, [messages.length, lastMessageSubmitted]);
|
40
41
|
return lastMessageHeight;
|
41
42
|
}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { FormEvent, KeyboardEvent, ReactNode } from "react";
|
2
|
-
import type {
|
2
|
+
import type { TextAreaAutoSizeProps } from "../../../Fields/TextAreaAutoSize/TextAreaAutoSize";
|
3
3
|
import type { ChatStatus } from "../../../Layout/Chat/types";
|
4
4
|
export declare type ChatPromptLocale = {
|
5
5
|
textareaLabel?: string;
|
@@ -7,7 +7,7 @@ export declare type ChatPromptLocale = {
|
|
7
7
|
stopResponseTooltip?: string;
|
8
8
|
sendMessageTooltip?: string;
|
9
9
|
};
|
10
|
-
export declare type ChatPromptProps = Omit<
|
10
|
+
export declare type ChatPromptProps = Omit<TextAreaAutoSizeProps, "onSubmit"> & {
|
11
11
|
/**
|
12
12
|
* Content to render above the textarea (e.g., a title or instructions).
|
13
13
|
*/
|
@@ -30,6 +30,14 @@ export declare type ChatPromptProps = Omit<TextAreaProps, "onSubmit"> & {
|
|
30
30
|
* - "error": An error occurred during the API request, preventing successful completion.
|
31
31
|
*/
|
32
32
|
status?: ChatStatus;
|
33
|
+
/**
|
34
|
+
* Whether to automatically focus the textarea when the user starts typing.
|
35
|
+
*/
|
36
|
+
autoFocusOnTyping?: boolean;
|
37
|
+
/**
|
38
|
+
* Optional locale.
|
39
|
+
*/
|
40
|
+
locale?: ChatPromptLocale;
|
33
41
|
/**
|
34
42
|
* Callback invoked when the user submits the form, either by clicking
|
35
43
|
* the submit button or pressing Enter (without Shift) inside the textarea.
|
@@ -39,12 +47,8 @@ export declare type ChatPromptProps = Omit<TextAreaProps, "onSubmit"> & {
|
|
39
47
|
* Callback invoked when the user requests to stop the current chat response stream.
|
40
48
|
*/
|
41
49
|
onStop?: () => void;
|
42
|
-
/**
|
43
|
-
* Optional locale.
|
44
|
-
*/
|
45
|
-
locale?: ChatPromptLocale;
|
46
50
|
};
|
47
|
-
export declare const ChatPrompt: import("react").ForwardRefExoticComponent<Omit<
|
51
|
+
export declare const ChatPrompt: import("react").ForwardRefExoticComponent<Omit<TextAreaAutoSizeProps, "onSubmit"> & {
|
48
52
|
/**
|
49
53
|
* Content to render above the textarea (e.g., a title or instructions).
|
50
54
|
*/
|
@@ -67,6 +71,14 @@ export declare const ChatPrompt: import("react").ForwardRefExoticComponent<Omit<
|
|
67
71
|
* - "error": An error occurred during the API request, preventing successful completion.
|
68
72
|
*/
|
69
73
|
status?: ChatStatus | undefined;
|
74
|
+
/**
|
75
|
+
* Whether to automatically focus the textarea when the user starts typing.
|
76
|
+
*/
|
77
|
+
autoFocusOnTyping?: boolean | undefined;
|
78
|
+
/**
|
79
|
+
* Optional locale.
|
80
|
+
*/
|
81
|
+
locale?: ChatPromptLocale | undefined;
|
70
82
|
/**
|
71
83
|
* Callback invoked when the user submits the form, either by clicking
|
72
84
|
* the submit button or pressing Enter (without Shift) inside the textarea.
|
@@ -76,8 +88,4 @@ export declare const ChatPrompt: import("react").ForwardRefExoticComponent<Omit<
|
|
76
88
|
* Callback invoked when the user requests to stop the current chat response stream.
|
77
89
|
*/
|
78
90
|
onStop?: (() => void) | undefined;
|
79
|
-
/**
|
80
|
-
* Optional locale.
|
81
|
-
*/
|
82
|
-
locale?: ChatPromptLocale | undefined;
|
83
91
|
} & import("react").RefAttributes<HTMLTextAreaElement>>;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
2
2
|
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
3
3
|
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
4
|
-
var _excluded = ["className", "renderHeader", "children", "renderFooter", "onSubmit", "onStop", "placeholder", "placeholders", "status", "defaultValue", "value", "locale"];
|
4
|
+
var _excluded = ["className", "renderHeader", "children", "renderFooter", "onSubmit", "onStop", "placeholder", "placeholders", "status", "autoFocusOnTyping", "defaultValue", "value", "locale"];
|
5
5
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
6
6
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
7
7
|
import { ArrowUpIcon, CircleStopIcon } from "lucide-react";
|
@@ -9,6 +9,7 @@ import { useState } from "react";
|
|
9
9
|
import { useRef } from "react";
|
10
10
|
import { forwardRef } from "react";
|
11
11
|
import { chatPromptStyles } from "./ChatPrompt.styles";
|
12
|
+
import { useFocusOnTyping } from "./useFocusTyping";
|
12
13
|
import { useTypewriter } from "./useTypewriter";
|
13
14
|
import { IconButton } from "./../../../Actions";
|
14
15
|
import { TextAreaAutoSize } from "./../../../Fields/TextAreaAutoSize/TextAreaAutoSize";
|
@@ -34,6 +35,7 @@ export var ChatPrompt = /*#__PURE__*/forwardRef(function (_ref, forwardedRef) {
|
|
34
35
|
placeholders = _ref$placeholders === void 0 ? [] : _ref$placeholders,
|
35
36
|
_ref$status = _ref.status,
|
36
37
|
status = _ref$status === void 0 ? "ready" : _ref$status,
|
38
|
+
autoFocusOnTyping = _ref.autoFocusOnTyping,
|
37
39
|
defaultValue = _ref.defaultValue,
|
38
40
|
valueProp = _ref.value,
|
39
41
|
propsLocale = _ref.locale,
|
@@ -53,6 +55,10 @@ export var ChatPrompt = /*#__PURE__*/forwardRef(function (_ref, forwardedRef) {
|
|
53
55
|
var buttonDisabled = !hasValue && !canStop;
|
54
56
|
var dynamicPlaceholder = useTypewriter(placeholders, !hasValue);
|
55
57
|
var computedPlaceholder = placeholders.length > 0 ? dynamicPlaceholder : staticPlaceholder;
|
58
|
+
useFocusOnTyping(internalRef, {
|
59
|
+
enabled: autoFocusOnTyping,
|
60
|
+
strictChars: true
|
61
|
+
});
|
56
62
|
var submit = function submit(e) {
|
57
63
|
e.preventDefault();
|
58
64
|
if (!hasValue || canStop) {
|
@@ -8,7 +8,7 @@ export var chatPromptStyles = tv({
|
|
8
8
|
header: stl(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["flex items-center gap-1.5"]))),
|
9
9
|
body: stl(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral([""]))),
|
10
10
|
textarea: stl(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["[&>textarea]:p-3 [&>textarea]:pb-0 [&>textarea]:thin-scrollbar"]))),
|
11
|
-
actions: stl(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["flex flex-row p-3 pt-2 justify-end"]))),
|
11
|
+
actions: stl(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["flex flex-row p-3 pt-2 justify-end cursor-text"]))),
|
12
12
|
submit: stl(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral([""]))),
|
13
13
|
footer: stl(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["flex items-center justify-between gap-1.5"])))
|
14
14
|
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
/// <reference types="react" />
|
2
|
+
declare type Editable = HTMLTextAreaElement | HTMLInputElement | HTMLElement;
|
3
|
+
interface Options {
|
4
|
+
enabled?: boolean;
|
5
|
+
strictChars?: boolean;
|
6
|
+
}
|
7
|
+
/**
|
8
|
+
* Focus `editableRef` the moment the user types anywhere on the page,
|
9
|
+
* *unless* their cursor is already inside an editable control.
|
10
|
+
*/
|
11
|
+
export declare function useFocusOnTyping<E extends Editable = Editable>(editableRef: React.RefObject<E>, { enabled, strictChars }?: Options): void;
|
12
|
+
export {};
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import { useCallback, useEffect } from "react";
|
2
|
+
var isTextualElement = function isTextualElement(el) {
|
3
|
+
if (el.isContentEditable) return true;
|
4
|
+
var tag = el.tagName.toUpperCase();
|
5
|
+
if (tag === "TEXTAREA") return true;
|
6
|
+
if (tag === "INPUT") {
|
7
|
+
// Empty `type` defaults to "text"
|
8
|
+
var t = (el.type || "text").toLowerCase();
|
9
|
+
return ["text", "search", "url", "email", "tel", "password", "number", "date", "datetime-local", "month", "week", "time"].includes(t);
|
10
|
+
}
|
11
|
+
return false;
|
12
|
+
};
|
13
|
+
var shouldFocusEditable = function shouldFocusEditable(target) {
|
14
|
+
var el = target;
|
15
|
+
if (!el) return true;
|
16
|
+
return !isTextualElement(el);
|
17
|
+
};
|
18
|
+
|
19
|
+
// e.g. <div contentEditable>
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Focus `editableRef` the moment the user types anywhere on the page,
|
23
|
+
* *unless* their cursor is already inside an editable control.
|
24
|
+
*/
|
25
|
+
export function useFocusOnTyping(editableRef) {
|
26
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
27
|
+
_ref$enabled = _ref.enabled,
|
28
|
+
enabled = _ref$enabled === void 0 ? true : _ref$enabled,
|
29
|
+
_ref$strictChars = _ref.strictChars,
|
30
|
+
strictChars = _ref$strictChars === void 0 ? false : _ref$strictChars;
|
31
|
+
var handleKeyDown = useCallback(function (e) {
|
32
|
+
if (!enabled || e.ctrlKey || e.altKey || e.metaKey || e.isComposing || e.repeat || (
|
33
|
+
// Printable-character test
|
34
|
+
strictChars ? !/^[A-Za-z0-9]$/.test(e.key) : e.key.length !== 1)) {
|
35
|
+
return;
|
36
|
+
}
|
37
|
+
var node = editableRef.current;
|
38
|
+
if (!node) return;
|
39
|
+
if (node.contains(document.activeElement)) return;
|
40
|
+
if (shouldFocusEditable(e.target)) {
|
41
|
+
node.focus();
|
42
|
+
}
|
43
|
+
}, [enabled, strictChars, editableRef]);
|
44
|
+
useEffect(function () {
|
45
|
+
if (typeof window === "undefined") return; // SSR/test guard
|
46
|
+
|
47
|
+
window.addEventListener("keydown", handleKeyDown, {
|
48
|
+
passive: true
|
49
|
+
});
|
50
|
+
return function () {
|
51
|
+
return window.removeEventListener("keydown", handleKeyDown);
|
52
|
+
};
|
53
|
+
}, [handleKeyDown]);
|
54
|
+
}
|